From 70debb716f512bb221e70cffe32a5e7f0a6d346f Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 27 Jul 2013 18:24:14 +0300 Subject: [PATCH] rework record template generation --- ipf/admin/models/_generated/BaseAdminLog.php | 3 +- ipf/auth/models/_generated/BaseUser.php | 3 +- ipf/orm/import/builder.php | 91 ++++---------------- ipf/orm/template.php | 12 --- 4 files changed, 19 insertions(+), 90 deletions(-) diff --git a/ipf/admin/models/_generated/BaseAdminLog.php b/ipf/admin/models/_generated/BaseAdminLog.php index e71aaed..1c6aa57 100644 --- a/ipf/admin/models/_generated/BaseAdminLog.php +++ b/ipf/admin/models/_generated/BaseAdminLog.php @@ -30,8 +30,7 @@ abstract class BaseAdminLog extends IPF_ORM_Record public function setUp() { - $timestampable0 = new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true))); - $this->getTable()->addTemplate($timestampable0); + $this->getTable()->addTemplate(new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true)))); } public static function table() diff --git a/ipf/auth/models/_generated/BaseUser.php b/ipf/auth/models/_generated/BaseUser.php index 296e39a..7213eeb 100644 --- a/ipf/auth/models/_generated/BaseUser.php +++ b/ipf/auth/models/_generated/BaseUser.php @@ -42,8 +42,7 @@ abstract class BaseUser extends IPF_ORM_Record $this->hasMany('UserPermission', array('local' => 'id', 'foreign' => 'user_id')); - $timestampable0 = new IPF_ORM_Template_Timestampable(); - $this->getTable()->addTemplate($timestampable0); + $this->getTable()->addTemplate(new IPF_ORM_Template_Timestampable()); } public static function table() diff --git a/ipf/orm/import/builder.php b/ipf/orm/import/builder.php index 2f6d6ae..b0633a9 100644 --- a/ipf/orm/import/builder.php +++ b/ipf/orm/import/builder.php @@ -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; diff --git a/ipf/orm/template.php b/ipf/orm/template.php index 9bca51f..d963da1 100644 --- a/ipf/orm/template.php +++ b/ipf/orm/template.php @@ -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."); -- 2.49.0