Commit a5f755f4 authored by zYne's avatar zYne

enhanced plugin initialization procedure

parent 206002de
...@@ -86,7 +86,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin ...@@ -86,7 +86,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin
* @param Doctrine_Table $table * @param Doctrine_Table $table
* @return boolean true on success otherwise false. * @return boolean true on success otherwise false.
*/ */
public function buildDefinition() public function setTableDefinition()
{ {
$name = $this->_options['table']->getComponentName(); $name = $this->_options['table']->getComponentName();
...@@ -99,19 +99,9 @@ class Doctrine_AuditLog extends Doctrine_Plugin ...@@ -99,19 +99,9 @@ class Doctrine_AuditLog extends Doctrine_Plugin
unset($columns[$column]['unique']); unset($columns[$column]['unique']);
} }
// the version column should be part of the primary key definition $this->hasColumns($columns);
$columns[$this->_options['versionColumn']] = array('type' => 'integer',
'length' => 8,
'primary' => true);
$id = $this->_options['table']->getIdentifier();
$relations = $this->buildRelation();
$this->generateClass($columns, $relations); // the version column should be part of the primary key definition
$this->hasColumn($this->_options['versionColumn'], 'integer', 8, array('primary' => true));
$this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']);
return true;
} }
} }
...@@ -35,25 +35,28 @@ class Doctrine_AuditLog_Listener extends Doctrine_Record_Listener ...@@ -35,25 +35,28 @@ class Doctrine_AuditLog_Listener extends Doctrine_Record_Listener
protected $_auditLog; protected $_auditLog;
public function __construct(Doctrine_AuditLog $auditLog) { public function __construct(Doctrine_AuditLog $auditLog)
{
$this->_auditLog = $auditLog; $this->_auditLog = $auditLog;
} }
public function preInsert(Doctrine_Event $event) public function preInsert(Doctrine_Event $event)
{ {
$versionColumn = $this->_auditLog->getOption('versionColumn'); $versionColumn = $this->_auditLog->getOption('versionColumn');
$event->getInvoker()->set($versionColumn, 1); $event->getInvoker()->set($versionColumn, 1);
} }
public function postInsert(Doctrine_Event $event) public function postInsert(Doctrine_Event $event)
{ {
$class = $this->_auditLog->getOption('className'); $class = $this->_auditLog->getOption('className');
$record = $event->getInvoker(); $record = $event->getInvoker();
$version = new $class(); $version = new $class();
$version->merge($record->toArray()); $version->merge($record->toArray());
$version->save(); $version->save();
} }
public function preDelete(Doctrine_Event $event) public function preDelete(Doctrine_Event $event)
{ {
$class = $this->_auditLog->getOption('className'); $class = $this->_auditLog->getOption('className');
...@@ -69,6 +72,7 @@ class Doctrine_AuditLog_Listener extends Doctrine_Record_Listener ...@@ -69,6 +72,7 @@ class Doctrine_AuditLog_Listener extends Doctrine_Record_Listener
$version->merge($record->toArray()); $version->merge($record->toArray());
$version->save(); $version->save();
} }
public function preUpdate(Doctrine_Event $event) public function preUpdate(Doctrine_Event $event)
{ {
$class = $this->_auditLog->getOption('className'); $class = $this->_auditLog->getOption('className');
......
...@@ -1174,16 +1174,14 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -1174,16 +1174,14 @@ class Doctrine_Export extends Doctrine_Connection_Module
{ {
$plugins = array(); $plugins = array();
foreach ($table->getTemplates() as $name => $template) { foreach ($table->getPlugins() as $name => $plugin) {
$plugin = $template->getPlugin();
if ($plugin === null) { if ($plugin === null) {
continue; continue;
} }
$plugins[] = $plugin; $plugins[] = $plugin;
$pluginTable = $plugin->getOption('pluginTable'); $pluginTable = $plugin->getTable();
if ($pluginTable instanceof Doctrine_Table) { if ($pluginTable instanceof Doctrine_Table) {
$plugins = array_merge($plugins, $this->getAllPlugins($pluginTable)); $plugins = array_merge($plugins, $this->getAllPlugins($pluginTable));
...@@ -1205,7 +1203,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -1205,7 +1203,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
$sql = array(); $sql = array();
foreach ($this->getAllPlugins($table) as $name => $plugin) { foreach ($this->getAllPlugins($table) as $name => $plugin) {
$table = $plugin->getOption('pluginTable'); $table = $plugin->getTable();
// Make sure plugin has a valid table // Make sure plugin has a valid table
if ($table instanceof Doctrine_Table) { if ($table instanceof Doctrine_Table) {
......
...@@ -52,26 +52,26 @@ class Doctrine_I18n extends Doctrine_Plugin ...@@ -52,26 +52,26 @@ class Doctrine_I18n extends Doctrine_Plugin
$this->_options = array_merge($this->_options, $options); $this->_options = array_merge($this->_options, $options);
} }
public function buildRelation()
{
$this->buildForeignRelation('Translation');
$this->buildLocalRelation();
}
/** /**
* buildDefinition * buildDefinition
* *
* @param object $Doctrine_Table * @param object $Doctrine_Table
* @return void * @return void
*/ */
public function buildDefinition() public function setTableDefinition()
{ {
if (empty($this->_options['fields'])) { if (empty($this->_options['fields'])) {
throw new Doctrine_I18n_Exception('Fields not set.'); throw new Doctrine_I18n_Exception('Fields not set.');
} }
$name = $this->_options['table']->getComponentName();
$columns = array();
$options = array('className' => $this->_options['className']); $options = array('className' => $this->_options['className']);
$fk = $this->buildForeignKeys($this->_options['table']);
$cols = $this->_options['table']->getColumns(); $cols = $this->_options['table']->getColumns();
foreach ($cols as $column => $definition) { foreach ($cols as $column => $definition) {
...@@ -81,23 +81,11 @@ class Doctrine_I18n extends Doctrine_Plugin ...@@ -81,23 +81,11 @@ class Doctrine_I18n extends Doctrine_Plugin
} }
} }
$columns['lang'] = array('type' => 'string', $this->hasColumns($columns);
'length' => 2,
'fixed' => true,
'primary' => true);
$relations = $this->buildRelation();
$columns += $fk;
$options = array('queryParts' => array('indexBy' => 'lang'));
$this->generateClass($columns, $relations, $options);
$this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']);
$this->_options['pluginTable']->bindQueryPart('indexBy', 'lang');
return true; $this->hasColumn('lang', 'string', 2, array('fixed' => true,
'primary' => true));
$this->bindQueryParts(array('indexBy' => 'lang'));
} }
} }
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
* @link www.phpdoctrine.com * @link www.phpdoctrine.com
* @since 1.0 * @since 1.0
*/ */
abstract class Doctrine_Plugin abstract class Doctrine_Plugin extends Doctrine_Record_Abstract
{ {
/** /**
* @var array $_options an array of plugin specific options * @var array $_options an array of plugin specific options
...@@ -41,7 +41,8 @@ abstract class Doctrine_Plugin ...@@ -41,7 +41,8 @@ abstract class Doctrine_Plugin
'table' => false, 'table' => false,
'pluginTable' => false, 'pluginTable' => false,
'children' => array()); 'children' => array());
protected $_initialized = false;
/** /**
* __get * __get
* an alias for getOption * an alias for getOption
...@@ -110,11 +111,19 @@ abstract class Doctrine_Plugin ...@@ -110,11 +111,19 @@ abstract class Doctrine_Plugin
return $this->_options; return $this->_options;
} }
public function buildPluginDefinition(Doctrine_Table $table) public function initialize(Doctrine_Table $table)
{ {
$this->initOptions(); if ($this->_initialized) {
return false;
}
$this->_initialized = true;
$this->initOptions();
$this->_options['table'] = $table; $table->addPlugin($this, get_class($this));
$this->_options['table'] = $table;
$this->_options['className'] = str_replace('%CLASS%', $this->_options['className'] = str_replace('%CLASS%',
$this->_options['table']->getComponentName(), $this->_options['table']->getComponentName(),
...@@ -125,35 +134,47 @@ abstract class Doctrine_Plugin ...@@ -125,35 +134,47 @@ abstract class Doctrine_Plugin
return false; return false;
} }
$this->buildDefinition(); $conn = $this->_options['table']->getConnection();
$this->_table = new Doctrine_Table($this->_options['className'], $conn);
$conn->addTable($this->_table);
$fk = $this->buildForeignKeys($this->_options['table']);
$this->_table->setColumns($fk);
$this->buildRelation();
$this->setTableDefinition();
$this->setUp();
$this->generateClass($this->_table->getColumns());
$this->buildChildDefinitions(); $this->buildChildDefinitions();
} }
/** /**
* empty template method for providing the concrete plugins the ability * empty template method for providing the concrete plugins the ability
* to initialize options before the actual definition is being built * to initialize options before the actual definition is being built
* *
* @return void * @return void
*/ */
public function initOptions() public function initOptions()
{ {
} }
abstract public function buildDefinition();
public function buildChildDefinitions() public function buildChildDefinitions()
{ {
if ( ! isset($this->_options['children'])) { if ( ! isset($this->_options['children'])) {
Doctrine::dump(debug_backtrace());
throw new Doctrine_Plugin_Exception("Unknown option 'children'."); throw new Doctrine_Plugin_Exception("Unknown option 'children'.");
} }
foreach ($this->_options['children'] as $child) { foreach ($this->_options['children'] as $child) {
$this->_options['pluginTable']->addTemplate(get_class($child), $child); $this->_table->addTemplate(get_class($child), $child);
$child->setTable($this->_table);
$child->setTable($this->_options['pluginTable']);
$child->setUp(); $child->setUp();
} }
} }
...@@ -186,6 +207,35 @@ abstract class Doctrine_Plugin ...@@ -186,6 +207,35 @@ abstract class Doctrine_Plugin
return $fk; return $fk;
} }
public function buildLocalRelation()
{
$options = array('local' => $this->_options['table']->getIdentifier(),
'foreign' => $this->_options['table']->getIdentifier(),
'type' => Doctrine_Relation::MANY);
$options['type'] = Doctrine_Relation::ONE;
$options['onDelete'] = 'CASCADE';
$options['onUpdate'] = 'CASCADE';
$this->_table->getRelationParser()->bind($this->_options['table']->getComponentName(), $options);
}
public function buildForeignRelation($alias = null)
{
$options = array('local' => $this->_options['table']->getIdentifier(),
'foreign' => $this->_options['table']->getIdentifier(),
'type' => Doctrine_Relation::MANY);
$aliasStr = '';
if ($alias !== null) {
$aliasStr = ' as ' . $alias;
}
$this->_options['table']->getRelationParser()->bind($this->_table->getComponentName() . $aliasStr,
$options);
}
/** /**
* build a relation array to given table * build a relation array to given table
* *
...@@ -196,13 +246,8 @@ abstract class Doctrine_Plugin ...@@ -196,13 +246,8 @@ abstract class Doctrine_Plugin
*/ */
public function buildRelation() public function buildRelation()
{ {
$relation = array($this->_options['table']->getComponentName() => $this->buildForeignRelation();
array('local' => $this->_options['table']->getIdentifier(), $this->buildLocalRelation();
'foreign' => $this->_options['table']->getIdentifier(),
'onDelete' => 'CASCADE',
'onUpdate' => 'CASCADE'));
return $relation;
} }
/** /**
...@@ -219,7 +264,7 @@ abstract class Doctrine_Plugin ...@@ -219,7 +264,7 @@ abstract class Doctrine_Plugin
*/ */
public function generateClass(array $columns = array(), array $relations = array(), array $options = array()) public function generateClass(array $columns = array(), array $relations = array(), array $options = array())
{ {
$options['className'] = $this->_options['className']; $options['className'] = $this->_options['className'];
$builder = new Doctrine_Import_Builder(); $builder = new Doctrine_Import_Builder();
......
...@@ -73,7 +73,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -73,7 +73,7 @@ class Doctrine_Search extends Doctrine_Plugin
*/ */
public function search($query) public function search($query)
{ {
$q = new Doctrine_Search_Query($this->_options['pluginTable']); $q = new Doctrine_Search_Query($this->_table);
$q->query($query); $q->query($query);
...@@ -100,7 +100,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -100,7 +100,7 @@ class Doctrine_Search extends Doctrine_Plugin
*/ */
public function updateIndex(array $data) public function updateIndex(array $data)
{ {
$this->buildDefinition(); $this->initialize($this->_options['table']);
$fields = $this->getOption('fields'); $fields = $this->getOption('fields');
$class = $this->getOption('className'); $class = $this->getOption('className');
...@@ -155,7 +155,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -155,7 +155,7 @@ class Doctrine_Search extends Doctrine_Plugin
*/ */
public function readTableData($limit = null, $offset = null) public function readTableData($limit = null, $offset = null)
{ {
$this->buildDefinition(); $this->initialize($this->_options['table']);
$conn = $this->_options['table']->getConnection(); $conn = $this->_options['table']->getConnection();
$tableName = $this->_options['table']->getTableName(); $tableName = $this->_options['table']->getTableName();
...@@ -164,7 +164,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -164,7 +164,7 @@ class Doctrine_Search extends Doctrine_Plugin
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName) $query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
. ' WHERE ' . $conn->quoteIdentifier($id) . ' WHERE ' . $conn->quoteIdentifier($id)
. ' IN (SELECT ' . $conn->quoteIdentifier($id) . ' IN (SELECT ' . $conn->quoteIdentifier($id)
. ' FROM ' . $conn->quoteIdentifier($this->_options['pluginTable']->getTableName()) . ' FROM ' . $conn->quoteIdentifier($this->_table->getTableName())
. ' WHERE keyword IS NULL)'; . ' WHERE keyword IS NULL)';
$query = $conn->modifyLimitQuery($query, $limit, $offset); $query = $conn->modifyLimitQuery($query, $limit, $offset);
...@@ -183,7 +183,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -183,7 +183,7 @@ class Doctrine_Search extends Doctrine_Plugin
*/ */
public function batchUpdateIndex($limit = null, $offset = null) public function batchUpdateIndex($limit = null, $offset = null)
{ {
$this->buildDefinition(); $this->initialize($this->_options['table']);
$id = $this->_options['table']->getIdentifier(); $id = $this->_options['table']->getIdentifier();
$class = $this->_options['className']; $class = $this->_options['className'];
...@@ -200,7 +200,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -200,7 +200,7 @@ class Doctrine_Search extends Doctrine_Plugin
} }
$conn->exec('DELETE FROM ' $conn->exec('DELETE FROM '
. $conn->quoteIdentifier($this->_options['pluginTable']->getTableName()) . $conn->quoteIdentifier($this->_table->getTableName())
. ' WHERE ' . $conn->quoteIdentifier($id) . ' IN (' . implode(', ', $ids) . ')'); . ' WHERE ' . $conn->quoteIdentifier($id) . ' IN (' . implode(', ', $ids) . ')');
foreach ($rows as $row) { foreach ($rows as $row) {
...@@ -236,10 +236,9 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -236,10 +236,9 @@ class Doctrine_Search extends Doctrine_Plugin
* *
* @return void * @return void
*/ */
public function buildDefinition() public function setTableDefinition()
{ {
if ( ! isset($this->_options['table'])) { if ( ! isset($this->_options['table'])) {
Doctrine::dump(debug_backtrace());
throw new Doctrine_Plugin_Exception("Unknown option 'table'."); throw new Doctrine_Plugin_Exception("Unknown option 'table'.");
} }
...@@ -263,22 +262,6 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -263,22 +262,6 @@ class Doctrine_Search extends Doctrine_Plugin
'primary' => true, 'primary' => true,
)); ));
$id = $this->_options['table']->getIdentifier(); $this->hasColumns($columns);
$fk = $this->buildForeignKeys($this->_options['table']);
$columns += $fk;
$relations = array();
// only generate relations for database based searches
if ( ! $this instanceof Doctrine_Search_File) {
$relations = $this->buildRelation();
}
$this->generateClass($columns, $relations);
$this->_options['pluginTable'] = $this->_options['connection']->getTable($this->_options['className']);
return true;
} }
} }
...@@ -51,8 +51,12 @@ class Doctrine_Search_File extends Doctrine_Search ...@@ -51,8 +51,12 @@ class Doctrine_Search_File extends Doctrine_Search
$this->_options['fields'] = array('url', 'content'); $this->_options['fields'] = array('url', 'content');
} }
$this->buildPluginDefinition($table); $this->initialize($table);
} }
public function buildRelation()
{
}
/** /**
* indexes given directory * indexes given directory
* *
......
...@@ -49,17 +49,9 @@ class Doctrine_Template_I18n extends Doctrine_Template ...@@ -49,17 +49,9 @@ class Doctrine_Template_I18n extends Doctrine_Template
*/ */
public function setUp() public function setUp()
{ {
$name = $this->_table->getComponentName(); $this->_plugin->initialize($this->_table);
$this->_plugin->buildPluginDefinition($this->_table);
$className = $this->_plugin->getOption('className');
$id = $this->_table->getIdentifier();
$this->hasMany($className . ' as Translation', array('local' => $id, 'foreign' => $id));
} }
/** /**
* getI18n * getI18n
* *
......
...@@ -37,18 +37,9 @@ class Doctrine_Template_Searchable extends Doctrine_Template ...@@ -37,18 +37,9 @@ class Doctrine_Template_Searchable extends Doctrine_Template
$this->_plugin = new Doctrine_Search($options); $this->_plugin = new Doctrine_Search($options);
} }
public function getPlugin()
{
return $this->_plugin;
}
public function setUp() public function setUp()
{ {
$id = $this->_table->getIdentifier(); $this->_plugin->initialize($this->_table);
$this->_plugin->buildPluginDefinition($this->_table);
$this->hasMany($this->_plugin->getOption('className'), array('local' => $id, 'foreign' => $id));
$this->addListener(new Doctrine_Search_Listener($this->_plugin)); $this->addListener(new Doctrine_Search_Listener($this->_plugin));
} }
......
...@@ -38,7 +38,7 @@ class Doctrine_Template_Versionable extends Doctrine_Template ...@@ -38,7 +38,7 @@ class Doctrine_Template_Versionable extends Doctrine_Template
} }
public function setUp() public function setUp()
{ {
$this->_plugin->buildPluginDefinition($this->_table); $this->_plugin->initialize($this->_table);
$this->hasColumn('version', 'integer', 8); $this->hasColumn('version', 'integer', 8);
......
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