Commit 5490247c authored by zYne's avatar zYne

new templating model

parent 90bf6241
...@@ -180,13 +180,13 @@ abstract class Doctrine_Configurable extends Doctrine_Object ...@@ -180,13 +180,13 @@ abstract class Doctrine_Configurable extends Doctrine_Object
*/ */
public function getImpl($template) public function getImpl($template)
{ {
if ( ! isset($this->_impl[$attribute])) { if ( ! isset($this->_impl[$template])) {
if (isset($this->parent)) { if (isset($this->parent)) {
return $this->parent->getImpl($attribute); return $this->parent->getImpl($template);
} }
return null; return null;
} }
return $this->_impl[$attribute]; return $this->_impl[$template];
} }
/** /**
* getCacheDriver * getCacheDriver
......
...@@ -72,10 +72,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -72,10 +72,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
* and saves will not cause infinite loops * and saves will not cause infinite loops
*/ */
const STATE_LOCKED = 6; const STATE_LOCKED = 6;
/**
* @var object Doctrine_Table $_table the factory that created this data access object
*/
protected $_table;
/** /**
* @var Doctrine_Node_<TreeImpl> node object * @var Doctrine_Node_<TreeImpl> node object
*/ */
......
...@@ -32,6 +32,10 @@ Doctrine::autoload('Doctrine_Access'); ...@@ -32,6 +32,10 @@ Doctrine::autoload('Doctrine_Access');
*/ */
abstract class Doctrine_Record_Abstract extends Doctrine_Access abstract class Doctrine_Record_Abstract extends Doctrine_Access
{ {
/**
* @param Doctrine_Table $_table reference to associated Doctrine_Table instance
*/
protected $_table;
/** /**
* addListener * addListener
* *
...@@ -244,7 +248,9 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access ...@@ -244,7 +248,9 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access
{ {
$tpl = new $template($options); $tpl = new $template($options);
$tpl->setTable($this->_table); $tpl->setTable($this->_table);
$tpl->setUp(); $tpl->setUp();
$tpl->setTableDefinition(); $tpl->setTableDefinition();
return $this; return $this;
} }
......
...@@ -110,59 +110,6 @@ class Doctrine_Relation_Parser ...@@ -110,59 +110,6 @@ class Doctrine_Relation_Parser
throw new Doctrine_Relation_Exception('Relation type not set.'); 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, "<?php \n" . $code);
fclose($fp);
// include the generated class
require_once($fileName);
}
}
$this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias)); $this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias));
$m = Doctrine_Manager::getInstance(); $m = Doctrine_Manager::getInstance();
...@@ -261,6 +208,30 @@ class Doctrine_Relation_Parser ...@@ -261,6 +208,30 @@ class Doctrine_Relation_Parser
return $this->_relations; 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 * Completes the given association definition
* *
...@@ -270,8 +241,9 @@ class Doctrine_Relation_Parser ...@@ -270,8 +241,9 @@ class Doctrine_Relation_Parser
public function completeAssocDefinition($def) public function completeAssocDefinition($def)
{ {
$conn = $this->_table->getConnection(); $conn = $this->_table->getConnection();
$def['table'] = $conn->getTable($def['class']); $def['table'] = $this->getImpl($def['class']);
$def['refTable'] = $conn->getTable($def['refClass']); $def['class'] = $def['table']->getComponentName();
$def['refTable'] = $this->getImpl($def['refClass']);
$id = $def['refTable']->getIdentifier(); $id = $def['refTable']->getIdentifier();
...@@ -378,7 +350,9 @@ class Doctrine_Relation_Parser ...@@ -378,7 +350,9 @@ class Doctrine_Relation_Parser
public function completeDefinition($def) public function completeDefinition($def)
{ {
$conn = $this->_table->getConnection(); $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'])); $foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));
$localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName())); $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
......
...@@ -32,10 +32,7 @@ Doctrine::autoload('Doctrine_Record_Abstract'); ...@@ -32,10 +32,7 @@ Doctrine::autoload('Doctrine_Record_Abstract');
*/ */
class Doctrine_Template extends Doctrine_Record_Abstract class Doctrine_Template extends Doctrine_Record_Abstract
{ {
/**
* @param Doctrine_Table $_table reference to associated Doctrine_Table instance
*/
protected $_table;
/** /**
* setTable * setTable
* *
...@@ -55,10 +52,10 @@ class Doctrine_Template extends Doctrine_Record_Abstract ...@@ -55,10 +52,10 @@ class Doctrine_Template extends Doctrine_Record_Abstract
{ {
return $this->_table; return $this->_table;
} }
public function setUp() public function setUp()
{ {
} }
public function setTableDefinition() public function setTableDefinition()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment