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