From: Andrey Kutejko Date: Sat, 27 Jul 2013 14:19:27 +0000 (+0300) Subject: unify adding templates to a record X-Git-Tag: 0.6~62 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=7fa19b13802766119eadc70efd21a9ec2881c96c;p=ipf-legacy-orm.git unify adding templates to a record --- diff --git a/ipf/orm/import/builder.php b/ipf/orm/import/builder.php index 3b34351..2f6d6ae 100644 --- a/ipf/orm/import/builder.php +++ b/ipf/orm/import/builder.php @@ -228,12 +228,12 @@ class IPF_ORM_Import_Builder if (is_array($options) && !empty($options)) { $optionsPhp = self::varExport($options); - $build .= " \$this->loadTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL; + $build .= " \$this->getTable()->addTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL; } else { if (isset($templates[0])) { - $build .= " \$this->loadTemplate('" . $options . "');" . PHP_EOL; + $build .= " \$this->getTable()->addTemplate('" . $options . "');" . PHP_EOL; } else { - $build .= " \$this->loadTemplate('" . $name . "');" . PHP_EOL; + $build .= " \$this->getTable()->addTemplate('" . $name . "');" . PHP_EOL; } } } @@ -258,7 +258,7 @@ class IPF_ORM_Import_Builder private function emitActAs($level, $name) { - return " \$this->actAs(\$" . strtolower($name) . "$level);" . PHP_EOL; + return " \$this->getTable()->addTemplate(\$" . strtolower($name) . "$level);" . PHP_EOL; } private function buildActAs($actAs) diff --git a/ipf/orm/record/abstract.php b/ipf/orm/record/abstract.php index 2bef507..f34c98c 100644 --- a/ipf/orm/record/abstract.php +++ b/ipf/orm/record/abstract.php @@ -114,11 +114,6 @@ abstract class IPF_ORM_Record_Abstract extends IPF_ORM_Access } } - public function loadTemplate($template, array $options = array()) - { - $this->actAs($template, $options); - } - public function bindQueryParts(array $queryParts) { $this->_table->bindQueryParts($queryParts); @@ -131,35 +126,6 @@ abstract class IPF_ORM_Record_Abstract extends IPF_ORM_Access $this->_table->addGenerator($generator, get_class($generator)); } - public function actAs($tpl, array $options = array()) - { - if ( ! is_object($tpl)) { - $className = 'IPF_ORM_Template_' . $tpl; - - if (class_exists($className, true)) { - $tpl = new $className($options); - } else if (class_exists($tpl, true)) { - $tpl = new $tpl($options); - } else { - throw new IPF_ORM_Record_Exception('Could not load behavior named: "' . $tpl . '"'); - } - } - - if ( ! ($tpl instanceof IPF_ORM_Template)) { - throw new IPF_ORM_Record_Exception('Loaded behavior class is not an istance of IPF_ORM_Template.'); - } - - $className = get_class($tpl); - - $this->_table->addTemplate($className, $tpl); - - $tpl->setTable($this->_table); - $tpl->setUp(); - $tpl->setTableDefinition(); - - return $this; - } - public function check($constraint, $name = null) { if (is_array($constraint)) { diff --git a/ipf/orm/table.php b/ipf/orm/table.php index 73ce2a4..5328b9f 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -997,11 +997,30 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable return isset($this->_templates[$template]); } - public function addTemplate($template, IPF_ORM_Template $impl) + public function addTemplate($tpl, array $options=array()) { - $this->_templates[$template] = $impl; + if (!is_object($tpl)) { + $className = 'IPF_ORM_Template_' . $tpl; - return $this; + if (class_exists($className, true)) { + $tpl = new $className($options); + } else if (class_exists($tpl, true)) { + $tpl = new $tpl($options); + } else { + throw new IPF_ORM_Record_Exception('Could not load behavior named: "' . $tpl . '"'); + } + } + + if (!($tpl instanceof IPF_ORM_Template)) { + throw new IPF_ORM_Record_Exception('Loaded behavior class is not an istance of IPF_ORM_Template.'); + } + + $className = get_class($tpl); + $this->_templates[$className] = $tpl; + + $tpl->setTable($this); + $tpl->setUp(); + $tpl->setTableDefinition(); } public function getGenerators()