Skip to content

Commit a4ccb79

Browse files
committed
Refactoring with ConsoleFormatter
1 parent 884c3a7 commit a4ccb79

File tree

8 files changed

+301
-44
lines changed

8 files changed

+301
-44
lines changed

Ubiquity

Lines changed: 111 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env php
22
<?php
33
use Ubiquity\cache\CacheManager;
4-
use Ubiquity\scaffolding\ConsoleScaffoldController;
4+
use Ubiquity\utils\base\UString;
55
include 'tools/Console.php';
66
include 'tools/Command.php';
77
include 'tools/utils/FileUtils.php';
@@ -17,7 +17,7 @@ class Ubiquity {
1717

1818
public static function createComposerFile(){
1919
$composer=json_encode(self::$toolsConfig["composer"]);
20-
echo "Composer file creation...\n";
20+
echo ConsoleFormatter::showInfo("Composer file creation...\n");
2121
FileUtils::writeFile("composer.json", $composer);
2222
}
2323

@@ -66,7 +66,7 @@ class Ubiquity {
6666
self::create($projectName,$options,true);
6767
return;
6868
}else{
69-
echo "Unable to return to parent folder!\n";
69+
ConsoleFormatter::showMessage( "Unable to return to parent folder!\n",'error');
7070
return;
7171
}
7272
}
@@ -82,11 +82,11 @@ class Ubiquity {
8282
FileUtils::safeMkdir("app/models");
8383
FileUtils::safeMkdir("app/config");
8484
define('ROOT', realpath('./app').DS);
85-
echo "Files copy...\n";
85+
echo ConsoleFormatter::showInfo("Files copy...");
8686
FileUtils::xcopy(self::$activeDir."/project-files/app/controllers/ControllerBase.php", "app/controllers/ControllerBase.php");
8787

8888

89-
echo "Config files creation...\n";
89+
echo ConsoleFormatter::showInfo("Config files creation...\n");
9090
self::openReplaceWrite(self::$activeDir."/project-files/.htaccess", getcwd()."/.htaccess", array("%rewriteBase%"=>$projectName));
9191
self::$configOptions=["%siteUrl%"=>"http://127.0.0.1/".$projectName."/"];
9292
self::$configOptions["%projectName%"]=$projectName;
@@ -121,12 +121,19 @@ class Ubiquity {
121121

122122
self::createController($config,"IndexController",self::$indexContent);
123123

124-
if(self::isBooleanTrue(self::$configOptions["%all-models%"]))
124+
if(self::isBooleanTrue(self::$configOptions["%all-models%"])){
125+
ob_start();
125126
(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();
126131
\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');
128135
}else{
129-
echo "The {$projectName} folder already exists !\n";
136+
echo ConsoleFormatter::showInfo("The <b>{$projectName}</b> folder already exists !\n");
130137
$answer=Console::question("Would you like to continue ?",["y","n"]);
131138
if(Console::isYes($answer)){
132139
self::create($projectName,$options,true);
@@ -138,10 +145,18 @@ class Ubiquity {
138145
private static function adminInstall($config=null){
139146
if(self::hasSemantic($config)){
140147
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");
142149
self::$toolsConfig["composer"]["require-dev"]["czproject/git-php"]="^3.13";
143150
}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+
}
145160
}
146161
}
147162

@@ -205,22 +220,19 @@ class Ubiquity {
205220
}
206221

207222
private static function showConfigOptions(){
208-
$output = implode(', ', array_map(
223+
$output = implode("\n", array_map(
209224
function ($v, $k) {if(is_array($v))
210225
$v=implode(",",$v );
211226
return sprintf("%s='%s'", str_ireplace("%", "", $k), $v); },
212227
self::$configOptions,
213228
array_keys(self::$configOptions)
214229
));
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');
220231
$phpmv=self::$configOptions["%phpmv%"];
221232
if($phpmv!==false){
222233
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);
224236
}
225237
}
226238
}
@@ -234,17 +246,18 @@ class Ubiquity {
234246
if(Console::isYes($answer))
235247
self::createController($config,$controllerName,$indexContent,$view,true);
236248
}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";
238250
$namespace="";
239251
if(isset($config["mvcNS"]["controllers"]) && $config["mvcNS"]["controllers"]!=="")
240252
$namespace="namespace ".$config["mvcNS"]["controllers"].";";
241253
self::openReplaceWrite(self::$activeDir."/project-files/templates/controller.tpl", $filename, ["%controllerName%"=>$controllerName,"%indexContent%"=>$indexContent,"%namespace%"=>$namespace]);
242254
if($view){
243255
$viewFolder="app/views/".$controllerName;
244256
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";
246258
self::openReplaceWrite(self::$activeDir."/project-files/templates/view.tpl", $viewFolder."/index.html", ["%controllerName%"=>$controllerName]);
247259
}
260+
ConsoleFormatter::showMessage($msg, "success","Controller creation");
248261
}
249262
}
250263

@@ -253,7 +266,7 @@ class Ubiquity {
253266
$microDir=$dir.DIRECTORY_SEPARATOR.self::$checkProjectDir;
254267
if(file_exists($microDir) && is_dir($microDir)){
255268
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");
257270
return true;
258271
}
259272
}
@@ -304,7 +317,7 @@ class Ubiquity {
304317
private static function selfUpdate(){
305318
$config=self::_init();
306319
define('ROOT', realpath('./app').DS);
307-
echo "Files copy...\n";
320+
echo ConsoleFormatter::showInfo("Files copy...\n");
308321

309322
$filename="app/controllers/Admin.php";
310323
if(file_exists($filename) && self::hasSemantic($config)){
@@ -316,13 +329,13 @@ class Ubiquity {
316329
system("composer global update");
317330
system("composer update");
318331
}
319-
echo "project in `".self::$activeDir."` successfully updated.\n";
332+
echo ConsoleFormatter::showMessage("project in <b>".self::$activeDir."</b> successfully updated.",'success','self-update');
320333
}
321334

322-
public static function init($command){
335+
public static function init($command,$current=2){
323336
global $argv;
324337
register_shutdown_function(array("Ubiquity","error"));
325-
$what=@$argv[2];
338+
$what=@$argv[$current];
326339
$options=self::parseArguments();
327340
switch ($command) {
328341
case "project":case "create-project":case "new":
@@ -346,9 +359,10 @@ class Ubiquity {
346359
self::createController($config,$what,$indexContent,$view);
347360
break;
348361
case "crud":case "crud-controller":
349-
self::_init();
362+
$config=self::_init();
350363
$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);
352366
if(class_exists($resource)){
353367
$crudDatas=self::getOption($options, 'd', 'datas',true);
354368
$crudViewer=self::getOption($options, 'v', 'viewer',true);
@@ -357,12 +371,12 @@ class Ubiquity {
357371
$routePath=self::getOption($options, 'p', 'path','');
358372
$scaffold->addCrudController($what, $resource,$crudDatas,$crudViewer,$crudEvents,$crudViews,$routePath);
359373
}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');
361375
}
362376
break;
363377
case "auth": case "auth-controller":
364378
self::_init();
365-
$scaffold=new ConsoleScaffoldController(self::$activeDir);
379+
$scaffold=new \ConsoleScaffoldController(self::$activeDir);
366380
$baseClass=self::getOption($options, 'e', 'extends',"\\Ubiquity\\controllers\\auth\\AuthController");
367381
$authView=self::getOption($options, 't', 'templates','index,info,noAccess,disconnected,message,baseTemplate');
368382
$routePath=self::getOption($options, 'p', 'path','');
@@ -373,10 +387,36 @@ class Ubiquity {
373387
$config=self::_init();
374388
CacheManager::clearCache($config,$type);
375389
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;
376413
case "init-cache":
377414
$type=self::getOption($options, "t", "type","all");
378415
$config=self::_init();
416+
ob_start();
379417
CacheManager::initCache($config,$type);
418+
$res=ob_get_clean();
419+
echo ConsoleFormatter::showMessage($res,'success','init-cache:'.$type);
380420
break;
381421
case "self-update":
382422
self::selfUpdate();
@@ -386,33 +426,71 @@ class Ubiquity {
386426
self::adminInstall($config);
387427
break;
388428
case "help":
389-
self::info();
429+
if(isset($what)){
430+
self::infoCmd($what);
431+
}else{
432+
self::info();
433+
}
390434
break;
391435
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+
}
393447
break;
394448
}
395449
}
396450

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+
397462
private static function info(){
398463
echo self::$appName." (".self::$version.")\n";
399464
$commands=Command::getCommands();
400465
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'];
402480
echo "\n";
403481
}
404482
}
405483
private static function _init(){
406484
self::$activeDir=dirname(__FILE__);
407485
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');
410487
die();
411488
}
412489
define('ROOT', realpath('./app').DS);
413490
$config=require_once 'app/config/config.php';
414491
require_once ROOT.'./../vendor/autoload.php';
415492
\Ubiquity\controllers\Startup::setConfig($config);
493+
include 'tools/ConsoleScaffoldController.php';
416494
return $config;
417495
}
418496

project-files/templates/action.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%route% public function %actionName%(%parameters%){
2+
%content%
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
*@%name%(%properties%)
3+
**/

0 commit comments

Comments
 (0)