make search non-static, add ISearch to server container, make legacy a static wrapper for it, move provider and result to public api

This commit is contained in:
Jörn Friedrich Dreyer 2014-06-06 01:17:02 +02:00
parent 5034bd1b12
commit aaf0d13171
9 changed files with 146 additions and 31 deletions

View File

@ -24,6 +24,45 @@
* provides an interface to all search providers * provides an interface to all search providers
* @deprecated see lib/search.php * @deprecated see lib/search.php
*/ */
class OC_Search extends \OC\Search{ class OC_Search {
/**
* @return \OCP\ISearch
*/
private static function getSearch() {
return \OC::$server->getSearch();
}
/**
* Search all providers for $query
* @param string $query
* @return array An array of OCP\Search\Result's
*/
public static function search($query) {
return self::getSearch()->search($query);
}
/**
* Register a new search provider to search with
* @param string $class class name of a OCP\Search\Provider
* @param array $options optional
*/
public static function registerProvider($class, $options = array()) {
return self::getSearch()->registerProvider($class, $options);
}
/**
* Remove one existing search provider
* @param string $provider class name of a OCP\Search\Provider
*/
public static function removeProvider($provider) {
return self::getSearch()->removeProvider($provider);
}
/**
* Remove all registered search providers
*/
public static function clearProviders() {
return self::getSearch()->clearProviders();
}
} }

View File

@ -21,27 +21,28 @@
*/ */
namespace OC; namespace OC;
use OC\Search\Provider; use OCP\Search\Provider;
use OCP\ISearch;
/** /**
* Provide an interface to all search providers * Provide an interface to all search providers
*/ */
class Search { class Search implements ISearch {
static private $providers=array(); private $providers = array();
static private $registeredProviders=array(); private $registeredProviders = array();
/** /**
* Search all providers for $query * Search all providers for $query
* @param string $query * @param string $query
* @return array An array of OC\Search\Result's * @return array An array of OC\Search\Result's
*/ */
public static function search($query) { public function search($query) {
self::initProviders(); $this->initProviders();
$results=array(); $results = array();
foreach(self::$providers as $provider) { foreach($this->providers as $provider) {
/** @var $provider Provider */ /** @var $provider Provider */
$results=array_merge($results, $provider->search($query)); $results = array_merge($results, $provider->search($query));
} }
return $results; return $results;
} }
@ -49,24 +50,24 @@ class Search {
/** /**
* Remove all registered search providers * Remove all registered search providers
*/ */
public static function clearProviders() { public function clearProviders() {
self::$providers=array(); $this->providers=array();
self::$registeredProviders=array(); $this->registeredProviders=array();
} }
/** /**
* Remove one existing search provider * Remove one existing search provider
* @param string $provider class name of a OC\Search\Provider * @param string $provider class name of a OC\Search\Provider
*/ */
public static function removeProvider($provider) { public function removeProvider($provider) {
self::$registeredProviders = array_filter( $this->registeredProviders = array_filter(
self::$registeredProviders, $this->registeredProviders,
function ($element) use ($provider) { function ($element) use ($provider) {
return ($element['class'] != $provider); return ($element['class'] != $provider);
} }
); );
// force regeneration of providers on next search // force regeneration of providers on next search
self::$providers=array(); $this->providers=array();
} }
/** /**
@ -74,21 +75,21 @@ class Search {
* @param string $class class name of a OC\Search\Provider * @param string $class class name of a OC\Search\Provider
* @param array $options optional * @param array $options optional
*/ */
public static function registerProvider($class, $options=array()) { public function registerProvider($class, $options=array()) {
self::$registeredProviders[]=array('class'=>$class, 'options'=>$options); $this->registeredProviders[]=array('class'=>$class, 'options'=>$options);
} }
/** /**
* Create instances of all the registered search providers * Create instances of all the registered search providers
*/ */
private static function initProviders() { private function initProviders() {
if(count(self::$providers)>0) { if(count($this->providers)>0) {
return; return;
} }
foreach(self::$registeredProviders as $provider) { foreach($this->registeredProviders as $provider) {
$class=$provider['class']; $class = $provider['class'];
$options=$provider['options']; $options = $provider['options'];
self::$providers[]=new $class($options); $this->providers[]=new $class($options);
} }
} }

View File

@ -23,12 +23,12 @@ use OC\Files\Filesystem;
/** /**
* Provide search results from the 'files' app * Provide search results from the 'files' app
*/ */
class File extends \OC\Search\Provider { class File extends \OCP\Search\Provider {
/** /**
* Search for files and folders matching the given query * Search for files and folders matching the given query
* @param string $query * @param string $query
* @return \OC\Search\Result * @return \OCP\Search\Result
*/ */
function search($query) { function search($query) {
$files = Filesystem::search($query); $files = Filesystem::search($query);

View File

@ -22,7 +22,7 @@ use \OC\Files\Filesystem;
/** /**
* A found file * A found file
*/ */
class File extends \OC\Search\Result { class File extends \OCP\Search\Result {
/** /**
* Type name; translated in templates * Type name; translated in templates

View File

@ -186,6 +186,9 @@ class Server extends SimpleContainer implements IServerContainer {
} }
return $router; return $router;
}); });
$this->registerService('Search', function($c){
return new Search();
});
$this->registerService('Db', function($c){ $this->registerService('Db', function($c){
return new Db(); return new Db();
}); });
@ -422,6 +425,13 @@ class Server extends SimpleContainer implements IServerContainer {
return $this->query('Router'); return $this->query('Router');
} }
/**
* Returns a search instance
* @return \OCP\ISearch
*/
function getSearch() {
return $this->query('Search');
}
/** /**
* Returns an instance of the db facade * Returns an instance of the db facade

57
lib/public/isearch.php Normal file
View File

@ -0,0 +1,57 @@
<?php
/**
* ownCloud - App Framework
*
* @author Jörn Dreyer
* @copyright 2014 Jörn Dreyer jfd@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP;
/**
* Small Interface for Search
*/
interface ISearch {
/**
* Search all providers for $query
* @param string $query
* @return array An array of OCP\Search\Result's
*/
public function search($query);
/**
* Register a new search provider to search with
* @param string $class class name of a OCP\Search\Provider
* @param array $options optional
*/
public function registerProvider($class, $options = array());
/**
* Remove one existing search provider
* @param string $provider class name of a OCP\Search\Provider
*/
public function removeProvider($provider);
/**
* Remove all registered search providers
*/
public function clearProviders();
}

View File

@ -204,4 +204,12 @@ interface IServerContainer {
* @return \OCP\Route\IRouter * @return \OCP\Route\IRouter
*/ */
function getRouter(); function getRouter();
/**
* Returns a search instance
*
* @return \OCP\ISearch
*/
function getSearch();
} }

View File

@ -17,7 +17,7 @@
* *
*/ */
namespace OC\Search; namespace OCP\Search;
/** /**
* Provides a template for search functionality throughout ownCloud; * Provides a template for search functionality throughout ownCloud;
@ -41,7 +41,7 @@ abstract class Provider {
/** /**
* Search for $query * Search for $query
* @param string $query * @param string $query
* @return array An array of OC\Search\Result's * @return array An array of OCP\Search\Result's
*/ */
abstract public function search($query); abstract public function search($query);
} }

View File

@ -17,7 +17,7 @@
* *
*/ */
namespace OC\Search; namespace OCP\Search;
/** /**
* The generic result of a search * The generic result of a search