Skip to content

Commit d6ec61d

Browse files
committed
added widgets functionality for dashboard
1 parent 8e8a8d5 commit d6ec61d

File tree

15 files changed

+417
-19
lines changed

15 files changed

+417
-19
lines changed

src/Darryldecode/Backend/BackendServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Darryldecode\Backend\Base\Registrar\ComponentLoader;
44
use Darryldecode\Backend\Base\Registrar\Registrar;
5+
use Darryldecode\Backend\Base\Registrar\WidgetLoader;
56
use Illuminate\Filesystem\Filesystem;
67
use Illuminate\Routing\Router;
78
use Illuminate\Support\ServiceProvider;
@@ -77,6 +78,14 @@ public function bootBackend()
7778
$this->loadViewsFrom($view['dir'], $view['namespace']);
7879
}
7980

81+
// load built-in widgets
82+
$builtInWidgetsLoader = new WidgetLoader(__DIR__.'/Widgets', new Filesystem());
83+
$customWidgetsLoader = new WidgetLoader(app_path().'/Backend/Widgets', new Filesystem());
84+
85+
// add widgets
86+
$backendRegistrar->addWidget($builtInWidgetsLoader->getAvailableWidgetInstances());
87+
$backendRegistrar->addWidget($customWidgetsLoader->getAvailableWidgetInstances());
88+
8089
$this->app['backend'] = $backendRegistrar;
8190
}
8291

src/Darryldecode/Backend/Base/Console/ComponentMake.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: darryl
5-
* Date: 7/2/2015
6-
* Time: 7:43 PM
7-
*/
82

93
namespace Darryldecode\Backend\Base\Console;
104

@@ -50,13 +44,17 @@ public function handle(Filesystem $filesystem)
5044
$componentNamespace = $this->formatToComponentNamespace($componentTitle);
5145
$componentUrl = $this->formatToComponentUrl($componentTitle);
5246

53-
$backendCustomComponentsPath = app_path().'/Backend';
54-
$componentPath = app_path().'/Backend/Components/'.$componentNamespace;
47+
$backendPath = app_path().'/Backend';
48+
$componentPath = $backendPath.'/Components/'.$componentNamespace;
5549

56-
if( ! $this->filesystem->isDirectory($backendCustomComponentsPath) )
50+
if( ! $this->filesystem->isDirectory($backendPath) )
5751
{
58-
$this->filesystem->makeDirectory($backendCustomComponentsPath);
59-
$this->filesystem->makeDirectory($backendCustomComponentsPath.'/Components');
52+
$this->filesystem->makeDirectory($backendPath);
53+
}
54+
55+
if( ! $this->filesystem->isDirectory($backendPath.'/Components') )
56+
{
57+
$this->filesystem->makeDirectory($backendPath.'/Components');
6058
}
6159

6260
if( $this->filesystem->isDirectory($componentPath) )
@@ -106,7 +104,7 @@ public function handle(Filesystem $filesystem)
106104
$routesScaffold = str_replace('{{componentUrl}}',$componentUrl,$routesScaffold);
107105
$this->filesystem->put($componentPath.'/routes.php',$routesScaffold);
108106

109-
$this->info(PHP_EOL.'Creating successfully created!'.PHP_EOL);
107+
$this->info(PHP_EOL.'Component successfully created!'.PHP_EOL);
110108
}
111109

112110
/**
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class WidgetMake extends Command {
9+
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'backend:widget-create';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new backend custom dashboard widget.';
23+
24+
/**
25+
* @var Filesystem
26+
*/
27+
protected $filesystem;
28+
29+
/**
30+
* Execute the console command.
31+
*
32+
* @param Filesystem $filesystem
33+
* @return mixed
34+
*/
35+
public function handle(Filesystem $filesystem)
36+
{
37+
$this->filesystem = $filesystem;
38+
$scaffoldsPath = __DIR__.'/../Etc/scaffolds/';
39+
40+
// component info
41+
$widgetTitle = $this->ask('Enter widget name');
42+
$widgetDescription = $this->ask('Enter widget description');
43+
$widgetNamespace = $this->formatToComponentNamespace($widgetTitle);
44+
45+
$backendPath = app_path().'/Backend';
46+
$widgetPath = $backendPath.'/Widgets/'.$widgetNamespace;
47+
48+
if( ! $this->filesystem->isDirectory($backendPath) )
49+
{
50+
$this->filesystem->makeDirectory($backendPath);
51+
}
52+
53+
if( ! $this->filesystem->isDirectory($backendPath.'/Widgets') )
54+
{
55+
$this->filesystem->makeDirectory($backendPath.'/Widgets');
56+
}
57+
58+
if( $this->filesystem->isDirectory($widgetPath) )
59+
{
60+
$this->error('Widget already exist.');
61+
exit;
62+
}
63+
64+
// create necessary component directories
65+
$this->filesystem->makeDirectory($widgetPath);
66+
67+
// process scaffolds
68+
$widgetScaffold = $this->filesystem->get($scaffoldsPath.'Widget');
69+
$widgetViewScaffold = $this->filesystem->get($scaffoldsPath.'widget-view');
70+
71+
$this->comment(PHP_EOL.'Creating widget...'.PHP_EOL);
72+
73+
$widgetScaffold = str_replace('{{widgetNamespace}}',$widgetNamespace,$widgetScaffold);
74+
$widgetScaffold = str_replace('{{widgetTitle}}',$widgetTitle,$widgetScaffold);
75+
$widgetScaffold = str_replace('{{widgetDescription}}',$widgetDescription,$widgetScaffold);
76+
$this->filesystem->put($widgetPath.'/Widget.php',$widgetScaffold);
77+
78+
$widgetViewScaffold = str_replace('{{widgetNamespace}}',$widgetNamespace,$widgetViewScaffold);
79+
$widgetViewScaffold = str_replace('{{widgetTitle}}',$widgetTitle,$widgetViewScaffold);
80+
$widgetViewScaffold = str_replace('{{widgetDescription}}',$widgetDescription,$widgetViewScaffold);
81+
$this->filesystem->put($widgetPath.'/widget-view.blade.php',$widgetViewScaffold);
82+
83+
$this->info(PHP_EOL.'Widget successfully created!'.PHP_EOL);
84+
}
85+
86+
/**
87+
* format any widget name
88+
*
89+
* @param $widgetTitle
90+
* @return mixed
91+
*/
92+
protected function formatToComponentNamespace($widgetTitle)
93+
{
94+
$widgetTitle = str_replace(['/','-'],' ',$widgetTitle);
95+
96+
return str_replace(' ','',ucwords($widgetTitle));
97+
}
98+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace App\Backend\Widgets\{{widgetNamespace}};
2+
3+
use Darryldecode\Backend\Base\Registrar\WidgetInterface;
4+
5+
class Widget implements WidgetInterface
6+
{
7+
/**
8+
* get the widget information
9+
*
10+
* @return array
11+
*/
12+
public function getWidgetInfo()
13+
{
14+
return array(
15+
'name' => '{{widgetTitle}}',
16+
'description' => '{{widgetDescription}}'
17+
);
18+
}
19+
20+
/**
21+
* get the widget template
22+
*
23+
* @return string
24+
*/
25+
public function getWidgetTemplate()
26+
{
27+
return __DIR__.'/widget-view.blade.php';
28+
}
29+
30+
/**
31+
* the widget position
32+
*
33+
* @return int
34+
*/
35+
public function getWidgetPosition()
36+
{
37+
return 5;
38+
}
39+
40+
/**
41+
* determine if widget is needed to be loaded or not
42+
*
43+
* @return bool
44+
*/
45+
public function isWidgetActive()
46+
{
47+
return true;
48+
}
49+
}
50+
51+
return new Widget();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1 class="text-center">{{widgetTitle}} Template</h1>

src/Darryldecode/Backend/Base/Registrar/ComponentLoader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace Darryldecode\Backend\Base\Registrar;
1010

1111
use Illuminate\Filesystem\Filesystem;
12-
use Darryldecode\Backend\Base\Registrar\ComponentInterface;
1312

1413
class ComponentLoader {
1514

src/Darryldecode/Backend/Base/Registrar/Registrar.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@
1111
class Registrar {
1212

1313
/**
14+
* backend active components
15+
*
1416
* @var array
1517
*/
1618
protected $activeComponents = array();
1719

20+
/**
21+
* backend active widgets
22+
*
23+
* @var array
24+
*/
25+
protected $activeWidgets = array();
26+
1827
/**
1928
* @var array
2029
*/
@@ -69,6 +78,29 @@ public function addComponent($component)
6978
return $this;
7079
}
7180

81+
/**
82+
* add widget to registrar
83+
*
84+
* @param \Darryldecode\Backend\Base\Registrar\WidgetInterface|array $widget
85+
* @return $this
86+
*/
87+
public function addWidget($widget)
88+
{
89+
if(is_array($widget))
90+
{
91+
foreach($widget as $w)
92+
{
93+
$this->addWidget($w);
94+
}
95+
}
96+
else
97+
{
98+
array_push($this->activeWidgets, $widget);
99+
}
100+
101+
return $this;
102+
}
103+
72104
/**
73105
* init navigations
74106
*/
@@ -168,4 +200,14 @@ public function getActiveComponents()
168200
{
169201
return $this->activeComponents;
170202
}
203+
204+
/**
205+
* returns all the registered active widgets
206+
*
207+
* @return array
208+
*/
209+
public function getActiveWidgets()
210+
{
211+
return $this->activeWidgets;
212+
}
171213
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Registrar;
4+
5+
6+
interface WidgetInterface {
7+
8+
/**
9+
* get the widget information
10+
*
11+
* @return array
12+
*/
13+
public function getWidgetInfo();
14+
15+
/**
16+
* the widget template
17+
*
18+
* @return callable
19+
*/
20+
public function getWidgetTemplate();
21+
22+
/**
23+
* the widget position. The greater the value, the higher the priority
24+
*
25+
* @return int
26+
*/
27+
public function getWidgetPosition();
28+
29+
/**
30+
* determine if widget is needed to be loaded or not
31+
*
32+
* @return bool
33+
*/
34+
public function isWidgetActive();
35+
}

0 commit comments

Comments
 (0)