diff --git a/lib/Doctrine/Search.php b/lib/Doctrine/Search.php index 42a1f29d6..23779f5cc 100644 --- a/lib/Doctrine/Search.php +++ b/lib/Doctrine/Search.php @@ -32,7 +32,29 @@ */ class Doctrine_Search { - public function buildDefinition(Doctrine_Record $record) + protected $_options = array('generateFiles' => true); + + public function __construct(array $options) + { + $this->_options = array_merge($this->_options, $options); + } + + public function getOption($option) + { + if (isset($this->_options[$option])) { + return $this->_option[$option]; + } + + return null; + } + + public function setOption($option, $value) + { + $this->_options[$option] = $value; + + return $this; + } + public function buildDefinition(Doctrine_Table $table) { $columns = array('keyword' => array('type' => 'string', @@ -44,15 +66,15 @@ class Doctrine_Search 'position' => array('type' => 'integer', 'length' => 8)); - $id = $record->getTable()->getIdentifier(); - $name = $record->getTable()->getComponentName(); + $id = $table->getIdentifier(); + $name = $table->getComponentName(); $options = array('className' => $name . 'Index'); $fk = array(); foreach ((array) $id as $column) { - $def = $record->getTable()->getDefinitionOf($column); + $def = $table->getDefinitionOf($column); unset($def['autoincrement']); unset($def['sequence']); @@ -77,18 +99,12 @@ class Doctrine_Search $def = $builder->buildDefinition($options, $columns, $relations); + if ( ! $this->_options['generateFiles']) { + eval($def); + } + /** print "
"; - print_r($def); + print_r(htmlentities($def)); + */ } } -/** -fields: -[keyword] [field] [foreign_id] [position] - - -fields: -[keyword] [field] [match] - -example data: -'orm' 'content' '1:36|2:23' -*/ diff --git a/lib/Doctrine/Search/Analyzer/Standard.php b/lib/Doctrine/Search/Analyzer/Standard.php index a97f7f36e..efbb345fd 100644 --- a/lib/Doctrine/Search/Analyzer/Standard.php +++ b/lib/Doctrine/Search/Analyzer/Standard.php @@ -34,6 +34,12 @@ class Doctrine_Search_Analyzer_Standard implements Doctrine_Search_Analyzer_Inte { public function analyze($text) { - + $text = explode(' ', $text); + + foreach ($text as $i => $term) { + $text[$i] = strtolower(trim($term)); + } + + return $text; } } diff --git a/lib/Doctrine/Search/Listener.php b/lib/Doctrine/Search/Listener.php index 38cf09e43..e475b965c 100644 --- a/lib/Doctrine/Search/Listener.php +++ b/lib/Doctrine/Search/Listener.php @@ -32,23 +32,37 @@ */ class Doctrine_Search_Listener extends Doctrine_Record_Listener { + protected $_search; + + public function __construct(Doctrine_Search $search) + { + $this->_search = $search; + } + public function preUpdate(Doctrine_Event $event) - { - + { } public function postUpdate(Doctrine_Event $event) - { - + { + } public function preInsert(Doctrine_Event $event) - { + { } public function postInsert(Doctrine_Event $event) - { + { + $fields = $this->_search->getOption('fields'); + foreach ($fields as $field) { + $terms = $this->_search->analyze($field); + + foreach ($terms as $term) { + + } + } } } diff --git a/lib/Doctrine/Search/Template.php b/lib/Doctrine/Search/Template.php index 730f6fbd2..38421ffbe 100644 --- a/lib/Doctrine/Search/Template.php +++ b/lib/Doctrine/Search/Template.php @@ -32,10 +32,20 @@ */ class Doctrine_Search_Template extends Doctrine_Template { + protected $_search; + + public function __construct(array $options) + { + $this->_search = new Doctrine_Search($options); + } public function setUp() { - $id = $record->getTable()->getIdentifier(); - $name = $record->getTable()->getComponentName() . 'Index'; + $this->_search->buildDefinition($this->_table); + + $id = $this->_table->getIdentifier(); + $name = $this->_table->getComponentName() . 'Index'; + + $this->_search->setOption('className', $name); foreach ((array) $id as $column) { $foreign[] = strtolower($name . '_' . $column); @@ -44,7 +54,7 @@ class Doctrine_Search_Template extends Doctrine_Template $foreign = (count($foreign) > 1) ? array_keys($foreign) : key($foreign); $this->hasMany($name, array('local' => $id, 'foreign' => $foreign)); - - $this->addListener(new Doctrine_Search_Listener()); + + $this->addListener(new Doctrine_Search_Listener($this->_search)); } }