Exceptions
2 weeks ago
Facades
2 weeks ago
Testing
2 weeks ago
Traits
2 weeks ago
AggregateServiceProvider.php
2 years ago
Benchmark.php
2 weeks ago
Carbon.php
2 weeks ago
Composer.php
2 weeks ago
ConfigurationUrlParser.php
2 weeks ago
DateFactory.php
2 weeks ago
Env.php
2 weeks ago
Fluent.php
2 weeks ago
HigherOrderTapProxy.php
2 years ago
HtmlString.php
2 years ago
InteractsWithTime.php
2 years ago
Js.php
2 weeks ago
Lottery.php
2 weeks ago
Manager.php
2 years ago
MessageBag.php
2 weeks ago
MultipleInstanceManager.php
2 weeks ago
NamespacedItemResolver.php
2 weeks ago
Optional.php
2 weeks ago
Pluralizer.php
2 weeks ago
ProcessUtils.php
2 weeks ago
Reflector.php
2 weeks ago
ServiceProvider.php
2 years ago
Str.php
2 weeks ago
Stringable.php
2 weeks ago
Timebox.php
2 weeks ago
ValidatedInput.php
2 weeks ago
ViewErrorBag.php
2 weeks ago
helpers.php
2 weeks ago
ServiceProvider.php
389 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Support; |
| 4 | |
| 5 | use Closure; |
| 6 | use IAWPSCOPED\Illuminate\Console\Application as Artisan; |
| 7 | use IAWPSCOPED\Illuminate\Contracts\Foundation\CachesConfiguration; |
| 8 | use IAWPSCOPED\Illuminate\Contracts\Foundation\CachesRoutes; |
| 9 | use IAWPSCOPED\Illuminate\Contracts\Support\DeferrableProvider; |
| 10 | use IAWPSCOPED\Illuminate\Database\Eloquent\Factory as ModelFactory; |
| 11 | use IAWPSCOPED\Illuminate\View\Compilers\BladeCompiler; |
| 12 | /** @internal */ |
| 13 | abstract class ServiceProvider |
| 14 | { |
| 15 | /** |
| 16 | * The application instance. |
| 17 | * |
| 18 | * @var \Illuminate\Contracts\Foundation\Application |
| 19 | */ |
| 20 | protected $app; |
| 21 | /** |
| 22 | * All of the registered booting callbacks. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | protected $bootingCallbacks = []; |
| 27 | /** |
| 28 | * All of the registered booted callbacks. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | protected $bootedCallbacks = []; |
| 33 | /** |
| 34 | * The paths that should be published. |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | public static $publishes = []; |
| 39 | /** |
| 40 | * The paths that should be published by group. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | public static $publishGroups = []; |
| 45 | /** |
| 46 | * Create a new service provider instance. |
| 47 | * |
| 48 | * @param \Illuminate\Contracts\Foundation\Application $app |
| 49 | * @return void |
| 50 | */ |
| 51 | public function __construct($app) |
| 52 | { |
| 53 | $this->app = $app; |
| 54 | } |
| 55 | /** |
| 56 | * Register any application services. |
| 57 | * |
| 58 | * @return void |
| 59 | */ |
| 60 | public function register() |
| 61 | { |
| 62 | // |
| 63 | } |
| 64 | /** |
| 65 | * Register a booting callback to be run before the "boot" method is called. |
| 66 | * |
| 67 | * @param \Closure $callback |
| 68 | * @return void |
| 69 | */ |
| 70 | public function booting(Closure $callback) |
| 71 | { |
| 72 | $this->bootingCallbacks[] = $callback; |
| 73 | } |
| 74 | /** |
| 75 | * Register a booted callback to be run after the "boot" method is called. |
| 76 | * |
| 77 | * @param \Closure $callback |
| 78 | * @return void |
| 79 | */ |
| 80 | public function booted(Closure $callback) |
| 81 | { |
| 82 | $this->bootedCallbacks[] = $callback; |
| 83 | } |
| 84 | /** |
| 85 | * Call the registered booting callbacks. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public function callBootingCallbacks() |
| 90 | { |
| 91 | $index = 0; |
| 92 | while ($index < \count($this->bootingCallbacks)) { |
| 93 | $this->app->call($this->bootingCallbacks[$index]); |
| 94 | $index++; |
| 95 | } |
| 96 | } |
| 97 | /** |
| 98 | * Call the registered booted callbacks. |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | public function callBootedCallbacks() |
| 103 | { |
| 104 | $index = 0; |
| 105 | while ($index < \count($this->bootedCallbacks)) { |
| 106 | $this->app->call($this->bootedCallbacks[$index]); |
| 107 | $index++; |
| 108 | } |
| 109 | } |
| 110 | /** |
| 111 | * Merge the given configuration with the existing configuration. |
| 112 | * |
| 113 | * @param string $path |
| 114 | * @param string $key |
| 115 | * @return void |
| 116 | */ |
| 117 | protected function mergeConfigFrom($path, $key) |
| 118 | { |
| 119 | if (!($this->app instanceof CachesConfiguration && $this->app->configurationIsCached())) { |
| 120 | $config = $this->app->make('config'); |
| 121 | $config->set($key, \array_merge(require $path, $config->get($key, []))); |
| 122 | } |
| 123 | } |
| 124 | /** |
| 125 | * Load the given routes file if routes are not already cached. |
| 126 | * |
| 127 | * @param string $path |
| 128 | * @return void |
| 129 | */ |
| 130 | protected function loadRoutesFrom($path) |
| 131 | { |
| 132 | if (!($this->app instanceof CachesRoutes && $this->app->routesAreCached())) { |
| 133 | require $path; |
| 134 | } |
| 135 | } |
| 136 | /** |
| 137 | * Register a view file namespace. |
| 138 | * |
| 139 | * @param string|array $path |
| 140 | * @param string $namespace |
| 141 | * @return void |
| 142 | */ |
| 143 | protected function loadViewsFrom($path, $namespace) |
| 144 | { |
| 145 | $this->callAfterResolving('view', function ($view) use($path, $namespace) { |
| 146 | if (isset($this->app->config['view']['paths']) && \is_array($this->app->config['view']['paths'])) { |
| 147 | foreach ($this->app->config['view']['paths'] as $viewPath) { |
| 148 | if (\is_dir($appPath = $viewPath . '/vendor/' . $namespace)) { |
| 149 | $view->addNamespace($namespace, $appPath); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | $view->addNamespace($namespace, $path); |
| 154 | }); |
| 155 | } |
| 156 | /** |
| 157 | * Register the given view components with a custom prefix. |
| 158 | * |
| 159 | * @param string $prefix |
| 160 | * @param array $components |
| 161 | * @return void |
| 162 | */ |
| 163 | protected function loadViewComponentsAs($prefix, array $components) |
| 164 | { |
| 165 | $this->callAfterResolving(BladeCompiler::class, function ($blade) use($prefix, $components) { |
| 166 | foreach ($components as $alias => $component) { |
| 167 | $blade->component($component, \is_string($alias) ? $alias : null, $prefix); |
| 168 | } |
| 169 | }); |
| 170 | } |
| 171 | /** |
| 172 | * Register a translation file namespace. |
| 173 | * |
| 174 | * @param string $path |
| 175 | * @param string $namespace |
| 176 | * @return void |
| 177 | */ |
| 178 | protected function loadTranslationsFrom($path, $namespace) |
| 179 | { |
| 180 | $this->callAfterResolving('translator', function ($translator) use($path, $namespace) { |
| 181 | $translator->addNamespace($namespace, $path); |
| 182 | }); |
| 183 | } |
| 184 | /** |
| 185 | * Register a JSON translation file path. |
| 186 | * |
| 187 | * @param string $path |
| 188 | * @return void |
| 189 | */ |
| 190 | protected function loadJsonTranslationsFrom($path) |
| 191 | { |
| 192 | $this->callAfterResolving('translator', function ($translator) use($path) { |
| 193 | $translator->addJsonPath($path); |
| 194 | }); |
| 195 | } |
| 196 | /** |
| 197 | * Register database migration paths. |
| 198 | * |
| 199 | * @param array|string $paths |
| 200 | * @return void |
| 201 | */ |
| 202 | protected function loadMigrationsFrom($paths) |
| 203 | { |
| 204 | $this->callAfterResolving('migrator', function ($migrator) use($paths) { |
| 205 | foreach ((array) $paths as $path) { |
| 206 | $migrator->path($path); |
| 207 | } |
| 208 | }); |
| 209 | } |
| 210 | /** |
| 211 | * Register Eloquent model factory paths. |
| 212 | * |
| 213 | * @deprecated Will be removed in a future Laravel version. |
| 214 | * |
| 215 | * @param array|string $paths |
| 216 | * @return void |
| 217 | */ |
| 218 | protected function loadFactoriesFrom($paths) |
| 219 | { |
| 220 | $this->callAfterResolving(ModelFactory::class, function ($factory) use($paths) { |
| 221 | foreach ((array) $paths as $path) { |
| 222 | $factory->load($path); |
| 223 | } |
| 224 | }); |
| 225 | } |
| 226 | /** |
| 227 | * Setup an after resolving listener, or fire immediately if already resolved. |
| 228 | * |
| 229 | * @param string $name |
| 230 | * @param callable $callback |
| 231 | * @return void |
| 232 | */ |
| 233 | protected function callAfterResolving($name, $callback) |
| 234 | { |
| 235 | $this->app->afterResolving($name, $callback); |
| 236 | if ($this->app->resolved($name)) { |
| 237 | $callback($this->app->make($name), $this->app); |
| 238 | } |
| 239 | } |
| 240 | /** |
| 241 | * Register paths to be published by the publish command. |
| 242 | * |
| 243 | * @param array $paths |
| 244 | * @param mixed $groups |
| 245 | * @return void |
| 246 | */ |
| 247 | protected function publishes(array $paths, $groups = null) |
| 248 | { |
| 249 | $this->ensurePublishArrayInitialized($class = static::class); |
| 250 | static::$publishes[$class] = \array_merge(static::$publishes[$class], $paths); |
| 251 | foreach ((array) $groups as $group) { |
| 252 | $this->addPublishGroup($group, $paths); |
| 253 | } |
| 254 | } |
| 255 | /** |
| 256 | * Ensure the publish array for the service provider is initialized. |
| 257 | * |
| 258 | * @param string $class |
| 259 | * @return void |
| 260 | */ |
| 261 | protected function ensurePublishArrayInitialized($class) |
| 262 | { |
| 263 | if (!\array_key_exists($class, static::$publishes)) { |
| 264 | static::$publishes[$class] = []; |
| 265 | } |
| 266 | } |
| 267 | /** |
| 268 | * Add a publish group / tag to the service provider. |
| 269 | * |
| 270 | * @param string $group |
| 271 | * @param array $paths |
| 272 | * @return void |
| 273 | */ |
| 274 | protected function addPublishGroup($group, $paths) |
| 275 | { |
| 276 | if (!\array_key_exists($group, static::$publishGroups)) { |
| 277 | static::$publishGroups[$group] = []; |
| 278 | } |
| 279 | static::$publishGroups[$group] = \array_merge(static::$publishGroups[$group], $paths); |
| 280 | } |
| 281 | /** |
| 282 | * Get the paths to publish. |
| 283 | * |
| 284 | * @param string|null $provider |
| 285 | * @param string|null $group |
| 286 | * @return array |
| 287 | */ |
| 288 | public static function pathsToPublish($provider = null, $group = null) |
| 289 | { |
| 290 | if (!\is_null($paths = static::pathsForProviderOrGroup($provider, $group))) { |
| 291 | return $paths; |
| 292 | } |
| 293 | return \IAWPSCOPED\collect(static::$publishes)->reduce(function ($paths, $p) { |
| 294 | return \array_merge($paths, $p); |
| 295 | }, []); |
| 296 | } |
| 297 | /** |
| 298 | * Get the paths for the provider or group (or both). |
| 299 | * |
| 300 | * @param string|null $provider |
| 301 | * @param string|null $group |
| 302 | * @return array |
| 303 | */ |
| 304 | protected static function pathsForProviderOrGroup($provider, $group) |
| 305 | { |
| 306 | if ($provider && $group) { |
| 307 | return static::pathsForProviderAndGroup($provider, $group); |
| 308 | } elseif ($group && \array_key_exists($group, static::$publishGroups)) { |
| 309 | return static::$publishGroups[$group]; |
| 310 | } elseif ($provider && \array_key_exists($provider, static::$publishes)) { |
| 311 | return static::$publishes[$provider]; |
| 312 | } elseif ($group || $provider) { |
| 313 | return []; |
| 314 | } |
| 315 | } |
| 316 | /** |
| 317 | * Get the paths for the provider and group. |
| 318 | * |
| 319 | * @param string $provider |
| 320 | * @param string $group |
| 321 | * @return array |
| 322 | */ |
| 323 | protected static function pathsForProviderAndGroup($provider, $group) |
| 324 | { |
| 325 | if (!empty(static::$publishes[$provider]) && !empty(static::$publishGroups[$group])) { |
| 326 | return \array_intersect_key(static::$publishes[$provider], static::$publishGroups[$group]); |
| 327 | } |
| 328 | return []; |
| 329 | } |
| 330 | /** |
| 331 | * Get the service providers available for publishing. |
| 332 | * |
| 333 | * @return array |
| 334 | */ |
| 335 | public static function publishableProviders() |
| 336 | { |
| 337 | return \array_keys(static::$publishes); |
| 338 | } |
| 339 | /** |
| 340 | * Get the groups available for publishing. |
| 341 | * |
| 342 | * @return array |
| 343 | */ |
| 344 | public static function publishableGroups() |
| 345 | { |
| 346 | return \array_keys(static::$publishGroups); |
| 347 | } |
| 348 | /** |
| 349 | * Register the package's custom Artisan commands. |
| 350 | * |
| 351 | * @param array|mixed $commands |
| 352 | * @return void |
| 353 | */ |
| 354 | public function commands($commands) |
| 355 | { |
| 356 | $commands = \is_array($commands) ? $commands : \func_get_args(); |
| 357 | Artisan::starting(function ($artisan) use($commands) { |
| 358 | $artisan->resolveCommands($commands); |
| 359 | }); |
| 360 | } |
| 361 | /** |
| 362 | * Get the services provided by the provider. |
| 363 | * |
| 364 | * @return array |
| 365 | */ |
| 366 | public function provides() |
| 367 | { |
| 368 | return []; |
| 369 | } |
| 370 | /** |
| 371 | * Get the events that trigger this service provider to register. |
| 372 | * |
| 373 | * @return array |
| 374 | */ |
| 375 | public function when() |
| 376 | { |
| 377 | return []; |
| 378 | } |
| 379 | /** |
| 380 | * Determine if the provider is deferred. |
| 381 | * |
| 382 | * @return bool |
| 383 | */ |
| 384 | public function isDeferred() |
| 385 | { |
| 386 | return $this instanceof DeferrableProvider; |
| 387 | } |
| 388 | } |
| 389 |