]> git.andy128k.dev Git - ipf.git/commitdiff
rework cli commands. allow custom per-project commands
authorAndrey Kutejko <andy128k@gmail.com>
Thu, 20 Jun 2013 21:04:34 +0000 (00:04 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Thu, 20 Jun 2013 21:04:34 +0000 (00:04 +0300)
ipf/cli.php
ipf/command/buildcontribmodels.php [new file with mode: 0644]
ipf/command/buildmodels.php [new file with mode: 0644]
ipf/command/createsuperuser.php [new file with mode: 0644]
ipf/command/fixtures.php [new file with mode: 0644]
ipf/command/sql.php [new file with mode: 0644]
ipf/command/syncdb.php [new file with mode: 0644]
ipf/command/syncperms.php [new file with mode: 0644]
ipf/project.php

index 14f480d4c213d0ad8ec41bb4c1977798ca197377..4fa232559f5d58ed13b4ffd65a9496bd1ff02746 100644 (file)
 <?php
 
-class IPF_Cli{
-
+class IPF_Cli
+{
     protected $commands;
 
-    public function __construct(){
-        $this->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 <subcommand> [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 <subcommand> [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 (file)
index 0000000..9393be5
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+class IPF_Command_BuildContribModels
+{
+    public $command = 'buildcontribmodels';
+    public $description = 'Build all contrib model classes';
+
+    public function run($args=null)
+    {
+        print "Build all contrib model classes\n";
+
+        $project = IPF_Project::getInstance();
+
+        foreach ($project->frameworkApps() as $app)
+            $app->generateModels();
+    }
+}
+
diff --git a/ipf/command/buildmodels.php b/ipf/command/buildmodels.php
new file mode 100644 (file)
index 0000000..fed3806
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+class IPF_Command_BuildModels
+{
+    public $command = 'buildmodels';
+    public $description = 'Build all model classes';
+
+    public function run($args=null)
+    {
+        print "Build All Model Classes\n";
+
+        $project = IPF_Project::getInstance();
+
+        IPF_ORM::generateModelsFromYaml(
+            IPF::get('project_path').DIRECTORY_SEPARATOR.'models.yml',
+            IPF::get('project_path').DIRECTORY_SEPARATOR.'models'
+        );
+
+        foreach ($project->customApps() as $app)
+            $app->generateModels();
+    }
+}
+
diff --git a/ipf/command/createsuperuser.php b/ipf/command/createsuperuser.php
new file mode 100644 (file)
index 0000000..e230e2c
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+class IPF_Command_CreateSuperUser
+{
+    public $command = 'createsuperuser';
+    public $description = 'Create superuser';
+
+    public function run($args=null)
+    {
+        print "Create superuser\n";
+
+        $username = $this->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 (file)
index 0000000..c350163
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+class IPF_Command_Fixtures
+{
+    public $command = 'fixtures';
+    public $description = 'Load fixtures into database';
+
+    public function run($args=null)
+    {
+        print "Load project fixtures to database\n";
+
+        $project = IPF_Project::getInstance();
+
+        $paths = array(IPF::get('project_path'));
+        foreach ($project->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 (file)
index 0000000..ac22e70
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+class IPF_Command_Sql
+{
+    public $command = 'sql';
+    public $description = 'Show all SQL DDL from model classes';
+
+    public function run($args=null)
+    {
+        print "Show all SQL DDL from model classes\n";
+
+        $project = IPF_Project::getInstance();
+
+        $sql = '';
+
+        foreach ($project->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 (file)
index 0000000..9c52373
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+class IPF_Command_SyncDB
+{
+    public $command = 'syncdb';
+    public $description = 'Create tables from model classes';
+
+    public function run($args=null)
+    {
+        print "Create tables from model classes\n";
+
+        $project = IPF_Project::getInstance();
+
+        foreach ($project->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 (file)
index 0000000..e2a5e5c
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+class IPF_Command_SyncPerms
+{
+    public $command = 'syncperms';
+    public $description = 'Create/Update permissions from model classes';
+
+    public function run($args=null)
+    {
+        print "Create/Update permissions from model classes\n";
+
+        $project = IPF_Project::getInstance();
+
+        $pathes = array();
+
+        foreach ($project->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);
+    }
+}
+
index 60328b34874723d05e60055826891b7da2df7303..24d5ac7b017ec9c09f7b53facd8b11d87aabf668 100644 (file)
@@ -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();