1
0
mirror of synced 2025-01-18 06:21:40 +03:00

enhanced plugin building procedure

This commit is contained in:
zYne 2007-11-25 12:34:37 +00:00
parent 3baf1af60e
commit ae4b715754
6 changed files with 67 additions and 61 deletions

View File

@ -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;
}

View File

@ -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');

View File

@ -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) {

View File

@ -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));

View File

@ -67,4 +67,4 @@ class Doctrine_Template_Sluggable extends Doctrine_Template
$this->addListener(new Doctrine_Template_Listener_Sluggable($this->_options));
}
}
}

View File

@ -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));
}