Completed
Push — dev ( 061f38...c1e495 )
by Marc
02:36
created

ArtificerServiceProvider::addPublishableFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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