From 2174bc99e7c5ce76bb95f6811094dfaf4a2b47fa Mon Sep 17 00:00:00 2001 From: zYne Date: Sun, 25 Nov 2007 21:10:49 +0000 Subject: [PATCH] enhanced the internal API of various plugins --- lib/Doctrine/AuditLog.php | 10 ++-- lib/Doctrine/I18n.php | 15 ++---- lib/Doctrine/Plugin.php | 69 ++++++++++++++-------------- lib/Doctrine/Record/Abstract.php | 2 +- lib/Doctrine/Search.php | 53 +++++++++------------ lib/Doctrine/Search/File.php | 6 +-- lib/Doctrine/Template/Searchable.php | 11 +---- 7 files changed, 68 insertions(+), 98 deletions(-) diff --git a/lib/Doctrine/AuditLog.php b/lib/Doctrine/AuditLog.php index a6455e288..7b5e221df 100644 --- a/lib/Doctrine/AuditLog.php +++ b/lib/Doctrine/AuditLog.php @@ -38,6 +38,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin 'generateFiles' => false, 'table' => false, 'pluginTable' => false, + 'children' => array(), ); /** @@ -105,14 +106,9 @@ class Doctrine_AuditLog extends Doctrine_Plugin $id = $this->_options['table']->getIdentifier(); - $options = array('className' => $this->_options['className']); + $relations = $this->buildRelation(); - $relations = array($name => array('local' => $id, - 'foreign' => $id, - 'onDelete' => 'CASCADE', - 'onUpdate' => 'CASCADE')); - - $this->generateClass($options, $columns, array()); + $this->generateClass($columns, $relations); $this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']); diff --git a/lib/Doctrine/I18n.php b/lib/Doctrine/I18n.php index 52bb00694..db5541c42 100644 --- a/lib/Doctrine/I18n.php +++ b/lib/Doctrine/I18n.php @@ -86,27 +86,18 @@ class Doctrine_I18n extends Doctrine_Plugin 'fixed' => true, 'primary' => true); - $local = (count($fk) > 1) ? array_keys($fk) : key($fk); - - $relations = array($name => array('local' => $local, - 'foreign' => $this->_options['table']->getIdentifier(), - 'onDelete' => 'CASCADE', - 'onUpdate' => 'CASCADE')); - + $relations = $this->buildRelation(); $columns += $fk; - $options = array('className' => $this->_options['className'], - 'queryParts' => array('indexBy' => 'lang')); + $options = array('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']->bindQueryPart('indexBy', 'lang'); - $this->generateChildDefinitions(); - return true; } } diff --git a/lib/Doctrine/Plugin.php b/lib/Doctrine/Plugin.php index 17ed3c2ec..d99e6c3ba 100644 --- a/lib/Doctrine/Plugin.php +++ b/lib/Doctrine/Plugin.php @@ -40,7 +40,7 @@ abstract class Doctrine_Plugin 'generateFiles' => false, 'table' => false, 'pluginTable' => false, - 'children' => array(),); + 'children' => array()); /** * __get @@ -112,6 +112,8 @@ abstract class Doctrine_Plugin public function buildPluginDefinition(Doctrine_Table $table) { + $this->initOptions(); + $this->_options['table'] = $table; $this->_options['className'] = str_replace('%CLASS%', @@ -122,32 +124,31 @@ abstract class Doctrine_Plugin if (class_exists($this->_options['className'])) { return false; } - + $this->buildDefinition(); + + $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 buildForeignKeys(Doctrine_Table $table) + public function buildChildDefinitions() { - $id = $table->getIdentifier(); + if ( ! isset($this->_options['children'])) { + Doctrine::dump(debug_backtrace()); + throw new Doctrine_Plugin_Exception("Unknown option 'children'."); + } - $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; - } - - public function generateChildDefinitions() - { foreach ($this->_options['children'] as $child) { $this->_options['pluginTable']->addTemplate(get_class($child), $child); @@ -166,7 +167,7 @@ abstract class Doctrine_Plugin * @param Doctrine_Table $table the table object that owns the plugin * @return array an array of foreign key definitions */ - public function generateForeignKeys(Doctrine_Table $table) + public function buildForeignKeys(Doctrine_Table $table) { $fk = array(); @@ -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 * 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 */ - public function generateRelation(Doctrine_Table $table, array $foreignKeys) + public function buildRelation() { - $local = (count($foreignKeys) > 1) ? array_keys($foreignKeys) : key($foreignKeys); - - $relation = array($table->getComponentName() => - array('local' => $local, - 'foreign' => $table->getIdentifier(), + $relation = array($this->_options['table']->getComponentName() => + array('local' => $this->_options['table']->getIdentifier(), + 'foreign' => $this->_options['table']->getIdentifier(), 'onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); @@ -211,15 +208,19 @@ abstract class Doctrine_Plugin /** * 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 * and values as column definitions + * * @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 */ - 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(); if ($this->_options['generateFiles']) { diff --git a/lib/Doctrine/Record/Abstract.php b/lib/Doctrine/Record/Abstract.php index b845ee446..2dc95db1e 100644 --- a/lib/Doctrine/Record/Abstract.php +++ b/lib/Doctrine/Record/Abstract.php @@ -275,7 +275,7 @@ abstract class Doctrine_Record_Abstract extends Doctrine_Access /** * actAs - * loads a given plugin + * loads the given plugin * * @param mixed $tpl * @param array $options diff --git a/lib/Doctrine/Search.php b/lib/Doctrine/Search.php index 3d73ba27f..4a5d4e432 100644 --- a/lib/Doctrine/Search.php +++ b/lib/Doctrine/Search.php @@ -40,15 +40,12 @@ class Doctrine_Search extends Doctrine_Plugin 'type' => self::INDEX_TABLES, 'className' => '%CLASS%Index', 'generatePath' => false, - 'resource' => null, + 'table' => null, 'batchUpdates' => false, 'pluginTable' => false, 'fields' => array(), - 'connection' => null); - - protected $_built = false; - - + 'connection' => null, + 'children' => array()); /** * __construct * @@ -71,7 +68,7 @@ class Doctrine_Search extends Doctrine_Plugin /** * search * - * @param string $query + * @param string $query * @return Doctrine_Collection The collection of search results */ public function search($query) @@ -107,10 +104,10 @@ class Doctrine_Search extends Doctrine_Plugin $fields = $this->getOption('fields'); $class = $this->getOption('className'); - $name = $this->getOption('resource')->getComponentName(); - $conn = $this->getOption('resource')->getConnection(); - $identifier = $this->_options['resource']->getIdentifier(); - + $name = $this->getOption('table')->getComponentName(); + $conn = $this->getOption('table')->getConnection(); + $identifier = $this->_options['table']->getIdentifier(); + $q = Doctrine_Query::create()->delete() ->from($class); foreach ((array) $identifier as $id) { @@ -121,7 +118,7 @@ class Doctrine_Search extends Doctrine_Plugin if ($this->_options['batchUpdates'] === true) { $index = new $class(); - foreach ((array) $this->_options['resource']->getIdentifier() as $id) { + foreach ((array) $this->_options['table']->getIdentifier() as $id) { $index->$id = $data[$id]; } @@ -139,7 +136,7 @@ class Doctrine_Search extends Doctrine_Plugin $index->keyword = $term; $index->position = $pos; $index->field = $field; - foreach ((array) $this->_options['resource']->getIdentifier() as $id) { + foreach ((array) $this->_options['table']->getIdentifier() as $id) { $index->$id = $data[$id]; } @@ -160,9 +157,9 @@ class Doctrine_Search extends Doctrine_Plugin { $this->buildDefinition(); - $conn = $this->_options['resource']->getConnection(); - $tableName = $this->_options['resource']->getTableName(); - $id = $this->_options['resource']->getIdentifier(); + $conn = $this->_options['table']->getConnection(); + $tableName = $this->_options['table']->getTableName(); + $id = $this->_options['table']->getIdentifier(); $query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName) . ' WHERE ' . $conn->quoteIdentifier($id) @@ -188,7 +185,7 @@ class Doctrine_Search extends Doctrine_Plugin { $this->buildDefinition(); - $id = $this->_options['resource']->getIdentifier(); + $id = $this->_options['table']->getIdentifier(); $class = $this->_options['className']; $fields = $this->_options['fields']; $conn = $this->_options['connection']; @@ -241,17 +238,12 @@ class Doctrine_Search extends Doctrine_Plugin */ public function buildDefinition() { - if ($this->_built) { - return true; + if ( ! isset($this->_options['table'])) { + Doctrine::dump(debug_backtrace()); + throw new Doctrine_Plugin_Exception("Unknown option 'table'."); } - $this->_built = true; - $componentName = $this->_options['resource']->getComponentName(); - - // check for placeholders - if (strpos($this->_options['className'], '%') !== false) { - $this->_options['className'] = str_replace('%CLASS%', $componentName, $this->_options['className']); - } + $componentName = $this->_options['table']->getComponentName(); $className = $this->getOption('className'); @@ -271,20 +263,19 @@ class Doctrine_Search extends Doctrine_Plugin '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; $relations = array(); // only generate relations for database based searches 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']); diff --git a/lib/Doctrine/Search/File.php b/lib/Doctrine/Search/File.php index a6858fb08..b0c919975 100644 --- a/lib/Doctrine/Search/File.php +++ b/lib/Doctrine/Search/File.php @@ -40,15 +40,13 @@ class Doctrine_Search_File extends Doctrine_Search $table = new Doctrine_Table('File', Doctrine_Manager::connection()); $table->setColumn('url', 'string', 255, array('primary' => true)); - - $this->_options['resource'] = $table; } if (empty($this->_options['fields'])) { $this->_options['fields'] = array('url', 'content'); } - $this->buildDefinition(); + $this->buildPluginDefinition($table); } public function indexDirectory($dir) @@ -58,7 +56,7 @@ class Doctrine_Search_File extends Doctrine_Search foreach ($it as $file) { if (strpos($file, DIRECTORY_SEPARATOR . '.svn') !== false) { - continue; + continue; } $this->updateIndex(array('url' => $file->getPathName(), diff --git a/lib/Doctrine/Template/Searchable.php b/lib/Doctrine/Template/Searchable.php index 56a682569..4e8abe823 100644 --- a/lib/Doctrine/Template/Searchable.php +++ b/lib/Doctrine/Template/Searchable.php @@ -45,17 +45,10 @@ class Doctrine_Template_Searchable extends Doctrine_Template public function setUp() { $id = $this->_table->getIdentifier(); - $name = $this->_table->getComponentName(); - $className = $this->_plugin->getOption('className'); - if (strpos($className, '%CLASS%') !== false) { - $this->_plugin->setOption('className', str_replace('%CLASS%', $name, $className)); - $className = $this->_plugin->getOption('className'); - } - $this->_plugin->setOption('resource', $this->_table); - $this->_plugin->buildDefinition(); + $this->_plugin->buildPluginDefinition($this->_table); - $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)); }