From: Andrey Kutejko Date: Thu, 20 Jun 2013 21:04:34 +0000 (+0300) Subject: rework cli commands. allow custom per-project commands X-Git-Tag: 0.5~224 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=d8d3b6e8fe2df6d438c98598d6be42581b6a3f0c;p=ipf.git rework cli commands. allow custom per-project commands --- diff --git a/ipf/cli.php b/ipf/cli.php index 14f480d..4fa2325 100644 --- a/ipf/cli.php +++ b/ipf/cli.php @@ -1,101 +1,67 @@ commands = array('help','sql','buildmodels','buildcontribmodels','syncdb', 'createsuperuser', 'syncperms', 'fixtures'); - } - - protected function usage(&$args){ - print "Type 'php index.php help' for usage.\n"; - } - - protected function main_help(&$args){ - print "php index.php [options] [args]\n"; - print "Available subcommands:\n"; - foreach ($this->commands as &$command) - print " $command\n"; - } - - protected function help($args){ - if (count($args)==2) - $this->main_help($args); - } - - protected function sql(&$args){ - print "Show All Sql DDL From Model Classes\n"; - print IPF_Project::getInstance()->generateSql(); - } - - protected function syncdb(&$args){ - print "Create Tables From Model Classes\n"; - IPF_Project::getInstance()->createTablesFromModels(); - } - - protected function syncperms(&$args) + public function __construct() { - print "Create/Update Permissions From Model Classes\n"; - IPF_Project::getInstance()->createPermissionsFromModels(); - } - - protected function buildmodels(&$args){ - print "Build All Model Classses\n"; - IPF_Project::getInstance()->generateModels(); - } - - protected function buildcontribmodels(&$args){ - print "Build All Contrib Model Classses\n"; - IPF_Project::getInstance()->generateContribModels(); - } - - protected function createSuperUser(&$args){ - print "Create SuperUser\n"; - - IPF_Project::getInstance()->loadModels(); - - $username = ''; while ($username==''){ print " Username: "; $username = trim(fgets(STDIN)); }; - $password = ''; while ($password==''){ print " Password: "; $password = trim(fgets(STDIN)); }; - $email = ''; while ($email==''){ print " e-mail: "; $email = trim(fgets(STDIN)); }; - - $su = new User(); - $su->username = $username; - $su->email = $email; - $su->is_staff = true; - $su->is_active = true; - $su->is_superuser = true; - $su->setPassword($password); - $su->save(); - print "Done\n"; + $this->commands = array( + new IPF_Command_BuildModels, + new IPF_Command_BuildContribModels, + new IPF_Command_Sql, + new IPF_Command_SyncDB, + new IPF_Command_Fixtures, + new IPF_Command_CreateSuperUser, + new IPF_Command_SyncPerms, + ); + + foreach (IPF::get('commands', array()) as $cmd) { + if (is_string($cmd)) + $cmd = new $cmd; + $this->commands[] = $cmd; + } } - protected function fixtures(&$args) + protected function usage() { - print "Load project fixtures to database\n"; - IPF_Project::getInstance()->loadFixtures(); + print "Usage: php index.php [options] [args]\n\n"; + print "Available subcommands:\n"; + + $firstColumnSize = 7; + foreach ($this->commands as $command) { + $l = strlen($command->command); + if ($l > $firstColumnSize) + $firstColumnSize = $l; + } + foreach ($this->commands as $command) { + print ' '.str_pad($command->command, $firstColumnSize) . "\t" . $command->description . "\n"; + } + print "\n"; } public function run() { print "IPF command line tool. Version: ".IPF_Version::$name."\n"; - print "Project config: ".IPF::get('settings_file')."\n"; + print "Project config: ".IPF::get('settings_file')."\n\n"; $opt = new IPF_Getopt(); //$z = $opt->getopt2($opt->readPHPArgv(), array('s',)); //, array('s',)); $args = $opt->readPHPArgv(); - if (count($args) == 1) { - $this->usage($args); + if (count($args) < 2) { + $this->usage(); return; } - - if (in_array($args[1], $this->commands)) { - eval('$this->'.$args[1].'($args);'); - return; + + foreach ($this->commands as $command) { + if ($command->command === $args[1]) { + $command->run(array_slice($args, 2)); + return; + } } - print "Unknown command: '".$args[1]."'\n"; - $this->usage($args); + print "Unknown command: '".$args[1]."'\n\n"; + $this->usage(); } } diff --git a/ipf/command/buildcontribmodels.php b/ipf/command/buildcontribmodels.php new file mode 100644 index 0000000..9393be5 --- /dev/null +++ b/ipf/command/buildcontribmodels.php @@ -0,0 +1,18 @@ +frameworkApps() as $app) + $app->generateModels(); + } +} + diff --git a/ipf/command/buildmodels.php b/ipf/command/buildmodels.php new file mode 100644 index 0000000..fed3806 --- /dev/null +++ b/ipf/command/buildmodels.php @@ -0,0 +1,23 @@ +customApps() as $app) + $app->generateModels(); + } +} + diff --git a/ipf/command/createsuperuser.php b/ipf/command/createsuperuser.php new file mode 100644 index 0000000..e230e2c --- /dev/null +++ b/ipf/command/createsuperuser.php @@ -0,0 +1,41 @@ +readString(" Username: "); + $password = $this->readString(" Password: "); + $email = $this->readString(" E-mail: "); + + $project = IPF_Project::getInstance(); + + $project->loadModels(); + + $su = new User; + $su->username = $username; + $su->email = $email; + $su->is_staff = true; + $su->is_active = true; + $su->is_superuser = true; + $su->setPassword($password); + $su->save(); + print "Done\n"; + } + + private function readString($prompt) + { + $value = ''; + while (!$value) { + print $prompt; + $value = trim(fgets(STDIN)); + } + return $value; + } +} + diff --git a/ipf/command/fixtures.php b/ipf/command/fixtures.php new file mode 100644 index 0000000..c350163 --- /dev/null +++ b/ipf/command/fixtures.php @@ -0,0 +1,59 @@ +customApps() as $app) + $paths[] = $app->path; + + $fixtures = array(); + foreach ($paths as $path) { + $path .= DIRECTORY_SEPARATOR.'fixtures.php'; + if (is_file($path)) + $fixtures = array_merge($fixtures, require $path); + } + + if (!count($fixtures)) { + echo "No fixtures found\n"; + return; + } + + $project->loadModels(); + + foreach ($fixtures as $fixture) { + $modelClass = $fixture['model']; + $key = $fixture['key']; + $records = $fixture['records']; + echo "Loading $modelClass "; + foreach ($records as $record) { + $model = IPF_ORM::getTable($modelClass) + ->createQuery() + ->where($key . ' = ?', array($record[$key])) + ->limit(1) + ->execute(); + + if ($model) + $model = $model[0]; + else + $model = new $modelClass; + + foreach ($record as $k => $v) + $model->$k = $v; + + $model->save(); + echo '.'; + } + echo "\n"; + } + } +} + diff --git a/ipf/command/sql.php b/ipf/command/sql.php new file mode 100644 index 0000000..ac22e70 --- /dev/null +++ b/ipf/command/sql.php @@ -0,0 +1,27 @@ +frameworkApps() as $app) + $sql .= $app->generateSql()."\n"; + + $sql .= IPF_ORM::generateSqlFromModels(IPF::get('project_path').DIRECTORY_SEPARATOR.'models')."\n"; + + foreach ($project->customApps() as $app) + $sql .= $app->generateSql()."\n"; + + print $sql; + } +} + diff --git a/ipf/command/syncdb.php b/ipf/command/syncdb.php new file mode 100644 index 0000000..9c52373 --- /dev/null +++ b/ipf/command/syncdb.php @@ -0,0 +1,21 @@ +frameworkApps() as $app) + $app->createTablesFromModels(); + IPF_ORM::createTablesFromModels(IPF::get('project_path').DIRECTORY_SEPARATOR.'models'); + foreach ($project->customApps() as $app) + $app->createTablesFromModels(); + } +} + diff --git a/ipf/command/syncperms.php b/ipf/command/syncperms.php new file mode 100644 index 0000000..e2a5e5c --- /dev/null +++ b/ipf/command/syncperms.php @@ -0,0 +1,26 @@ +apps as $appname => &$app) { + $app = $project->getApp($appName); + $pathes[] = $app->getPath().'models'; + } + + $pathes[] = IPF::get('project_path').DIRECTORY_SEPARATOR.'models'; + + return IPF_Auth_App::createPermissionsFromModels($pathes); + } +} + diff --git a/ipf/project.php b/ipf/project.php index 60328b3..24d5ac7 100644 --- a/ipf/project.php +++ b/ipf/project.php @@ -61,7 +61,7 @@ final class IPF_Project $this->getApp($appname); } - protected function frameworkApps() + public function frameworkApps() { $result = array(); foreach ($this->apps as $appname => &$app) { @@ -71,7 +71,7 @@ final class IPF_Project return $result; } - protected function customApps() + public function customApps() { $result = array(); foreach ($this->apps as $appname => &$app) { @@ -81,99 +81,6 @@ final class IPF_Project return $result; } - public function generateModels() - { - IPF_ORM::generateModelsFromYaml( - IPF::get('project_path').DIRECTORY_SEPARATOR.'models.yml', - IPF::get('project_path').DIRECTORY_SEPARATOR.'models' - ); - foreach ($this->customApps() as $app) - $app->generateModels(); - } - - public function generateContribModels() - { - foreach ($this->frameworkApps() as $app) - $app->generateModels(); - } - - public function createTablesFromModels() - { - foreach ($this->frameworkApps() as $app) - $app->createTablesFromModels(); - IPF_ORM::createTablesFromModels(IPF::get('project_path').DIRECTORY_SEPARATOR.'models'); - foreach ($this->customApps() as $app) - $app->createTablesFromModels(); - } - - public function createPermissionsFromModels() - { - $pathes = array(); - - foreach ($this->apps as $appname => &$app) { - $app = $this->getApp($appName); - $pathes[] = $app->getPath().'models'; - } - - $pathes[] = IPF::get('project_path').DIRECTORY_SEPARATOR.'models'; - - return IPF_Auth_App::createPermissionsFromModels($pathes); - } - - public function generateSql() - { - $sql = ''; - - foreach ($this->frameworkApps() as $app) - $sql .= $app->generateSql()."\n"; - - $sql .= IPF_ORM::generateSqlFromModels(IPF::get('project_path').DIRECTORY_SEPARATOR.'models')."\n"; - - foreach ($this->customApps() as $app) - $sql .= $app->generateSql()."\n"; - - return $sql; - } - - public function loadFixtures() - { - $ficturesPath = IPF::get('project_path').DIRECTORY_SEPARATOR.'fixtures.php'; - if (!is_file($ficturesPath)) { - echo "No fixtures found\n"; - return; - } - - $this->loadModels(); - - $fixtures = require $ficturesPath; - - foreach ($fixtures as $fixture) { - $modelClass = $fixture['model']; - $key = $fixture['key']; - $records = $fixture['records']; - echo "Loading $modelClass "; - foreach ($records as $record) { - $model = IPF_ORM::getTable($modelClass) - ->createQuery() - ->where($key . ' = ?', array($record[$key])) - ->limit(1) - ->execute(); - - if ($model) - $model = $model[0]; - else - $model = new $modelClass; - - foreach ($record as $k => $v) - $model->$k = $v; - - $model->save(); - echo '.'; - } - echo "\n"; - } - } - public function loadModels() { foreach ($this->frameworkApps() as $app) @@ -185,12 +92,6 @@ final class IPF_Project $app->loadModels(); } - private function cli() - { - $cli = new IPF_Cli(); - $cli->run(); - } - public function run() { if (IPF::get('debug')) { @@ -201,7 +102,8 @@ final class IPF_Project IPF_ORM_Manager::getInstance()->openConnection(IPF::get('database', IPF::get('dsn'))); if (php_sapi_name() === 'cli') { - $this->cli(); + $cli = new IPF_Cli; + $cli->run(); } else { $this->loadModels(); $this->router = new IPF_Router();