Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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