Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 0f63c1d

Browse files
committed
v3.3.5
1 parent 7ce18e9 commit 0f63c1d

10 files changed

+81
-48
lines changed

logs/v3.2.3.txt

-6
This file was deleted.

logs/v3.3.5.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- use role & perm models from the config instead of spatie
2+
- fix anchor class in render() not using passed className
3+
- cleanup
4+
- fix not working with swoole

src/Controllers/Admin/PagesController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function edit($id)
7878
$locales = SimpleMenu::AppLocales();
7979
$roles = $this->roleModel->pluck('name', 'name');
8080
$permissions = $this->permissionModel->pluck('name', 'name');
81-
$page = $this->cache->tags('sm')->get('pages')->find($id) ?: abort(404);
81+
$page = $this->pageModel->findOrFail($id);
8282
$menus = $this->cache->tags('sm')->get('menus')->pluck('name', 'id');
8383
$templates = array_unique($this->cache->tags('sm')->get('pages')->pluck('template')->filter()->all());
8484

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace ctf0\SimpleMenu\Controllers\Admin\Traits;
4+
5+
use Illuminate\Support\Collection;
6+
use Illuminate\Pagination\Paginator;
7+
use Illuminate\Pagination\LengthAwarePaginator;
8+
9+
/**
10+
* https://gist.github.com/PaulaAguirre/5473cde2a4b066d262bf96cdb231e91a.
11+
*/
12+
trait Paginate
13+
{
14+
public function paginate($items, $perPage = 15, $page = null, $options = [])
15+
{
16+
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
17+
$items = $items instanceof Collection ? $items : Collection::make($items);
18+
19+
return new LengthAwarePaginator(
20+
$items->forPage($page, $perPage),
21+
$items->count(),
22+
$perPage,
23+
$page,
24+
$options ?? [
25+
'path' => Paginator::resolveCurrentPath(),
26+
'pageName' => 'page',
27+
]
28+
);
29+
}
30+
}

src/Controllers/Admin/UsersController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function store(Request $request)
7272
*/
7373
public function edit($id)
7474
{
75-
$user = $this->cache->get('sm-users')->find($id) ?: abort(404);
75+
$user = $this->userModel->findOrFail($id);
7676
$roles = $this->roleModel->pluck('name', 'name');
7777
$permissions = $this->permissionModel->pluck('name', 'name');
7878

src/Controllers/BaseController.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,36 @@
33
namespace ctf0\SimpleMenu\Controllers;
44

55
use App\Http\Controllers\Controller;
6+
use ctf0\SimpleMenu\Controllers\Admin\Traits\Paginate;
67

78
class BaseController extends Controller
89
{
10+
use Paginate;
11+
912
protected $cache;
1013
protected $adminPath;
1114
protected $crud_prefix;
15+
1216
protected $userModel;
1317
protected $pageModel;
1418
protected $menuModel;
19+
protected $roleModel;
20+
protected $permissionModel;
1521

1622
public function __construct()
1723
{
1824
if (is_callable('parent::__construct')) {
1925
parent::__construct();
2026
}
2127

28+
$config = config('simpleMenu');
2229
$this->cache = app('cache');
2330
$this->adminPath = 'SimpleMenu::admin';
24-
$this->crud_prefix = config('simpleMenu.crud_prefix');
31+
$this->crud_prefix = array_get($config, 'crud_prefix');
32+
$this->userModel = app(array_get($config, 'models.user'));
33+
$this->pageModel = app(array_get($config, 'models.page'));
34+
$this->menuModel = app(array_get($config, 'models.menu'));
2535

26-
$this->userModel = app(config('simpleMenu.models.user'));
27-
$this->pageModel = app(config('simpleMenu.models.page'));
28-
$this->menuModel = app(config('simpleMenu.models.menu'));
2936
$this->roleModel = app(config('permission.models.role'));
3037
$this->permissionModel = app(config('permission.models.permission'));
3138
}

src/SimpleMenu.php

-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public function __construct()
2424

2525
if ($this->listFileDir !== '') {
2626
static::create_LFD($this->listFileDir);
27-
}
2827

29-
if (!app()->runningInConsole()) {
3028
// create caches
3129
$this->createCaches();
3230

src/SimpleMenuServiceProvider.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SimpleMenuServiceProvider extends ServiceProvider
2020
*/
2121
public function boot()
2222
{
23-
$this->file = app('files');
23+
$this->file = $this->app['files'];
2424

2525
$this->packagePublish();
2626
$this->observers();
@@ -29,7 +29,7 @@ public function boot()
2929
$this->app['simplemenu'];
3030

3131
// append extra data
32-
if (!app('cache')->store('file')->has('ct-sm')) {
32+
if (!$this->app['cache']->store('file')->has('ct-sm')) {
3333
$this->autoReg();
3434
}
3535
}
@@ -76,12 +76,12 @@ protected function packagePublish()
7676
*/
7777
protected function observers()
7878
{
79-
$config = config('simpleMenu.models');
79+
$models = $this->app['config']->get('simpleMenu.models');
8080

81-
if ($config) {
82-
app(array_get($config, 'page'))->observe(PageObserver::class);
83-
app(array_get($config, 'menu'))->observe(MenuObserver::class);
84-
app(array_get($config, 'user'))->observe(UserObserver::class);
81+
if ($models) {
82+
$this->app->make(array_get($models, 'page'))->observe(PageObserver::class);
83+
$this->app->make(array_get($models, 'menu'))->observe(MenuObserver::class);
84+
$this->app->make(array_get($models, 'user'))->observe(UserObserver::class);
8585
}
8686
}
8787

@@ -92,16 +92,14 @@ protected function observers()
9292
*/
9393
protected function macros()
9494
{
95-
// alias to "Route::is()" but with support for params
96-
app('url')->macro('is', function ($route_name, $params = null) {
97-
if ($params) {
98-
return request()->url() == route($route_name, $params);
99-
}
100-
101-
return request()->url() == route($route_name);
95+
// same as "Route::is()" but better
96+
$this->app['url']->macro('is', function ($route_name, $params = null) {
97+
return $params
98+
? request()->url() == route($route_name, $params)
99+
: request()->url() == route($route_name);
102100
});
103101

104-
app('url')->macro('has', function ($needle) {
102+
$this->app['url']->macro('has', function ($needle) {
105103
return str_contains($this->current(), $needle);
106104
});
107105
}
@@ -115,7 +113,7 @@ protected function viewComp()
115113
{
116114
view()->composer('SimpleMenu::admin.*', function ($view) {
117115
$view->with([
118-
'crud_prefix' => config('simpleMenu.crud_prefix'),
116+
'crud_prefix' => $this->app['config']->get('simpleMenu.crud_prefix'),
119117
]);
120118
});
121119
}
@@ -148,7 +146,7 @@ protected function autoReg()
148146
}
149147

150148
// run check once
151-
app('cache')->store('file')->rememberForever('ct-sm', function () {
149+
$this->app['cache']->store('file')->rememberForever('ct-sm', function () {
152150
return 'added';
153151
});
154152
}

src/Traits/NavigationTrait.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,19 @@ public function render($pages, $classes = null, $params = null)
182182
/**
183183
* generate menu html.
184184
*
185-
* @param [type] $pages [description]
186-
* @param [type] $ul [description]
187-
* @param [type] $li [description]
188-
* @param [type] $a [description]
189-
* @param [type] $params [description]
190-
* @param [type] $url [description]
185+
* @param [type] $pages [description]
186+
* @param [type] $params [description]
187+
* @param [type] $url [description]
188+
* @param mixed $ul_ClassName
189+
* @param mixed $li_ClassName
190+
* @param mixed $a_ClassName
191191
*
192192
* @return [type] [description]
193193
*/
194-
protected function getHtml($pages, $ul, $li, $a, $params, $url)
194+
protected function getHtml($pages, $ul_ClassName, $li_ClassName, $a_ClassName, $params, $url)
195195
{
196196
$html = '';
197-
$html .= "<ul class=\"{$ul}\">";
197+
$html .= "<ul class=\"{$ul_ClassName}\">";
198198

199199
foreach ($pages as $page) {
200200
// escape empty url
@@ -203,13 +203,13 @@ protected function getHtml($pages, $ul, $li, $a, $params, $url)
203203
}
204204

205205
$routeUrl = $this->getRoute($page->route_name, $params);
206-
$isActive = ($url == $routeUrl ? 'is-active' : '');
206+
$isActive = ($url == $routeUrl ? $a_ClassName : '');
207207

208-
$html .= "<li class=\"{$li}\">";
208+
$html .= "<li class=\"{$li_ClassName}\">";
209209
$html .= "<a href=\"{$routeUrl}\" class=\"{$isActive}\">{$page->title}</a>";
210210

211211
if ($childs = $page->nests) {
212-
$html .= $this->getHtml($childs, $ul, $li, $a, $params, $url);
212+
$html .= $this->getHtml($childs, $ul_ClassName, $li_ClassName, $a_ClassName, $params, $url);
213213
}
214214
$html .= '</li>';
215215
}

src/Traits/Ops.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ protected function getCrntLocale()
2626
*/
2727
protected function createCaches()
2828
{
29-
$this->cache->tags('sm')->rememberForever('menus', function () {
30-
return app(config('simpleMenu.models.menu'))->with('pages')->get();
29+
$models = config('simpleMenu.models');
30+
31+
$this->cache->tags('sm')->rememberForever('menus', function () use ($models) {
32+
return app(array_get($models, 'menu'))->with('pages')->get();
3133
});
3234

33-
$this->cache->tags('sm')->rememberForever('pages', function () {
34-
return app(config('simpleMenu.models.page'))->withTrashed()->get();
35+
$this->cache->tags('sm')->rememberForever('pages', function () use ($models) {
36+
return app(array_get($models, 'page'))->withTrashed()->get();
3537
});
3638

37-
$this->cache->rememberForever('sm-users', function () {
38-
return app(config('simpleMenu.models.user'))->with(['roles', 'permissions'])->get();
39+
$this->cache->rememberForever('sm-users', function () use ($models) {
40+
return app(array_get($models, 'user'))->with(['roles', 'permissions'])->get();
3941
});
4042
}
4143

0 commit comments

Comments
 (0)