ComponentRegistrar.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Component;
  7. /**
  8. * Provides ability to statically register components.
  9. *
  10. * @author Josh Di Fabio <joshdifabio@gmail.com>
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class ComponentRegistrar implements ComponentRegistrarInterface
  16. {
  17. /**#@+
  18. * Different types of components
  19. */
  20. const MODULE = 'module';
  21. const LIBRARY = 'library';
  22. const THEME = 'theme';
  23. const LANGUAGE = 'language';
  24. const SETUP = 'setup';
  25. /**#@- */
  26. /**#@- */
  27. private static $paths = [
  28. self::MODULE => [],
  29. self::LIBRARY => [],
  30. self::LANGUAGE => [],
  31. self::THEME => [],
  32. self::SETUP => []
  33. ];
  34. /**
  35. * Sets the location of a component.
  36. *
  37. * @param string $type component type
  38. * @param string $componentName Fully-qualified component name
  39. * @param string $path Absolute file path to the component
  40. * @throws \LogicException
  41. * @return void
  42. */
  43. public static function register($type, $componentName, $path)
  44. {
  45. self::validateType($type);
  46. if (isset(self::$paths[$type][$componentName])) {
  47. throw new \LogicException(
  48. ucfirst($type) . ' \'' . $componentName . '\' from \'' . $path . '\' '
  49. . 'has been already defined in \'' . self::$paths[$type][$componentName] . '\'.'
  50. );
  51. } else {
  52. self::$paths[$type][$componentName] = str_replace('\\', '/', $path);
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getPaths($type)
  59. {
  60. self::validateType($type);
  61. return self::$paths[$type];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getPath($type, $componentName)
  67. {
  68. self::validateType($type);
  69. return self::$paths[$type][$componentName] ?? null;
  70. }
  71. /**
  72. * Checks if type of component is valid
  73. *
  74. * @param string $type
  75. * @return void
  76. * @throws \LogicException
  77. */
  78. private static function validateType($type)
  79. {
  80. if (!isset(self::$paths[$type])) {
  81. throw new \LogicException('\'' . $type . '\' is not a valid component type');
  82. }
  83. }
  84. }