๐Ÿ“ฆ anlutro / laravel-4-core

๐Ÿ“„ CoreServiceProvider.php ยท 312 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312<?php
/**
 * Laravel 4 Core
 *
 * @author   Andreas Lutro <anlutro@gmail.com>
 * @license  http://opensource.org/licenses/MIT
 * @package  l4-core
 */

namespace anlutro\Core;

use anlutro\Core\Auth\Users\UserModel;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View;

class CoreServiceProvider extends ServiceProvider
{
	use \anlutro\Core\RouteProviderTrait;

	/**
	 * The name of the package.
	 *
	 * @var string
	 */
	const PACKAGE = 'anlutro/l4-core';

	/**
	 * The namespace that should be registered with the config, translator,
	 * view finder and so on.
	 *
	 * @var string
	 */
	const DOMAIN = 'c';

	/**
	 * Whether or not the service provider should be deferred/lazy loaded.
	 *
	 * @var boolean
	 */
	protected $defer = false;

	/**
	 * The name of the user model class.
	 *
	 * @var string
	 */
	protected $userModel;

	/**
	 * The path to the packages's resources directory.
	 *
	 * @var string
	 */
	protected static $resPath;

	/**
	 * Register IoC bindings.
	 *
	 * @return void
	 */
	public function register()
	{
		static::$resPath = dirname(__DIR__).'/resources';

		$this->commands([
			'anlutro\Core\Console\PublishCommand',
			'anlutro\Core\Auth\Console\CreateUserCommand',
			'anlutro\Core\Auth\Console\ChangePasswordCommand',
		]);

		$this->app->bind('anlutro\Core\Auth\UserManager');

		$this->app->bind('Illuminate\Database\Connection', function($app) {
			return $app->make('db')->connection();
		});

		$this->userModel = $this->app['config']->get('auth.model') ?: 'anlutro\Core\Auth\Users\UserModel';
		$this->app->bind('anlutro\Core\Auth\Users\UserModel', $this->userModel);

		$this->app->bindShared('anlutro\Core\Html\ScriptManager', function($app) {
			return new Html\ScriptManager($app['config']->get('app.debug'));
		});
	}

	/**
	 * Run on application boot.
	 *
	 * @return void
	 */
	public function boot()
	{
		$this->registerConfigFiles();
		$this->registerLangFiles();
		$this->registerViewFiles();

		$this->registerAuthDriver();
		$this->registerRouteFilters();
		$this->registerRoutes('core');
		if ($this->app['config']->get('c::support-email')) {
			$this->registerRoutes('support');
		}
		$this->registerViewEvents();
		$this->registerUserEvents($this->userModel);

		$this->app->booted(function() {
			$this->registerErrorHandlers();
		});
	}

	/**
	 * Register our config file location with the config repository.
	 *
	 * @return void
	 */
	protected function registerConfigFiles()
	{
		$this->app['config']->package(static::PACKAGE, static::$resPath . '/config', static::DOMAIN);
	}

	/**
	 * Register our language file location with the translator.
	 *
	 * @return void
	 */
	protected function registerLangFiles()
	{
		$this->app['translator']->addNamespace(static::DOMAIN, static::$resPath . '/lang');
	}

	/**
	 * Register our views with the view file loader.
	 *
	 * @return void
	 */
	protected function registerViewFiles()
	{
		$appView = $this->getAppViewPath(static::PACKAGE, static::DOMAIN);
		
		if ($this->app['files']->isDirectory($appView)) {
			$this->app['view']->addNamespace(static::DOMAIN, $appView);
		}

		$this->app['view']->addNamespace(static::DOMAIN, static::$resPath . '/views');
	}

	/**
	 * Register the custom "eloquent-exceptions" driver that throws descriptive
	 * exceptions when authentication fails.
	 *
	 * @return void
	 */
	protected function registerAuthDriver()
	{
		$this->app['auth']->extend('eloquent-exceptions', function() {
			$model = $this->app['config']['auth.model'];
			return new Auth\Laravel\EloquentUserProvider($this->app['hash'], $model);
		});
	}

	/**
	 * Add route filters.
	 * 
	 * @return void
	 */
	protected function registerRouteFilters()
	{
		$this->app['router']->filter('guest', 'anlutro\Core\Web\Filters\GuestFilter');
		$this->app['router']->filter('auth', 'anlutro\Core\Web\Filters\AuthFilter');
		$this->app['router']->filter('access', 'anlutro\Core\Web\Filters\AccessFilter');

		$this->app->bind('anlutro\Core\Web\Filters\CsrfFilter', function($app) {
			$regenerate = $app['config']->get('c::regenerate-csrf');
			return new Web\Filters\CsrfFilter($app['session']->driver(), $regenerate);
		});

		$this->app['router']->filter('csrf', 'anlutro\Core\Web\Filters\CsrfFilter');
		$this->app['router']->before(function(Request $request) {
			if ($request->ajax() || $request->isJson() || $request->wantsJson()) {
				$this->app->bind('anlutro\Core\Web\AuthController', 'anlutro\Core\Web\ApiAuthController');
				$this->app->bind('anlutro\Core\Web\UserController', 'anlutro\Core\Web\ApiUserController');
			}
		});
	}

	/**
	 * Register the user model events.
	 *
	 * @param  string|Model $userModel
	 *
	 * @return void
	 */
	protected function registerUserEvents($userModel)
	{
		// set a random login token on creation.
		$userModel::creating(function(UserModel $user) {
			$user->generateLoginToken();
		});

		// set last_login on every successful login.
		$this->app['events']->listen('auth.login', function(UserModel $user) {
			$user->updateLastLogin();
		});
	}

	/**
	 * Register view events (composers and creators).
	 *
	 * @return void
	 */
	protected function registerViewEvents()
	{
		$view = $this->app['view'];

		$view->creator('c::layout.main-generic', 'anlutro\Core\Web\Composers\GenericLayoutCreator');

		$view->creator(['c::layout.main-sidebar', 'c::layout.main-nosidebar'], 'anlutro\Core\Web\Composers\MainLayoutCreator');

		$view->creator('c::layout.main-sidebar', 'anlutro\Core\Web\Composers\SidebarLayoutCreator');

		$view->creator('c::alerts', 'anlutro\Core\Web\Composers\AlertsViewCreator');

		$view->creator('c::sidebar', function(View $view) {
			$view->with('sidebar', new \Illuminate\Support\Collection);
		});

		$view->creator('c::menu', 'anlutro\Core\Web\Composers\MenuViewCreator');

		$view->composer('c::menu', function() {
			$this->registerMenus();
		});
	}

	/**
	 * Add menu items.
	 *
	 * @return void
	 */
	protected function registerMenus()
	{
		/** @var \anlutro\Menu\Builder $menu */
		$menu = $this->app['anlutro\Menu\Builder'];
		/** @var \Illuminate\Translation\Translator $lang */
		$lang = $this->app['translator'];
		/** @var \Illuminate\Routing\UrlGenerator $url */
		$url = $this->app['url'];
		/** @var \anlutro\Core\Auth\Users\UserModel|null $user */
		$user = $this->app['auth']->user();

		if ($user !== null) {
			$subMenu = $menu->getMenu('right')
				->addSubmenu($user->name, ['id' => 'user', 'glyphicon' => 'user'], 99);
			$subMenu->addItem(
				$lang->get('c::user.profile-title'),
				$url->route('c::profile'),
				['id' => 'profile']
			);

			$subMenu->addItem(
				$lang->get('c::auth.logout'),
				$url->route('c::logout'),
				['id' => 'log-out'],
				50
			);
		} else {
			$menu->getMenu('right')->addItem(
				$lang->get('c::auth.login-title'),
				$url->route('c::login'),
				['id' => 'log-in', 'glyph' => 'log-in'],
				99
			);
		}
	}

	/**
	 * Register the default/fallback error handlers.
	 *
	 * @return void
	 */
	protected function registerErrorHandlers()
	{
		if (!$this->providerLoaded('anlutro\L4SmartErrors\L4SmartErrorsServiceProvider')) return;

		(new ErrorHandler($this->app))->register();
	}

	/**
	 * Check if a service provider is present in the application.
	 *
	 * @param  string $provider
	 *
	 * @return boolean
	 */
	protected function providerLoaded($provider)
	{
		$providers = $this->app['config']->get('app.providers')
			+ array_keys($this->app->getLoadedProviders());

		return in_array($provider, $providers);
	}

	/**
	 * Get the path to the core resource files.
	 *
	 * @return string
	 */
	public static function getResPath()
	{
		return static::$resPath;
	}
}