1
0

CapabilitiesManager.php 2.4 KB

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