]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
rework record template generation
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Jul 2013 15:24:14 +0000 (18:24 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Jul 2013 15:24:14 +0000 (18:24 +0300)
ipf/orm/import/builder.php
ipf/orm/template.php

index 2f6d6aefdc5b68d522a506f2251e243308299857..b0633a9329504eeaba3679218e79b834ddbf7775 100644 (file)
@@ -241,89 +241,32 @@ class IPF_ORM_Import_Builder
         return $build;
     }
 
-    private function emitAssign($level, $name, $option)
-    {
-        // find class matching $name
-        $classname = $name;
-        if (class_exists("IPF_ORM_Template_$name", true)) {
-            $classname = "IPF_ORM_Template_$name";
-        }
-        return "    \$" . strtolower($name) . "$level = new $classname($option);". PHP_EOL;
-    }
-
-    private function emitAddChild($level, $parent, $name)
-    {
-        return "    \$" . strtolower($parent) . ($level - 1) . "->addChild(\$" . strtolower($name) . "$level);" . PHP_EOL;
-    }
-
-    private function emitActAs($level, $name)
-    {
-        return "    \$this->getTable()->addTemplate(\$" . strtolower($name) . "$level);" . PHP_EOL;
-    }
-
     private function buildActAs($actAs)
-    {
-        $emittedActAs = array();
-        $build = $this->innerBuildActAs($actAs, 0, null, $emittedActAs);
-        foreach($emittedActAs as $str) {
-            $build .= $str;
-        }
-        return $build;
-    }
-
-    private function innerBuildActAs($actAs, $level, $parent, array &$emittedActAs)
     {
         // rewrite special case of actAs: [Behavior] which gave [0] => Behavior
-        if(is_array($actAs) && isset($actAs[0]) && !is_array($actAs[0])) {
+        if (is_array($actAs) && isset($actAs[0]) && !is_array($actAs[0])) {
             $actAs = array_flip($actAs);
         }
 
+        // rewrite special case of actAs: Behavior
+        if (!is_array($actAs))
+            $actAs = array($actAs => '');
+
         $build = '';
-        $currentParent = $parent;
-        if(is_array($actAs)) {
-            foreach($actAs as $template => $options) {
-                if ($template == 'actAs') {
-                    // found another actAs
-                    $build .= $this->innerBuildActAs($options, $level + 1, $parent, $emittedActAs);
-                } else if (is_array($options)) {
-                    // remove actAs from options
-                    $realOptions = array();
-                    $leftActAs = array();
-                    foreach($options as $name => $value) {
-                        if ($name != 'actAs') {
-                            $realOptions[$name] = $options[$name];
-                        } else {
-                            $leftActAs[$name] = $options[$name];
-                        }
-                    } 
-
-                    $optionPHP = self::varExport($realOptions);
-                    $build .= $this->emitAssign($level, $template, $optionPHP); 
-                    if ($level == 0) {
-                        $emittedActAs[] = $this->emitActAs($level, $template);
-                    } else {
-                        $build .= $this->emitAddChild($level, $currentParent, $template);
-                    }
-                    // descend for the remainings actAs
-                    $parent = $template;            
-                    $build .= $this->innerBuildActAs($leftActAs, $level, $template, $emittedActAs);
-                } else {
-                    $build .= $this->emitAssign($level, $template, null);
-                    if ($level == 0) {
-                        $emittedActAs[] = $this->emitActAs($level, $template);
-                    } else {
-                        $build .= $this->emitAddChild($level, $currentParent, $template);
-                    }
-                    $parent = $template;            
-                }
-            }
-        } else {
-            $build .= $this->emitAssign($level, $actAs, null);
-            if ($level == 0) {
-                $emittedActAs[] = $this->emitActAs($level, $actAs);
+        foreach($actAs as $template => $options) {
+            // find class matching $name
+            if (class_exists("IPF_ORM_Template_$template", true)) {
+                $classname = "IPF_ORM_Template_$template";
             } else {
-                $build .= $this->emitAddChild($level, $currentParent, $actAs);
+                $classname = $template;
             }
+
+            if (is_array($options))
+                $options = self::varExport($options);
+            else
+                $options = '';
+
+            $build .= "    \$this->getTable()->addTemplate(new $classname($options));" . PHP_EOL;
         }
 
         return $build;
index 9bca51fc25045e3bf5d18b94567b58c19c322264..d963da11453a156b70352e53e74eff8f930cdf18 100644 (file)
@@ -3,7 +3,6 @@
 abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract
 {
     protected $_invoker;
-    protected $_plugin;
 
     public function setTable(IPF_ORM_Table $table)
     {
@@ -25,17 +24,6 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract
         return $this->_invoker;
     }
 
-    public function addChild(IPF_ORM_Template $template)
-    {
-        $this->_plugin->addChild($template);
-        return $this;
-    }
-
-    public function getPlugin()
-    {
-        return $this->_plugin;
-    }
-
     public function get($name) 
     {
         throw new IPF_ORM_Exception("Templates doesn't support accessors.");