enhanced plugin building procedure
This commit is contained in:
parent
3baf1af60e
commit
ae4b715754
@ -46,7 +46,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin
|
|||||||
* @param array $options An array of options
|
* @param array $options An array of options
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($options)
|
public function __construct(array $options = array())
|
||||||
{
|
{
|
||||||
$this->_options = array_merge($this->_options, $options);
|
$this->_options = array_merge($this->_options, $options);
|
||||||
}
|
}
|
||||||
@ -85,22 +85,11 @@ class Doctrine_AuditLog extends Doctrine_Plugin
|
|||||||
* @param Doctrine_Table $table
|
* @param Doctrine_Table $table
|
||||||
* @return boolean true on success otherwise false.
|
* @return boolean true on success otherwise false.
|
||||||
*/
|
*/
|
||||||
public function buildDefinition(Doctrine_Table $table)
|
public function buildDefinition()
|
||||||
{
|
{
|
||||||
$this->_options['className'] = str_replace('%CLASS%',
|
$name = $this->_options['table']->getComponentName();
|
||||||
$this->_options['table']->getComponentName(),
|
|
||||||
$this->_options['className']);
|
|
||||||
|
|
||||||
$name = $table->getComponentName();
|
$columns = $this->_options['table']->getColumns();
|
||||||
|
|
||||||
$className = $name . 'Version';
|
|
||||||
|
|
||||||
// check that class doesn't exist (otherwise we cannot create it)
|
|
||||||
if (class_exists($className)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$columns = $table->getColumns();
|
|
||||||
|
|
||||||
// remove all sequence, autoincrement and unique constraint definitions
|
// remove all sequence, autoincrement and unique constraint definitions
|
||||||
foreach ($columns as $column => $definition) {
|
foreach ($columns as $column => $definition) {
|
||||||
@ -114,10 +103,10 @@ class Doctrine_AuditLog extends Doctrine_Plugin
|
|||||||
'length' => 8,
|
'length' => 8,
|
||||||
'primary' => true);
|
'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,
|
$relations = array($name => array('local' => $id,
|
||||||
'foreign' => $id,
|
'foreign' => $id,
|
||||||
'onDelete' => 'CASCADE',
|
'onDelete' => 'CASCADE',
|
||||||
@ -125,7 +114,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin
|
|||||||
|
|
||||||
$this->generateClass($options, $columns, array());
|
$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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -58,48 +58,29 @@ class Doctrine_I18n extends Doctrine_Plugin
|
|||||||
* @param object $Doctrine_Table
|
* @param object $Doctrine_Table
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function buildDefinition(Doctrine_Table $table)
|
public function buildDefinition()
|
||||||
{
|
{
|
||||||
if (empty($this->_options['fields'])) {
|
if (empty($this->_options['fields'])) {
|
||||||
throw new Doctrine_I18n_Exception('Fields not set.');
|
throw new Doctrine_I18n_Exception('Fields not set.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_options['className'] = str_replace('%CLASS%',
|
$name = $this->_options['table']->getComponentName();
|
||||||
$this->_options['table']->getComponentName(),
|
|
||||||
$this->_options['className']);
|
|
||||||
|
|
||||||
$name = $table->getComponentName();
|
|
||||||
|
|
||||||
if (class_exists($this->_options['className'])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$columns = array();
|
$columns = array();
|
||||||
|
|
||||||
$id = $table->getIdentifier();
|
|
||||||
|
|
||||||
$options = array('className' => $this->_options['className']);
|
$options = array('className' => $this->_options['className']);
|
||||||
|
|
||||||
$fk = array();
|
$fk = $this->buildForeignKeys($this->_options['table']);
|
||||||
foreach ((array) $id as $column) {
|
|
||||||
$def = $table->getDefinitionOf($column);
|
|
||||||
|
|
||||||
unset($def['autoincrement']);
|
$cols = $this->_options['table']->getColumns();
|
||||||
unset($def['sequence']);
|
|
||||||
unset($def['unique']);
|
|
||||||
|
|
||||||
$fk[$column] = $def;
|
|
||||||
}
|
|
||||||
|
|
||||||
$cols = $table->getColumns();
|
|
||||||
|
|
||||||
foreach ($cols as $column => $definition) {
|
foreach ($cols as $column => $definition) {
|
||||||
if (in_array($column, $this->_options['fields'])) {
|
if (in_array($column, $this->_options['fields'])) {
|
||||||
$columns[$column] = $definition;
|
$columns[$column] = $definition;
|
||||||
$table->removeColumn($column);
|
$this->_options['table']->removeColumn($column);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$columns['lang'] = array('type' => 'string',
|
$columns['lang'] = array('type' => 'string',
|
||||||
'length' => 2,
|
'length' => 2,
|
||||||
'fixed' => true,
|
'fixed' => true,
|
||||||
@ -108,7 +89,7 @@ class Doctrine_I18n extends Doctrine_Plugin
|
|||||||
$local = (count($fk) > 1) ? array_keys($fk) : key($fk);
|
$local = (count($fk) > 1) ? array_keys($fk) : key($fk);
|
||||||
|
|
||||||
$relations = array($name => array('local' => $local,
|
$relations = array($name => array('local' => $local,
|
||||||
'foreign' => $id,
|
'foreign' => $this->_options['table']->getIdentifier(),
|
||||||
'onDelete' => 'CASCADE',
|
'onDelete' => 'CASCADE',
|
||||||
'onUpdate' => 'CASCADE'));
|
'onUpdate' => 'CASCADE'));
|
||||||
|
|
||||||
@ -120,7 +101,7 @@ class Doctrine_I18n extends Doctrine_Plugin
|
|||||||
|
|
||||||
$this->generateClass($options, $columns, $relations);
|
$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');
|
$this->_options['pluginTable']->bindQueryPart('indexBy', 'lang');
|
||||||
|
|
||||||
|
@ -30,13 +30,17 @@
|
|||||||
* @link www.phpdoctrine.com
|
* @link www.phpdoctrine.com
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
class Doctrine_Plugin
|
abstract class Doctrine_Plugin
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array $_options an array of plugin specific options
|
* @var array $_options an array of plugin specific options
|
||||||
*/
|
*/
|
||||||
protected $_options = array('generateFiles' => false,
|
protected $_options = array('generateFiles' => false,
|
||||||
'identifier' => false);
|
'identifier' => false,
|
||||||
|
'generateFiles' => false,
|
||||||
|
'table' => false,
|
||||||
|
'pluginTable' => false,
|
||||||
|
'children' => array(),);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* __get
|
* __get
|
||||||
@ -93,7 +97,7 @@ class Doctrine_Plugin
|
|||||||
|
|
||||||
public function addChild(Doctrine_Template $template)
|
public function addChild(Doctrine_Template $template)
|
||||||
{
|
{
|
||||||
$this->_options['children'][] = $template;
|
$this->_options['children'][] = $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -106,6 +110,42 @@ class Doctrine_Plugin
|
|||||||
return $this->_options;
|
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()
|
public function generateChildDefinitions()
|
||||||
{
|
{
|
||||||
foreach ($this->_options['children'] as $child) {
|
foreach ($this->_options['children'] as $child) {
|
||||||
|
@ -38,7 +38,7 @@ class Doctrine_Template_I18n extends Doctrine_Template
|
|||||||
* @param string $array
|
* @param string $array
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(array $options)
|
public function __construct(array $options = array())
|
||||||
{
|
{
|
||||||
$this->_plugin = new Doctrine_I18n($options);
|
$this->_plugin = new Doctrine_I18n($options);
|
||||||
}
|
}
|
||||||
@ -49,17 +49,12 @@ class Doctrine_Template_I18n extends Doctrine_Template
|
|||||||
*/
|
*/
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->_plugin->setOption('table', $this->_table);
|
|
||||||
$name = $this->_table->getComponentName();
|
$name = $this->_table->getComponentName();
|
||||||
|
|
||||||
|
$this->_plugin->buildPluginDefinition($this->_table);
|
||||||
|
|
||||||
$className = $this->_plugin->getOption('className');
|
$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();
|
$id = $this->_table->getIdentifier();
|
||||||
|
|
||||||
$this->hasMany($className . ' as Translation', array('local' => $id, 'foreign' => $id));
|
$this->hasMany($className . ' as Translation', array('local' => $id, 'foreign' => $id));
|
||||||
|
@ -67,4 +67,4 @@ class Doctrine_Template_Sluggable extends Doctrine_Template
|
|||||||
|
|
||||||
$this->addListener(new Doctrine_Template_Listener_Sluggable($this->_options));
|
$this->addListener(new Doctrine_Template_Listener_Sluggable($this->_options));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,9 @@ class Doctrine_Template_Versionable extends Doctrine_Template
|
|||||||
}
|
}
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->_plugin->setOption('table', $this->_table);
|
$this->_plugin->buildPluginDefinition($this->_table);
|
||||||
$this->_plugin->buildDefinition($this->_table);
|
|
||||||
|
$this->hasColumn('version', 'integer', 8);
|
||||||
|
|
||||||
$this->addListener(new Doctrine_AuditLog_Listener($this->_plugin));
|
$this->addListener(new Doctrine_AuditLog_Listener($this->_plugin));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user