enhanced the internal API of various plugins
This commit is contained in:
parent
c25d481d5c
commit
2174bc99e7
@ -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
|
|||||||
|
|
||||||
$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
|
|||||||
'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
|
|||||||
'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
|
|||||||
|
|
||||||
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%',
|
||||||
@ -122,32 +124,31 @@ abstract class Doctrine_Plugin
|
|||||||
if (class_exists($this->_options['className'])) {
|
if (class_exists($this->_options['className'])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->buildDefinition();
|
$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();
|
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) {
|
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
|
|||||||
* @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
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
|||||||
/**
|
/**
|
||||||
* 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
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
|||||||
'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
|
||||||
*
|
*
|
||||||
@ -71,7 +68,7 @@ class Doctrine_Search extends Doctrine_Plugin
|
|||||||
/**
|
/**
|
||||||
* search
|
* search
|
||||||
*
|
*
|
||||||
* @param string $query
|
* @param string $query
|
||||||
* @return Doctrine_Collection The collection of search results
|
* @return Doctrine_Collection The collection of search results
|
||||||
*/
|
*/
|
||||||
public function search($query)
|
public function search($query)
|
||||||
@ -107,10 +104,10 @@ 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);
|
||||||
foreach ((array) $identifier as $id) {
|
foreach ((array) $identifier as $id) {
|
||||||
@ -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
|
|||||||
$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
|
|||||||
{
|
{
|
||||||
$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
|
|||||||
{
|
{
|
||||||
$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
|
|||||||
*/
|
*/
|
||||||
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();
|
$componentName = $this->_options['table']->getComponentName();
|
||||||
|
|
||||||
// check for placeholders
|
|
||||||
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
|
|||||||
'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
|
|||||||
$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)
|
||||||
@ -58,7 +56,7 @@ class Doctrine_Search_File extends Doctrine_Search
|
|||||||
|
|
||||||
foreach ($it as $file) {
|
foreach ($it as $file) {
|
||||||
if (strpos($file, DIRECTORY_SEPARATOR . '.svn') !== false) {
|
if (strpos($file, DIRECTORY_SEPARATOR . '.svn') !== false) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->updateIndex(array('url' => $file->getPathName(),
|
$this->updateIndex(array('url' => $file->getPathName(),
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user