]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
refactor relation definition
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Jul 2013 19:41:06 +0000 (22:41 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Jul 2013 19:41:06 +0000 (22:41 +0300)
ipf/orm/import/builder.php
ipf/orm/record/abstract.php
ipf/orm/relation/parser.php
ipf/orm/table.php

index f6b234fb5fa3f5bdd04766547e43c1103f32c92b..9cf200a7854d2d2c424f2552a842b6a13b5ca0bd 100644 (file)
@@ -57,22 +57,20 @@ class IPF_ORM_Import_Builder
     private function buildSetUp(array $definition)
     {
         $ret = array();
-        $i = 0;
 
-        if (isset($definition['relations']) && is_array($definition['relations']) && ! empty($definition['relations'])) {
+        if (isset($definition['relations']) && is_array($definition['relations']) && !empty($definition['relations'])) {
             foreach ($definition['relations'] as $name => $relation) {
-                $class = isset($relation['class']) ? $relation['class']:$name;
-                $alias = (isset($relation['alias']) && $relation['alias'] !== $relation['class']) ? ' as ' . $relation['alias'] : '';
+                $class = isset($relation['class']) ? $relation['class'] : $name;
+                $alias = (isset($relation['alias']) && $relation['alias'] !== $relation['class']) ? $relation['alias'] : '';
 
-                if ( ! isset($relation['type'])) {
+                if (!isset($relation['type']))
                     $relation['type'] = IPF_ORM_Relation::ONE;
-                }
 
                 if ($relation['type'] === IPF_ORM_Relation::ONE ||
                     $relation['type'] === IPF_ORM_Relation::ONE_COMPOSITE) {
-                    $ret[$i] = "    ".'$this->hasOne(\'' . $class . $alias . '\'';
+                    $r = "    \$table->hasOne('$class', '$alias'";
                 } else {
-                    $ret[$i] = "    ".'$this->hasMany(\'' . $class . $alias . '\'';
+                    $r = "    \$table->hasMany('$class', '$alias'";
                 }
 
                 $a = array();
@@ -113,51 +111,44 @@ class IPF_ORM_Import_Builder
                     $a[] = '\'exclude\' => ' . self::varExport($relation['exclude']);
                 }
 
-                if ( ! empty($a)) {
-                    $ret[$i] .= ', ' . 'array(';
-                    $length = strlen($ret[$i]);
-                    $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
-                }
+                if (!empty($a))
+                    $r .= ', array(' . implode(', ', $a) . ')';
 
-                $ret[$i] .= ');'.PHP_EOL;
-                $i++;
+                $ret[] = $r.');';
             }
         }
 
         if (isset($definition['templates']) && is_array($definition['templates']) && !empty($definition['templates'])) {
-            $ret[$i] = $this->buildTemplates($definition['templates']);
-            $i++;
+            $this->buildTemplates($definition['templates'], $ret);
         }
 
         if (isset($definition['actAs']) && is_array($definition['actAs']) && !empty($definition['actAs'])) {
-            $ret[$i] = $this->buildActAs($definition['actAs']);
-            $i++;
+            $this->buildActAs($definition['actAs'], $ret);
         }
 
         if (isset($definition['listeners']) && is_array($definition['listeners']) && !empty($definition['listeners'])) {
-            foreach($definition['listeners'] as $listener) {
-                $ret[$i] = PHP_EOL.'    $this->getTable()->listeners[\''.$listener.'\'] = new '.$listener.'();';
-                $i++;
+            foreach ($definition['listeners'] as $listener) {
+                $ret[] = "    \$table->listeners['$listener'] = new $listener;";
             }
         }
 
-        $code = implode(PHP_EOL, $ret);
-        $code = trim($code);
-
-        // If the body of the function has contents then we need to 
-        if ($code) {
-            // If the body of the function has contents and we are using inheritance
-            // then we need call the parent::setUp() before the body of the function
-            // Class table inheritance is the only one we shouldn't call parent::setUp() for
-            if ($code && isset($definition['inheritance']['type']) && $definition['inheritance']['type'] != 'class_table') {
-                $code = "parent::setUp();" . PHP_EOL . '    ' . $code;
-            }
+        // If the body of the function has contents and we are using inheritance
+        // then we need call the parent::setUp() before the body of the function
+        // Class table inheritance is the only one we shouldn't call parent::setUp() for
+        if (count($ret) && isset($definition['inheritance']['type']) && $definition['inheritance']['type'] != 'class_table') {
+            array_unshift($ret, '    parent::setUp();');
         }
 
         // If we have some code for the function then lets define it and return it
-        if ($code) {
-            return '  public function setUp()' . PHP_EOL . '  {' . PHP_EOL . '    ' . $code . PHP_EOL . '  }';
+        if (count($ret)) {
+            array_unshift($ret,
+                '  public function setUp()',
+                '  {',
+                '    $table = $this->getTable();'
+            );
+            $ret[] = '  }';
         }
+        return $ret;
     }
 
     private function buildColumns(array $columns)
@@ -222,28 +213,20 @@ class IPF_ORM_Import_Builder
         return implode(PHP_EOL, $result);
     }
 
-    private function buildTemplates(array $templates)
+    private function buildTemplates(array $templates, array &$build)
     {
-        $build = '';
         foreach ($templates as $name => $options) {
-
             if (is_array($options) && !empty($options)) {
-                $optionsPhp = self::varExport($options);
-
-                $build .= "    \$this->getTable()->addTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL;
+                $build[] = "    \$table->addTemplate('$name', " . self::varExport($options) . ");";
+            } elseif (isset($templates[0])) {
+                $build[] = "    \$table->addTemplate('$options');";
             } else {
-                if (isset($templates[0])) {
-                    $build .= "    \$this->getTable()->addTemplate('" . $options . "');" . PHP_EOL;
-                } else {
-                    $build .= "    \$this->getTable()->addTemplate('" . $name . "');" . PHP_EOL;
-                }
+                $build[] = "    \$table->addTemplate('$name');";
             }
         }
-
-        return $build;
     }
 
-    private function buildActAs($actAs)
+    private function buildActAs($actAs, array &$build)
     {
         // rewrite special case of actAs: [Behavior] which gave [0] => Behavior
         if (is_array($actAs) && isset($actAs[0]) && !is_array($actAs[0])) {
@@ -254,24 +237,20 @@ class IPF_ORM_Import_Builder
         if (!is_array($actAs))
             $actAs = array($actAs => '');
 
-        $build = '';
-        foreach($actAs as $template => $options) {
+        foreach ($actAs as $template => $options) {
             // find class matching $name
-            if (class_exists("IPF_ORM_Template_$template", true)) {
-                $classname = "IPF_ORM_Template_$template";
-            } else {
+            if (class_exists('IPF_ORM_Template_'.$template, true))
+                $classname = 'IPF_ORM_Template_'.$template;
+            else
                 $classname = $template;
-            }
 
             if (is_array($options))
                 $options = self::varExport($options);
             else
                 $options = '';
 
-            $build .= "    \$this->getTable()->addTemplate(new $classname($options));" . PHP_EOL;
+            $build[] = "    \$table->addTemplate(new $classname($options));";
         }
-
-        return $build;
     }
 
     private function buildAttributes(array $attributes)
@@ -341,7 +320,7 @@ class IPF_ORM_Import_Builder
         $code[] = '{';
         $code[] = $this->buildTableDefinition($definition);
         $code[] = '';
-        $code[] = $this->buildSetUp($definition);
+        $code   = array_merge($code, $this->buildSetUp($definition));
         $code[] = '';
         $code   = array_merge($code, $this->buildShortcuts($definition));
         $code[] = '}';
index 15183d60d161b6b3ca477ba2d6c1cac6c25eb7e6..dc9f4a7c1006ddc171006dd6944701e25db7443b 100644 (file)
@@ -37,31 +37,6 @@ abstract class IPF_ORM_Record_Abstract extends IPF_ORM_Access
         }    
     }
 
-    public function ownsOne()
-    {
-        $this->_table->bind(func_get_args(), IPF_ORM_Relation::ONE_COMPOSITE);
-        
-        return $this;
-    }
-
-    public function ownsMany()
-    {
-        $this->_table->bind(func_get_args(), IPF_ORM_Relation::MANY_COMPOSITE);
-        return $this;
-    }
-
-    public function hasOne()
-    {
-        $this->_table->bind(func_get_args(), IPF_ORM_Relation::ONE_AGGREGATE);
-        return $this;
-    }
-
-    public function hasMany()
-    {
-        $this->_table->bind(func_get_args(), IPF_ORM_Relation::MANY_AGGREGATE);
-        return $this;
-    }
-
     public function bindQueryParts(array $queryParts)
     {
         $this->_table->bindQueryParts($queryParts);
index a5e36d6deb1e481345093baf4101273922acf7ef..1ba5da69eabe84d559ea30c8def1456069e0892e 100644 (file)
@@ -37,31 +37,21 @@ class IPF_ORM_Relation_Parser
 
     public function hasRelation($name)
     {
-        if ( ! isset($this->_pending[$name]) && ! isset($this->_relations[$name])) {
-            return false;
-        }
-
-        return true;
+        return isset($this->_pending[$name]) || isset($this->_relations[$name]);
     }
 
-    public function bind($name, $options = array())
+    public function bind($name, $alias, $type, $options=array())
     {
-        $e    = explode(' as ', $name);
-        $name = $e[0];
-        $alias = isset($e[1]) ? $e[1] : $name;
+        if (!$alias)
+            $alias = $name;
 
-        if ( ! isset($options['type'])) {
-            throw new IPF_ORM_Exception('Relation type not set.');
-        }
-
-        if ($this->hasRelation($alias)) {
-            unset($this->relations[$alias]);
-            unset($this->_pending[$alias]);
-        }
+        unset($this->relations[$alias]);
 
-        $this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias));
+        $options['class'] = $name;
+        $options['alias'] = $alias;
+        $options['type']  = $type;
 
-        return $this->_pending[$alias];
+        $this->_pending[$alias] = $options;
     }
 
     public function getRelation($alias, $recursive = true)
@@ -86,18 +76,18 @@ class IPF_ORM_Relation_Parser
 
                     $parser = $def['refTable']->getRelationParser();
                     if ( ! $parser->hasRelation($this->_table->getComponentName())) {
-                        $parser->bind($this->_table->getComponentName(),
-                                      array('type'    => IPF_ORM_Relation::ONE,
-                                            'local'   => $def['local'],
-                                            'foreign' => $idColumnName,
-                                            'localKey' => true,
-                                            ));
+                        $parser->bind($this->_table->getComponentName(), null, IPF_ORM_Relation::ONE, array(
+                            'local'     => $def['local'],
+                            'foreign'   => $idColumnName,
+                            'localKey'  => true,
+                        ));
                     }
 
-                    if ( ! $this->hasRelation($def['refClass'])) {
-                        $this->bind($def['refClass'], array('type' => IPF_ORM_Relation::MANY,
-                                                            'foreign' => $def['local'],
-                                                            'local'   => $idColumnName));
+                    if (!$this->hasRelation($def['refClass'])) {
+                        $this->bind($def['refClass'], null, IPF_ORM_Relation::MANY, array(
+                            'foreign' => $def['local'],
+                            'local'   => $idColumnName,
+                        ));
                     }
                 }
                 if (in_array($def['class'], $localClasses)) {
index f938222f519f852e20a14b87c1b83027506c3fc5..5a5f1931544d36b11f51cf634db0cd5e03486d5a 100644 (file)
@@ -319,47 +319,29 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable
         return false;
     }
 
-    public function bind($args, $type)
+    public function bind($class, $alias, $type, array $options)
     {
-        $options = array();
-        $options['type'] = $type;
-
-        if ( ! isset($args[1])) {
-            $args[1] = array();
-        }
-
-        // the following is needed for backwards compatibility
-        if (is_string($args[1])) {
-            if ( ! isset($args[2])) {
-                $args[2] = array();
-            } elseif (is_string($args[2])) {
-                $args[2] = (array) $args[2];
-            }
-
-            $classes = array_merge($this->_options['parents'], array($this->getComponentName()));
+        $this->_parser->bind($class, $alias, $type, $options);
+    }
 
-            $e = explode('.', $args[1]);
-            if (in_array($e[0], $classes)) {
-                if ($options['type'] >= IPF_ORM_Relation::MANY) {
-                    $options['foreign'] = $e[1];
-                } else {
-                    $options['local'] = $e[1];
-                }
-            } else {
-                $e2 = explode(' as ', $args[0]);
-                if ($e[0] !== $e2[0] && ( ! isset($e2[1]) || $e[0] !== $e2[1])) {
-                    $options['refClass'] = $e[0];
-                }
+    public function ownsOne($class, $alias, $options=array())
+    {
+        $this->bind($class, $alias, IPF_ORM_Relation::ONE_COMPOSITE, $options);
+    }
 
-                $options['foreign'] = $e[1];
-            }
+    public function ownsMany($class, $alias, $options=array())
+    {
+        $this->bind($class, $alias, IPF_ORM_Relation::MANY_COMPOSITE, $options);
+    }
 
-            $options = array_merge($args[2], $options);
-        } else {
-            $options = array_merge($args[1], $options);
-        }
+    public function hasOne($class, $alias, $options=array())
+    {
+        $this->bind($class, $alias, IPF_ORM_Relation::ONE_AGGREGATE, $options);
+    }
 
-        $this->_parser->bind($args[0], $options);
+    public function hasMany($class, $alias, $options=array())
+    {
+        $this->bind($class, $alias, IPF_ORM_Relation::MANY_AGGREGATE, $options);
     }
 
     public function hasRelation($alias)