Completed
Push — dev ( 33f3c0...cb64be )
by Marc
02:48
created

ArtificerServiceProvider::getNotLoadedProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 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->loadProviders();
50
		$this->loadAliases();
51
52
		$this->commands(config('admin.commands'));
53
54
		App::make('ArtificerWidgetManager')->boot();
55
		App::make('ArtificerPluginManager')->boot();
56
57
		$this->requireFiles();
58
	}
59
60
    protected function loadProviders() {
61
		$loadedProviders = [];
62
63
        while (($providers = $this->getNotLoadedProviders($loadedProviders)) != []) {
64
			foreach ($providers as $provider) {
65
				$this->app->register($provider);
66
67
				$loadedProviders[] = $provider;
68
			}
69
        }
70
    }
71
72
	/**
73
	 * Will reevaluate providers array looking for third party providers declared in the given Service Providers
74
	 */
75
	protected function getNotLoadedProviders($loadedProviders) {
76
		return array_diff(config('admin.providers'), $loadedProviders);
77
	}
78
79
    protected function loadAliases() {
80
        $aliases = config('admin.aliases');
81
        $loader = Loader::getInstance();
82
83
        foreach ($aliases as $alias => $class) {
84
            $loader->alias($alias, $class);
85
        }
86
    }
87
88
    /**
89
     * Determines if is on admin
90
     *
91
     * @return bool
92
     */
93
    public function isBootable($path, $routePrefix = null) {
94
        if (App::runningInConsole() || App::runningUnitTests()) return true;
95
96
        return (
97
            $path == $routePrefix || Str::startsWith($path, $routePrefix . '/')
98
        );
99
    }
100
101
	private function requireFiles()
102
	{
103
		require_once __DIR__ . '/Http/filters.php';
104
		require_once __DIR__ . '/Http/routes.php';
105
	}
106
107
	protected function getConfigPath() {
108
		return config_path($this->name) . DIRECTORY_SEPARATOR;
109
	}
110
111
	private function addPublishableFiles()
112
    {
113
		$this->publishes([
114
			__DIR__.'/../resources/assets' => public_path('packages/mascame/' . $this->name),
115
		], 'public');
116
117
        $this->publishes([
118
            __DIR__.'/../config/' => $this->getConfigPath(),
119
        ], 'config');
120
121
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->name);
122
123
//        $this->publishes([
124
//            __DIR__.'/../database/migrations/' => database_path('migrations')
125
//        ], 'migrations');
126
//
127
//        $this->publishes([
128
//            __DIR__.'/../database/seeds/' => database_path('seeds')
129
//        ], 'seeds');
130
    }
131
132
	private function addModel()
133
	{
134
		App::singleton('ArtificerModel', function () {
135
			return new Model(new ModelSchema(new ModelObtainer()));
136
		});
137
	}
138
139
	private function addLocalization()
140
	{
141
		App::singleton('ArtificerLocalization', function () {
142
			return new Localization();
143
		});
144
	}
145
146
	private function addManagers()
147
	{
148
		$widgetsConfig = $this->getConfigPath() . 'extensions/widgets.php';
149
150
		$widgetManager = new WidgetManager(
151
			new FileInstaller(new FileWriter(), $widgetsConfig),
152
			new Booter(),
153
			new Event(app('events'))
154
		);
155
156
		App::singleton('ArtificerWidgetManager', function () use ($widgetManager) {
157
			return $widgetManager;
158
		});
159
160
		$pluginsConfig = $this->getConfigPath() . 'extensions/plugins.php';
161
162
		$pluginManager = new PluginManager(
163
			new FileInstaller(new FileWriter(), $pluginsConfig),
164
			new Booter(),
165
			new Event(app('events'))
166
		);
167
168
		App::singleton('ArtificerPluginManager', function () use ($pluginManager) {
169
			return $pluginManager;
170
		});
171
	}
172
173
	/**
174
	 * Register the service provider.
175
	 *
176
	 * @return void
177
	 */
178
	public function register()
179
	{
180
		// We still haven't modified config, that's why 'admin.admin'
181
		$routePrefix = config('admin.admin.route_prefix');
182
183
		// Avoid bloating the App with files that will not be needed
184
		$this->isBootable = $this->isBootable(request()->path(), $routePrefix);
185
186
		if (! $this->isBootable) return;
187
188
		// We need the config published before we can use this package!
189
		if ($this->isPublished()) {
190
			$this->loadConfig();
191
192
			$this->addModel();
193
			$this->addLocalization();
194
			$this->addManagers();
195
		}
196
	}
197
198
	/**
199
	 * Moves admin/admin.php keys to the root level for commodity
200
	 */
201
	protected function loadConfig() {
202
		$config = config('admin');
203
		$config = ['admin' => array_merge($config, $config['admin'])];
204
		unset($config['admin']['admin']);
205
206
		config()->set($config);
207
	}
208
209
	/**
210
	 * Get the services provided by the provider.
211
	 *
212
	 * @return array
213
	 */
214
	public function provides()
215
	{
216
		return array();
217
	}
218
219
}
220