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

164 lines
4.2 KiB
PHP
Raw Normal View History

2018-06-20 05:06:27 +00:00
<?php
namespace Webkul\Admin\Providers;
use Illuminate\Support\ServiceProvider;
use Webkul\Core\Tree;
2018-06-20 05:06:27 +00:00
class AdminServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
2019-04-09 01:01:52 +00:00
$this->loadRoutesFrom(__DIR__ . '/../Http/routes.php');
2018-06-20 05:06:27 +00:00
2018-07-11 05:41:27 +00:00
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'admin');
2020-08-24 14:38:25 +00:00
$this->publishes([__DIR__.'/../Resources/lang' => resource_path('lang/vendor/webkul/admin')]);
2018-07-11 05:41:27 +00:00
2018-06-20 05:06:27 +00:00
$this->publishes([
2018-06-20 08:22:48 +00:00
__DIR__ . '/../../publishable/assets' => public_path('vendor/webkul/admin/assets'),
2018-06-20 05:06:27 +00:00
], 'public');
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'admin');
2018-06-25 11:00:42 +00:00
$this->composeView();
2018-12-20 10:54:30 +00:00
$this->registerACL();
$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
/**
* Bind the the data to the views
*
* @return void
*/
protected function composeView()
{
2018-07-12 07:12:48 +00:00
view()->composer(['admin::layouts.nav-left', 'admin::layouts.nav-aside', 'admin::layouts.tabs'], 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
/**
* Registers acl to entire application
*
* @return void
*/
public function registerACL()
{
$this->app->singleton('acl', function () {
return $this->createACL();
});
}
2018-12-20 10:54:30 +00:00
/**
* Create acl tree
*
* @return mixed
*/
public function createACL()
{
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-02-21 11:06:27 +00:00
2018-06-20 05:06:27 +00:00
/**
* Register package config.
2018-06-20 05:06:27 +00:00
*
* @return void
*/
protected function registerConfig()
2018-06-20 05:06:27 +00:00
{
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/menu.php', 'menu.admin'
);
2018-06-20 05:06:27 +00:00
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/acl.php', 'acl'
);
2019-02-21 11:06:27 +00:00
$this->mergeConfigFrom(
dirname(__DIR__) . '/Config/system.php', 'core'
);
2018-06-20 05:06:27 +00:00
}
2019-03-31 17:11:14 +00:00
}