1
|
|
|
<?php namespace Mascame\Artificer; |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use Illuminate\Support\ServiceProvider; |
5
|
|
|
use App; |
6
|
|
|
use Config; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Mascame\Artificer\Extension\Booter; |
9
|
|
|
use Mascame\Artificer\Extension\PluginManager; |
10
|
|
|
use Mascame\Artificer\Extension\WidgetManager; |
11
|
|
|
use Mascame\Artificer\Model\Model; |
12
|
|
|
use Mascame\Artificer\Model\ModelObtainer; |
13
|
|
|
use Mascame\Artificer\Model\ModelSchema; |
14
|
|
|
use Mascame\Extender\Event\Event; |
15
|
|
|
use Mascame\Extender\Installer\FileInstaller; |
16
|
|
|
use Mascame\Extender\Installer\FileWriter; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class ArtificerServiceProvider extends ServiceProvider { |
20
|
|
|
|
21
|
|
|
protected $name = 'admin'; |
22
|
|
|
/** |
23
|
|
|
* Indicates if loading of the provider is deferred. |
24
|
|
|
* |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $defer = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Bootstrap the application events. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function boot() |
35
|
|
|
{ |
36
|
|
|
if (! Artificer::isBooted()) return; |
37
|
|
|
|
38
|
|
|
$this->addPublishing(); |
39
|
|
|
$this->requireFiles(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determines if is on admin |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function isBootable($path, $routePrefix = null) { |
48
|
|
|
if (App::runningInConsole() || App::runningUnitTests()) return true; |
49
|
|
|
|
50
|
|
|
return ( |
51
|
|
|
$path == $routePrefix || Str::startsWith($path, $routePrefix . '/') |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function requireFiles() |
56
|
|
|
{ |
57
|
|
|
require_once __DIR__ . '/Http/filters.php'; |
58
|
|
|
require_once __DIR__ . '/Http/routes.php'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function addPublishing() |
62
|
|
|
{ |
63
|
|
|
$this->publishes([ |
64
|
|
|
__DIR__.'/../config/' => config_path($this->name) .'/', |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->name); |
68
|
|
|
|
69
|
|
|
$this->publishes([ |
70
|
|
|
__DIR__.'/../database/migrations/' => database_path('migrations') |
71
|
|
|
], 'migrations'); |
72
|
|
|
|
73
|
|
|
$this->publishes([ |
74
|
|
|
__DIR__.'/../database/seeds/' => database_path('seeds') |
75
|
|
|
], 'seeds'); |
76
|
|
|
|
77
|
|
|
$this->publishes([ |
78
|
|
|
__DIR__.'/../resources/assets/' => public_path('packages/mascame/' . $this->name), |
79
|
|
|
], 'public'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function addModel() |
83
|
|
|
{ |
84
|
|
|
App::singleton('artificer-model', function () { |
85
|
|
|
return new Model(new ModelSchema(new ModelObtainer())); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function addLocalization() |
90
|
|
|
{ |
91
|
|
|
App::singleton('artificer-localization', function () { |
92
|
|
|
return new Localization(); |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function addManagers() |
97
|
|
|
{ |
98
|
|
View Code Duplication |
App::singleton('ArtificerPluginManager', function () { |
|
|
|
|
99
|
|
|
$pluginsPath = config_path() . '/'. $this->name .'/plugins.php'; |
100
|
|
|
|
101
|
|
|
return new PluginManager( |
102
|
|
|
new FileInstaller(new FileWriter(), $pluginsPath), |
103
|
|
|
new Booter(), |
104
|
|
|
new Event(app('events')) |
105
|
|
|
); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
View Code Duplication |
App::singleton('ArtificerWidgetManager', function () { |
|
|
|
|
109
|
|
|
$widgetsPath = config_path() . '/'. $this->name .'/widgets.php'; |
110
|
|
|
|
111
|
|
|
return new WidgetManager( |
112
|
|
|
new FileInstaller(new FileWriter(), $widgetsPath), |
113
|
|
|
new Booter(), |
114
|
|
|
new Event(app('events')) |
115
|
|
|
); |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Register the service provider. |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function register() |
125
|
|
|
{ |
126
|
|
|
$configPath = __DIR__ . '/../config/admin.php'; |
127
|
|
|
$this->mergeConfigFrom($configPath, $this->name); |
128
|
|
|
|
129
|
|
|
// Avoid bloating the App with files that will not be needed |
130
|
|
|
if (! $this->isBootable(request()->path(), config('admin.route_prefix'))) return; |
131
|
|
|
|
132
|
|
|
$this->addModel(); |
133
|
|
|
$this->addLocalization(); |
134
|
|
|
$this->addManagers(); |
135
|
|
|
|
136
|
|
|
// $am = new AssetManager(); |
137
|
|
|
// $am->set('jquery', new HttpAsset('https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js')); |
138
|
|
|
// $am->set('artificer', new AssetCollection(array( |
139
|
|
|
// new AssetReference($am, 'jquery'), |
140
|
|
|
// new FileAsset(__DIR__.'/../resources/assets/core/restfulizer.js'), |
141
|
|
|
// ))); |
142
|
|
|
// |
143
|
|
|
// $ac = new AssetCollection($am); |
144
|
|
|
// var_dump($ac->dump()); die(); |
145
|
|
|
|
146
|
|
|
Artificer::$booted = true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the services provided by the provider. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function provides() |
155
|
|
|
{ |
156
|
|
|
return array(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.