1
1
#!/usr/bin/env php
2
2
<?php
3
3
use Ubiquity \cache \CacheManager ;
4
- use Ubiquity \scaffolding \ ConsoleScaffoldController ;
4
+ use Ubiquity \utils \ base \ UString ;
5
5
include 'tools/Console.php ' ;
6
6
include 'tools/Command.php ' ;
7
7
include 'tools/utils/FileUtils.php ' ;
@@ -17,7 +17,7 @@ class Ubiquity {
17
17
18
18
public static function createComposerFile (){
19
19
$ composer =json_encode (self ::$ toolsConfig ["composer " ]);
20
- echo "Composer file creation... \n" ;
20
+ echo ConsoleFormatter:: showInfo ( "Composer file creation... \n" ) ;
21
21
FileUtils::writeFile ("composer.json " , $ composer );
22
22
}
23
23
@@ -66,7 +66,7 @@ class Ubiquity {
66
66
self ::create ($ projectName ,$ options ,true );
67
67
return ;
68
68
}else {
69
- echo "Unable to return to parent folder! \n" ;
69
+ ConsoleFormatter:: showMessage ( "Unable to return to parent folder! \n" , ' error ' ) ;
70
70
return ;
71
71
}
72
72
}
@@ -82,11 +82,11 @@ class Ubiquity {
82
82
FileUtils::safeMkdir ("app/models " );
83
83
FileUtils::safeMkdir ("app/config " );
84
84
define ('ROOT ' , realpath ('./app ' ).DS );
85
- echo "Files copy... \n" ;
85
+ echo ConsoleFormatter:: showInfo ( "Files copy... " ) ;
86
86
FileUtils::xcopy (self ::$ activeDir ."/project-files/app/controllers/ControllerBase.php " , "app/controllers/ControllerBase.php " );
87
87
88
88
89
- echo "Config files creation... \n" ;
89
+ echo ConsoleFormatter:: showInfo ( "Config files creation... \n" ) ;
90
90
self ::openReplaceWrite (self ::$ activeDir ."/project-files/.htaccess " , getcwd ()."/.htaccess " , array ("%rewriteBase% " =>$ projectName ));
91
91
self ::$ configOptions =["%siteUrl% " =>"http://127.0.0.1/ " .$ projectName ."/ " ];
92
92
self ::$ configOptions ["%projectName% " ]=$ projectName ;
@@ -121,12 +121,19 @@ class Ubiquity {
121
121
122
122
self ::createController ($ config ,"IndexController " ,self ::$ indexContent );
123
123
124
- if (self ::isBooleanTrue (self ::$ configOptions ["%all-models% " ]))
124
+ if (self ::isBooleanTrue (self ::$ configOptions ["%all-models% " ])){
125
+ ob_start ();
125
126
(new \Ubiquity \orm \creator \database \DbModelsCreator ())->create ($ config );
127
+ $ res =ob_get_clean ();
128
+ echo ConsoleFormatter::showMessage ($ res ,'success ' ,'models generation ' );
129
+ }
130
+ ob_start ();
126
131
\Ubiquity \cache \CacheManager::checkCache ($ config );
127
- echo "project ` {$ projectName }` successfully created. \n" ;
132
+ $ res =ob_get_clean ();
133
+ echo ConsoleFormatter::showMessage ($ res ,'info ' ,'cache initialization ' );
134
+ echo ConsoleFormatter::showMessage ("project <b> {$ projectName }</b> successfully created. " ,'success ' ,'new-project ' );
128
135
}else {
129
- echo "The {$ projectName } folder already exists ! \n" ;
136
+ echo ConsoleFormatter:: showInfo ( "The <b> {$ projectName }</b> folder already exists ! \n" ) ;
130
137
$ answer =Console::question ("Would you like to continue ? " ,["y " ,"n " ]);
131
138
if (Console::isYes ($ answer )){
132
139
self ::create ($ projectName ,$ options ,true );
@@ -138,10 +145,18 @@ class Ubiquity {
138
145
private static function adminInstall ($ config =null ){
139
146
if (self ::hasSemantic ($ config )){
140
147
FileUtils::xcopy (self ::$ activeDir ."/project-files/app/controllers/Admin.php " ,"app/controllers/Admin.php " );
141
- echo "Creating the Controller Admin at the location app/controllers/Admin.php \n" ;
148
+ echo ConsoleFormatter:: showInfo ( "Creating the Controller <b> Admin</b> at the location <b> app/controllers/Admin.php<:b> \n" ) ;
142
149
self ::$ toolsConfig ["composer " ]["require-dev " ]["czproject/git-php " ]="^3.13 " ;
143
150
}else {
144
- throw new Exception ("UbiquityMyAdmin require phpmv=semantic option. " );
151
+ echo ConsoleFormatter::showMessage ("UbiquityMyAdmin require phpmv=semantic option. " ,'error ' );
152
+ $ answer =Console::question ("Would you like to add Semantic-UI ? " ,["y " ,"n " ]);
153
+ if (Console::isYes ($ answer )){
154
+ self ::$ configOptions ["%phpmv% " ]="semantic " ;
155
+ self ::includePhpmv ();
156
+ self ::adminInstall ($ config );
157
+ }else {
158
+ exit (1 );
159
+ }
145
160
}
146
161
}
147
162
@@ -205,22 +220,19 @@ class Ubiquity {
205
220
}
206
221
207
222
private static function showConfigOptions (){
208
- $ output = implode (' , ' , array_map (
223
+ $ output = implode ("\n" , array_map (
209
224
function ($ v , $ k ) {if (is_array ($ v ))
210
225
$ v =implode (", " ,$ v );
211
226
return sprintf ("%s='%s' " , str_ireplace ("% " , "" , $ k ), $ v ); },
212
227
self ::$ configOptions ,
213
228
array_keys (self ::$ configOptions )
214
229
));
215
- echo "command line arguments : \n" ;
216
- echo $ output ."\n" ;
217
- if (self ::isBooleanTrue (self ::$ configOptions ["%admin% " ]) && !self ::hasSemantic ()){
218
- throw new Exception ("UbiquityMyAdmin require phpmv=semantic option. " );
219
- }
230
+ echo ConsoleFormatter::showMessage ($ output ."\n" ,'info ' ,'Command line arguments ' );
220
231
$ phpmv =self ::$ configOptions ["%phpmv% " ];
221
232
if ($ phpmv !==false ){
222
233
if ($ phpmv !=="bootstrap " && $ phpmv !=="semantic " ){
223
- throw new Exception ($ phpmv ." is not a valid option for phpMv-UI. " );
234
+ echo ConsoleFormatter::showMessage ("<b> " .$ phpmv ."</b> is not a valid option for phpMv-UI. \n" ,'error ' );
235
+ exit (1 );
224
236
}
225
237
}
226
238
}
@@ -234,17 +246,18 @@ class Ubiquity {
234
246
if (Console::isYes ($ answer ))
235
247
self ::createController ($ config ,$ controllerName ,$ indexContent ,$ view ,true );
236
248
}else {
237
- echo "Creation of the Controller {$ controllerName } at the location {$ filename }\n" ;
249
+ $ msg = "Creation of the Controller {$ controllerName } at the location {$ filename }\n" ;
238
250
$ namespace ="" ;
239
251
if (isset ($ config ["mvcNS " ]["controllers " ]) && $ config ["mvcNS " ]["controllers " ]!=="" )
240
252
$ namespace ="namespace " .$ config ["mvcNS " ]["controllers " ]."; " ;
241
253
self ::openReplaceWrite (self ::$ activeDir ."/project-files/templates/controller.tpl " , $ filename , ["%controllerName% " =>$ controllerName ,"%indexContent% " =>$ indexContent ,"%namespace% " =>$ namespace ]);
242
254
if ($ view ){
243
255
$ viewFolder ="app/views/ " .$ controllerName ;
244
256
FileUtils::safeMkdir ($ viewFolder );
245
- echo "Creation of the the associated view index.html at the location {$ viewFolder }/index.html \n" ;
257
+ $ msg .= "Creation of the the associated view index.html at the location {$ viewFolder }/index.html \n" ;
246
258
self ::openReplaceWrite (self ::$ activeDir ."/project-files/templates/view.tpl " , $ viewFolder ."/index.html " , ["%controllerName% " =>$ controllerName ]);
247
259
}
260
+ ConsoleFormatter::showMessage ($ msg , "success " ,"Controller creation " );
248
261
}
249
262
}
250
263
@@ -253,7 +266,7 @@ class Ubiquity {
253
266
$ microDir =$ dir .DIRECTORY_SEPARATOR .self ::$ checkProjectDir ;
254
267
if (file_exists ($ microDir ) && is_dir ($ microDir )){
255
268
chdir ($ dir );
256
- echo "The project folder is " .FileUtils::cleanPathname ($ dir )."\n" ;
269
+ echo ConsoleFormatter:: showInfo ( "The project folder is <b> " .FileUtils::cleanPathname ($ dir )."</b> \n" ) ;
257
270
return true ;
258
271
}
259
272
}
@@ -304,7 +317,7 @@ class Ubiquity {
304
317
private static function selfUpdate (){
305
318
$ config =self ::_init ();
306
319
define ('ROOT ' , realpath ('./app ' ).DS );
307
- echo "Files copy... \n" ;
320
+ echo ConsoleFormatter:: showInfo ( "Files copy... \n" ) ;
308
321
309
322
$ filename ="app/controllers/Admin.php " ;
310
323
if (file_exists ($ filename ) && self ::hasSemantic ($ config )){
@@ -316,13 +329,13 @@ class Ubiquity {
316
329
system ("composer global update " );
317
330
system ("composer update " );
318
331
}
319
- echo "project in ` " .self ::$ activeDir ."` successfully updated.\n" ;
332
+ echo ConsoleFormatter:: showMessage ( "project in <b> " .self ::$ activeDir ."</b> successfully updated." , ' success ' , ' self-update ' ) ;
320
333
}
321
334
322
- public static function init ($ command ){
335
+ public static function init ($ command, $ current = 2 ){
323
336
global $ argv ;
324
337
register_shutdown_function (array ("Ubiquity " ,"error " ));
325
- $ what =@$ argv [2 ];
338
+ $ what =@$ argv [$ current ];
326
339
$ options =self ::parseArguments ();
327
340
switch ($ command ) {
328
341
case "project " :case "create-project " :case "new " :
@@ -346,9 +359,10 @@ class Ubiquity {
346
359
self ::createController ($ config ,$ what ,$ indexContent ,$ view );
347
360
break ;
348
361
case "crud " :case "crud-controller " :
349
- self ::_init ();
362
+ $ config = self ::_init ();
350
363
$ resource =self ::getOption ($ options , 'r ' , 'resource ' ,null );
351
- $ scaffold =new ConsoleScaffoldController (self ::$ activeDir );
364
+ $ resource =self ::getCompleteClassname ($ config , $ resource );
365
+ $ scaffold =new \ConsoleScaffoldController (self ::$ activeDir );
352
366
if (class_exists ($ resource )){
353
367
$ crudDatas =self ::getOption ($ options , 'd ' , 'datas ' ,true );
354
368
$ crudViewer =self ::getOption ($ options , 'v ' , 'viewer ' ,true );
@@ -357,12 +371,12 @@ class Ubiquity {
357
371
$ routePath =self ::getOption ($ options , 'p ' , 'path ' ,'' );
358
372
$ scaffold ->addCrudController ($ what , $ resource ,$ crudDatas ,$ crudViewer ,$ crudEvents ,$ crudViews ,$ routePath );
359
373
}else {
360
- echo "The models class {$ resource } does not exists! \n" ;
374
+ echo ConsoleFormatter:: showMessage ( "The models class <b> {$ resource }</b> does not exists!" , ' error ' , ' crud-controller ' ) ;
361
375
}
362
376
break ;
363
377
case "auth " : case "auth-controller " :
364
378
self ::_init ();
365
- $ scaffold =new ConsoleScaffoldController (self ::$ activeDir );
379
+ $ scaffold =new \ ConsoleScaffoldController (self ::$ activeDir );
366
380
$ baseClass =self ::getOption ($ options , 'e ' , 'extends ' ,"\\Ubiquity \\controllers \\auth \\AuthController " );
367
381
$ authView =self ::getOption ($ options , 't ' , 'templates ' ,'index,info,noAccess,disconnected,message,baseTemplate ' );
368
382
$ routePath =self ::getOption ($ options , 'p ' , 'path ' ,'' );
@@ -373,10 +387,36 @@ class Ubiquity {
373
387
$ config =self ::_init ();
374
388
CacheManager::clearCache ($ config ,$ type );
375
389
break ;
390
+ case "action " : case "new-action " :
391
+ $ config =self ::_init ();
392
+ $ scaffold =new \ConsoleScaffoldController (self ::$ activeDir );
393
+ @list ($ controller ,$ action )=explode ('. ' , $ what );
394
+ if ($ controller !=null && $ action !=null ){
395
+ $ controller =self ::getCompleteClassname ($ config , $ controller ,'controllers ' );
396
+ if (class_exists ($ controller )){
397
+ $ parameters =self ::getOption ($ options , 'p ' , 'params ' );
398
+ $ routePath =self ::getOption ($ options , 'r ' , 'route ' );
399
+ $ createView =self ::getOption ($ options , 'v ' , 'create-view ' ,false );
400
+ $ routeInfo =null ;
401
+ if ($ routePath !=null ){
402
+ $ routeInfo =["path " =>$ routePath ,"methods " =>null ];
403
+ }
404
+ $ scaffold ->_newAction ($ controller , $ action ,$ parameters ,'' ,$ routeInfo ,$ createView );
405
+ }
406
+ else {
407
+ echo ConsoleFormatter::showMessage ("The controller class <b> {$ controller }</b> does not exists! " ,'error ' ,'new-action ' );
408
+ }
409
+ }else {
410
+ echo ConsoleFormatter::showMessage ("You must use <b>controller.action</b> notation! " ,'error ' ,'new-action ' );
411
+ }
412
+ break ;
376
413
case "init-cache " :
377
414
$ type =self ::getOption ($ options , "t " , "type " ,"all " );
378
415
$ config =self ::_init ();
416
+ ob_start ();
379
417
CacheManager::initCache ($ config ,$ type );
418
+ $ res =ob_get_clean ();
419
+ echo ConsoleFormatter::showMessage ($ res ,'success ' ,'init-cache: ' .$ type );
380
420
break ;
381
421
case "self-update " :
382
422
self ::selfUpdate ();
@@ -386,33 +426,71 @@ class Ubiquity {
386
426
self ::adminInstall ($ config );
387
427
break ;
388
428
case "help " :
389
- self ::info ();
429
+ if (isset ($ what )){
430
+ self ::infoCmd ($ what );
431
+ }else {
432
+ self ::info ();
433
+ }
390
434
break ;
391
435
default :
392
- self ::info ();
436
+ $ commands =Command::getInfo ($ command );
437
+ echo ConsoleFormatter::showMessage ("The command {$ command } does not exists! " ,'warning ' );
438
+ if (sizeof ($ commands )>0 ){
439
+ $ cmd =current ($ commands )["cmd " ];
440
+ $ answer =Console::question ("Did you mean {$ cmd ->getName ()}? " ,["y " ,"n " ]);
441
+ if (Console::isYes ($ answer )){
442
+ self ::init ($ cmd ->getName (),1 );
443
+ }else {
444
+ exit (1 );
445
+ }
446
+ }
393
447
break ;
394
448
}
395
449
}
396
450
451
+ private static function getCompleteClassname ($ config ,$ classname ,$ type ='models ' ){
452
+ $ prefix =$ config ["mvcNS " ][$ type ]??null ;
453
+ $ classname =ltrim ($ classname ,"\\" );
454
+ if (isset ($ prefix )){
455
+ if (!UString::startswith ($ classname ,$ prefix )){
456
+ $ classname =$ prefix ."\\" .$ classname ;
457
+ }
458
+ }
459
+ return $ classname ;
460
+ }
461
+
397
462
private static function info (){
398
463
echo self ::$ appName ." ( " .self ::$ version .") \n" ;
399
464
$ commands =Command::getCommands ();
400
465
foreach ($ commands as $ command ){
401
- echo $ command ->longString ();
466
+ echo ConsoleFormatter::formatHtml ($ command ->longString ());
467
+ echo "\n" ;
468
+ }
469
+ }
470
+
471
+ private static function infoCmd ($ cmd ){
472
+ $ infos =Command::getInfo ($ cmd );
473
+ $ command =null ;
474
+ foreach ($ infos as $ info ){
475
+ echo ConsoleFormatter::showInfo ($ info ['info ' ]);
476
+ if ($ command !==$ info ['cmd ' ]){
477
+ echo ConsoleFormatter::formatHtml ($ info ['cmd ' ]->longString ());
478
+ }
479
+ $ command =$ info ['cmd ' ];
402
480
echo "\n" ;
403
481
}
404
482
}
405
483
private static function _init (){
406
484
self ::$ activeDir =dirname (__FILE__ );
407
485
if (!self ::setDir (getcwd ())){
408
- echo "Failed to locate project root folder \n" ;
409
- echo "An Ubiquity project must contain the " .self ::$ checkProjectDir ." folder. \n" ;
486
+ echo ConsoleFormatter::showMessage ("Failed to locate project root folder \nAn Ubiquity project must contain the <b> " .self ::$ checkProjectDir ."</b> folder. " ,'error ' );
410
487
die ();
411
488
}
412
489
define ('ROOT ' , realpath ('./app ' ).DS );
413
490
$ config =require_once 'app/config/config.php ' ;
414
491
require_once ROOT .'./../vendor/autoload.php ' ;
415
492
\Ubiquity \controllers \Startup::setConfig ($ config );
493
+ include 'tools/ConsoleScaffoldController.php ' ;
416
494
return $ config ;
417
495
}
418
496
0 commit comments