1 | <?php |
||
26 | final class ConfigurationFactory |
||
27 | { |
||
28 | /** |
||
29 | * @var Strategy[] All strategies that are used by the ConfigurationFactory. |
||
30 | */ |
||
31 | private $strategies = []; |
||
32 | |||
33 | /** |
||
34 | * A series of callables that take the configuration array as parameter and should return that array or a modified |
||
35 | * version of it. |
||
36 | * |
||
37 | * @var callable[] |
||
38 | */ |
||
39 | private $middlewares = []; |
||
40 | |||
41 | /** |
||
42 | * @var string[] |
||
43 | */ |
||
44 | private $defaultFiles = []; |
||
45 | |||
46 | /** |
||
47 | * Initializes the ConfigurationFactory. |
||
48 | * |
||
49 | * @param Strategy[]|iterable $strategies |
||
50 | * @param array $defaultFiles |
||
51 | */ |
||
52 | 8 | public function __construct(iterable $strategies, array $defaultFiles) |
|
59 | |||
60 | /** |
||
61 | * Adds a middleware callback that allows the consumer to alter the configuration array when it is constructed. |
||
62 | * |
||
63 | * @param callable $middleware |
||
64 | */ |
||
65 | 2 | public function addMiddleware(callable $middleware): void |
|
69 | |||
70 | /** |
||
71 | * Attempts to load a configuration from the default locations for phpDocumentor |
||
72 | * |
||
73 | * @return Configuration |
||
74 | */ |
||
75 | 3 | public function fromDefaultLocations(): Configuration |
|
87 | |||
88 | /** |
||
89 | * Converts the phpDocumentor configuration xml to an array. |
||
90 | * |
||
91 | * @param Uri $uri The location of the file to be loaded. |
||
92 | * |
||
93 | * @return Configuration |
||
94 | * @throws RuntimeException if no matching strategy can be found. |
||
95 | */ |
||
96 | 3 | public function fromUri(Uri $uri): Configuration |
|
97 | { |
||
98 | 3 | $filename = (string) $uri; |
|
99 | |||
100 | 3 | if (!file_exists($filename)) { |
|
101 | 1 | throw new \InvalidArgumentException(sprintf('File %s could not be found', $filename)); |
|
102 | } |
||
103 | |||
104 | 2 | $xml = new \SimpleXMLElement($filename, 0, true); |
|
105 | 2 | foreach ($this->strategies as $strategy) { |
|
106 | 1 | if ($strategy->supports($xml) !== true) { |
|
107 | 1 | continue; |
|
108 | } |
||
109 | |||
110 | 1 | return new Configuration($this->applyMiddleware($strategy->convert($xml))); |
|
111 | } |
||
112 | |||
113 | 1 | throw new RuntimeException('No supported configuration files were found'); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Adds strategies that are used in the ConfigurationFactory. |
||
118 | * |
||
119 | * @param Strategy $strategy |
||
120 | */ |
||
121 | 5 | private function registerStrategy(Strategy $strategy): void |
|
125 | |||
126 | /** |
||
127 | * Applies all middleware callbacks onto the configuration. |
||
128 | * |
||
129 | * @param array $configuration |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | 5 | private function applyMiddleware(array $configuration): array |
|
141 | } |
||
142 |