Passed
Push — master ( bc35bb...9630a7 )
by Caen
03:22 queued 11s
created

ManagesExtensions::getExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Concerns;
6
7
use BadMethodCallException;
8
use InvalidArgumentException;
9
use function array_keys;
10
use function array_map;
11
use function array_merge;
12
use function array_unique;
13
use function in_array;
14
use function is_subclass_of;
15
16
/**
17
 * @internal Single-use trait for the HydeKernel class.
18
 *
19
 * @see \Hyde\Foundation\HydeKernel
20
 */
21
trait ManagesExtensions
22
{
23
    /**
24
     * Register a HydePHP extension within the HydeKernel.
25
     *
26
     * Typically, you would call this method in the register method of a service provider.
27
     * If your package uses the standard Laravel (Composer) package discovery feature,
28
     * the extension will automatically be enabled when the package is installed.
29
     *
30
     * @param  class-string<\Hyde\Foundation\Concerns\HydeExtension>  $extension
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Found...Concerns\HydeExtension> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Foundation\Concerns\HydeExtension>.
Loading history...
31
     */
32
    public function registerExtension(string $extension): void
33
    {
34
        if ($this->booted) {
35
            // We throw an exception here to prevent the developer from registering aa extension after the Kernel has been booted.
36
            // The reason we do this is because at this point all the source files have already been discovered and parsed.
37
            // If we allowed new classes after this point, we would have to reboot everything which adds complexity.
38
39
            throw new BadMethodCallException('Cannot register an extension after the Kernel has been booted.');
40
        }
41
42
        if (! is_subclass_of($extension, HydeExtension::class)) {
43
            // We want to make sure that the extension class extends the HydeExtension class,
44
            // so that we won't have to check the methods we need to call exist later on.
45
46
            throw new InvalidArgumentException('The specified class must extend the HydeExtension class.');
47
        }
48
49
        if (in_array($extension, $this->getRegisteredExtensions(), true)) {
50
            // While throwing an exception here is not required since we are using an associative array,
51
            // it may be helpful for the developer to know that their registration logic may be flawed.
52
53
            throw new InvalidArgumentException("Extension [$extension] is already registered.");
54
        }
55
56
        $this->extensions[$extension] = new $extension();
0 ignored issues
show
Bug Best Practice introduced by
The property extensions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
    }
58
59
    /**
60
     * Get the singleton instance of the specified extension.
61
     *
62
     * @param  class-string<\Hyde\Foundation\Concerns\HydeExtension>  $extension
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Found...Concerns\HydeExtension> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Foundation\Concerns\HydeExtension>.
Loading history...
63
     */
64
    public function getExtension(string $extension): HydeExtension
65
    {
66
        if (! isset($this->extensions[$extension])) {
67
            throw new InvalidArgumentException("Extension [$extension] is not registered.");
68
        }
69
70
        return $this->extensions[$extension];
71
    }
72
73
    /**
74
     * Determine if the specified extension is registered.
75
     *
76
     * @param  class-string<\Hyde\Foundation\Concerns\HydeExtension>  $extension
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Found...Concerns\HydeExtension> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Foundation\Concerns\HydeExtension>.
Loading history...
77
     */
78
    public function hasExtension(string $extension): bool
79
    {
80
        return isset($this->extensions[$extension]);
81
    }
82
83
    /** @return array<\Hyde\Foundation\Concerns\HydeExtension> */
84
    public function getExtensions(): array
85
    {
86
        return $this->extensions;
87
    }
88
89
    /** @return array<class-string<\Hyde\Foundation\Concerns\HydeExtension>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Hyde...oncerns\HydeExtension>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Hyde\Foundation\Concerns\HydeExtension>>.
Loading history...
90
    public function getRegisteredExtensions(): array
91
    {
92
        return array_keys($this->extensions);
93
    }
94
95
    /** @return array<class-string<\Hyde\Pages\Concerns\HydePage>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Hyde...ges\Concerns\HydePage>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Hyde\Pages\Concerns\HydePage>>.
Loading history...
96
    public function getRegisteredPageClasses(): array
97
    {
98
        return array_unique(array_merge(...array_map(function (string $extension): array {
99
            /** @var <class-string<\Hyde\Foundation\Concerns\HydeExtension>> $extension */
100
            return $extension::getPageClasses();
101
        }, $this->getRegisteredExtensions())));
102
    }
103
}
104