1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use Illuminate\Support\Facades\Route; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use A17\Twill\Services\Capsules\Manager; |
9
|
|
|
use A17\Twill\Services\Routing\HasRoutes; |
10
|
|
|
use A17\Twill\Services\Capsules\HasCapsules; |
11
|
|
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider; |
|
|
|
|
12
|
|
|
|
13
|
|
|
class CapsulesServiceProvider extends RouteServiceProvider |
14
|
|
|
{ |
15
|
|
|
use HasRoutes, HasCapsules; |
16
|
|
|
|
17
|
|
|
protected $manager; |
18
|
|
|
|
19
|
|
|
protected function mergeTwillConfig() |
20
|
|
|
{ |
21
|
|
|
$this->mergeConfigFrom( |
22
|
|
|
__DIR__ . '/../config/capsules.php', |
23
|
|
|
'twill.capsules' |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->app |
27
|
|
|
->make('config') |
28
|
|
|
->set('twill.capsules.list', $this->getCapsuleList()); |
29
|
|
|
|
30
|
|
|
$this->app->make('config')->set('twill.capsules.loaded', true); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function register() |
34
|
|
|
{ |
35
|
|
|
$this->registerConfig(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function registerConfig() |
39
|
|
|
{ |
40
|
|
|
$this->registerManager(); |
41
|
|
|
|
42
|
|
|
$this->mergeTwillConfig(); |
43
|
|
|
|
44
|
|
|
$this->registerCapsules(); |
45
|
|
|
|
46
|
|
|
$this->registerViewPaths(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function registerCapsules() |
50
|
|
|
{ |
51
|
|
|
$this->manager->getCapsuleList()->map(function ($capsule) { |
52
|
|
|
$this->registerCapsule($capsule); |
53
|
|
|
}); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function registerCapsule($capsule) |
57
|
|
|
{ |
58
|
|
|
$this->loadMigrationsFrom($capsule['migrations_dir']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function map(Router $router) |
62
|
|
|
{ |
63
|
|
|
$this->manager |
64
|
|
|
->getCapsuleList() |
65
|
|
|
->each(function ($capsule) use ($router) { |
66
|
|
|
$this->registerCapsuleRoutes($router, $capsule); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function registerViewPaths() |
71
|
|
|
{ |
72
|
|
|
$this->callAfterResolving('view', function ($view) { |
73
|
|
|
$view->addLocation(config('twill.capsules.path')); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function registerManager() |
78
|
|
|
{ |
79
|
|
|
$this->app->instance( |
80
|
|
|
'twill.capsules.manager', |
81
|
|
|
$this->manager = new Manager() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testCanMakeCapsule() |
86
|
|
|
{ |
87
|
|
|
$this->assertExitCodeIsGood( |
|
|
|
|
88
|
|
|
$this->artisan('twill:make:capsule', [ |
|
|
|
|
89
|
|
|
'moduleName' => 'Cars', |
90
|
|
|
'--hasBlocks' => true, |
91
|
|
|
'--hasTranslation' => true, |
92
|
|
|
'--hasSlug' => true, |
93
|
|
|
'--hasMedias' => true, |
94
|
|
|
'--hasFiles' => true, |
95
|
|
|
'--hasPosition' => true, |
96
|
|
|
'--hasRevisions' => true, |
97
|
|
|
])->run() |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->assertFileExists( |
|
|
|
|
101
|
|
|
twill_path('Twill/Capsules/Cars/app/Data/Models/Car.php') |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this->assertIsObject( |
|
|
|
|
105
|
|
|
$this->app->make('App\Twill\Capsules\Cars\Data\Models\Car') |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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: