]> git.andy128k.dev Git - ipf.git/commitdiff
simple fixtures
authorAndrey Kutejko <andy128k@gmail.com>
Thu, 13 Jun 2013 19:41:38 +0000 (22:41 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Thu, 13 Jun 2013 19:41:38 +0000 (22:41 +0300)
ipf/cli.php
ipf/project.php

index 00e2132694899658089527f5f0ff73376184c29f..14f480d4c213d0ad8ec41bb4c1977798ca197377 100644 (file)
@@ -5,7 +5,7 @@ class IPF_Cli{
     protected $commands;
 
     public function __construct(){
-        $this->commands = array('help','sql','buildmodels','buildcontribmodels','syncdb', 'createsuperuser', 'syncperms');
+        $this->commands = array('help','sql','buildmodels','buildcontribmodels','syncdb', 'createsuperuser', 'syncperms', 'fixtures');
     }
     
     protected function usage(&$args){
@@ -58,7 +58,7 @@ class IPF_Cli{
         $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;
@@ -70,27 +70,32 @@ class IPF_Cli{
         print "Done\n";
     }
 
-    public function run(){
-        
+    protected function fixtures(&$args)
+    {
+        print "Load project fixtures to database\n";
+        IPF_Project::getInstance()->loadFixtures();
+    }
+
+    public function run()
+    {
         print "IPF command line tool. Version: ".IPF_Version::$name."\n";
         print "Project config: ".IPF::get('settings_file')."\n";
-        
+
         $opt  = new IPF_Getopt();
         //$z = $opt->getopt2($opt->readPHPArgv(), array('s',)); //, array('s',));
         $args = $opt->readPHPArgv();
-        if (count($args)==1)
-        {
+        if (count($args) == 1) {
             $this->usage($args);
             return;
         }
             
-        if (in_array($args[1],$this->commands))
-        {
+        if (in_array($args[1], $this->commands)) {
             eval('$this->'.$args[1].'($args);');
             return;
         }
-        
+
         print "Unknown command: '".$args[1]."'\n";
         $this->usage($args);
     }
 }
+
index 342e731967f429c920726b437d706f641163664b..a2b36794995de7792bb4817e327a07068972becd 100644 (file)
@@ -101,6 +101,45 @@ final class IPF_Project{
         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->apps as $appname=>&$app){
             if (substr($appname,0,4)=='IPF_')