These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of phpDocumentor. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @copyright 2010-2015 Mike van Riel<[email protected]> |
||
9 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
||
10 | * @link http://phpdoc.org |
||
11 | */ |
||
12 | |||
13 | namespace phpDocumentor\Application\Configuration; |
||
14 | |||
15 | use phpDocumentor\Application\Configuration\Factory\Strategy; |
||
16 | use phpDocumentor\Application\Configuration\Factory\Version3; |
||
17 | use phpDocumentor\DomainModel\Uri; |
||
18 | |||
19 | /** |
||
20 | * The ConfigurationFactory converts the configuration xml from a Uri into an array. |
||
21 | */ |
||
22 | final class ConfigurationFactory |
||
23 | { |
||
24 | /** @var Uri The Uri that contains the path to the configuration file. */ |
||
25 | private $uri; |
||
26 | |||
27 | /** @var string[] The cached configuration as an array so that we improve performance */ |
||
28 | private $cachedConfiguration = []; |
||
29 | |||
30 | /** @var Strategy[] All strategies that are used by the ConfigurationFactory. */ |
||
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 | * Initializes the ConfigurationFactory. |
||
43 | * |
||
44 | * @param Strategy[] $strategies |
||
45 | * @param callable[] $middlewares |
||
46 | */ |
||
47 | 3 | public function __construct(array $strategies, Uri $uri, array $middlewares = []) |
|
48 | { |
||
49 | 3 | foreach ($strategies as $strategy) { |
|
50 | 3 | $this->registerStrategy($strategy); |
|
51 | } |
||
52 | |||
53 | 3 | $this->replaceLocation($uri); |
|
54 | 3 | $this->middlewares = $middlewares; |
|
55 | 3 | } |
|
56 | |||
57 | public static function createInstance(iterable $strategiesBuilder): self |
||
58 | { |
||
59 | $strategies = []; |
||
60 | |||
61 | foreach ($strategiesBuilder as $stategy) { |
||
62 | $strategies[] = $stategy; |
||
63 | } |
||
64 | |||
65 | return new static($strategies, new Uri('file://' . getcwd() . '/phpdoc.dist.xml')); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Adds a middleware callback that allows the consumer to alter the configuration array when it is constructed. |
||
70 | * |
||
71 | * @param callable $middleware |
||
72 | */ |
||
73 | 2 | public function addMiddleware(callable $middleware) |
|
74 | { |
||
75 | 2 | $this->middlewares[] = $middleware; |
|
76 | |||
77 | // Middleware's changed; we must rebuild the cache |
||
78 | 2 | $this->clearCache(); |
|
79 | 2 | } |
|
80 | |||
81 | /** |
||
82 | * Replaces the location of the configuration file if it differs from the existing one. |
||
83 | */ |
||
84 | 2 | public function replaceLocation(Uri $uri) |
|
85 | { |
||
86 | 2 | if (!isset($this->uri) || !$this->uri->equals($uri)) { |
|
87 | 2 | $this->uri = $uri; |
|
88 | 2 | $this->clearCache(); |
|
89 | } |
||
90 | 2 | } |
|
91 | |||
92 | /** |
||
93 | * Converts the phpDocumentor configuration xml to an array. |
||
94 | * |
||
95 | * @return array |
||
96 | * |
||
97 | * @throws \RuntimeException if no matching strategy can be found. |
||
98 | */ |
||
99 | 5 | public function get() |
|
100 | { |
||
101 | 5 | if ($this->cachedConfiguration) { |
|
0 ignored issues
–
show
|
|||
102 | 1 | return $this->cachedConfiguration; |
|
103 | } |
||
104 | |||
105 | 5 | if (file_exists($this->uri)) { |
|
106 | 5 | $xml = new \SimpleXMLElement($this->uri, 0, true); |
|
107 | 5 | $this->cachedConfiguration = $this->extractConfigurationArray($xml); |
|
108 | } else { |
||
109 | $this->cachedConfiguration = Version3::buildDefault(); |
||
110 | } |
||
111 | |||
112 | 4 | $this->applyMiddleware(); |
|
113 | |||
114 | 4 | return $this->cachedConfiguration; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Clears the cache for the configuration. |
||
119 | */ |
||
120 | 2 | public function clearCache() |
|
121 | { |
||
122 | 2 | return $this->cachedConfiguration = null; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Adds strategies that are used in the ConfigurationFactory. |
||
127 | */ |
||
128 | 6 | private function registerStrategy(Strategy $strategy) |
|
129 | { |
||
130 | 6 | $this->strategies[] = $strategy; |
|
131 | 6 | } |
|
132 | |||
133 | /** |
||
134 | * Converts the given XML structure into an array containing the configuration. |
||
135 | * |
||
136 | * @param \SimpleXMLElement $xml |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | 7 | private function extractConfigurationArray($xml) |
|
141 | { |
||
142 | 7 | foreach ($this->strategies as $strategy) { |
|
143 | 6 | if ($strategy->match($xml) === true) { |
|
144 | 6 | return $strategy->convert($xml); |
|
145 | } |
||
146 | } |
||
147 | |||
148 | 1 | throw new \RuntimeException('No strategy found that matches the configuration xml'); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Applies all middleware callbacks onto the configuration. |
||
153 | */ |
||
154 | 6 | private function applyMiddleware() |
|
155 | { |
||
156 | 6 | foreach ($this->middlewares as $middleware) { |
|
157 | 2 | $this->cachedConfiguration = $middleware($this->cachedConfiguration); |
|
158 | } |
||
159 | 6 | } |
|
160 | } |
||
161 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.