1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Registration loader |
7
|
|
|
* |
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
19
|
|
|
* |
20
|
|
|
* @author Glynn Quelch <[email protected]> |
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
22
|
|
|
* @package PinkCrab\Perique\Registration |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace PinkCrab\Perique\Services\Registration; |
26
|
|
|
|
27
|
|
|
use PinkCrab\Loader\Hook_Loader; |
28
|
|
|
use PinkCrab\Perique\Application\Hooks; |
29
|
|
|
use PinkCrab\Perique\Interfaces\DI_Container; |
30
|
|
|
use PinkCrab\Perique\Interfaces\Registration_Middleware; |
31
|
|
|
use PinkCrab\Perique\Exceptions\Module_Manager_Exception; |
32
|
|
|
|
33
|
|
|
class Registration_Service { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Holds all the defined registration middlewares |
37
|
|
|
* |
38
|
|
|
* @var array<Registration_Middleware> |
39
|
|
|
*/ |
40
|
|
|
protected array $middleware = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Holds all classes that are to be registered. |
44
|
|
|
* |
45
|
|
|
* @var array<string> |
46
|
|
|
*/ |
47
|
|
|
protected array $class_list = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Access to the DI Container |
51
|
|
|
* |
52
|
|
|
* @var DI_Container |
53
|
|
|
*/ |
54
|
|
|
protected DI_Container $di_container; |
55
|
|
|
|
56
|
|
|
public function __construct( DI_Container $di_container ) { |
57
|
|
|
$this->di_container = $di_container; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Pushes a piece of middleware to the collection. |
62
|
|
|
* |
63
|
|
|
* @param Registration_Middleware $middleware |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
|
|
public function push_middleware( Registration_Middleware $middleware ): self { |
67
|
|
|
$this->middleware[ \get_class( $middleware ) ] = $middleware; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Adds a class to the list of classes to be registered. |
73
|
|
|
* @template Class_Name of object |
74
|
|
|
* @param class-string<Class_Name> $class |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
public function push_class( string $class ): self { |
77
|
|
|
// If the class is already in the list, skip. |
78
|
|
|
if ( \in_array( $class, $this->class_list, true ) ) { |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// If $class is not a class, throw exception. |
83
|
|
|
if ( ! \class_exists( $class ) ) { |
84
|
|
|
throw Module_Manager_Exception::none_class_string_passed_to_registration( $class ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->class_list[] = $class; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Runs all the defined classes through the middleware stack. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function process(): void { |
97
|
|
|
// Filter all classes, before processing. |
98
|
|
|
$class_list = apply_filters( Hooks::APP_INIT_REGISTRATION_CLASS_LIST, $this->class_list ); |
99
|
|
|
|
100
|
|
|
// If class list is empty, skip. |
101
|
|
|
if ( empty( $class_list ) ) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ( $this->middleware as $middleware ) { |
106
|
|
|
// Run middleware setup |
107
|
|
|
$middleware->setup(); |
108
|
|
|
|
109
|
|
|
// Pass each class to the middleware. |
110
|
|
|
foreach ( $class_list as $class ) { |
111
|
|
|
// Construct class using container, |
112
|
|
|
$class_instance = $this->di_container->create( $class ); |
113
|
|
|
|
114
|
|
|
// if valid object process via current middleware |
115
|
|
|
if ( is_object( $class_instance ) ) { |
116
|
|
|
$middleware->process( $class_instance ); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Run middleware setup |
121
|
|
|
$middleware->tear_down(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|