Commit 2174bc99 authored by zYne's avatar zYne

enhanced the internal API of various plugins

parent c25d481d
...@@ -38,6 +38,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin ...@@ -38,6 +38,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin
'generateFiles' => false, 'generateFiles' => false,
'table' => false, 'table' => false,
'pluginTable' => false, 'pluginTable' => false,
'children' => array(),
); );
/** /**
...@@ -105,14 +106,9 @@ class Doctrine_AuditLog extends Doctrine_Plugin ...@@ -105,14 +106,9 @@ class Doctrine_AuditLog extends Doctrine_Plugin
$id = $this->_options['table']->getIdentifier(); $id = $this->_options['table']->getIdentifier();
$options = array('className' => $this->_options['className']); $relations = $this->buildRelation();
$relations = array($name => array('local' => $id, $this->generateClass($columns, $relations);
'foreign' => $id,
'onDelete' => 'CASCADE',
'onUpdate' => 'CASCADE'));
$this->generateClass($options, $columns, array());
$this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']); $this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']);
......
...@@ -86,27 +86,18 @@ class Doctrine_I18n extends Doctrine_Plugin ...@@ -86,27 +86,18 @@ class Doctrine_I18n extends Doctrine_Plugin
'fixed' => true, 'fixed' => true,
'primary' => true); 'primary' => true);
$local = (count($fk) > 1) ? array_keys($fk) : key($fk); $relations = $this->buildRelation();
$relations = array($name => array('local' => $local,
'foreign' => $this->_options['table']->getIdentifier(),
'onDelete' => 'CASCADE',
'onUpdate' => 'CASCADE'));
$columns += $fk; $columns += $fk;
$options = array('className' => $this->_options['className'], $options = array('queryParts' => array('indexBy' => 'lang'));
'queryParts' => array('indexBy' => 'lang'));
$this->generateClass($options, $columns, $relations); $this->generateClass($columns, $relations, $options);
$this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']); $this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']);
$this->_options['pluginTable']->bindQueryPart('indexBy', 'lang'); $this->_options['pluginTable']->bindQueryPart('indexBy', 'lang');
$this->generateChildDefinitions();
return true; return true;
} }
} }
...@@ -40,7 +40,7 @@ abstract class Doctrine_Plugin ...@@ -40,7 +40,7 @@ abstract class Doctrine_Plugin
'generateFiles' => false, 'generateFiles' => false,
'table' => false, 'table' => false,
'pluginTable' => false, 'pluginTable' => false,
'children' => array(),); 'children' => array());
/** /**
* __get * __get
...@@ -112,6 +112,8 @@ abstract class Doctrine_Plugin ...@@ -112,6 +112,8 @@ abstract class Doctrine_Plugin
public function buildPluginDefinition(Doctrine_Table $table) public function buildPluginDefinition(Doctrine_Table $table)
{ {
$this->initOptions();
$this->_options['table'] = $table; $this->_options['table'] = $table;
$this->_options['className'] = str_replace('%CLASS%', $this->_options['className'] = str_replace('%CLASS%',
...@@ -124,30 +126,29 @@ abstract class Doctrine_Plugin ...@@ -124,30 +126,29 @@ abstract class Doctrine_Plugin
} }
$this->buildDefinition(); $this->buildDefinition();
}
abstract public function buildDefinition();
public function buildForeignKeys(Doctrine_Table $table) $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()
{ {
$id = $table->getIdentifier();
$fk = array();
foreach ((array) $id as $column) {
$def = $table->getDefinitionOf($column);
unset($def['autoincrement']);
unset($def['sequence']);
unset($def['unique']);
$fk[$column] = $def;
} }
return $fk; abstract public function buildDefinition();
}
public function generateChildDefinitions() public function buildChildDefinitions()
{ {
if ( ! isset($this->_options['children'])) {
Doctrine::dump(debug_backtrace());
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->_options['pluginTable']->addTemplate(get_class($child), $child);
...@@ -166,7 +167,7 @@ abstract class Doctrine_Plugin ...@@ -166,7 +167,7 @@ abstract class Doctrine_Plugin
* @param Doctrine_Table $table the table object that owns the plugin * @param Doctrine_Table $table the table object that owns the plugin
* @return array an array of foreign key definitions * @return array an array of foreign key definitions
*/ */
public function generateForeignKeys(Doctrine_Table $table) public function buildForeignKeys(Doctrine_Table $table)
{ {
$fk = array(); $fk = array();
...@@ -186,22 +187,18 @@ abstract class Doctrine_Plugin ...@@ -186,22 +187,18 @@ abstract class Doctrine_Plugin
} }
/** /**
* generates a relation array to given table * build a relation array to given table
* *
* this method can be used for generating the relation from the plugin * this method can be used for generating the relation from the plugin
* table to the owner table * table to the owner table
* *
* @param Doctrine_Table $table the table object to construct the relation to
* @param array $foreignKeys an array of foreign keys
* @return array the generated relation array * @return array the generated relation array
*/ */
public function generateRelation(Doctrine_Table $table, array $foreignKeys) public function buildRelation()
{ {
$local = (count($foreignKeys) > 1) ? array_keys($foreignKeys) : key($foreignKeys); $relation = array($this->_options['table']->getComponentName() =>
array('local' => $this->_options['table']->getIdentifier(),
$relation = array($table->getComponentName() => 'foreign' => $this->_options['table']->getIdentifier(),
array('local' => $local,
'foreign' => $table->getIdentifier(),
'onDelete' => 'CASCADE', 'onDelete' => 'CASCADE',
'onUpdate' => 'CASCADE')); 'onUpdate' => 'CASCADE'));
...@@ -211,15 +208,19 @@ abstract class Doctrine_Plugin ...@@ -211,15 +208,19 @@ abstract class Doctrine_Plugin
/** /**
* generates the class definition for plugin class * generates the class definition for plugin class
* *
* @param array $options plugin class options, keys representing the option names
* and values as option values
* @param array $columns the plugin class columns, keys representing the column names * @param array $columns the plugin class columns, keys representing the column names
* and values as column definitions * and values as column definitions
*
* @param array $relations the bound relations of the plugin class * @param array $relations the bound relations of the plugin class
*
* @param array $options plugin class options, keys representing the option names
* and values as option values
* @return void * @return void
*/ */
public function generateClass($options, $columns, $relations) public function generateClass(array $columns = array(), array $relations = array(), array $options = array())
{ {
$options['className'] = $this->_options['className'];
$builder = new Doctrine_Import_Builder(); $builder = new Doctrine_Import_Builder();
if ($this->_options['generateFiles']) { if ($this->_options['generateFiles']) {
......
...@@ -275,7 +275,7 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access ...@@ -275,7 +275,7 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access
/** /**
* actAs * actAs
* loads a given plugin * loads the given plugin
* *
* @param mixed $tpl * @param mixed $tpl
* @param array $options * @param array $options
......
...@@ -40,15 +40,12 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -40,15 +40,12 @@ class Doctrine_Search extends Doctrine_Plugin
'type' => self::INDEX_TABLES, 'type' => self::INDEX_TABLES,
'className' => '%CLASS%Index', 'className' => '%CLASS%Index',
'generatePath' => false, 'generatePath' => false,
'resource' => null, 'table' => null,
'batchUpdates' => false, 'batchUpdates' => false,
'pluginTable' => false, 'pluginTable' => false,
'fields' => array(), 'fields' => array(),
'connection' => null); 'connection' => null,
'children' => array());
protected $_built = false;
/** /**
* __construct * __construct
* *
...@@ -107,9 +104,9 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -107,9 +104,9 @@ class Doctrine_Search extends Doctrine_Plugin
$fields = $this->getOption('fields'); $fields = $this->getOption('fields');
$class = $this->getOption('className'); $class = $this->getOption('className');
$name = $this->getOption('resource')->getComponentName(); $name = $this->getOption('table')->getComponentName();
$conn = $this->getOption('resource')->getConnection(); $conn = $this->getOption('table')->getConnection();
$identifier = $this->_options['resource']->getIdentifier(); $identifier = $this->_options['table']->getIdentifier();
$q = Doctrine_Query::create()->delete() $q = Doctrine_Query::create()->delete()
->from($class); ->from($class);
...@@ -121,7 +118,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -121,7 +118,7 @@ class Doctrine_Search extends Doctrine_Plugin
if ($this->_options['batchUpdates'] === true) { if ($this->_options['batchUpdates'] === true) {
$index = new $class(); $index = new $class();
foreach ((array) $this->_options['resource']->getIdentifier() as $id) { foreach ((array) $this->_options['table']->getIdentifier() as $id) {
$index->$id = $data[$id]; $index->$id = $data[$id];
} }
...@@ -139,7 +136,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -139,7 +136,7 @@ class Doctrine_Search extends Doctrine_Plugin
$index->keyword = $term; $index->keyword = $term;
$index->position = $pos; $index->position = $pos;
$index->field = $field; $index->field = $field;
foreach ((array) $this->_options['resource']->getIdentifier() as $id) { foreach ((array) $this->_options['table']->getIdentifier() as $id) {
$index->$id = $data[$id]; $index->$id = $data[$id];
} }
...@@ -160,9 +157,9 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -160,9 +157,9 @@ class Doctrine_Search extends Doctrine_Plugin
{ {
$this->buildDefinition(); $this->buildDefinition();
$conn = $this->_options['resource']->getConnection(); $conn = $this->_options['table']->getConnection();
$tableName = $this->_options['resource']->getTableName(); $tableName = $this->_options['table']->getTableName();
$id = $this->_options['resource']->getIdentifier(); $id = $this->_options['table']->getIdentifier();
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName) $query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
. ' WHERE ' . $conn->quoteIdentifier($id) . ' WHERE ' . $conn->quoteIdentifier($id)
...@@ -188,7 +185,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -188,7 +185,7 @@ class Doctrine_Search extends Doctrine_Plugin
{ {
$this->buildDefinition(); $this->buildDefinition();
$id = $this->_options['resource']->getIdentifier(); $id = $this->_options['table']->getIdentifier();
$class = $this->_options['className']; $class = $this->_options['className'];
$fields = $this->_options['fields']; $fields = $this->_options['fields'];
$conn = $this->_options['connection']; $conn = $this->_options['connection'];
...@@ -241,17 +238,12 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -241,17 +238,12 @@ class Doctrine_Search extends Doctrine_Plugin
*/ */
public function buildDefinition() public function buildDefinition()
{ {
if ($this->_built) { if ( ! isset($this->_options['table'])) {
return true; Doctrine::dump(debug_backtrace());
throw new Doctrine_Plugin_Exception("Unknown option 'table'.");
} }
$this->_built = true;
$componentName = $this->_options['resource']->getComponentName();
// check for placeholders $componentName = $this->_options['table']->getComponentName();
if (strpos($this->_options['className'], '%') !== false) {
$this->_options['className'] = str_replace('%CLASS%', $componentName, $this->_options['className']);
}
$className = $this->getOption('className'); $className = $this->getOption('className');
...@@ -271,20 +263,19 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -271,20 +263,19 @@ class Doctrine_Search extends Doctrine_Plugin
'primary' => true, 'primary' => true,
)); ));
$id = $this->_options['resource']->getIdentifier(); $id = $this->_options['table']->getIdentifier();
$options = array('className' => $className); $fk = $this->buildForeignKeys($this->_options['table']);
$fk = $this->generateForeignKeys($this->_options['resource']);
$columns += $fk; $columns += $fk;
$relations = array(); $relations = array();
// only generate relations for database based searches // only generate relations for database based searches
if ( ! $this instanceof Doctrine_Search_File) { if ( ! $this instanceof Doctrine_Search_File) {
$relations = $this->generateRelation($this->_options['resource'], $fk); $relations = $this->buildRelation();
} }
$this->generateClass($options, $columns, $relations); $this->generateClass($columns, $relations);
$this->_options['pluginTable'] = $this->_options['connection']->getTable($this->_options['className']); $this->_options['pluginTable'] = $this->_options['connection']->getTable($this->_options['className']);
......
...@@ -40,15 +40,13 @@ class Doctrine_Search_File extends Doctrine_Search ...@@ -40,15 +40,13 @@ class Doctrine_Search_File extends Doctrine_Search
$table = new Doctrine_Table('File', Doctrine_Manager::connection()); $table = new Doctrine_Table('File', Doctrine_Manager::connection());
$table->setColumn('url', 'string', 255, array('primary' => true)); $table->setColumn('url', 'string', 255, array('primary' => true));
$this->_options['resource'] = $table;
} }
if (empty($this->_options['fields'])) { if (empty($this->_options['fields'])) {
$this->_options['fields'] = array('url', 'content'); $this->_options['fields'] = array('url', 'content');
} }
$this->buildDefinition(); $this->buildPluginDefinition($table);
} }
public function indexDirectory($dir) public function indexDirectory($dir)
......
...@@ -45,17 +45,10 @@ class Doctrine_Template_Searchable extends Doctrine_Template ...@@ -45,17 +45,10 @@ class Doctrine_Template_Searchable extends Doctrine_Template
public function setUp() public function setUp()
{ {
$id = $this->_table->getIdentifier(); $id = $this->_table->getIdentifier();
$name = $this->_table->getComponentName();
$className = $this->_plugin->getOption('className');
if (strpos($className, '%CLASS%') !== false) { $this->_plugin->buildPluginDefinition($this->_table);
$this->_plugin->setOption('className', str_replace('%CLASS%', $name, $className));
$className = $this->_plugin->getOption('className');
}
$this->_plugin->setOption('resource', $this->_table);
$this->_plugin->buildDefinition();
$this->hasMany($className, array('local' => $id, 'foreign' => $id)); $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));
} }
......
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