1
0

CapabilitiesManager.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC;
  23. use OCP\AppFramework\QueryException;
  24. use OCP\Capabilities\ICapability;
  25. use OCP\ILogger;
  26. class CapabilitiesManager {
  27. /** @var \Closure[] */
  28. private $capabilities = array();
  29. /** @var ILogger */
  30. private $logger;
  31. public function __construct(ILogger $logger) {
  32. $this->logger = $logger;
  33. }
  34. /**
  35. * Get an array of al the capabilities that are registered at this manager
  36. *
  37. * @throws \InvalidArgumentException
  38. * @return array
  39. */
  40. public function getCapabilities() {
  41. $capabilities = [];
  42. foreach($this->capabilities as $capability) {
  43. try {
  44. $c = $capability();
  45. } catch (QueryException $e) {
  46. $this->logger->error('CapabilitiesManager: {message}', ['app' => 'core', 'message' => $e->getMessage()]);
  47. continue;
  48. }
  49. if ($c instanceof ICapability) {
  50. $capabilities = array_replace_recursive($capabilities, $c->getCapabilities());
  51. } else {
  52. throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface');
  53. }
  54. }
  55. return $capabilities;
  56. }
  57. /**
  58. * In order to improve lazy loading a closure can be registered which will be called in case
  59. * capabilities are actually requested
  60. *
  61. * $callable has to return an instance of OCP\Capabilities\ICapability
  62. *
  63. * @param \Closure $callable
  64. */
  65. public function registerCapability(\Closure $callable) {
  66. array_push($this->capabilities, $callable);
  67. }
  68. }