From dfe7dfc9d9f1a125cc5f3aa61d2a6104916fa301 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Wed, 20 Aug 2014 15:58:41 +0300 Subject: [PATCH] application level commands --- ipf/application.php | 10 +++ ipf/auth/app.php | 7 ++ .../commands}/createsuperuser.php | 18 +--- ipf/cli.php | 84 +++++++++++-------- ipf/shell.php | 10 +++ 5 files changed, 82 insertions(+), 47 deletions(-) rename ipf/{command => auth/commands}/createsuperuser.php (56%) diff --git a/ipf/application.php b/ipf/application.php index 037f86d..d6b049d 100644 --- a/ipf/application.php +++ b/ipf/application.php @@ -73,6 +73,16 @@ abstract class IPF_Application return strtolower($e[count($e)-1]); } + /** + * Returns CLI commands + * + * @return array List of commands + */ + public function commands() + { + return array(); + } + /** * Returns additional context for templates * diff --git a/ipf/auth/app.php b/ipf/auth/app.php index 8c6f4a2..96bebb6 100644 --- a/ipf/auth/app.php +++ b/ipf/auth/app.php @@ -181,5 +181,12 @@ class IPF_Auth_App extends IPF_Application return $app->getTitle().' | '.$admin->verbose_name().' | '.ucfirst($parts[2]); } + + public function commands() + { + return array( + new IPF_Auth_Command_CreateSuperUser, + ); + } } diff --git a/ipf/command/createsuperuser.php b/ipf/auth/commands/createsuperuser.php similarity index 56% rename from ipf/command/createsuperuser.php rename to ipf/auth/commands/createsuperuser.php index 780f6dd..19a76fd 100644 --- a/ipf/command/createsuperuser.php +++ b/ipf/auth/commands/createsuperuser.php @@ -1,6 +1,6 @@ readString(" Username: "); - $password = $this->readString(" Password: "); - $email = $this->readString(" E-mail: "); + $username = IPF_Shell::ask(' Username: '); + $password = IPF_Shell::ask(' Password: '); + $email = IPF_Shell::ask(' E-mail: '); $project = IPF_Project::getInstance(); @@ -25,15 +25,5 @@ class IPF_Command_CreateSuperUser $su->save(); print "Done\n"; } - - private function readString($prompt) - { - $value = ''; - while (!$value) { - print $prompt; - $value = trim(fgets(STDIN)); - } - return $value; - } } diff --git a/ipf/cli.php b/ipf/cli.php index 3e9a0ce..eb704a5 100644 --- a/ipf/cli.php +++ b/ipf/cli.php @@ -1,5 +1,13 @@ commands = array( - new IPF_Command_Shell, - new IPF_Command_DebugServer, - new IPF_Command_Routes, - new IPF_Command_BuildModels, - new IPF_Command_BuildContribModels, - new IPF_Command_Sql, - new IPF_Command_SyncDB, - new IPF_Command_Fixtures, - new IPF_Command_DB, - new IPF_Command_DBDump, - new IPF_Command_DBRestore, - new IPF_Command_CollectStatic, - new IPF_Command_Pack, - new IPF_Command_Unpack, - new IPF_Command_CreateSuperUser, - new IPF_Command_CreateMigration, - new IPF_Command_Migrate, - new IPF_Command_SyncPerms, + 'IPF' => array( + new IPF_Command_Shell, + new IPF_Command_DebugServer, + new IPF_Command_Routes, + new IPF_Command_BuildModels, + new IPF_Command_BuildContribModels, + new IPF_Command_Sql, + new IPF_Command_SyncDB, + new IPF_Command_Fixtures, + new IPF_Command_DB, + new IPF_Command_DBDump, + new IPF_Command_DBRestore, + new IPF_Command_CollectStatic, + new IPF_Command_Pack, + new IPF_Command_Unpack, + new IPF_Command_CreateMigration, + new IPF_Command_Migrate, + new IPF_Command_SyncPerms, + ), ); - + + foreach (IPF_Project::getInstance()->appList() as $app) { + $commands = $app->commands(); + if (count($commands)) + $this->commands[$app->getName()] = $commands; + } + + $project = array(); foreach (IPF::get('commands', array()) as $cmd) { if (is_string($cmd)) $cmd = new $cmd; - $this->commands[] = $cmd; + $project[] = $cmd; } + if (count($project)) + $this->commands['Project'] = $project; } protected function usage() { - print "Usage: php index.php [options] [args]\n\n"; - print "Available subcommands:\n"; + print "Usage: php index.php [options] [args]\n"; - $rows = array(); - foreach ($this->commands as $command) - $rows[] = array( - ' ' . $command->command, - $command->description, - ); + foreach ($this->commands as $group => $commands) { + print "\n\033[1;36m$group:\033[0m\n"; - IPF_Shell::displayTwoColumns($rows); + foreach ($commands as $command) { + echo ' '.str_pad($command->command, 20) . "\t" . $command->description . "\n"; + } + } print "\n"; } @@ -75,10 +91,12 @@ class IPF_Cli return; } - foreach ($this->commands as $command) { - if ($command->command === $args[1]) { - $command->run(array_slice($args, 2)); - return; + foreach ($this->commands as $group => $commands) { + foreach ($commands as $command) { + if ($command->command === $args[1]) { + $command->run(array_slice($args, 2)); + return; + } } } diff --git a/ipf/shell.php b/ipf/shell.php index c543e82..a5f686c 100644 --- a/ipf/shell.php +++ b/ipf/shell.php @@ -42,5 +42,15 @@ class IPF_Shell echo str_pad($row[0], $firstColumnSize) . "\t" . $row[1] . "\n"; } } + + public static function ask($prompt) + { + $value = ''; + while (!$value) { + print $prompt; + $value = trim(fgets(STDIN)); + } + return $value; + } } -- 2.49.0