Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Application/Configuration/ConfigurationFactory.php (3 issues)

Upgrade to new PHP Analysis Engine

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 4
    public function __construct(array $strategies, Uri $uri, array $middlewares = [])
48
    {
49 4
        foreach ($strategies as $strategy) {
50 3
            $this->registerStrategy($strategy);
51
        }
52
53 4
        $this->replaceLocation($uri);
54 4
        $this->middlewares = $middlewares;
55 4
    }
56
57 1
    public static function createInstance(iterable $strategiesBuilder): self
58
    {
59 1
        $strategies = [];
60
61 1
        foreach ($strategiesBuilder as $stategy) {
62
            $strategies[] = $stategy;
63
        }
64
65 1
        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 6
    public function get()
100
    {
101 6
        if ($this->cachedConfiguration) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cachedConfiguration of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
102 1
            return $this->cachedConfiguration;
103
        }
104
105 6
        if (file_exists($this->uri)) {
106 5
            $xml = new \SimpleXMLElement($this->uri, 0, true);
107 5
            $this->cachedConfiguration = $this->extractConfigurationArray($xml);
108
        } else {
109 1
            $this->cachedConfiguration = Version3::buildDefault();
0 ignored issues
show
Documentation Bug introduced by
It seems like \phpDocumentor\Applicati...ersion3::buildDefault() of type array<string,array<strin...:\\\"array\\\"}>\"}>"}> is incompatible with the declared type array<integer,string> of property $cachedConfiguration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
        }
111
112 5
        $this->applyMiddleware();
113
114 5
        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;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,string> of property $cachedConfiguration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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 7
    private function applyMiddleware()
155
    {
156 7
        foreach ($this->middlewares as $middleware) {
157 2
            $this->cachedConfiguration = $middleware($this->cachedConfiguration);
158
        }
159 7
    }
160
}
161