-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService_Provider.php
More file actions
244 lines (208 loc) · 6.07 KB
/
Copy pathService_Provider.php
File metadata and controls
244 lines (208 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* Service_Provider class file.
*
* @package Mantle
*/
namespace Mantle\Support;
use Mantle\Console\Application as Console_Application;
use Mantle\Console\Command;
use Mantle\Contracts\Application;
use Mantle\Support\Traits\Hookable;
use Mantle\Types\Validator;
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait};
use Symfony\Component\Console\Command\Command as Symfony_Command;
use function Mantle\Support\Helpers\collect;
/**
* Application Service Provider
*/
abstract class Service_Provider implements LoggerAwareInterface {
use Hookable;
use LoggerAwareTrait;
/**
* The paths that should be published.
*
* @var array<class-string<Service_Provider>, string[]>
*/
public static array $publishes = [];
/**
* The paths that should be published by group.
*
* @var array<string, string[]>
*/
public static array $publish_tags = [];
/**
* The application instance.
*/
protected Application $app;
/**
* Commands to register.
* Register commands through `Service_Provider::add_command()`.
*
* @var \Mantle\Console\Command[]
*/
protected array $commands;
/**
* Create a new service provider instance.
*
* @param Application $app Application Instance.
*/
public function __construct( Application $app ) {
$this->app = $app;
}
/**
* Register the service provider.
*
* @phpstan-ignore missingType.return
*/
public function register() {}
/**
* Boot the service provider.
*
* @phpstan-ignore missingType.return
*/
public function boot() {}
/**
* Bootstrap services.
*/
public function boot_provider(): void {
if ( isset( $this->app['log'] ) ) {
$this->setLogger( $this->app['log']->driver() );
}
if ( $this->should_boot_provider() ) {
$this->register_hooks();
$this->boot();
}
}
/**
* Register a console command.
*
* @param array<class-string<Command|Symfony_Command>>|class-string<Command|Symfony_Command>|Symfony_Command|Command $command Command instance or class name to register.
*/
public function add_command( array|string|Symfony_Command|Command $command ): Service_Provider {
Console_Application::starting(
fn ( Console_Application $console ) => $console->resolve_commands( $command )
);
return $this;
}
/**
* Setup an after resolving listener, or fire immediately if already resolved.
*
* @param string $name Abstract name.
* @param \Closure $callback Callback.
*/
protected function call_after_resolving( string $name, \Closure $callback ): void {
$this->app->after_resolving( $name, $callback );
if ( $this->app->resolved( $name ) ) {
$callback( $this->app->make( $name ), $this->app );
}
}
/**
* Register paths to be published by the publish command.
*
* @param string[] $paths Paths to publish.
* @param string|array<string>|null $tags Tags to publish.
*/
public function publishes( array $paths, $tags = null ): void {
$class = static::class;
if ( ! array_key_exists( $class, static::$publishes ) ) {
static::$publishes[ $class ] = [];
}
static::$publishes[ $class ] = array_merge( static::$publishes[ $class ], $paths );
foreach ( (array) $tags as $tag ) {
if ( ! array_key_exists( $tag, static::$publish_tags ) ) {
static::$publish_tags[ $tag ] = [];
}
static::$publish_tags[ $tag ] = array_merge(
static::$publish_tags[ $tag ],
$paths,
);
}
}
/**
* Get the service providers available for publishing.
*
* @return array<class-string<Service_Provider>>
*/
public static function publishable_providers(): array {
return array_keys( static::$publishes );
}
/**
* Get the groups available for publishing.
*
* @return string[]
*/
public static function publishable_tags(): array {
return array_keys( static::$publish_tags );
}
/**
* Load routes from the given path.
*
* @param string $path Path to routes file.
*/
public function load_routes_from( string $path ): void {
require $path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
}
/**
* Load views from the given path.
*
* @param string $path Path to views directory.
* @param string $alias Alias to register views under.
*/
public function load_views_from( string $path, string $alias ): void {
$this->call_after_resolving(
'view.loader',
fn ( \Mantle\Http\View\View_Finder $finder ) => $finder->add_path( $path, $alias ),
);
}
/**
* Get the paths to publish.
*
* Passing both a provider and a tag will return all paths that are
* published by that provider and tag.
*
* @param class-string<Service_Provider>|array<class-string<Service_Provider>>|null $providers The service provider class name.
* @param string|array<string>|null $tags The tag name.
* @return array<string, string> The paths to publish. Index is the source path, value is the destination path.
*/
public static function paths_to_publish( array|string|null $providers = null, array|string|null $tags = null ): array {
if ( ! $providers && ! $tags ) {
return [];
}
$provider_paths = collect();
$tag_paths = collect();
if ( $providers ) {
foreach ( (array) $providers as $item ) {
$provider_paths = $provider_paths->merge( static::$publishes[ $item ] ?? [] );
}
}
if ( $tags ) {
foreach ( (array) $tags as $item ) {
$tag_paths = $tag_paths->merge( static::$publish_tags[ $item ] ?? [] );
}
}
return match ( true ) {
! empty( $providers ) && ! empty( $tags ) => $provider_paths->intersect_by_keys( $tag_paths )->all(),
! empty( $providers ) => $provider_paths->all(),
default => $tag_paths->all(),
};
}
/**
* Determine if the provider should be booted.
*
* Checks for any Validator attributes on the class and runs them.
*/
protected function should_boot_provider(): bool {
$validator = Reflector::get_attributes_for_class( $this, Validator::class, \ReflectionAttribute::IS_INSTANCEOF );
if ( empty( $validator ) ) {
return true;
}
foreach ( $validator as $attribute ) {
$instance = $attribute->newInstance();
if ( ! $instance->validate() ) {
return false;
}
}
return true;
}
}