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; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class CapsulesServiceProvider extends RouteServiceProvider |
11
|
|
|
{ |
12
|
|
|
use HasRoutes, HasCapsules; |
|
|
|
|
13
|
|
|
|
14
|
|
|
protected $manager; |
15
|
|
|
|
16
|
|
|
protected function mergeTwillConfig() |
17
|
|
|
{ |
18
|
|
|
$this->mergeConfigFrom( |
19
|
|
|
__DIR__ . '/../config/capsules.php', |
20
|
|
|
'twill.capsules' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
$this->app |
24
|
|
|
->make('config') |
25
|
|
|
->set('twill.capsules.list', $this->getCapsuleList()->toArray()); |
26
|
|
|
|
27
|
|
|
$this->app->make('config')->set('twill.capsules.loaded', true); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function register() |
31
|
|
|
{ |
32
|
|
|
$this->registerManager(); |
33
|
|
|
$this->mergeTwillConfig(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function boot() |
37
|
|
|
{ |
38
|
|
|
$this->registerCapsules(); |
39
|
|
|
$this->registerViewPaths(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function registerCapsules() |
43
|
|
|
{ |
44
|
|
|
$this->manager->getCapsuleList()->map(function ($capsule) { |
45
|
|
|
$this->registerCapsule($capsule); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function registerCapsule($capsule) |
50
|
|
|
{ |
51
|
|
|
$this->loadMigrationsFrom($capsule['migrations_dir']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function registerViewPaths() |
55
|
|
|
{ |
56
|
|
|
if (file_exists(config('twill.capsules.path'))) { |
57
|
|
|
$callback = function ($view) { |
58
|
|
|
$view->addLocation(config('twill.capsules.path')); |
59
|
|
|
}; |
60
|
|
|
|
61
|
|
|
$this->app->afterResolving('view', $callback); |
62
|
|
|
|
63
|
|
|
if ($this->app->resolved('view')) { |
64
|
|
|
$callback($this->app->make('view'), $this->app); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function registerManager() |
70
|
|
|
{ |
71
|
|
|
$this->app->instance( |
72
|
|
|
'twill.capsules.manager', |
73
|
|
|
$this->manager = new Manager() |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testCanMakeCapsule() |
78
|
|
|
{ |
79
|
|
|
$this->assertExitCodeIsGood( |
|
|
|
|
80
|
|
|
$this->artisan('twill:make:capsule', [ |
|
|
|
|
81
|
|
|
'moduleName' => 'Cars', |
82
|
|
|
'--hasBlocks' => true, |
83
|
|
|
'--hasTranslation' => true, |
84
|
|
|
'--hasSlug' => true, |
85
|
|
|
'--hasMedias' => true, |
86
|
|
|
'--hasFiles' => true, |
87
|
|
|
'--hasPosition' => true, |
88
|
|
|
'--hasRevisions' => true, |
89
|
|
|
])->run() |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->assertFileExists( |
|
|
|
|
93
|
|
|
twill_path('Twill/Capsules/Cars/app/Models/Car.php') |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$this->assertIsObject( |
|
|
|
|
97
|
|
|
$this->app->make('App\Twill\Capsules\Cars\Models\Car') |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: