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