sarga/packages/Webkul/Admin/src/Providers/AdminServiceProvider.php

180 lines
4.5 KiB
PHP
Raw Normal View History

2018-06-20 05:06:27 +00:00
<?php
namespace Webkul\Admin\Providers;
2021-06-07 09:27:30 +00:00
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Webkul\Admin\Http\Middleware\Locale;
2021-10-22 03:32:45 +00:00
use Webkul\Core\Tree;
2018-06-20 05:06:27 +00:00
class AdminServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
2021-06-07 09:27:30 +00:00
public function boot(Router $router)
2018-06-20 05:06:27 +00:00
{
2021-10-21 13:51:00 +00:00
$this->loadRoutesFrom(__DIR__ . '/../Routes/web.php');
2018-06-20 05:06:27 +00:00
2018-07-11 05:41:27 +00:00
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'admin');
2021-06-07 09:27:30 +00:00
2018-06-20 05:06:27 +00:00
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'admin');
2021-10-22 03:32:45 +00:00
$this->loadPublishers();
2018-06-25 11:00:42 +00:00
$this->composeView();
2018-12-20 10:54:30 +00:00
$this->registerACL();
2021-06-07 09:27:30 +00:00
$router->aliasMiddleware('admin_locale', Locale::class);
$this->app->register(EventServiceProvider::class);
2018-06-25 11:00:42 +00:00
}
2019-02-21 11:06:27 +00:00
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->registerConfig();
}
2018-06-25 11:00:42 +00:00
2021-10-22 03:32:45 +00:00
/**
* Register package config.
*
* @return void
*/
protected function registerConfig()
{
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/menu.php',
'menu.admin'
);
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/acl.php',
'acl'
);
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/system.php',
'core'
);
}
/**
* Load publishers.
*
* @return void
*/
protected function loadPublishers(): void
{
$this->publishes([__DIR__ . '/../Resources/lang' => lang_path('vendor/admin')]);
2021-10-22 03:32:45 +00:00
$this->publishes([__DIR__ . '/../../publishable/assets' => public_path('vendor/webkul/admin/assets')], 'public');
}
2018-06-25 11:00:42 +00:00
/**
2021-06-07 09:27:30 +00:00
* Bind the data to the views.
2018-06-25 11:00:42 +00:00
*
* @return void
*/
protected function composeView()
{
2021-12-30 14:48:00 +00:00
view()->composer(['admin::layouts.nav-left','admin::layouts.tabs','admin::layouts.mobile-nav'], function ($view) {
$tree = Tree::create();
2019-04-02 06:06:33 +00:00
$permissionType = auth()->guard('admin')->user()->role->permission_type;
2019-04-02 06:04:45 +00:00
$allowedPermissions = auth()->guard('admin')->user()->role->permissions;
2019-03-30 11:14:21 +00:00
foreach (config('menu.admin') as $index => $item) {
2019-04-02 18:40:34 +00:00
if (! bouncer()->hasPermission($item['key'])) {
continue;
}
2019-04-05 14:08:44 +00:00
2019-04-02 18:40:34 +00:00
if ($index + 1 < count(config('menu.admin')) && $permissionType != 'all') {
$permission = config('menu.admin')[$index + 1];
if (substr_count($permission['key'], '.') == 2 && substr_count($item['key'], '.') == 1) {
foreach ($allowedPermissions as $key => $value) {
if ($item['key'] == $value) {
$neededItem = $allowedPermissions[$key + 1];
foreach (config('menu.admin') as $key1 => $findMatced) {
if ($findMatced['key'] == $neededItem) {
$item['route'] = $findMatced['route'];
2019-03-30 11:14:21 +00:00
}
}
}
}
}
2019-03-26 10:30:37 +00:00
}
2019-04-02 18:40:34 +00:00
$tree->add($item, 'menu');
2019-03-26 10:30:37 +00:00
}
$tree->items = core()->sortItems($tree->items);
$view->with('menu', $tree);
});
2018-07-02 09:29:27 +00:00
view()->composer(['admin::users.roles.create', 'admin::users.roles.edit'], function ($view) {
2018-12-20 10:54:30 +00:00
$view->with('acl', $this->createACL());
});
2019-06-28 14:18:52 +00:00
view()->composer(['admin::catalog.products.create'], function ($view) {
$items = array();
foreach (config('product_types') as $item) {
$item['children'] = [];
array_push($items, $item);
}
$types = core()->sortItems($items);
$view->with('productTypes', $types);
});
2018-12-20 10:54:30 +00:00
}
2018-12-20 10:54:30 +00:00
/**
2021-10-22 03:32:45 +00:00
* Register ACL to entire application.
2018-12-20 10:54:30 +00:00
*
* @return void
*/
2021-10-22 03:32:45 +00:00
protected function registerACL()
2018-12-20 10:54:30 +00:00
{
$this->app->singleton('acl', function () {
return $this->createACL();
});
}
2018-12-20 10:54:30 +00:00
/**
2021-06-07 09:27:30 +00:00
* Create ACL tree.
2018-12-20 10:54:30 +00:00
*
* @return mixed
*/
2021-10-22 03:32:45 +00:00
protected function createACL()
2018-12-20 10:54:30 +00:00
{
static $tree;
2020-02-19 10:26:44 +00:00
if ($tree) {
2018-12-20 10:54:30 +00:00
return $tree;
2020-02-19 10:26:44 +00:00
}
2018-12-20 10:54:30 +00:00
$tree = Tree::create();
2019-01-15 11:54:41 +00:00
foreach (config('acl') as $item) {
2018-12-20 10:54:30 +00:00
$tree->add($item, 'acl');
}
$tree->items = core()->sortItems($tree->items);
return $tree;
2018-06-20 05:06:27 +00:00
}
2019-03-31 17:11:14 +00:00
}