Failed Conditions
Push — master ( d49c03...6343d0 )
by Bernhard
05:03
created

src/Api/Package/RootPackageFile.php (1 issue)

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
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Api\Package;
13
14
use Puli\Manager\Api\Config\Config;
15
use Puli\Manager\Api\InvalidConfigException;
16
17
/**
18
 * The package file of the root package.
19
 *
20
 * You can pass a base configuration to the constructor that the package's
21
 * configuration will inherit.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class RootPackageFile extends PackageFile
28
{
29
    /**
30
     * @var Config
31
     */
32
    private $config;
33
34
    /**
35
     * @var string[]
36
     */
37
    private $overrideOrder = array();
38
39
    /**
40
     * @var InstallInfo[]
41
     */
42
    private $installInfos = array();
43
44
    /**
45
     * @var string[]
46
     */
47
    private $pluginClasses = array();
48
49
    /**
50
     * Creates a new root package file.
51
     *
52
     * The file's configuration will inherit its settings from the base
53
     * configuration passed to the constructor.
54
     *
55
     * @param string|null $packageName The package name. Optional.
56
     * @param string|null $path        The path where the configuration is
57
     *                                 stored or `null` if this configuration is
58
     *                                 not stored on the file system.
59
     * @param Config|null $baseConfig  The configuration that the package will
60
     *                                 inherit its configuration values from.
61
     */
62 498
    public function __construct($packageName = null, $path = null, Config $baseConfig = null)
63
    {
64 498
        parent::__construct($packageName, $path);
65
66 498
        $this->config = new Config($baseConfig);
67 498
    }
68
69
    /**
70
     * Returns the configuration of the package.
71
     *
72
     * @return Config The configuration.
73
     */
74 340
    public function getConfig()
75
    {
76 340
        return $this->config;
77
    }
78
79
    /**
80
     * Returns the order in which packages should be loaded.
81
     *
82
     * If packages contain path mappings for the same resource paths, this
83
     * setting can be used to specify in which order these packages should be
84
     * loaded. Alternatively, you can use {@link setOverriddenPackages()} to
85
     * mark one of the packages to override the other one.
86
     *
87
     * @return string[] A list of package names.
88
     */
89 81
    public function getOverrideOrder()
90
    {
91 81
        return $this->overrideOrder;
92
    }
93
94
    /**
95
     * Sets the order in which packages should be loaded.
96
     *
97
     * If packages contain path mappings for the same resource paths, this
98
     * setting can be used to specify in which order these packages should be
99
     * loaded. Alternatively, you can use {@link setOverriddenPackages()} to
100
     * mark one of the packages to override the other one.
101
     *
102
     * @param string[] $overrideOrder A list of package names.
103
     */
104 4
    public function setOverrideOrder(array $overrideOrder)
105
    {
106 4
        $this->overrideOrder = $overrideOrder;
107 4
    }
108
109
    /**
110
     * Sets the install infos of all installed packages.
111
     *
112
     * @param InstallInfo[] The install infos.
113
     */
114 2
    public function setInstallInfos(array $installInfos)
115
    {
116 2
        $this->installInfos = array();
117
118 2
        foreach ($installInfos as $installInfo) {
119 2
            $this->addInstallInfo($installInfo);
120
        }
121 2
    }
122
123
    /**
124
     * Adds install info for an installed package.
125
     *
126
     * @param InstallInfo $installInfo The install info.
127
     */
128 172
    public function addInstallInfo(InstallInfo $installInfo)
129
    {
130 172
        $this->installInfos[$installInfo->getPackageName()] = $installInfo;
131 172
    }
132
133
    /**
134
     * Removes the install info of an installed package.
135
     *
136
     * @param string $packageName The package name.
137
     */
138 10
    public function removeInstallInfo($packageName)
139
    {
140 10
        unset($this->installInfos[$packageName]);
141 10
    }
142
143
    /**
144
     * Removes all install infos.
145
     */
146 1
    public function clearInstallInfos()
147
    {
148 1
        $this->installInfos = array();
149 1
    }
150
151
    /**
152
     * Returns the install info of an installed package.
153
     *
154
     * @param string $packageName The package name.
155
     *
156
     * @return InstallInfo The install info.
157
     *
158
     * @throws NoSuchPackageException If no package is installed with that name.
159
     */
160 9
    public function getInstallInfo($packageName)
161
    {
162 9
        if (!isset($this->installInfos[$packageName])) {
163 1
            throw new NoSuchPackageException(sprintf(
164 1
                'Could not get install info: The package "%s" is not installed.',
165
                $packageName
166
            ));
167
        }
168
169 8
        return $this->installInfos[$packageName];
170
    }
171
172
    /**
173
     * Returns the install infos of all installed packages.
174
     *
175
     * @return InstallInfo[] The install infos.
176
     */
177 66
    public function getInstallInfos()
178
    {
179
        // The package names as array keys are for internal use only
180 66
        return array_values($this->installInfos);
181
    }
182
183
    /**
184
     * Returns whether an install info with a given name exists.
185
     *
186
     * @param string $packageName The name of the package.
187
     *
188
     * @return bool Whether install info with that name exists.
189
     */
190 10
    public function hasInstallInfo($packageName)
191
    {
192 10
        return isset($this->installInfos[$packageName]);
193
    }
194
195
    /**
196
     * Returns whether the package file contains any install infos.
197
     *
198
     * @return bool Returns `true` if the file contains install infos and
199
     *              `false` otherwise.
200
     */
201 2
    public function hasInstallInfos()
202
    {
203 2
        return count($this->installInfos) > 0;
204
    }
205
206
    /**
207
     * Returns the plugin classes.
208
     *
209
     * @return string[] The fully qualified plugin class names.
0 ignored issues
show
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
210
     *
211
     * @see setPluginClasses()
212
     */
213 57
    public function getPluginClasses()
214
    {
215 57
        return array_keys($this->pluginClasses);
216
    }
217
218
    /**
219
     * Sets the plugin classes.
220
     *
221
     * The plugin classes must be fully-qualified class names that implement
222
     * {@link Puli\Manager\Api\PuliPlugin}. If a class is not
223
     * found or does not implement that interface, an exception is thrown.
224
     *
225
     * The plugin classes must not have required parameters in their constructor
226
     * so that they can be successfully instantiated. If a constructor has
227
     * required parameters, an exception is thrown.
228
     *
229
     * Leading backslashes are removed from the fully-qualified class names.
230
     *
231
     * @param string[] $pluginClasses The fully qualified plugin class names.
232
     *
233
     * @throws InvalidConfigException If the class is not found, is not a class,
234
     *                                does not implement {@link PuliPlugin}
235
     *                                or has required constructor parameters.
236
     */
237 36
    public function setPluginClasses(array $pluginClasses)
238
    {
239 36
        $this->pluginClasses = array();
240
241 36
        $this->addPluginClasses($pluginClasses);
242 36
    }
243
244
    /**
245
     * Adds multiple plugin classes.
246
     *
247
     * The plugin classes must be fully-qualified class names that implement
248
     * {@link Puli\Manager\Api\PuliPlugin}. If a class is not
249
     * found or does not implement that interface, an exception is thrown.
250
     *
251
     * The plugin classes must not have required parameters in their constructor
252
     * so that they can be successfully instantiated. If a constructor has
253
     * required parameters, an exception is thrown.
254
     *
255
     * Leading backslashes are removed from the fully-qualified class names.
256
     *
257
     * @param string[] $pluginClasses The fully qualified plugin class names.
258
     */
259 37
    public function addPluginClasses(array $pluginClasses)
260
    {
261 37
        foreach ($pluginClasses as $pluginClass) {
262 36
            $this->addPluginClass($pluginClass);
263
        }
264 37
    }
265
266
    /**
267
     * Adds a plugin class.
268
     *
269
     * The plugin class must be a fully-qualified class name that implements
270
     * {@link PuliPlugin}. If the class is not found or does not implement
271
     * that interface, an exception is thrown.
272
     *
273
     * The plugin class must not have required parameters in its constructor
274
     * so that it can be successfully instantiate. If the constructor has
275
     * required parameters, an exception is thrown.
276
     *
277
     * Leading backslashes are removed from the fully-qualified class name.
278
     *
279
     * @param string $pluginClass The fully qualified plugin class name.
280
     */
281 57
    public function addPluginClass($pluginClass)
282
    {
283 57
        $this->pluginClasses[ltrim($pluginClass, '\\')] = true;
284 57
    }
285
286
    /**
287
     * Removes a plugin class.
288
     *
289
     * If the plugin class has not been added, this method does nothing. This
290
     * method also does not validate whether the passed value is actually a
291
     * plugin class.
292
     *
293
     * Leading backslashes are removed from the fully-qualified class name.
294
     *
295
     * @param string $pluginClass The fully qualified plugin class name.
296
     */
297 10
    public function removePluginClass($pluginClass)
298
    {
299 10
        unset($this->pluginClasses[ltrim($pluginClass, '\\')]);
300 10
    }
301
302
    /**
303
     * Removes all plugin classes.
304
     */
305 1
    public function clearPluginClasses()
306
    {
307 1
        $this->pluginClasses = array();
308 1
    }
309
310
    /**
311
     * Returns whether the configuration contains a plugin class.
312
     *
313
     * @param string $pluginClass The fully qualified plugin class name.
314
     *
315
     * @return bool Whether the configuration contains the plugin class.
316
     */
317 14
    public function hasPluginClass($pluginClass)
318
    {
319 14
        return isset($this->pluginClasses[ltrim($pluginClass, '\\')]);
320
    }
321
322
    /**
323
     * Returns whether the configuration contains any plugin classes.
324
     *
325
     * @return bool Whether the configuration contains any plugin classes.
326
     */
327 2
    public function hasPluginClasses()
328
    {
329 2
        return count($this->pluginClasses) > 0;
330
    }
331
}
332