Completed
Push — dev ( 110236...0d804d )
by Marc
02:57
created

ArtificerServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
        \Mascame\Artificer\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
        Artificer::pluginManager()->boot();
58
        Artificer::widgetManager()->boot();
59
60
        $this->manageCorePlugins();
61
62
        $this->requireFiles();
63
	}
64
65
    /**
66
     * Ensure core plugins are installed
67
     *
68
     * @throws \Exception
69
     */
70
	protected function manageCorePlugins() {
71
	    // Avoid installing plugins when using CLI
72
        if (App::runningInConsole() || App::runningUnitTests()) return true;
73
74
        $pluginManager = Artificer::pluginManager();
75
        $needsRefresh = false;
76
77
        foreach ($this->corePlugins as $corePlugin) {
78
            if (! $pluginManager->isInstalled($corePlugin)) {
79
                $installed = $pluginManager->installer()->install($corePlugin);
80
81
                if (! $installed) {
82
                    throw new \Exception("Unable to install Artificer core plugin {$corePlugin}");
83
                }
84
85
                $needsRefresh = true;
86
            }
87
        }
88
89
        // Refresh to allow changes made by core plugins to take effect
90
        if ($needsRefresh) {
91
            /**
92
             * File driver is slow... wait some seconds (else we would have too many redirects)
93
             *
94
             * Fortunately we only do this in the first run. Ye, I don't like it either.
95
             */
96
            sleep(2);
97
98
            header('Location: '. \URL::current());
99
            die();
100
        }
101
    }
102
103
    /**
104
     * Determines if is on admin
105
     *
106
     * @return bool
107
     */
108
    public function isBootable($path, $routePrefix = null) {
109
        if (App::runningInConsole() || App::runningUnitTests()) return true;
110
111
        return (
112
            $path == $routePrefix || Str::startsWith($path, $routePrefix . '/')
113
        );
114
    }
115
116
	private function requireFiles()
117
	{
118
		require_once __DIR__ . '/../routes/admin.php';
119
	}
120
121
	protected function getConfigPath() {
122
		return config_path($this->name) . DIRECTORY_SEPARATOR;
123
	}
124
125
	private function addPublishableFiles()
126
    {
127
		$this->publishes([
128
			__DIR__.'/../resources/assets' => public_path('packages/mascame/' . $this->name),
129
		], 'public');
130
131
        $this->publishes([
132
            __DIR__.'/../config/' => $this->getConfigPath(),
133
        ], 'config');
134
135
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->name);
136
    }
137
138
	private function addModel()
139
	{
140
		App::singleton('ArtificerModel', function () {
141
			return new Model(new ModelSchema(new ModelObtainer()));
142
		});
143
	}
144
145
	private function addLocalization()
146
	{
147
		App::singleton('ArtificerLocalization', function () {
148
			return new Localization();
149
		});
150
	}
151
152
	private function addManagers()
153
	{
154 View Code Duplication
		App::singleton('ArtificerWidgetManager', function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
155
            $widgetsConfig = $this->getConfigPath() . 'extensions/widgets.php';
156
157
            return new WidgetManager(
158
                new FileInstaller(new FileWriter(), $widgetsConfig),
159
                new Booter(),
160
                new Event(app('events'))
161
            );
162
        });
163
164 View Code Duplication
		App::singleton('ArtificerPluginManager', function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
165
            $pluginsConfig = $this->getConfigPath() . 'extensions/plugins.php';
166
167
            return new PluginManager(
168
                new FileInstaller(new FileWriter(), $pluginsConfig),
169
                new \Mascame\Artificer\Plugin\Booter(),
170
                new Event(app('events'))
171
            );
172
		});
173
174
        App::singleton('ArtificerAssetManager', function() {
175
            return \Assets::config([
176
                // Reset those dirs to avoid wrong paths
177
                'css_dir' => '',
178
                'js_dir' => '',
179
            ]);
180
        });
181
	}
182
183
	/**
184
	 * Register the service provider.
185
	 *
186
	 * @return void
187
	 */
188
	public function register()
189
	{
190
		// We still haven't modified config, that's why 'admin.admin'
191
		$routePrefix = config('admin.admin.routePrefix');
192
193
		// Avoid bloating the App with files that will not be needed
194
		$this->isBootable = $this->isBootable(request()->path(), $routePrefix);
195
196
		if (! $this->isBootable) return;
197
198
		// We need the config published before we can use this package!
199
		if ($this->isPublished()) {
200
			$this->loadConfig();
201
202
			$this->addModel();
203
			$this->addLocalization();
204
			$this->addManagers();
205
		}
206
	}
207
208
	/**
209
	 * Moves admin/admin.php keys to the root level for commodity
210
	 */
211
	protected function loadConfig() {
212
		$config = config('admin');
213
		$config = ['admin' => array_merge($config, $config['admin'])];
214
		unset($config['admin']['admin']);
215
216
		config()->set($config);
217
	}
218
219
	/**
220
	 * Get the services provided by the provider.
221
	 *
222
	 * @return array
223
	 */
224
	public function provides()
225
	{
226
		return [];
227
	}
228
229
}
230