Commit a5f755f4 authored by zYne's avatar zYne

enhanced plugin initialization procedure

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