From 5490247cc7fda485ec8bb622072a0b7b5e64a706 Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 3 Aug 2007 11:52:24 +0000 Subject: [PATCH] new templating model --- lib/Doctrine/Configurable.php | 6 +-- lib/Doctrine/Record.php | 5 +- lib/Doctrine/Record/Abstract.php | 6 +++ lib/Doctrine/Relation/Parser.php | 86 +++++++++++--------------------- lib/Doctrine/Template.php | 9 ++-- 5 files changed, 43 insertions(+), 69 deletions(-) diff --git a/lib/Doctrine/Configurable.php b/lib/Doctrine/Configurable.php index bdbb7a107..5b0835eb8 100644 --- a/lib/Doctrine/Configurable.php +++ b/lib/Doctrine/Configurable.php @@ -180,13 +180,13 @@ abstract class Doctrine_Configurable extends Doctrine_Object */ public function getImpl($template) { - if ( ! isset($this->_impl[$attribute])) { + if ( ! isset($this->_impl[$template])) { if (isset($this->parent)) { - return $this->parent->getImpl($attribute); + return $this->parent->getImpl($template); } return null; } - return $this->_impl[$attribute]; + return $this->_impl[$template]; } /** * getCacheDriver diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 6de670391..b2148dd69 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -72,10 +72,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count * and saves will not cause infinite loops */ const STATE_LOCKED = 6; - /** - * @var object Doctrine_Table $_table the factory that created this data access object - */ - protected $_table; + /** * @var Doctrine_Node_ node object */ diff --git a/lib/Doctrine/Record/Abstract.php b/lib/Doctrine/Record/Abstract.php index 31f479e9e..90fa70a25 100644 --- a/lib/Doctrine/Record/Abstract.php +++ b/lib/Doctrine/Record/Abstract.php @@ -32,6 +32,10 @@ Doctrine::autoload('Doctrine_Access'); */ abstract class Doctrine_Record_Abstract extends Doctrine_Access { + /** + * @param Doctrine_Table $_table reference to associated Doctrine_Table instance + */ + protected $_table; /** * addListener * @@ -244,7 +248,9 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access { $tpl = new $template($options); $tpl->setTable($this->_table); + $tpl->setUp(); + $tpl->setTableDefinition(); return $this; } diff --git a/lib/Doctrine/Relation/Parser.php b/lib/Doctrine/Relation/Parser.php index 2c17c6107..7a217fe49 100644 --- a/lib/Doctrine/Relation/Parser.php +++ b/lib/Doctrine/Relation/Parser.php @@ -110,59 +110,6 @@ class Doctrine_Relation_Parser throw new Doctrine_Relation_Exception('Relation type not set.'); } - if (strpos($name, '[Component]') !== false) { - $name = str_replace('[Component]', $this->_table->getComponentName(), $name); - $templateName = substr($name, strlen($this->_table->getComponentName())); - - if (substr($name, -8) === 'Template') { - $name = substr($name, 0, -8); - } - - $parent = new ReflectionClass($this->_table->getComponentName()); - - $fileName = dirname($parent->getFileName()) . DIRECTORY_SEPARATOR . $name . '.php'; - - if (file_exists($fileName)) { - require_once($fileName); - } - if ( ! class_exists($name)) { - $template = new $templateName(); - - $conn = $this->_table->getConnection(); - - $refl = new ReflectionClass($templateName); - $file = file($refl->getFileName()); - - $lines[] = 'class ' . $name . ' extends Doctrine_Record' . "\n"; - $lines[] = '{'. "\n"; - - // read all template method definitions - foreach ($refl->getMethods() as $method) { - if ($method->getDeclaringClass()->getName() === $refl->getName()) { - $start = $method->getStartLine() - 1; - $end = $method->getEndLine() - 1; - // append method definitions - $lines = array_merge($lines, array_slice($file, $start, ($end - $start) + 1)); - } - } - - $lines[] = '}' . "\n"; - - if (file_exists($fileName)) { - throw new Doctrine_Template_Exception("Couldn't generate class for template."); - } - $code = str_replace('[Component]', $this->_table->getComponentName(), implode("", $lines)); - - // create the actual class file - $fp = fopen($fileName, 'w+'); - fwrite($fp, "_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias)); $m = Doctrine_Manager::getInstance(); @@ -261,6 +208,30 @@ class Doctrine_Relation_Parser return $this->_relations; } + /** + * getImpl + * returns the table class of the concrete implementation for given template + * if the given template is not a template then this method just returns the + * table class for the given record + * + * @param string $template + */ + public function getImpl($template) + { + $conn = $this->_table->getConnection(); + + if (in_array('Doctrine_Template', class_parents($template))) { + $impl = $this->_table->getImpl($template); + + if ($impl === null) { + throw new Doctrine_Relation_Parser_Exception("Couldn't find concrete implementation for template " . $template); + } + } else { + $impl = $template; + } + + return $conn->getTable($impl); + } /** * Completes the given association definition * @@ -270,8 +241,9 @@ class Doctrine_Relation_Parser public function completeAssocDefinition($def) { $conn = $this->_table->getConnection(); - $def['table'] = $conn->getTable($def['class']); - $def['refTable'] = $conn->getTable($def['refClass']); + $def['table'] = $this->getImpl($def['class']); + $def['class'] = $def['table']->getComponentName(); + $def['refTable'] = $this->getImpl($def['refClass']); $id = $def['refTable']->getIdentifier(); @@ -378,7 +350,9 @@ class Doctrine_Relation_Parser public function completeDefinition($def) { $conn = $this->_table->getConnection(); - $def['table'] = $conn->getTable($def['class']); + $def['table'] = $this->getImpl($def['class']); + $def['class'] = $def['table']->getComponentName(); + $foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class'])); $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName())); diff --git a/lib/Doctrine/Template.php b/lib/Doctrine/Template.php index 1dee3ae1f..17878cffc 100644 --- a/lib/Doctrine/Template.php +++ b/lib/Doctrine/Template.php @@ -32,10 +32,7 @@ Doctrine::autoload('Doctrine_Record_Abstract'); */ class Doctrine_Template extends Doctrine_Record_Abstract { - /** - * @param Doctrine_Table $_table reference to associated Doctrine_Table instance - */ - protected $_table; + /** * setTable * @@ -55,10 +52,10 @@ class Doctrine_Template extends Doctrine_Record_Abstract { return $this->_table; } - + public function setUp() { - + } public function setTableDefinition()