Issues (426)

src/CapsulesServiceProvider.php (6 issues)

Labels
Severity
1
<?php
2
3
namespace A17\Twill;
4
5
use A17\Twill\Services\Capsules\HasCapsules;
6
use A17\Twill\Services\Capsules\Manager;
7
use A17\Twill\Services\Routing\HasRoutes;
8
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
0 ignored issues
show
This use statement conflicts with another class in this namespace, A17\Twill\RouteServiceProvider. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
10
class CapsulesServiceProvider extends RouteServiceProvider
11
{
12
    use HasRoutes, HasCapsules;
0 ignored issues
show
The trait A17\Twill\Services\Capsules\HasCapsules requires the property $command which is not provided by A17\Twill\CapsulesServiceProvider.
Loading history...
13
14
    public static $capsulesBootstrapped = false;
15
16
    protected $manager;
17
18
    protected function mergeTwillConfig()
19
    {
20
        $this->mergeConfigFrom(
21
            __DIR__ . '/../config/capsules.php',
22
            'twill.capsules'
23
        );
24
25
        $this->app
26
            ->make('config')
27
            ->set('twill.capsules.list', $this->getCapsuleList()->toArray());
28
29
        $this->app->make('config')->set('twill.capsules.loaded', true);
30
    }
31
32
    public function register()
33
    {
34
        $this->registerManager();
35
        $this->mergeTwillConfig();
36
        $this->bootCapsules();
37
    }
38
39
    public function boot()
40
    {
41
        $this->registerCapsules();
42
        $this->registerViewPaths();
43
    }
44
45
    public function registerCapsules()
46
    {
47
        $this->manager->getCapsuleList()->map(function ($capsule) {
48
            $this->registerCapsule($capsule);
49
        });
50
    }
51
52
    /*
53
     * Boot the capsules so their psr, config and service providers are booted.
54
     *
55
     * @see HasCapsules::bootstrapCapsule
56
     */
57
    public function bootCapsules()
58
    {
59
        if (!self::$capsulesBootstrapped) {
60
            $this->getCapsuleList()
61
                ->where('enabled', true)
62
                ->each(function ($capsule) {
63
                    $this->bootstrapCapsule($capsule);
64
                });
65
            self::$capsulesBootstrapped = true;
66
        }
67
    }
68
69
    protected function registerCapsule($capsule)
70
    {
71
        $this->loadMigrationsFrom($capsule['migrations_dir']);
72
        $this->loadTranslationsFrom($capsule['lang_dir'], 'twill:capsules:' . $capsule['module']);
73
    }
74
75
    public function registerViewPaths()
76
    {
77
        if (file_exists(config('twill.capsules.path'))) {
78
            $callback = function ($view) {
79
                $view->addLocation(config('twill.capsules.path'));
80
            };
81
82
            $this->app->afterResolving('view', $callback);
83
84
            if ($this->app->resolved('view')) {
85
                $callback($this->app->make('view'), $this->app);
86
            }
87
        }
88
    }
89
90
    public function registerManager()
91
    {
92
        $this->app->instance(
93
            'twill.capsules.manager',
94
            $this->manager = new Manager()
95
        );
96
    }
97
98
    public function testCanMakeCapsule()
99
    {
100
        $this->assertExitCodeIsGood(
0 ignored issues
show
The method assertExitCodeIsGood() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

100
        $this->/** @scrutinizer ignore-call */ 
101
               assertExitCodeIsGood(
Loading history...
101
            $this->artisan('twill:make:capsule', [
0 ignored issues
show
The method artisan() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

101
            $this->/** @scrutinizer ignore-call */ 
102
                   artisan('twill:make:capsule', [
Loading history...
102
                'moduleName' => 'Cars',
103
                '--hasBlocks' => true,
104
                '--hasTranslation' => true,
105
                '--hasSlug' => true,
106
                '--hasMedias' => true,
107
                '--hasFiles' => true,
108
                '--hasPosition' => true,
109
                '--hasRevisions' => true,
110
            ])->run()
111
        );
112
113
        $this->assertFileExists(
0 ignored issues
show
The method assertFileExists() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

113
        $this->/** @scrutinizer ignore-call */ 
114
               assertFileExists(
Loading history...
114
            twill_path('Twill/Capsules/Cars/app/Models/Car.php')
115
        );
116
117
        $this->assertIsObject(
0 ignored issues
show
The method assertIsObject() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

117
        $this->/** @scrutinizer ignore-call */ 
118
               assertIsObject(
Loading history...
118
            $this->app->make('App\Twill\Capsules\Cars\Models\Car')
119
        );
120
    }
121
}
122