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+ }
0 commit comments