From aaf0d131718dc9079a5b3717276455f8701feeea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 6 Jun 2014 01:17:02 +0200 Subject: [PATCH] make search non-static, add ISearch to server container, make legacy a static wrapper for it, move provider and result to public api --- lib/private/legacy/search.php | 41 ++++++++++++++- lib/private/search.php | 49 +++++++++--------- lib/private/search/provider/file.php | 4 +- lib/private/search/result/file.php | 2 +- lib/private/server.php | 10 ++++ lib/public/isearch.php | 57 +++++++++++++++++++++ lib/public/iservercontainer.php | 8 +++ lib/{private => public}/search/provider.php | 4 +- lib/{private => public}/search/result.php | 2 +- 9 files changed, 146 insertions(+), 31 deletions(-) create mode 100644 lib/public/isearch.php rename lib/{private => public}/search/provider.php (93%) rename lib/{private => public}/search/result.php (98%) diff --git a/lib/private/legacy/search.php b/lib/private/legacy/search.php index e639602022..f77b43be2e 100644 --- a/lib/private/legacy/search.php +++ b/lib/private/legacy/search.php @@ -24,6 +24,45 @@ * provides an interface to all search providers * @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(); + } } diff --git a/lib/private/search.php b/lib/private/search.php index 8ac9e9fdf3..bcaebdddd9 100644 --- a/lib/private/search.php +++ b/lib/private/search.php @@ -21,27 +21,28 @@ */ namespace OC; -use OC\Search\Provider; +use OCP\Search\Provider; +use OCP\ISearch; /** * Provide an interface to all search providers */ -class Search { +class Search implements ISearch { - static private $providers=array(); - static private $registeredProviders=array(); + private $providers = array(); + private $registeredProviders = array(); /** * Search all providers for $query * @param string $query * @return array An array of OC\Search\Result's */ - public static function search($query) { - self::initProviders(); - $results=array(); - foreach(self::$providers as $provider) { + public function search($query) { + $this->initProviders(); + $results = array(); + foreach($this->providers as $provider) { /** @var $provider Provider */ - $results=array_merge($results, $provider->search($query)); + $results = array_merge($results, $provider->search($query)); } return $results; } @@ -49,24 +50,24 @@ class Search { /** * Remove all registered search providers */ - public static function clearProviders() { - self::$providers=array(); - self::$registeredProviders=array(); + public function clearProviders() { + $this->providers=array(); + $this->registeredProviders=array(); } /** * Remove one existing search provider * @param string $provider class name of a OC\Search\Provider */ - public static function removeProvider($provider) { - self::$registeredProviders = array_filter( - self::$registeredProviders, + public function removeProvider($provider) { + $this->registeredProviders = array_filter( + $this->registeredProviders, function ($element) use ($provider) { return ($element['class'] != $provider); } ); // 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 array $options optional */ - public static function registerProvider($class, $options=array()) { - self::$registeredProviders[]=array('class'=>$class, 'options'=>$options); + public function registerProvider($class, $options=array()) { + $this->registeredProviders[]=array('class'=>$class, 'options'=>$options); } /** * Create instances of all the registered search providers */ - private static function initProviders() { - if(count(self::$providers)>0) { + private function initProviders() { + if(count($this->providers)>0) { return; } - foreach(self::$registeredProviders as $provider) { - $class=$provider['class']; - $options=$provider['options']; - self::$providers[]=new $class($options); + foreach($this->registeredProviders as $provider) { + $class = $provider['class']; + $options = $provider['options']; + $this->providers[]=new $class($options); } } diff --git a/lib/private/search/provider/file.php b/lib/private/search/provider/file.php index daf73cf79c..cbd3a25373 100644 --- a/lib/private/search/provider/file.php +++ b/lib/private/search/provider/file.php @@ -23,12 +23,12 @@ use OC\Files\Filesystem; /** * 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 * @param string $query - * @return \OC\Search\Result + * @return \OCP\Search\Result */ function search($query) { $files = Filesystem::search($query); diff --git a/lib/private/search/result/file.php b/lib/private/search/result/file.php index 32a4aebc70..da5fa64ef4 100644 --- a/lib/private/search/result/file.php +++ b/lib/private/search/result/file.php @@ -22,7 +22,7 @@ use \OC\Files\Filesystem; /** * A found file */ -class File extends \OC\Search\Result { +class File extends \OCP\Search\Result { /** * Type name; translated in templates diff --git a/lib/private/server.php b/lib/private/server.php index 47bdee4b0f..da70586307 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -186,6 +186,9 @@ class Server extends SimpleContainer implements IServerContainer { } return $router; }); + $this->registerService('Search', function($c){ + return new Search(); + }); $this->registerService('Db', function($c){ return new Db(); }); @@ -422,6 +425,13 @@ class Server extends SimpleContainer implements IServerContainer { return $this->query('Router'); } + /** + * Returns a search instance + * @return \OCP\ISearch + */ + function getSearch() { + return $this->query('Search'); + } /** * Returns an instance of the db facade diff --git a/lib/public/isearch.php b/lib/public/isearch.php new file mode 100644 index 0000000000..3b83dbf35e --- /dev/null +++ b/lib/public/isearch.php @@ -0,0 +1,57 @@ +. + * + */ + +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(); + +} diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index 22176c36b8..8bf9782858 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -204,4 +204,12 @@ interface IServerContainer { * @return \OCP\Route\IRouter */ function getRouter(); + + /** + * Returns a search instance + * + * @return \OCP\ISearch + */ + function getSearch(); + } diff --git a/lib/private/search/provider.php b/lib/public/search/provider.php similarity index 93% rename from lib/private/search/provider.php rename to lib/public/search/provider.php index 45d9f823c5..0506f091dd 100644 --- a/lib/private/search/provider.php +++ b/lib/public/search/provider.php @@ -17,7 +17,7 @@ * */ -namespace OC\Search; +namespace OCP\Search; /** * Provides a template for search functionality throughout ownCloud; @@ -41,7 +41,7 @@ abstract class Provider { /** * Search for $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); } diff --git a/lib/private/search/result.php b/lib/public/search/result.php similarity index 98% rename from lib/private/search/result.php rename to lib/public/search/result.php index d228f83381..c70f1bde88 100644 --- a/lib/private/search/result.php +++ b/lib/public/search/result.php @@ -17,7 +17,7 @@ * */ -namespace OC\Search; +namespace OCP\Search; /** * The generic result of a search