From 7e6a2b71fd80fedabc64d10bee5ba0edc0b19cf0 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sat, 21 Mar 2015 20:03:56 +0100 Subject: Added Capabilities Manager * This should allow the capabilities to be intergrated into the appframework * Unit tests * Throw exception if closure does not return ICapability instance --- lib/private/capabilitiesmanager.php | 65 +++++++++++++++++++++++++++++++++ lib/private/ocs/cloud.php | 8 +++- lib/private/server.php | 14 +++++++ lib/public/capabilities/icapability.php | 46 +++++++++++++++++++++++ lib/public/capabilities/imanager.php | 44 ++++++++++++++++++++++ lib/public/iservercontainer.php | 8 ++++ 6 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 lib/private/capabilitiesmanager.php create mode 100644 lib/public/capabilities/icapability.php create mode 100644 lib/public/capabilities/imanager.php (limited to 'lib') diff --git a/lib/private/capabilitiesmanager.php b/lib/private/capabilitiesmanager.php new file mode 100644 index 00000000000..ebbd9664897 --- /dev/null +++ b/lib/private/capabilitiesmanager.php @@ -0,0 +1,65 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ +namespace OC; + + +use OCP\Capabilities\IManager; +use OCP\Capabilities\ICapability; + +class CapabilitiesManager implements IManager { + + /** + * @var \Closure[] + */ + private $capabilities = array(); + + /** + * Get an array of al the capabilities that are registered at this manager + * + * @throws \InvalidArgumentException + * @return array + */ + public function getCapabilities() { + $capabilities = []; + foreach($this->capabilities as $capability) { + $c = $capability(); + if ($c instanceof ICapability) { + $capabilities = array_replace_recursive($capabilities, $c->getCapabilities()); + } else { + throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface'); + } + } + + return $capabilities; + } + + /** + * In order to improve lazy loading a closure can be registered which will be called in case + * capabilities are actually requested + * + * $callable has to return an instance of OCP\Capabilities\ICapability + * + * @param \Closure $callable + */ + public function registerCapability(\Closure $callable) { + array_push($this->capabilities, $callable); + } +} diff --git a/lib/private/ocs/cloud.php b/lib/private/ocs/cloud.php index f662bde2858..ff6b7977cd9 100644 --- a/lib/private/ocs/cloud.php +++ b/lib/private/ocs/cloud.php @@ -3,6 +3,7 @@ * @author Bart Visscher * @author Morris Jobke * @author Robin McCorkell + * @author Roeland Jago Douma * @author Thomas Müller * @author Tom Needham * @@ -40,8 +41,11 @@ class OC_OCS_Cloud { 'core' => array( 'pollinterval' => OC_Config::getValue('pollinterval', 60), ), - ); - + ); + + + $result['capabilities'] = array_merge_recursive($result['capabilities'], \OC::$server->getCapabilitiesManager()->getCapabilities()); + return new OC_OCS_Result($result); } diff --git a/lib/private/server.php b/lib/private/server.php index 12981fe7f19..2c0793f4c1d 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -59,6 +59,7 @@ use OC\Security\SecureRandom; use OC\Security\TrustedDomainHelper; use OC\Tagging\TagMapper; use OCP\IServerContainer; +use OC\CapabilitiesManager; /** * Class Server @@ -449,6 +450,9 @@ class Server extends SimpleContainer implements IServerContainer { $c->getURLGenerator(), \OC::$configDir); }); + $this->registerService('CapabilitiesManager', function () { + return new CapabilitiesManager(); + }); } /** @@ -945,4 +949,14 @@ class Server extends SimpleContainer implements IServerContainer { public function getMimeTypeDetector() { return $this->query('MimeTypeDetector'); } + + /** + * Get the manager of all the capabilities + * + * @return \OCP\Capabilities\IManager + * @since 8.2.0 + */ + public function getCapabilitiesManager() { + return $this->query('CapabilitiesManager'); + } } diff --git a/lib/public/capabilities/icapability.php b/lib/public/capabilities/icapability.php new file mode 100644 index 00000000000..71a56128d26 --- /dev/null +++ b/lib/public/capabilities/icapability.php @@ -0,0 +1,46 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCP\Capabilities; + +/** + * Minimal interface that has to be implemented for a class to be considered + * a capability. + * + * In an application use: + * $this->getContainer()->registerCapability('OCA\MY_APP\Capabilities'); + * To register capabilities. + * + * The class 'OCA\MY_APP\Capabilities' must then implement ICapability + * + * @since 8.2.0 + */ +interface ICapability { + + /** + * Function an app uses to return the capabilities + * + * @return array Array containing the apps capabilities + * @since 8.2.0 + */ + public function getCapabilities(); +} + diff --git a/lib/public/capabilities/imanager.php b/lib/public/capabilities/imanager.php new file mode 100644 index 00000000000..df563bd2b25 --- /dev/null +++ b/lib/public/capabilities/imanager.php @@ -0,0 +1,44 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ +namespace OCP\Capabilities; + + +interface IManager { + + /** + * Get an array of al the capabilities that are registered at this manager + * + * @return array All the capabilities registered in this manager + * @since 8.2.0 + */ + public function getCapabilities(); + + /** + * In order to improve lazy loading a closure can be registered which will be called in case + * activity consumers are actually requested + * + * $callable has to return an instance of OCP\Capabilities\ICapability + * + * @param \Closure $callable + * @since 8.2.0 + */ + public function registerCapability(\Closure $callable); +} diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index f3165db33da..6cb8d668658 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -438,4 +438,12 @@ interface IServerContainer { * @since 8.2.0 */ public function getMimeTypeDetector(); + + /** + * Get the manager of all the capabilities + * + * @return \OCP\Capabilities\IManager + * @since 8.2.0 + */ + public function getCapabilitiesManager(); } -- cgit v1.2.3 From c80c9819dcc1a4c79b5c2621a3ec07623e1cd140 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 21 Jul 2015 21:44:59 +0200 Subject: Move core capabilities to new class --- lib/private/ocs/cloud.php | 9 +----- lib/private/ocs/corecapabilities.php | 56 ++++++++++++++++++++++++++++++++++++ lib/private/server.php | 11 +++++-- 3 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 lib/private/ocs/corecapabilities.php (limited to 'lib') diff --git a/lib/private/ocs/cloud.php b/lib/private/ocs/cloud.php index ff6b7977cd9..8f4f1769e9c 100644 --- a/lib/private/ocs/cloud.php +++ b/lib/private/ocs/cloud.php @@ -37,14 +37,7 @@ class OC_OCS_Cloud { 'edition' => OC_Util::getEditionString(), ); - $result['capabilities'] = array( - 'core' => array( - 'pollinterval' => OC_Config::getValue('pollinterval', 60), - ), - ); - - - $result['capabilities'] = array_merge_recursive($result['capabilities'], \OC::$server->getCapabilitiesManager()->getCapabilities()); + $result['capabilities'] = \OC::$server->getCapabilitiesManager()->getCapabilities(); return new OC_OCS_Result($result); } diff --git a/lib/private/ocs/corecapabilities.php b/lib/private/ocs/corecapabilities.php new file mode 100644 index 00000000000..6b620ab0a84 --- /dev/null +++ b/lib/private/ocs/corecapabilities.php @@ -0,0 +1,56 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ + +namespace OC\OCS; + +use OCP\Capabilities\ICapability; +use OCP\IConfig; + +/** + * Class Capabilities + * + * @package OC\OCS + */ +class CoreCapabilities implements ICapability { + + /** @var IConfig */ + private $config; + + /** + * @param IConfig $config + */ + public function __construct(IConfig $config) { + $this->config = $config; + } + + /** + * Return this classes capabilities + * + * @return array + */ + public function getCapabilities() { + return [ + 'core' => [ + 'pollinterval' => $this->config->getSystemValue('pollinterval', 60) + ] + ]; + } +} diff --git a/lib/private/server.php b/lib/private/server.php index 2c0793f4c1d..5bf3121ec0f 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -59,7 +59,6 @@ use OC\Security\SecureRandom; use OC\Security\TrustedDomainHelper; use OC\Tagging\TagMapper; use OCP\IServerContainer; -use OC\CapabilitiesManager; /** * Class Server @@ -450,8 +449,14 @@ class Server extends SimpleContainer implements IServerContainer { $c->getURLGenerator(), \OC::$configDir); }); - $this->registerService('CapabilitiesManager', function () { - return new CapabilitiesManager(); + $this->registerService('CapabilitiesManager', function (Server $c) { + $manager = new \OC\CapabilitiesManager(); + $manager->registerCapability(function() use ($c) { + return new \OC\OCS\CoreCapabilities( + $c->getConfig() + ); + }); + return $manager; }); } -- cgit v1.2.3 From f0b617b50825566ec1b417eed09688e88de5f0bb Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 22 Jul 2015 13:04:56 +0200 Subject: Use DI * Register OCP\Capability\IManager at DIContainer * Add register capabilities to appframework * Register capabilities in DI way * Make unit test pass again * Remove CapabiltiesManager from OCP --- apps/files/appinfo/application.php | 5 +-- apps/files_sharing/appinfo/application.php | 6 +-- apps/files_trashbin/appinfo/application.php | 5 +-- apps/files_versions/appinfo/application.php | 5 +-- .../dependencyinjection/dicontainer.php | 14 +++++++ lib/private/capabilitiesmanager.php | 3 +- lib/private/server.php | 8 ++-- lib/public/appframework/iappcontainer.php | 7 ++++ lib/public/capabilities/imanager.php | 44 ---------------------- lib/public/iservercontainer.php | 7 ---- tests/lib/capabilitiesmanager.php | 31 +++++++-------- tests/lib/server.php | 2 +- 12 files changed, 44 insertions(+), 93 deletions(-) delete mode 100644 lib/public/capabilities/imanager.php (limited to 'lib') diff --git a/apps/files/appinfo/application.php b/apps/files/appinfo/application.php index f121eb7a401..6ba77e09556 100644 --- a/apps/files/appinfo/application.php +++ b/apps/files/appinfo/application.php @@ -26,7 +26,6 @@ use OCA\Files\Controller\ApiController; use OCP\AppFramework\App; use \OCA\Files\Service\TagService; use \OCP\IContainer; -use OCA\Files\Capabilities; class Application extends App { public function __construct(array $urlParams=array()) { @@ -71,8 +70,6 @@ class Application extends App { /* * Register capabilities */ - $server->getCapabilitiesManager()->registerCapability(function() { - return new Capabilities(); - }); + $container->registerCapability('OCA\Files\Capabilities'); } } diff --git a/apps/files_sharing/appinfo/application.php b/apps/files_sharing/appinfo/application.php index 6ddd25acb52..2fe9019d54e 100644 --- a/apps/files_sharing/appinfo/application.php +++ b/apps/files_sharing/appinfo/application.php @@ -128,11 +128,7 @@ class Application extends App { /* * Register capabilities */ - $server->getCapabilitiesManager()->registerCapability(function() use ($server) { - return new Capabilities( - $server->getConfig() - ); - }); + $container->registerCapability('OCA\Files_Sharing\Capabilities'); } public function registerMountProviders() { diff --git a/apps/files_trashbin/appinfo/application.php b/apps/files_trashbin/appinfo/application.php index 851de142e5c..8d76d40f639 100644 --- a/apps/files_trashbin/appinfo/application.php +++ b/apps/files_trashbin/appinfo/application.php @@ -22,7 +22,6 @@ namespace OCA\Files_Trashbin\AppInfo; use OCP\AppFramework\App; -use OCA\Files_Trashbin\Capabilities; class Application extends App { public function __construct(array $urlParams = array()) { @@ -33,8 +32,6 @@ class Application extends App { /* * Register capabilities */ - $server->getCapabilitiesManager()->registerCapability(function() { - return new Capabilities(); - }); + $container->registerCapability('OCA\Files_Trashbin\Capabilities'); } } diff --git a/apps/files_versions/appinfo/application.php b/apps/files_versions/appinfo/application.php index 69485bc1f22..bab36b48510 100644 --- a/apps/files_versions/appinfo/application.php +++ b/apps/files_versions/appinfo/application.php @@ -22,7 +22,6 @@ namespace OCA\Files_Versions\AppInfo; use OCP\AppFramework\App; -use OCA\Files_Versions\Capabilities; class Application extends App { public function __construct(array $urlParams = array()) { @@ -33,8 +32,6 @@ class Application extends App { /* * Register capabilities */ - $server->getCapabilitiesManager()->registerCapability(function() { - return new Capabilities(); - }); + $container->registerCapability('OCA\Files_Versions\Capabilities'); } } diff --git a/lib/private/appframework/dependencyinjection/dicontainer.php b/lib/private/appframework/dependencyinjection/dicontainer.php index 927b6d43302..c66b792064d 100644 --- a/lib/private/appframework/dependencyinjection/dicontainer.php +++ b/lib/private/appframework/dependencyinjection/dicontainer.php @@ -96,6 +96,10 @@ class DIContainer extends SimpleContainer implements IAppContainer { return $this->getServer()->getMemCacheFactory(); }); + $this->registerService('OC\\CapabilitiesManager', function($c) { + return $this->getServer()->getCapabilitiesManager(); + }); + $this->registerService('OCP\\IConfig', function($c) { return $this->getServer()->getConfig(); }); @@ -390,5 +394,15 @@ class DIContainer extends SimpleContainer implements IAppContainer { \OCP\Util::writeLog($this->getAppName(), $message, $level); } + /** + * Register a capability + * + * @param string $serviceName e.g. 'OCA\Files\Capabilities' + */ + public function registerCapability($serviceName) { + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { + return $this->query($serviceName); + }); + } } diff --git a/lib/private/capabilitiesmanager.php b/lib/private/capabilitiesmanager.php index ebbd9664897..74154f2c631 100644 --- a/lib/private/capabilitiesmanager.php +++ b/lib/private/capabilitiesmanager.php @@ -21,10 +21,9 @@ namespace OC; -use OCP\Capabilities\IManager; use OCP\Capabilities\ICapability; -class CapabilitiesManager implements IManager { +class CapabilitiesManager { /** * @var \Closure[] diff --git a/lib/private/server.php b/lib/private/server.php index 5bf3121ec0f..9503cf16ff7 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -452,12 +452,11 @@ class Server extends SimpleContainer implements IServerContainer { $this->registerService('CapabilitiesManager', function (Server $c) { $manager = new \OC\CapabilitiesManager(); $manager->registerCapability(function() use ($c) { - return new \OC\OCS\CoreCapabilities( - $c->getConfig() - ); + return new \OC\OCS\CoreCapabilities($c->getConfig()); }); return $manager; }); + } /** @@ -958,8 +957,7 @@ class Server extends SimpleContainer implements IServerContainer { /** * Get the manager of all the capabilities * - * @return \OCP\Capabilities\IManager - * @since 8.2.0 + * @return \OC\CapabilitiesManager */ public function getCapabilitiesManager() { return $this->query('CapabilitiesManager'); diff --git a/lib/public/appframework/iappcontainer.php b/lib/public/appframework/iappcontainer.php index 64b1082aa97..7338dbd858d 100644 --- a/lib/public/appframework/iappcontainer.php +++ b/lib/public/appframework/iappcontainer.php @@ -86,4 +86,11 @@ interface IAppContainer extends IContainer { */ function log($message, $level); + /** + * Register a capability + * + * @param string $serviceName e.g. 'OCA\Files\Capabilities' + * @since 8.2.0 + */ + public function registerCapability($serviceName); } diff --git a/lib/public/capabilities/imanager.php b/lib/public/capabilities/imanager.php deleted file mode 100644 index df563bd2b25..00000000000 --- a/lib/public/capabilities/imanager.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program 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, version 3, - * along with this program. If not, see - * - */ -namespace OCP\Capabilities; - - -interface IManager { - - /** - * Get an array of al the capabilities that are registered at this manager - * - * @return array All the capabilities registered in this manager - * @since 8.2.0 - */ - public function getCapabilities(); - - /** - * In order to improve lazy loading a closure can be registered which will be called in case - * activity consumers are actually requested - * - * $callable has to return an instance of OCP\Capabilities\ICapability - * - * @param \Closure $callable - * @since 8.2.0 - */ - public function registerCapability(\Closure $callable); -} diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index 6cb8d668658..ab1729da255 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -439,11 +439,4 @@ interface IServerContainer { */ public function getMimeTypeDetector(); - /** - * Get the manager of all the capabilities - * - * @return \OCP\Capabilities\IManager - * @since 8.2.0 - */ - public function getCapabilitiesManager(); } diff --git a/tests/lib/capabilitiesmanager.php b/tests/lib/capabilitiesmanager.php index 738715344e0..b5dac80ee51 100644 --- a/tests/lib/capabilitiesmanager.php +++ b/tests/lib/capabilitiesmanager.php @@ -70,18 +70,14 @@ class CapabilitiesManagerTest extends TestCase { public function testMergedCapabilities() { $manager = new \OC\CapabilitiesManager(); - $simple1 = new SimpleCapability(); - $simple2 = new SimpleCapability2(); - $simple3 = new SimpleCapability3(); - - $manager->registerCapability(function() use ($simple1) { - return $simple1; + $manager->registerCapability(function() { + return new SimpleCapability(); }); - $manager->registerCapability(function() use ($simple2) { - return $simple2; + $manager->registerCapability(function() { + return new SimpleCapability2(); }); - $manager->registerCapability(function() use ($simple3) { - return $simple3; + $manager->registerCapability(function() { + return new SimpleCapability3(); }); $res = $manager->getCapabilities(); @@ -146,6 +142,14 @@ class SimpleCapability3 implements \OCP\Capabilities\ICapability { } } +class NoCapability { + public function getCapabilities() { + return [ + 'baz' => 'z' + ]; + } +} + class DeepCapability implements \OCP\Capabilities\ICapability { public function getCapabilities() { return [ @@ -158,10 +162,3 @@ class DeepCapability implements \OCP\Capabilities\ICapability { } } -class NoCapability { - public function getCapabilities() { - return [ - 'baz' => 'z' - ]; - } -} diff --git a/tests/lib/server.php b/tests/lib/server.php index 1eb91173deb..9c5c83ceb5c 100644 --- a/tests/lib/server.php +++ b/tests/lib/server.php @@ -51,7 +51,7 @@ class Server extends \Test\TestCase { ['AvatarManager', '\OC\AvatarManager'], ['AvatarManager', '\OCP\IAvatarManager'], - ['CapabilitiesManager', '\OCP\Capabilities\IManager'], + ['CapabilitiesManager', '\OC\CapabilitiesManager'], ['ContactsManager', '\OC\ContactsManager'], ['ContactsManager', '\OCP\Contacts\IManager'], ['Crypto', '\OC\Security\Crypto'], -- cgit v1.2.3