Passed
Push — master ( 7cab09...7d94f9 )
by Jonas
13:15
created

Application::path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LumenWpApp;
4
5
use Illuminate\Config\Repository as ConfigRepository;
6
use Illuminate\Container\Container;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Log\LogManager;
9
use Illuminate\Support\Composer;
10
use Illuminate\Support\Facades\Facade;
11
use Illuminate\Support\ServiceProvider;
12
use Illuminate\Support\Str;
13
use RuntimeException;
14
15
class Application extends Container
16
{
17
    /**
18
     * Indicates if the class aliases have been registered.
19
     *
20
     * @var bool
21
     */
22
    protected static $aliasesRegistered = false;
23
24
    /**
25
     * The base path of the application installation.
26
     *
27
     * @var string
28
     */
29
    protected $basePath;
30
31
    /**
32
     * All of the loaded configuration files.
33
     *
34
     * @var array
35
     */
36
    protected $loadedConfigurations = [];
37
38
    /**
39
     * Indicates if the application has "booted".
40
     *
41
     * @var bool
42
     */
43
    protected $booted = false;
44
45
    /**
46
     * The loaded service providers.
47
     *
48
     * @var array
49
     */
50
    protected $loadedProviders = [];
51
52
    /**
53
     * The service binding methods that have been executed.
54
     *
55
     * @var array
56
     */
57
    protected $ranServiceBinders = [];
58
59
    /**
60
     * The custom storage path defined by the developer.
61
     *
62
     * @var string
63
     */
64
    protected $storagePath;
65
66
    /**
67
     * The application namespace.
68
     *
69
     * @var string
70
     */
71
    protected $namespace;
72
73
    /**
74
     * The Router instance.
75
     *
76
     * @var \Laravel\Lumen\Routing\Router
0 ignored issues
show
Bug introduced by
The type Laravel\Lumen\Routing\Router was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
     */
78
    public $router;
79
80
    /**
81
     * Create a new Lumen application instance.
82
     *
83
     * @param  string|null  $basePath
84
     * @return void
85
     */
86
    public function __construct($basePath = null)
87
    {
88
        if (! empty(env('APP_TIMEZONE'))) {
89
            date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
90
        }
91
92
        $this->basePath = $basePath;
93
94
        $this->bootstrapContainer();
95
    }
96
97
    /**
98
     * Bootstrap the application container.
99
     *
100
     * @return void
101
     */
102
    protected function bootstrapContainer()
103
    {
104
        static::setInstance($this);
105
106
        $this->instance('app', $this);
107
        $this->instance(self::class, $this);
108
109
        $this->instance('path', $this->path());
110
111
        $this->instance('env', $this->environment());
112
113
        $this->registerContainerAliases();
114
    }
115
116
    /**
117
     * Register all of the configured providers.
118
     *
119
     * @return $this
120
     */
121
    public function registerConfiguredProviders()
122
    {
123
        foreach ($this->config['app.providers'] as $provider) {
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on LumenWpApp\Application. Since you implemented __get, consider adding a @property annotation.
Loading history...
124
            $this->register($provider);
125
        }
126
127
        return $this;
128
    }
129
130
    /**
131
     * Get the version number of the application.
132
     *
133
     * @return string
134
     */
135
    public function version()
136
    {
137
        return 'Lumen (7.1.1) (Laravel Components ^7.0)';
138
    }
139
140
    /**
141
     * Determine if the application is currently down for maintenance.
142
     *
143
     * @return bool
144
     */
145
    public function isDownForMaintenance()
146
    {
147
        return false;
148
    }
149
150
    /**
151
     * Get or check the current application environment.
152
     *
153
     * @param  mixed
154
     * @return string
155
     */
156
    public function environment()
157
    {
158
        $env = env('WP_ENV', config('app.env', 'production'));
159
160
        if (func_num_args() > 0) {
161
            $patterns = is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args();
162
163
            foreach ($patterns as $pattern) {
164
                if (Str::is($pattern, $env)) {
165
                    return true;
166
                }
167
            }
168
169
            return false;
170
        }
171
172
        return $env;
173
    }
174
175
    /**
176
     * Register a service provider with the application.
177
     *
178
     * @param  \Illuminate\Support\ServiceProvider|string  $provider
179
     * @return \Illuminate\Support\ServiceProvider
180
     */
181
    public function register($provider)
182
    {
183
        if (! $provider instanceof ServiceProvider) {
184
            $provider = new $provider($this);
185
        }
186
187
        if (array_key_exists($providerName = get_class($provider), $this->loadedProviders)) {
188
            return;
189
        }
190
191
        $this->loadedProviders[$providerName] = $provider;
192
193
        if (method_exists($provider, 'register')) {
194
            $provider->register();
195
        }
196
197
        if ($this->booted) {
198
            $this->bootProvider($provider);
199
        }
200
    }
201
202
    /**
203
     * Register a deferred provider and service.
204
     *
205
     * @param  string  $provider
206
     * @return void
207
     */
208
    public function registerDeferredProvider($provider)
209
    {
210
        return $this->register($provider);
211
    }
212
213
    /**
214
     * Boots the registered providers.
215
     */
216
    public function boot()
217
    {
218
        if ($this->booted) {
219
            return;
220
        }
221
222
        array_walk($this->loadedProviders, function ($p) {
223
            $this->bootProvider($p);
224
        });
225
226
        $this->booted = true;
227
    }
228
229
    /**
230
     * Boot the given service provider.
231
     *
232
     * @param  \Illuminate\Support\ServiceProvider  $provider
233
     * @return mixed
234
     */
235
    protected function bootProvider(ServiceProvider $provider)
236
    {
237
        if (method_exists($provider, 'boot')) {
238
            return $this->call([$provider, 'boot']);
239
        }
240
    }
241
242
    /**
243
     * Resolve the given type from the container.
244
     *
245
     * @param  string  $abstract
246
     * @param  array  $parameters
247
     * @return mixed
248
     */
249
    public function make($abstract, array $parameters = [])
250
    {
251
        $abstract = $this->getAlias($abstract);
252
253
        if (! $this->bound($abstract) &&
254
            array_key_exists($abstract, $this->availableBindings) &&
255
            ! array_key_exists($this->availableBindings[$abstract], $this->ranServiceBinders)) {
256
            $this->{$method = $this->availableBindings[$abstract]}();
257
258
            $this->ranServiceBinders[$method] = true;
259
        }
260
261
        return parent::make($abstract, $parameters);
262
    }
263
264
    /**
265
     * Register container bindings for the application.
266
     *
267
     * @return void
268
     */
269
    protected function registerCacheBindings()
270
    {
271
        $this->singleton('cache', function () {
272
            return $this->loadComponent('cache', 'Illuminate\Cache\CacheServiceProvider');
273
        });
274
        $this->singleton('cache.store', function () {
275
            return $this->loadComponent('cache', 'Illuminate\Cache\CacheServiceProvider', 'cache.store');
276
        });
277
    }
278
279
    /**
280
     * Register container bindings for the application.
281
     *
282
     * @return void
283
     */
284
    protected function registerComposerBindings()
285
    {
286
        $this->singleton('composer', function ($app) {
287
            return new Composer($app->make('files'), $this->basePath());
288
        });
289
    }
290
291
    /**
292
     * Register container bindings for the application.
293
     *
294
     * @return void
295
     */
296
    protected function registerConfigBindings()
297
    {
298
        $this->singleton('config', function () {
299
            return new ConfigRepository;
300
        });
301
    }
302
303
    /**
304
     * Register container bindings for the application.
305
     *
306
     * @return void
307
     */
308
    protected function registerEncrypterBindings()
309
    {
310
        $this->singleton('encrypter', function () {
311
            return $this->loadComponent('app', 'Illuminate\Encryption\EncryptionServiceProvider', 'encrypter');
312
        });
313
    }
314
315
    /**
316
     * Register container bindings for the application.
317
     *
318
     * @return void
319
     */
320
    protected function registerFilesBindings()
321
    {
322
        $this->singleton('files', function () {
323
            return new Filesystem;
324
        });
325
    }
326
327
    /**
328
     * Register container bindings for the application.
329
     *
330
     * @return void
331
     */
332
    protected function registerFilesystemBindings()
333
    {
334
        $this->singleton('filesystem', function () {
335
            return $this->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem');
336
        });
337
        $this->singleton('filesystem.disk', function () {
338
            return $this->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem.disk');
339
        });
340
        $this->singleton('filesystem.cloud', function () {
341
            return $this->loadComponent('filesystems', 'Illuminate\Filesystem\FilesystemServiceProvider', 'filesystem.cloud');
342
        });
343
    }
344
345
    /**
346
     * Register container bindings for the application.
347
     *
348
     * @return void
349
     */
350
    protected function registerHashBindings()
351
    {
352
        $this->singleton('hash', function () {
353
            $this->register('Illuminate\Hashing\HashServiceProvider');
354
355
            return $this->make('hash');
356
        });
357
    }
358
359
    /**
360
     * Register container bindings for the application.
361
     *
362
     * @return void
363
     */
364
    protected function registerLogBindings()
365
    {
366
        $this->singleton('Psr\Log\LoggerInterface', function () {
367
            $this->configure('logging');
368
369
            return new LogManager($this);
0 ignored issues
show
Bug introduced by
$this of type LumenWpApp\Application is incompatible with the type Illuminate\Contracts\Foundation\Application expected by parameter $app of Illuminate\Log\LogManager::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

369
            return new LogManager(/** @scrutinizer ignore-type */ $this);
Loading history...
370
        });
371
    }
372
373
    /**
374
     * Register container bindings for the application.
375
     *
376
     * @return void
377
     */
378
    protected function registerEventBindings()
379
    {
380
        $this->singleton('events', function () {
381
            $this->register('Illuminate\Events\EventServiceProvider');
382
            return $this->make('events');
383
        });
384
    }
385
386
    /**
387
     * Register container bindings for the application.
388
     *
389
     * @return void
390
     */
391
    protected function registerTranslationBindings()
392
    {
393
        $this->singleton('translator', function () {
394
            $this->configure('app');
395
396
            $this->instance('path.lang', $this->getLanguagePath());
397
398
            $this->register('Illuminate\Translation\TranslationServiceProvider');
399
400
            return $this->make('translator');
401
        });
402
    }
403
404
    /**
405
     * Get the path to the application's language files.
406
     *
407
     * @return string
408
     */
409
    protected function getLanguagePath()
410
    {
411
        if (is_dir($langPath = $this->basePath().'/resources/lang')) {
412
            return $langPath;
413
        } else {
414
            return __DIR__.'/../resources/lang';
415
        }
416
    }
417
418
    /**
419
     * Register container bindings for the application.
420
     *
421
     * @return void
422
     */
423
    protected function registerValidatorBindings()
424
    {
425
        $this->singleton('validator', function () {
426
            $this->register('Illuminate\Validation\ValidationServiceProvider');
427
428
            return $this->make('validator');
429
        });
430
    }
431
432
    /**
433
     * Configure and load the given component and provider.
434
     *
435
     * @param  string  $config
436
     * @param  array|string  $providers
437
     * @param  string|null  $return
438
     * @return mixed
439
     */
440
    public function loadComponent($config, $providers, $return = null)
441
    {
442
        $this->configure($config);
443
444
        foreach ((array) $providers as $provider) {
445
            $this->register($provider);
446
        }
447
448
        return $this->make($return ?: $config);
449
    }
450
451
    /**
452
     * Load a configuration file into the application.
453
     *
454
     * @param  string  $name
455
     * @return void
456
     */
457
    public function configure($name)
458
    {
459
        if (isset($this->loadedConfigurations[$name])) {
460
            return;
461
        }
462
463
        $this->loadedConfigurations[$name] = true;
464
465
        $path = $this->getConfigurationPath($name);
466
467
        if ($path) {
468
            $this->make('config')->set($name, require $path);
469
        }
470
    }
471
472
    /**
473
     * Get the path to the given configuration file.
474
     *
475
     * If no name is provided, then we'll return the path to the config folder.
476
     *
477
     * @param  string|null  $name
478
     * @return string
479
     */
480
    public function getConfigurationPath($name = null)
481
    {
482
        if (! $name) {
483
            $appConfigDir = $this->basePath('config').'/';
484
485
            if (file_exists($appConfigDir)) {
486
                return $appConfigDir;
487
            } elseif (file_exists($path = __DIR__.'/../config/')) {
488
                return $path;
489
            }
490
        } else {
491
            $appConfigPath = $this->basePath('config').'/'.$name.'.php';
492
493
            if (file_exists($appConfigPath)) {
494
                return $appConfigPath;
495
            } elseif (file_exists($path = __DIR__.'/../config/'.$name.'.php')) {
496
                return $path;
497
            }
498
        }
499
    }
500
501
    /**
502
     * Register the facades for the application.
503
     *
504
     * @param  bool  $aliases
505
     * @param  array $userAliases
506
     * @return void
507
     */
508
    public function withFacades($aliases = true, $userAliases = [])
509
    {
510
        Facade::setFacadeApplication($this);
0 ignored issues
show
Bug introduced by
$this of type LumenWpApp\Application is incompatible with the type Illuminate\Contracts\Foundation\Application expected by parameter $app of Illuminate\Support\Facad...:setFacadeApplication(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

510
        Facade::setFacadeApplication(/** @scrutinizer ignore-type */ $this);
Loading history...
511
512
        if ($aliases) {
513
            $this->withAliases($userAliases);
514
        }
515
    }
516
517
    /**
518
     * Register the aliases for the application.
519
     *
520
     * @param  array  $userAliases
521
     * @return void
522
     */
523
    public function withAliases($userAliases = [])
524
    {
525
        $defaults = [
526
            'Illuminate\Support\Facades\Cache' => 'Cache',
527
            'Illuminate\Support\Facades\Event' => 'Event',
528
            'Illuminate\Support\Facades\Log' => 'Log',
529
            'Illuminate\Support\Facades\Storage' => 'Storage',
530
            'Illuminate\Support\Facades\Validator' => 'Validator',
531
        ];
532
533
        if (! static::$aliasesRegistered) {
534
            static::$aliasesRegistered = true;
535
536
            $merged = array_merge($defaults, $userAliases);
537
538
            foreach ($merged as $original => $alias) {
539
                class_alias($original, $alias);
540
            }
541
        }
542
    }
543
544
    /**
545
     * Get the path to the application "app" directory.
546
     *
547
     * @return string
548
     */
549
    public function path()
550
    {
551
        return $this->basePath.DIRECTORY_SEPARATOR.'app';
552
    }
553
554
    /**
555
     * Get the base path for the application.
556
     *
557
     * @param  string|null  $path
558
     * @return string
559
     */
560
    public function basePath($path = null)
561
    {
562
        if (isset($this->basePath)) {
563
            return $this->basePath.($path ? '/'.$path : $path);
564
        }
565
566
        if ($this->runningInConsole()) {
567
            $this->basePath = getcwd();
568
        } else {
569
            $this->basePath = realpath(getcwd().'/../');
570
        }
571
572
        return $this->basePath($path);
573
    }
574
575
    /**
576
     * Get the path to the application configuration files.
577
     *
578
     * @param  string  $path
579
     * @return string
580
     */
581
    public function configPath($path = '')
582
    {
583
        return $this->basePath.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
584
    }
585
586
    /**
587
     * Get the path to the database directory.
588
     *
589
     * @param  string  $path
590
     * @return string
591
     */
592
    public function databasePath($path = '')
593
    {
594
        return $this->basePath.DIRECTORY_SEPARATOR.'database'.($path ? DIRECTORY_SEPARATOR.$path : $path);
595
    }
596
597
    /**
598
     * Get the storage path for the application.
599
     *
600
     * @param  string|null  $path
601
     * @return string
602
     */
603
    public function storagePath($path = '')
604
    {
605
        return ($this->storagePath ?: $this->basePath.DIRECTORY_SEPARATOR.'storage').($path ? DIRECTORY_SEPARATOR.$path : $path);
606
    }
607
608
    /**
609
     * Set the storage directory.
610
     *
611
     * @param  string  $path
612
     * @return $this
613
     */
614
    public function useStoragePath($path)
615
    {
616
        $this->storagePath = $path;
617
618
        $this->instance('path.storage', $path);
619
620
        return $this;
621
    }
622
623
    /**
624
     * Get the path to the resources directory.
625
     *
626
     * @param  string|null  $path
627
     * @return string
628
     */
629
    public function resourcePath($path = '')
630
    {
631
        return $this->basePath.DIRECTORY_SEPARATOR.'resources'.($path ? DIRECTORY_SEPARATOR.$path : $path);
632
    }
633
    /**
634
     * Determine if the application configuration is cached.
635
     *
636
     * @return bool
637
     */
638
    public function configurationIsCached()
639
    {
640
        return false;
641
    }
642
643
    /**
644
     * Determine if the application is running in the console.
645
     *
646
     * @return bool
647
     */
648
    public function runningInConsole()
649
    {
650
        return \PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg';
651
    }
652
653
    /**
654
     * Determine if we are running unit tests.
655
     *
656
     * @return bool
657
     */
658
    public function runningUnitTests()
659
    {
660
        return $this->environment() == 'testing';
661
    }
662
663
    /**
664
     * Determine if the application events are cached.
665
     *
666
     * @return bool
667
     */
668
    public function eventsAreCached()
669
    {
670
        return false;
671
    }
672
673
    /**
674
     * Prepare the application to execute a console command.
675
     *
676
     * @param  bool  $aliases
677
     * @return void
678
     */
679
    public function prepareForConsoleCommand($aliases = true)
680
    {
681
        $this->withFacades($aliases);
682
683
        $this->make('cache');
684
        $this->make('queue');
685
686
        $this->configure('database');
687
688
        $this->register('Illuminate\Database\MigrationServiceProvider');
689
        $this->register('Laravel\Lumen\Console\ConsoleServiceProvider');
690
    }
691
692
    /**
693
     * Get the application namespace.
694
     *
695
     * @return string
696
     *
697
     * @throws \RuntimeException
698
     */
699
    public function getNamespace()
700
    {
701
        if (! is_null($this->namespace)) {
0 ignored issues
show
introduced by
The condition is_null($this->namespace) is always false.
Loading history...
702
            return $this->namespace;
703
        }
704
705
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
706
707
        foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
708
            foreach ((array) $path as $pathChoice) {
709
                if (realpath(app()->path()) == realpath(base_path().'/'.$pathChoice)) {
710
                    return $this->namespace = $namespace;
711
                }
712
            }
713
        }
714
715
        throw new RuntimeException('Unable to detect application namespace.');
716
    }
717
718
    /**
719
     * Flush the container of all bindings and resolved instances.
720
     *
721
     * @return void
722
     */
723
    public function flush()
724
    {
725
        parent::flush();
726
727
        $this->loadedProviders = [];
728
        $this->reboundCallbacks = [];
729
        $this->resolvingCallbacks = [];
730
        $this->availableBindings = [];
731
        $this->ranServiceBinders = [];
732
        $this->loadedConfigurations = [];
733
        $this->afterResolvingCallbacks = [];
734
735
        $this->router = null;
736
        $this->dispatcher = null;
0 ignored issues
show
Bug Best Practice introduced by
The property dispatcher does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
737
        static::$instance = null;
738
    }
739
740
    /**
741
     * Get the current application locale.
742
     *
743
     * @return string
744
     */
745
    public function getLocale()
746
    {
747
        return $this['config']->get('app.locale');
748
    }
749
750
    /**
751
     * Set the current application locale.
752
     *
753
     * @param  string  $locale
754
     * @return void
755
     */
756
    public function setLocale($locale)
757
    {
758
        $this['config']->set('app.locale', $locale);
759
        $this['translator']->setLocale($locale);
760
    }
761
762
    /**
763
     * Determine if application locale is the given locale.
764
     *
765
     * @param  string  $locale
766
     * @return bool
767
     */
768
    public function isLocale($locale)
769
    {
770
        return $this->getLocale() == $locale;
771
    }
772
773
    /**
774
     * Register the core container aliases.
775
     *
776
     * @return void
777
     */
778
    protected function registerContainerAliases()
779
    {
780
        $this->aliases = [
781
            'Illuminate\Contracts\Foundation\Application' => 'app',
782
            'Illuminate\Contracts\Cache\Factory' => 'cache',
783
            'Illuminate\Contracts\Cache\Repository' => 'cache.store',
784
            'Illuminate\Contracts\Config\Repository' => 'config',
785
            'Illuminate\Container\Container' => 'app',
786
            'Illuminate\Contracts\Container\Container' => 'app',
787
            'Illuminate\Contracts\Events\Dispatcher' => 'events',
788
            'Illuminate\Contracts\Filesystem\Factory' => 'filesystem',
789
            'Illuminate\Contracts\Filesystem\Filesystem' => 'filesystem.disk',
790
            'Illuminate\Contracts\Filesystem\Cloud' => 'filesystem.cloud',
791
            'Illuminate\Contracts\Hashing\Hasher' => 'hash',
792
            'log' => 'Psr\Log\LoggerInterface',
793
            'Illuminate\Contracts\Translation\Translator' => 'translator',
794
            'Illuminate\Contracts\Validation\Factory' => 'validator',
795
        ];
796
    }
797
798
    /**
799
     * The available container bindings and their respective load methods.
800
     *
801
     * @var array
802
     */
803
    public $availableBindings = [
804
        'composer' => 'registerComposerBindings',
805
        'cache' => 'registerCacheBindings',
806
        'cache.store' => 'registerCacheBindings',
807
        'Illuminate\Contracts\Cache\Factory' => 'registerCacheBindings',
808
        'Illuminate\Contracts\Cache\Repository' => 'registerCacheBindings',
809
        'config' => 'registerConfigBindings',
810
        'filesystem' => 'registerFilesystemBindings',
811
        'filesystem.cloud' => 'registerFilesystemBindings',
812
        'filesystem.disk' => 'registerFilesystemBindings',
813
        'Illuminate\Contracts\Filesystem\Cloud' => 'registerFilesystemBindings',
814
        'Illuminate\Contracts\Filesystem\Filesystem' => 'registerFilesystemBindings',
815
        'Illuminate\Contracts\Filesystem\Factory' => 'registerFilesystemBindings',
816
        'files' => 'registerFilesBindings',
817
        'events' => 'registerEventBindings',
818
        'Illuminate\Contracts\Events\Dispatcher' => 'registerEventBindings',
819
        'log' => 'registerLogBindings',
820
        'Psr\Log\LoggerInterface' => 'registerLogBindings',
821
        'translator' => 'registerTranslationBindings',
822
        'encrypter' => 'registerEncrypterBindings',
823
        'Illuminate\Contracts\Encryption\Encrypter' => 'registerEncrypterBindings',
824
        'hash' => 'registerHashBindings',
825
        'Illuminate\Contracts\Hashing\Hasher' => 'registerHashBindings',
826
        'validator' => 'registerValidatorBindings',
827
        'Illuminate\Contracts\Validation\Factory' => 'registerValidatorBindings',
828
    ];
829
}
830