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

src/Api/Package/PackageFile.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 InvalidArgumentException;
15
use Puli\Manager\Api\Discovery\BindingDescriptor;
16
use Puli\Manager\Api\Discovery\BindingTypeDescriptor;
17
use Puli\Manager\Api\Discovery\NoSuchBindingException;
18
use Puli\Manager\Api\Repository\NoSuchPathMappingException;
19
use Puli\Manager\Api\Repository\PathMapping;
20
use Puli\Manager\Assert\Assert;
21
use Rhumsaa\Uuid\Uuid;
22
23
/**
24
 * Stores the configuration of a package.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class PackageFile
31
{
32
    /**
33
     * The current puli.json version.
34
     */
35
    const DEFAULT_VERSION = '1.0';
36
37
    /**
38
     * @var string
39
     */
40
    private $version = self::DEFAULT_VERSION;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $packageName;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $path;
51
52
    /**
53
     * @var PathMapping[]
54
     */
55
    private $pathMappings = array();
56
57
    /**
58
     * @var BindingDescriptor[]
59
     */
60
    private $bindingDescriptors = array();
61
62
    /**
63
     * @var BindingTypeDescriptor[]
64
     */
65
    private $typeDescriptors = array();
66
67
    /**
68
     * @var bool[]
69
     */
70
    private $overriddenPackages = array();
71
72
    /**
73
     * @var array
74
     */
75
    private $extra = array();
76
77
    /**
78
     * Creates a new package file.
79
     *
80
     * @param string|null $packageName The package name. Optional.
81
     * @param string|null $path        The path where the file is stored or
82
     *                                 `null` if this configuration is not
83
     *                                 stored on the file system.
84
     *
85
     * @throws InvalidArgumentException If the name/path is not a string or empty.
86
     */
87 626
    public function __construct($packageName = null, $path = null)
88
    {
89 626
        Assert::nullOrPackageName($packageName);
90 624
        Assert::nullOrAbsoluteSystemPath($path);
91
92 622
        $this->path = $path;
93 622
        $this->packageName = $packageName;
94 622
    }
95
96
    /**
97
     * Returns the version of the package file.
98
     *
99
     * @return string The package file version.
100
     */
101 9
    public function getVersion()
102
    {
103 9
        return $this->version;
104
    }
105
106
    /**
107
     * Sets the version of the package file.
108
     *
109
     * @param string $version The package file version.
110
     */
111 7
    public function setVersion($version)
112
    {
113 7
        Assert::string($version, 'The package file version must be a string. Got: %s');
114 7
        Assert::regex($version, '~^\d\.\d$~', 'The package file version must have the format "<digit>.<digit>". Got: %s</digit>');
115
116 7
        $this->version = $version;
117 7
    }
118
119
    /**
120
     * Returns the package name.
121
     *
122
     * @return string|null The package name or `null` if none is set.
123
     */
124 453
    public function getPackageName()
125
    {
126 453
        return $this->packageName;
127
    }
128
129
    /**
130
     * Sets the package name.
131
     *
132
     * @param string|null $packageName The package name or `null` to unset.
133
     *
134
     * @throws InvalidArgumentException If the name is not a string or empty.
135
     */
136 42
    public function setPackageName($packageName)
137
    {
138 42
        Assert::nullOrPackageName($packageName);
139
140 40
        $this->packageName = $packageName;
141 40
    }
142
143
    /**
144
     * Returns the path to the package file.
145
     *
146
     * @return string|null The path or `null` if this file is not stored on the
147
     *                     file system.
148
     */
149 25
    public function getPath()
150
    {
151 25
        return $this->path;
152
    }
153
154
    /**
155
     * Sets the names of the packages this package overrides.
156
     *
157
     * @param string[] $packageNames The names of the overridden packages.
158
     */
159 52
    public function setOverriddenPackages(array $packageNames)
160
    {
161 52
        $this->overriddenPackages = array();
162
163 52
        foreach ($packageNames as $packageName) {
164 52
            $this->overriddenPackages[$packageName] = true;
165
        }
166 52
    }
167
168
    /**
169
     * Adds an overridden package.
170
     *
171
     * @param string $packageName The name of the overridden package.
172
     */
173 10
    public function addOverriddenPackage($packageName)
174
    {
175 10
        $this->overriddenPackages[$packageName] = true;
176 10
    }
177
178
    /**
179
     * Adds an overridden package.
180
     *
181
     * @param string $packageName The name of the overridden package.
182
     */
183 2
    public function removeOverriddenPackage($packageName)
184
    {
185 2
        unset($this->overriddenPackages[$packageName]);
186 2
    }
187
188
    /**
189
     * Removes all overridden packages.
190
     */
191
    public function clearOverriddenPackages()
192
    {
193
        $this->overriddenPackages = array();
194
    }
195
196
    /**
197
     * Returns the names of the packages this package overrides.
198
     *
199
     * @return string[] The names of the overridden packages.
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...
200
     */
201 120
    public function getOverriddenPackages()
202
    {
203 120
        return array_keys($this->overriddenPackages);
204
    }
205
206
    /**
207
     * Returns whether the package overrides a given package.
208
     *
209
     * @param string $packageName The name of the overridden package.
210
     *
211
     * @return bool Returns `true` if the package is overridden in the package
212
     *              file.
213
     */
214 7
    public function hasOverriddenPackage($packageName)
215
    {
216 7
        return isset($this->overriddenPackages[$packageName]);
217
    }
218
219
    /**
220
     * Returns whether the package overrides any other package.
221
     *
222
     * @return bool Returns `true` if the package overrides other packgaes and
223
     *              `false` otherwise.
224
     */
225
    public function hasOverriddenPackages()
226
    {
227
        return count($this->overriddenPackages) > 0;
228
    }
229
230
    /**
231
     * Returns the path mappings.
232
     *
233
     * @return PathMapping[] The path mappings.
234
     */
235 89
    public function getPathMappings()
236
    {
237 89
        return $this->pathMappings;
238
    }
239
240
    /**
241
     * Returns the path mapping for a repository path.
242
     *
243
     * @param string $repositoryPath The repository path.
244
     *
245
     * @return PathMapping The corresponding path mapping.
246
     *
247
     * @throws NoSuchPathMappingException If the repository path is not mapped.
248
     */
249 15
    public function getPathMapping($repositoryPath)
250
    {
251 15
        if (!isset($this->pathMappings[$repositoryPath])) {
252 1
            throw NoSuchPathMappingException::forRepositoryPath($repositoryPath);
253
        }
254
255 14
        return $this->pathMappings[$repositoryPath];
256
    }
257
258
    /**
259
     * Returns whether the file contains a path mapping for a repository path.
260
     *
261
     * @param string $repositoryPath The repository path.
262
     *
263
     * @return bool Returns `true` if the file contains a mapping for the path.
264
     */
265 30
    public function hasPathMapping($repositoryPath)
266
    {
267 30
        return isset($this->pathMappings[$repositoryPath]);
268
    }
269
270
    /**
271
     * Returns whether the file contains any path mappings.
272
     *
273
     * @return bool Returns `true` if the file contains path mappings and
274
     *              `false` otherwise.
275
     */
276 1
    public function hasPathMappings()
277
    {
278 1
        return count($this->pathMappings) > 0;
279
    }
280
281
    /**
282
     * Adds a path mapping.
283
     *
284
     * @param PathMapping $mapping The path mapping.
285
     */
286 67
    public function addPathMapping(PathMapping $mapping)
287
    {
288 67
        $this->pathMappings[$mapping->getRepositoryPath()] = $mapping;
289
290 67
        ksort($this->pathMappings);
291 67
    }
292
293
    /**
294
     * Removes the path mapping for a repository path.
295
     *
296
     * @param string $repositoryPath The repository path.
297
     */
298 16
    public function removePathMapping($repositoryPath)
299
    {
300 16
        unset($this->pathMappings[$repositoryPath]);
301 16
    }
302
303
    /**
304
     * Removes all path mappings.
305
     */
306
    public function clearPathMappings()
307
    {
308
        $this->pathMappings = array();
309
    }
310
311
    /**
312
     * Returns the binding descriptors.
313
     *
314
     * @return BindingDescriptor[] The binding descriptors.
315
     */
316 123
    public function getBindingDescriptors()
317
    {
318 123
        return array_values($this->bindingDescriptors);
319
    }
320
321
    /**
322
     * Returns the binding descriptor with the given UUID.
323
     *
324
     * @param Uuid $uuid The UUID of the binding descriptor.
325
     *
326
     * @return BindingDescriptor The binding descriptor.
327
     *
328
     * @throws NoSuchBindingException If the UUID was not found.
329
     */
330 10
    public function getBindingDescriptor(Uuid $uuid)
331
    {
332 10
        $uuidString = $uuid->toString();
333
334 10
        if (!isset($this->bindingDescriptors[$uuidString])) {
335 1
            throw NoSuchBindingException::forUuid($uuid);
336
        }
337
338 9
        return $this->bindingDescriptors[$uuidString];
339
    }
340
341
    /**
342
     * Adds a binding descriptor.
343
     *
344
     * @param BindingDescriptor $descriptor The binding descriptor to add.
345
     */
346 66
    public function addBindingDescriptor(BindingDescriptor $descriptor)
347
    {
348 66
        $this->bindingDescriptors[$descriptor->getUuid()->toString()] = $descriptor;
349 66
    }
350
351
    /**
352
     * Removes a binding descriptor.
353
     *
354
     * @param Uuid $uuid The UUID of the binding descriptor to remove.
355
     */
356 9
    public function removeBindingDescriptor(Uuid $uuid)
357
    {
358 9
        unset($this->bindingDescriptors[$uuid->toString()]);
359 9
    }
360
361
    /**
362
     * Removes all binding descriptors.
363
     */
364
    public function clearBindingDescriptors()
365
    {
366
        $this->bindingDescriptors = array();
367
    }
368
369
    /**
370
     * Returns whether the binding descriptor exists in this file.
371
     *
372
     * @param Uuid $uuid The UUID of the binding descriptor.
373
     *
374
     * @return bool Whether the file contains the binding descriptor.
375
     */
376 13
    public function hasBindingDescriptor(Uuid $uuid)
377
    {
378 13
        return isset($this->bindingDescriptors[$uuid->toString()]);
379
    }
380
381
    /**
382
     * Returns whether the file contains any binding descriptors.
383
     *
384
     * @return bool Returns `true` if the file contains binding descriptors and
385
     *              `false` otherwise.
386
     */
387 1
    public function hasBindingDescriptors()
388
    {
389 1
        return count($this->bindingDescriptors) > 0;
390
    }
391
392
    /**
393
     * Adds a type descriptor.
394
     *
395
     * @param BindingTypeDescriptor $descriptor The type descriptor.
396
     */
397 85
    public function addTypeDescriptor(BindingTypeDescriptor $descriptor)
398
    {
399 85
        $this->typeDescriptors[$descriptor->getTypeName()] = $descriptor;
400 85
    }
401
402
    /**
403
     * Removes a type descriptor.
404
     *
405
     * @param string $typeName The type name.
406
     */
407 14
    public function removeTypeDescriptor($typeName)
408
    {
409 14
        unset($this->typeDescriptors[$typeName]);
410 14
    }
411
412
    /**
413
     * Removes all type descriptors.
414
     */
415
    public function clearTypeDescriptors()
416
    {
417
        $this->typeDescriptors = array();
418
    }
419
420
    /**
421
     * Returns the type descriptor with the given name.
422
     *
423
     * @param string $typeName The type name.
424
     *
425
     * @return BindingTypeDescriptor The type descriptor.
426
     */
427 13
    public function getTypeDescriptor($typeName)
428
    {
429 13
        return $this->typeDescriptors[$typeName];
430
    }
431
432
    /**
433
     * Returns the type descriptors.
434
     *
435
     * @return BindingTypeDescriptor[] The type descriptors.
436
     */
437 116
    public function getTypeDescriptors()
438
    {
439
        // Names as keys are for internal use only
440 116
        return array_values($this->typeDescriptors);
441
    }
442
443
    /**
444
     * Returns whether a type is defined in this file.
445
     *
446
     * @param string $typeName The type name.
447
     *
448
     * @return bool Whether the type is defined in the file.
449
     */
450 17
    public function hasTypeDescriptor($typeName)
451
    {
452 17
        return isset($this->typeDescriptors[$typeName]);
453
    }
454
455
    /**
456
     * Returns whether the file contains any type descriptors.
457
     *
458
     * @return bool Returns `true` if the file contains type descriptors and
459
     *              `false` otherwise.
460
     */
461 2
    public function hasTypeDescriptors()
462
    {
463 2
        return count($this->typeDescriptors) > 0;
464
    }
465
466
    /**
467
     * Sets an extra key in the package file.
468
     *
469
     * Extra keys can be freely set by the user. They are stored in a separate
470
     * area of the package file and not validated in any way.
471
     *
472
     * @param string $key   The name of the key.
473
     * @param mixed  $value The value to store.
474
     */
475 85
    public function setExtraKey($key, $value)
476
    {
477 85
        $this->extra[$key] = $value;
478 85
    }
479
480
    /**
481
     * Sets multiple extra keys at once.
482
     *
483
     * Existing extra keys are overridden.
484
     *
485
     * @param array $values The values indexed by their key names.
486
     *
487
     * @see setExtraKey()
488
     */
489 9
    public function setExtraKeys(array $values)
490
    {
491 9
        $this->extra = array();
492
493 9
        foreach ($values as $key => $value) {
494 9
            $this->setExtraKey($key, $value);
495
        }
496 9
    }
497
498
    /**
499
     * Sets multiple extra keys at once.
500
     *
501
     * Existing extra keys are preserved.
502
     *
503
     * @param array $values The values indexed by their key names.
504
     *
505
     * @see setExtraKey()
506
     */
507 1
    public function addExtraKeys(array $values)
508
    {
509 1
        foreach ($values as $key => $value) {
510 1
            $this->setExtraKey($key, $value);
511
        }
512 1
    }
513
514
    /**
515
     * Removes an extra key.
516
     *
517
     * @param string $key The name of the key.
518
     *
519
     * @see setExtraKey()
520
     */
521 7
    public function removeExtraKey($key)
522
    {
523 7
        unset($this->extra[$key]);
524 7
    }
525
526
    /**
527
     * Removes all extra keys.
528
     *
529
     * @see setExtraKey()
530
     */
531 4
    public function clearExtraKeys()
532
    {
533 4
        $this->extra = array();
534 4
    }
535
536
    /**
537
     * Returns the value of an extra key.
538
     *
539
     * @param string $key     The name of the key.
540
     * @param mixed  $default The value to return if the key was not set.
541
     *
542
     * @return mixed The value stored for the key.
543
     *
544
     * @see setExtraKey()
545
     */
546 96
    public function getExtraKey($key, $default = null)
547
    {
548 96
        return array_key_exists($key, $this->extra) ? $this->extra[$key] : $default;
549
    }
550
551
    /**
552
     * Returns all stored extra keys.
553
     *
554
     * @return array The stored values indexed by their key names.
555
     *
556
     * @see setExtraKey()
557
     */
558 38
    public function getExtraKeys()
559
    {
560 38
        return $this->extra;
561
    }
562
563
    /**
564
     * Returns whether the given extra key exists.
565
     *
566
     * @param string $key The name of the key.
567
     *
568
     * @return bool Returns `true` if the given extra key exists and `false`
569
     *              otherwise.
570
     *
571
     * @see setExtraKey()
572
     */
573 14
    public function hasExtraKey($key)
574
    {
575 14
        return array_key_exists($key, $this->extra);
576
    }
577
578
    /**
579
     * Returns whether the file contains any extra keys.
580
     *
581
     * @return bool Returns `true` if the file contains extra keys and `false`
582
     *              otherwise.
583
     *
584
     * @see setExtraKey()
585
     */
586 2
    public function hasExtraKeys()
587
    {
588 2
        return count($this->extra) > 0;
589
    }
590
}
591