Skip to content

Commit eb46e96

Browse files
committed
Merge pull request #172 from serpentblade/master
Performance Improvements
2 parents 489b8c7 + 52c689a commit eb46e96

File tree

5 files changed

+109
-70
lines changed

5 files changed

+109
-70
lines changed

src/Serverfireteam/Panel/libs/dashboard.php

+27-13
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,51 @@
33

44

55
class dashboard
6-
{
7-
8-
public static $urls;
9-
6+
{
7+
8+
public static $dashboardItems;
9+
10+
public static $urls;
11+
12+
public static function getItems()
13+
{
14+
if(!self::$dashboardItems) {
15+
self::$dashboardItems = self::create();
16+
}
17+
18+
return self::$dashboardItems;
19+
}
20+
1021
public static function create()
1122
{
1223
self::$urls = \Config::get('panel.panelControllers');
13-
14-
$config = \Serverfireteam\Panel\Link::all();
24+
25+
$config = \Serverfireteam\Panel\Link::allCached();
1526
$dashboard = array();
1627

28+
$appHelper = new AppHelper();
29+
1730
// Make Dashboard Items
18-
foreach ($config as $key => $value) {
31+
foreach ($config as $value) {
32+
33+
$modelName = $value['url'];
1934

20-
$modelName = $value['url'];
2135
if ( in_array($modelName, self::$urls)) {
22-
$model = "Serverfireteam\Panel\\".$modelName;
36+
$model = "Serverfireteam\\Panel\\".$modelName;
2337
} else {
24-
$appHelper = new AppHelper();
2538
$model = $appHelper->getNameSpace() . $modelName;
2639
}
2740

2841
//if (class_exists($value)) {
2942
$dashboard[] = array(
43+
'modelName' => $modelName,
3044
'title' => $value['display'],
31-
'count' => $model::all()->count(),
45+
'count' => $model::count(),
3246
'showListUrl' => 'panel/' . $modelName . '/all',
3347
'addUrl' => 'panel/' . $modelName . '/edit',
34-
);
48+
);
3549
}
3650

37-
return $dashboard;
51+
return $dashboard;
3852
}
3953
}

src/controllers/CrudController.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
class CrudController extends Controller
77
{
8+
const ID_COLUMN = 'id';
9+
810
public $grid;
911
public $entity;
1012
public $set;
@@ -47,17 +49,37 @@ public function setEntity($entity) {
4749
$this->entity = $entity;
4850
}
4951

50-
public function addStylesToGrid($orderByColumn = 'id', $paginateCount = 10)
52+
public function getEntityModel() {
53+
54+
$entity = $this->getEntity();
55+
56+
$appHelper = new libs\AppHelper;
57+
58+
if ( in_array($entity, Link::getMainUrls()) ) {
59+
$modelClass = 'Serverfireteam\\Panel\\'.$entity;
60+
} else {
61+
$modelClass = $appHelper->getNameSpace().$this->getEntity();
62+
}
63+
64+
return new $modelClass;
65+
}
66+
67+
public function addStylesToGrid($orderByColumn = self::ID_COLUMN, $paginateCount = 10)
5168
{
5269
$this->grid->edit('edit', trans('panel::fields.edit'), 'show|modify|delete');
5370

71+
72+
if ($orderByColumn === self::ID_COLUMN) {
73+
$orderByColumn = $this->getEntityModel()->getKeyName();
74+
}
75+
5476
$this->grid->orderBy($orderByColumn, 'desc');
55-
$this->grid->paginate($paginateCount);
77+
$this->grid->paginate($paginateCount);
5678
}
5779

5880
public function addHelperMessage($message)
5981
{
60-
$this->helper_message = $message;
82+
$this->helper_message = $message;
6183
}
6284

6385
public function returnView()

src/models/Link.php

+31-21
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,47 @@
44
use Illuminate\Database\Eloquent\Model;
55

66
class Link extends Model {
7-
7+
88
protected $table = 'links';
9-
10-
public static function returnUrls(){
11-
$configs = Link::all();
12-
$allUrls = array();
13-
14-
foreach ( $configs as $config ){
15-
$allUrls[] = $config['url'];
9+
10+
static $cache = [];
11+
12+
public static function allCached($forceRefresh = false)
13+
{
14+
if (!isset(self::$cache['all']) || $forceRefresh) {
15+
self::$cache['all'] = Link::all();
1616
}
17-
return $allUrls;
17+
18+
return self::$cache['all'];
1819
}
19-
20-
public static function getMainUrls(){
21-
$configs = Link::where('main', '=', true)->get();
22-
$mainUrls = array();
23-
24-
foreach ( $configs as $config ){
25-
$mainUrls[] = $config['url'];
20+
21+
public static function returnUrls($forceRefresh = false) {
22+
23+
if (!isset(self::$cache['all_urls']) || $forceRefresh) {
24+
$configs = Link::allCached($forceRefresh);
25+
self::$cache['all_urls'] = $configs->pluck('url')->toArray();
2626
}
27-
return $mainUrls;
27+
28+
return self::$cache['all_urls'];
2829
}
29-
30-
//where('url', '=', 'Admin')->take(1)->get()
31-
30+
31+
public static function getMainUrls($forceRefresh = false){
32+
33+
if (!isset(self::$cache['main_urls']) || $forceRefresh) {
34+
$configs = Link::where('main', '=', true)->get(['url']);
35+
self::$cache['main_urls'] = $configs->pluck('url')->toArray();
36+
}
37+
38+
return self::$cache['main_urls'];
39+
}
40+
41+
3242
public function getAndSave($url, $display){
3343
$this->url = $url;
3444
$this->display = $display;
3545
$this->save();
3646
}
37-
47+
3848

3949
protected $fillable = array('url', 'display');
4050

src/views/dashboard.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="row box-holder">
1414

1515
@if(is_array(\Serverfireteam\Panel\Link::returnUrls()))
16-
@foreach (Serverfireteam\Panel\libs\dashboard::create() as $box)
16+
@foreach (Serverfireteam\Panel\libs\dashboard::getItems() as $box)
1717
<div class="col-lg-3 col-md-6">
1818
<div class="panel ">
1919
<div class="panel-heading">

src/views/mainTemplate.blade.php

+25-32
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
@extends('panelViews::master')
2-
@section('bodyClass')
3-
dashboard
4-
@stop
2+
@section('bodyClass', 'dashboard')
53
@section('body')
4+
<?php
5+
$urls = \Config::get('panel.panelControllers');
6+
$linkItems = \Serverfireteam\Panel\libs\dashboard::getItems();
7+
?>
68

7-
8-
{{--*/
9-
$urls = \Config::get('panel.panelControllers');
10-
/*--}}
11-
129
<div class="loading">
1310
<h1> LOADING </h1>
1411
<div class="spinner">
@@ -44,32 +41,28 @@
4441
<div class="user-info">{{Auth::guard('panel')->user()->first_name.' '.Auth::guard('panel')->user()->last_name}}</div>
4542
<a class="visit-site" href="{{$app['url']->to('/')}}">{{ \Lang::get('panel::fields.visiteSite') }} </a>
4643
<ul class="nav" id="side-menu">
47-
<li class="{{ (Request::url() === url('panel')) ? 'active' : '' }}">
48-
<a href="{{ url('panel') }}" ><i class="fa fa-dashboard fa-fw"></i> {{ \Lang::get('panel::fields.dashboard') }}</a>
49-
</li>
50-
51-
{{--*/ $links = \Serverfireteam\Panel\Link::all(); /*--}}
44+
<li class="{{ (Request::url() === url('panel')) ? 'active' : '' }}">
45+
<a href="{{ url('panel') }}" ><i class="fa fa-dashboard fa-fw"></i> {{ \Lang::get('panel::fields.dashboard') }}</a>
46+
</li>
5247

53-
@foreach($links as $key => $value )
54-
55-
@if (in_array($value['url'], $urls))
56-
{{--*/ $model = "Serverfireteam\Panel\\".$value['url'] /*--}}
57-
<li >
58-
<a href="{{ url('panel/'.$value['url'].'/all') }}" class=" s-link {{ (Request::segment(2)==$value['url'])?'active':'' }}"><i class="fa fa-edit fa-fw"></i> {{{$value['display']}}} </a> <span class="badge pull-right">{!!$model::all()->count()!!}</span> <div class="items-bar"> <a href="{{ url('panel/'.$value['url'].'/edit') }}" class="ic-plus" title="Add"></a> <a title="List" class="ic-lines" href="{{ url('panel/'.$value['url'].'/all') }}"> </a> </div>
59-
60-
</li>
61-
@else
48+
@foreach($linkItems as $linkItem)
49+
<?php
50+
$isActive = Request::segment(2) == $linkItem['modelName'];
51+
?>
52+
<li class="s-link {{ $isActive ? 'active' : '' }}">
53+
<a href="{{ url($linkItem['showListUrl']) }}" class="{{ $isActive ? 'active' : '' }}">
54+
<i class="fa fa-edit fa-fw"></i>
55+
{{{$linkItem['title']}}}
56+
</a>
57+
<span class="badge pull-right">{!!$linkItem['count']!!}</span>
58+
<div class="items-bar">
59+
<a href="{{ url($linkItem['addUrl']) }}" class="ic-plus" title="Add" ></a>
60+
<a title="List" class="ic-lines" href="{{ url($linkItem['showListUrl']) }}" ></a>
61+
</div>
62+
</li>
63+
@endforeach
64+
</ul>
6265

63-
{{--*/ $appHelper = new \Serverfireteam\Panel\libs\AppHelper(); /*--}}
64-
65-
{{--*/ $model = $appHelper->getNameSpace().$value['url'] /*--}}
66-
<li class="s-link {{ (Request::segment(2)==$value['url'])?'active':'' }}">
67-
<a href="{{ url('panel/'.$value['url'].'/all') }}" class="{{ (Request::segment(2)==$value['url'])?'active':'' }}"><i class="fa fa-edit fa-fw"></i> {{{$value['display']}}} </a> <span class="badge pull-right">{!!$model::all()->count()!!}</span> <div class="items-bar"> <a href="{{ url('panel/'.$value['url'].'/edit') }}" class="ic-plus" title="Add" ></a> <a title="List" class="ic-lines" href="{{ url('panel/'.$value['url'].'/all') }}" > </a> </div>
68-
</li>
69-
@endif
70-
@endforeach
71-
</ul>
72-
7366
</li>
7467
</ul>
7568
</div>

0 commit comments

Comments
 (0)