Completed
Push — dev ( 621ef9...96c89c )
by Marc
02:17
created

ArtificerServiceProvider   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 26
Bugs 6 Features 3
Metric Value
wmc 22
c 26
b 6
f 3
lcom 1
cbo 14
dl 0
loc 197
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 22 3
A loadProviders() 0 7 2
A loadAliases() 0 8 2
A isBootable() 0 7 4
A requireFiles() 0 5 1
A getConfigPath() 0 3 1
A addPublishableFiles() 0 20 1
A addModel() 0 6 1
A addLocalization() 0 6 1
B addManagers() 0 26 1
A register() 0 22 3
A loadConfig() 0 7 1
A provides() 0 4 1
1
<?php namespace Mascame\Artificer;
2
3
use Illuminate\Support\ServiceProvider;
4
use Illuminate\Support\Facades\App;
5
use Illuminate\Support\Str;
6
use Mascame\Artificer\Extension\Booter;
7
use Mascame\Artificer\Extension\PluginManager;
8
use Mascame\Artificer\Extension\WidgetManager;
9
use Mascame\Artificer\Model\Model;
10
use Mascame\Artificer\Model\ModelObtainer;
11
use Mascame\Artificer\Model\ModelSchema;
12
use Mascame\Extender\Event\Event;
13
use Mascame\Extender\Installer\FileInstaller;
14
use Mascame\Extender\Installer\FileWriter;
15
use Illuminate\Foundation\AliasLoader as Loader;
16
17
18
class ArtificerServiceProvider extends ServiceProvider {
19
20
	use AutoPublishable;
21
	
22
	protected $name = 'admin';
23
	/**
24
	 * Indicates if loading of the provider is deferred.
25
	 *
26
	 * @var bool
27
	 */
28
	protected $defer = false;
29
30
	/**
31
	 * @var bool
32
	 */
33
	protected $isBootable = false;
34
35
	/**
36
	 * Bootstrap the application events.
37
	 *
38
	 * @return void
39
	 */
40
	public function boot()
41
	{
42
		if (! $this->isBootable) return;
43
		
44
		$this->addPublishableFiles();
45
46
		// Wait until app is ready for config to be published
47
		if (! $this->isPublished()) return;
48
49
		$this->requireFiles();
50
51
//		$this->app->register(ArtificerLogreaderPluginServiceProvider::class);
52
//		$this->app->register(ArtificerWidgetsServiceProvider::class);
53
54
		App::make('ArtificerPluginManager')->boot();
55
		App::make('ArtificerWidgetManager')->boot();
56
57
        $this->loadProviders();
58
        $this->loadAliases();
59
60
		$this->commands(config('admin.commands'));
61
	}
62
63
    protected function loadProviders() {
64
        $providers = config('admin.providers');
65
66
        foreach ($providers as $provider) {
67
            $this->app->register($provider);
68
        }
69
    }
70
71
    protected function loadAliases() {
72
        $aliases = config('admin.aliases');
73
        $loader = Loader::getInstance();
74
75
        foreach ($aliases as $alias => $class) {
76
            $loader->alias($alias, $class);
77
        }
78
    }
79
80
    /**
81
     * Determines if is on admin
82
     *
83
     * @return bool
84
     */
85
    public function isBootable($path, $routePrefix = null) {
86
        if (App::runningInConsole() || App::runningUnitTests()) return true;
87
88
        return (
89
            $path == $routePrefix || Str::startsWith($path, $routePrefix . '/')
90
        );
91
    }
92
93
	private function requireFiles()
94
	{
95
		require_once __DIR__ . '/Http/filters.php';
96
		require_once __DIR__ . '/Http/routes.php';
97
	}
98
99
	protected function getConfigPath() {
100
		return config_path($this->name) . DIRECTORY_SEPARATOR;
101
	}
102
103
    private function addPublishableFiles()
104
    {
105
        $this->publishes([
106
            __DIR__.'/../config/' => $this->getConfigPath(),
107
        ], 'config');
108
109
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->name);
110
111
//        $this->publishes([
112
//            __DIR__.'/../database/migrations/' => database_path('migrations')
113
//        ], 'migrations');
114
//
115
//        $this->publishes([
116
//            __DIR__.'/../database/seeds/' => database_path('seeds')
117
//        ], 'seeds');
118
119
        $this->publishes([
120
            __DIR__.'/../resources/assets/' => public_path('packages/mascame/' . $this->name),
121
        ], 'public');
122
    }
123
124
	private function addModel()
125
	{
126
		App::singleton('ArtificerModel', function () {
127
			return new Model(new ModelSchema(new ModelObtainer()));
128
		});
129
	}
130
131
	private function addLocalization()
132
	{
133
		App::singleton('ArtificerLocalization', function () {
134
			return new Localization();
135
		});
136
	}
137
138
	private function addManagers()
139
	{
140
		$widgetsConfig = $this->getConfigPath() . 'extensions/widgets.php';
141
142
		$widgetManager = new WidgetManager(
143
			new FileInstaller(new FileWriter(), $widgetsConfig),
144
			new Booter(),
145
			new Event(app('events'))
146
		);
147
		
148
		App::singleton('ArtificerWidgetManager', function () use ($widgetManager) {
149
			return $widgetManager;
150
		});
151
152
		$pluginsConfig = $this->getConfigPath() . 'extensions/plugins.php';
153
154
		$pluginManager = new PluginManager(
155
			new FileInstaller(new FileWriter(), $pluginsConfig),
156
			new Booter(),
157
			new Event(app('events'))
158
		);
159
160
		App::singleton('ArtificerPluginManager', function () use ($pluginManager) {
161
			return $pluginManager;
162
		});
163
	}
164
165
	/**
166
	 * Register the service provider.
167
	 *
168
	 * @return void
169
	 */
170
	public function register()
171
	{
172
		// We still haven't modified config, that's why 'admin.admin'
173
		$routePrefix = config('admin.admin.route_prefix');
174
175
		// Avoid bloating the App with files that will not be needed
176
		$this->isBootable = $this->isBootable(request()->path(), $routePrefix);
177
178
		if (! $this->isBootable) return;
179
180
		// We need the config published before we can use this package!
181
		if (! $this->isPublished()) {
182
			$this->autoPublish();
183
			return;
184
		}
185
186
		$this->loadConfig();
187
188
		$this->addModel();
189
		$this->addLocalization();
190
		$this->addManagers();
191
	}
192
193
	/**
194
	 * Moves admin/admin.php keys to the root level for commodity
195
	 */
196
	protected function loadConfig() {
197
		$config = config('admin');
198
		$config = ['admin' => array_merge($config, $config['admin'])];
199
		unset($config['admin']['admin']);
200
201
		config()->set($config);
202
	}
203
204
	/**
205
	 * Get the services provided by the provider.
206
	 *
207
	 * @return array
208
	 */
209
	public function provides()
210
	{
211
		return array();
212
	}
213
214
}
215