Completed
Push — dev ( c21f59...82e0fe )
by Marc
13:56
created

ArtificerServiceProvider::loadAliases()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
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\Model\Model;
8
use Mascame\Artificer\Model\ModelObtainer;
9
use Mascame\Artificer\Model\ModelSchema;
10
use Mascame\Artificer\Widget\Manager as WidgetManager;
11
use Mascame\Artificer\Plugin\Manager as PluginManager;
12
use Mascame\Extender\Event\Event;
13
use Mascame\Extender\Installer\FileInstaller;
14
use Mascame\Extender\Installer\FileWriter;
15
16
17
class ArtificerServiceProvider extends ServiceProvider {
18
19
	use AutoPublishable, ServiceProviderLoader;
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
	 * @var bool
31
	 */
32
	protected $isBootable = false;
33
34
	/**
35
	 * Bootstrap the application events.
36
	 *
37
	 * @return void
38
	 */
39
	public function boot()
40
	{
41
		if (! $this->isBootable) return;
42
		
43
		$this->addPublishableFiles();
44
45
		// Wait until app is ready for config to be published
46
		if (! $this->isPublished()) return;
47
48
		$this->providers(config('admin.providers'));
49
		$this->aliases(config('admin.aliases'));
50
		$this->commands(config('admin.commands'));
51
52
		App::make('ArtificerWidgetManager')->boot();
53
		App::make('ArtificerPluginManager')->boot();
54
55
		$this->requireFiles();
56
	}
57
58
    /**
59
     * Determines if is on admin
60
     *
61
     * @return bool
62
     */
63
    public function isBootable($path, $routePrefix = null) {
64
        if (App::runningInConsole() || App::runningUnitTests()) return true;
65
66
        return (
67
            $path == $routePrefix || Str::startsWith($path, $routePrefix . '/')
68
        );
69
    }
70
71
	private function requireFiles()
72
	{
73
		require_once __DIR__ . '/Http/filters.php';
74
		require_once __DIR__ . '/Http/routes.php';
75
	}
76
77
	protected function getConfigPath() {
78
		return config_path($this->name) . DIRECTORY_SEPARATOR;
79
	}
80
81
	private function addPublishableFiles()
82
    {
83
		$this->publishes([
84
			__DIR__.'/../resources/assets' => public_path('packages/mascame/' . $this->name),
85
		], 'public');
86
87
        $this->publishes([
88
            __DIR__.'/../config/' => $this->getConfigPath(),
89
        ], 'config');
90
91
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->name);
92
93
//        $this->publishes([
94
//            __DIR__.'/../database/migrations/' => database_path('migrations')
95
//        ], 'migrations');
96
//
97
//        $this->publishes([
98
//            __DIR__.'/../database/seeds/' => database_path('seeds')
99
//        ], 'seeds');
100
    }
101
102
	private function addModel()
103
	{
104
		App::singleton('ArtificerModel', function () {
105
			return new Model(new ModelSchema(new ModelObtainer()));
106
		});
107
	}
108
109
	private function addLocalization()
110
	{
111
		App::singleton('ArtificerLocalization', function () {
112
			return new Localization();
113
		});
114
	}
115
116
	private function addManagers()
117
	{
118
		$widgetsConfig = $this->getConfigPath() . 'extensions/widgets.php';
119
120
		$widgetManager = new WidgetManager(
121
			new FileInstaller(new FileWriter(), $widgetsConfig),
122
			new Booter(),
123
			new Event(app('events'))
124
		);
125
126
		App::singleton('ArtificerWidgetManager', function () use ($widgetManager) {
127
			return $widgetManager;
128
		});
129
130
		$pluginsConfig = $this->getConfigPath() . 'extensions/plugins.php';
131
132
		$pluginManager = new PluginManager(
133
			new FileInstaller(new FileWriter(), $pluginsConfig),
134
			new \Mascame\Artificer\Plugin\Booter(),
135
			new Event(app('events'))
136
		);
137
138
		App::singleton('ArtificerPluginManager', function () use ($pluginManager) {
139
			return $pluginManager;
140
		});
141
	}
142
143
	/**
144
	 * Register the service provider.
145
	 *
146
	 * @return void
147
	 */
148
	public function register()
149
	{
150
		// We still haven't modified config, that's why 'admin.admin'
151
		$routePrefix = config('admin.admin.route_prefix');
152
153
		// Avoid bloating the App with files that will not be needed
154
		$this->isBootable = $this->isBootable(request()->path(), $routePrefix);
155
156
		if (! $this->isBootable) return;
157
158
		// We need the config published before we can use this package!
159
		if ($this->isPublished()) {
160
			$this->loadConfig();
161
162
			$this->addModel();
163
			$this->addLocalization();
164
			$this->addManagers();
165
		}
166
	}
167
168
	/**
169
	 * Moves admin/admin.php keys to the root level for commodity
170
	 */
171
	protected function loadConfig() {
172
		$config = config('admin');
173
		$config = ['admin' => array_merge($config, $config['admin'])];
174
		unset($config['admin']['admin']);
175
176
		config()->set($config);
177
	}
178
179
	/**
180
	 * Get the services provided by the provider.
181
	 *
182
	 * @return array
183
	 */
184
	public function provides()
185
	{
186
		return [];
187
	}
188
189
}
190