From ae4b715754a7640d0888d84d0dbf4a5f5259f68c Mon Sep 17 00:00:00 2001 From: zYne Date: Sun, 25 Nov 2007 12:34:37 +0000 Subject: [PATCH] enhanced plugin building procedure --- lib/Doctrine/AuditLog.php | 27 +++++----------- lib/Doctrine/I18n.php | 35 +++++--------------- lib/Doctrine/Plugin.php | 46 +++++++++++++++++++++++++-- lib/Doctrine/Template/I18n.php | 13 +++----- lib/Doctrine/Template/Sluggable.php | 2 +- lib/Doctrine/Template/Versionable.php | 5 +-- 6 files changed, 67 insertions(+), 61 deletions(-) diff --git a/lib/Doctrine/AuditLog.php b/lib/Doctrine/AuditLog.php index 996ea258f..a6455e288 100644 --- a/lib/Doctrine/AuditLog.php +++ b/lib/Doctrine/AuditLog.php @@ -46,7 +46,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin * @param array $options An array of options * @return void */ - public function __construct($options) + public function __construct(array $options = array()) { $this->_options = array_merge($this->_options, $options); } @@ -85,22 +85,11 @@ class Doctrine_AuditLog extends Doctrine_Plugin * @param Doctrine_Table $table * @return boolean true on success otherwise false. */ - public function buildDefinition(Doctrine_Table $table) + public function buildDefinition() { - $this->_options['className'] = str_replace('%CLASS%', - $this->_options['table']->getComponentName(), - $this->_options['className']); + $name = $this->_options['table']->getComponentName(); - $name = $table->getComponentName(); - - $className = $name . 'Version'; - - // check that class doesn't exist (otherwise we cannot create it) - if (class_exists($className)) { - return false; - } - - $columns = $table->getColumns(); + $columns = $this->_options['table']->getColumns(); // remove all sequence, autoincrement and unique constraint definitions foreach ($columns as $column => $definition) { @@ -114,10 +103,10 @@ class Doctrine_AuditLog extends Doctrine_Plugin 'length' => 8, 'primary' => true); - $id = $table->getIdentifier(); + $id = $this->_options['table']->getIdentifier(); + + $options = array('className' => $this->_options['className']); - $options = array('className' => $className); - $relations = array($name => array('local' => $id, 'foreign' => $id, 'onDelete' => 'CASCADE', @@ -125,7 +114,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin $this->generateClass($options, $columns, array()); - $this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']); + $this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']); return true; } diff --git a/lib/Doctrine/I18n.php b/lib/Doctrine/I18n.php index 3fea6f7dc..52bb00694 100644 --- a/lib/Doctrine/I18n.php +++ b/lib/Doctrine/I18n.php @@ -58,48 +58,29 @@ class Doctrine_I18n extends Doctrine_Plugin * @param object $Doctrine_Table * @return void */ - public function buildDefinition(Doctrine_Table $table) + public function buildDefinition() { if (empty($this->_options['fields'])) { throw new Doctrine_I18n_Exception('Fields not set.'); } - $this->_options['className'] = str_replace('%CLASS%', - $this->_options['table']->getComponentName(), - $this->_options['className']); - - $name = $table->getComponentName(); - - if (class_exists($this->_options['className'])) { - return false; - } + $name = $this->_options['table']->getComponentName(); $columns = array(); - $id = $table->getIdentifier(); - $options = array('className' => $this->_options['className']); - $fk = array(); - foreach ((array) $id as $column) { - $def = $table->getDefinitionOf($column); + $fk = $this->buildForeignKeys($this->_options['table']); - unset($def['autoincrement']); - unset($def['sequence']); - unset($def['unique']); - - $fk[$column] = $def; - } - - $cols = $table->getColumns(); + $cols = $this->_options['table']->getColumns(); foreach ($cols as $column => $definition) { if (in_array($column, $this->_options['fields'])) { $columns[$column] = $definition; - $table->removeColumn($column); + $this->_options['table']->removeColumn($column); } } - + $columns['lang'] = array('type' => 'string', 'length' => 2, 'fixed' => true, @@ -108,7 +89,7 @@ class Doctrine_I18n extends Doctrine_Plugin $local = (count($fk) > 1) ? array_keys($fk) : key($fk); $relations = array($name => array('local' => $local, - 'foreign' => $id, + 'foreign' => $this->_options['table']->getIdentifier(), 'onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); @@ -120,7 +101,7 @@ class Doctrine_I18n extends Doctrine_Plugin $this->generateClass($options, $columns, $relations); - $this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']); + $this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']); $this->_options['pluginTable']->bindQueryPart('indexBy', 'lang'); diff --git a/lib/Doctrine/Plugin.php b/lib/Doctrine/Plugin.php index d4e8bb011..17ed3c2ec 100644 --- a/lib/Doctrine/Plugin.php +++ b/lib/Doctrine/Plugin.php @@ -30,13 +30,17 @@ * @link www.phpdoctrine.com * @since 1.0 */ -class Doctrine_Plugin +abstract class Doctrine_Plugin { /** * @var array $_options an array of plugin specific options */ protected $_options = array('generateFiles' => false, - 'identifier' => false); + 'identifier' => false, + 'generateFiles' => false, + 'table' => false, + 'pluginTable' => false, + 'children' => array(),); /** * __get @@ -93,7 +97,7 @@ class Doctrine_Plugin public function addChild(Doctrine_Template $template) { - $this->_options['children'][] = $template; + $this->_options['children'][] = $template; } /** @@ -106,6 +110,42 @@ class Doctrine_Plugin return $this->_options; } + public function buildPluginDefinition(Doctrine_Table $table) + { + $this->_options['table'] = $table; + + $this->_options['className'] = str_replace('%CLASS%', + $this->_options['table']->getComponentName(), + $this->_options['className']); + + // check that class doesn't exist (otherwise we cannot create it) + if (class_exists($this->_options['className'])) { + return false; + } + + $this->buildDefinition(); + } + + abstract public function buildDefinition(); + + public function buildForeignKeys(Doctrine_Table $table) + { + $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; + } + public function generateChildDefinitions() { foreach ($this->_options['children'] as $child) { diff --git a/lib/Doctrine/Template/I18n.php b/lib/Doctrine/Template/I18n.php index a3f17cde7..7938814c0 100644 --- a/lib/Doctrine/Template/I18n.php +++ b/lib/Doctrine/Template/I18n.php @@ -38,7 +38,7 @@ class Doctrine_Template_I18n extends Doctrine_Template * @param string $array * @return void */ - public function __construct(array $options) + public function __construct(array $options = array()) { $this->_plugin = new Doctrine_I18n($options); } @@ -49,17 +49,12 @@ class Doctrine_Template_I18n extends Doctrine_Template */ public function setUp() { - $this->_plugin->setOption('table', $this->_table); $name = $this->_table->getComponentName(); + + $this->_plugin->buildPluginDefinition($this->_table); + $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->buildDefinition($this->_table); - $id = $this->_table->getIdentifier(); $this->hasMany($className . ' as Translation', array('local' => $id, 'foreign' => $id)); diff --git a/lib/Doctrine/Template/Sluggable.php b/lib/Doctrine/Template/Sluggable.php index 791707cbf..3358b99e7 100644 --- a/lib/Doctrine/Template/Sluggable.php +++ b/lib/Doctrine/Template/Sluggable.php @@ -67,4 +67,4 @@ class Doctrine_Template_Sluggable extends Doctrine_Template $this->addListener(new Doctrine_Template_Listener_Sluggable($this->_options)); } -} \ No newline at end of file +} diff --git a/lib/Doctrine/Template/Versionable.php b/lib/Doctrine/Template/Versionable.php index dcab9ec47..a3cfdcb44 100644 --- a/lib/Doctrine/Template/Versionable.php +++ b/lib/Doctrine/Template/Versionable.php @@ -38,8 +38,9 @@ class Doctrine_Template_Versionable extends Doctrine_Template } public function setUp() { - $this->_plugin->setOption('table', $this->_table); - $this->_plugin->buildDefinition($this->_table); + $this->_plugin->buildPluginDefinition($this->_table); + + $this->hasColumn('version', 'integer', 8); $this->addListener(new Doctrine_AuditLog_Listener($this->_plugin)); }