You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

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