Completed
Push — dev ( dbd261...7a8390 )
by Marc
02:45
created

ArtificerServiceProvider::addLocalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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