CapabilitiesManager.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  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\IInitialStateExcludedCapability;
  31. use OCP\Capabilities\IPublicCapability;
  32. use Psr\Log\LoggerInterface;
  33. class CapabilitiesManager {
  34. /** @var \Closure[] */
  35. private $capabilities = [];
  36. /** @var LoggerInterface */
  37. private $logger;
  38. public function __construct(LoggerInterface $logger) {
  39. $this->logger = $logger;
  40. }
  41. /**
  42. * Get an array of al the capabilities that are registered at this manager
  43. *
  44. * @param bool $public get public capabilities only
  45. * @throws \InvalidArgumentException
  46. * @return array<string, mixed>
  47. */
  48. public function getCapabilities(bool $public = false, bool $initialState = false) : array {
  49. $capabilities = [];
  50. foreach ($this->capabilities as $capability) {
  51. try {
  52. $c = $capability();
  53. } catch (QueryException $e) {
  54. $this->logger->error('CapabilitiesManager', [
  55. 'exception' => $e,
  56. ]);
  57. continue;
  58. }
  59. if ($c instanceof ICapability) {
  60. if (!$public || $c instanceof IPublicCapability) {
  61. if ($initialState && ($c instanceof IInitialStateExcludedCapability)) {
  62. // Remove less important capabilities information that are expensive to query
  63. // that we would otherwise inject to every page load
  64. continue;
  65. }
  66. $capabilities = array_replace_recursive($capabilities, $c->getCapabilities());
  67. }
  68. } else {
  69. throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface');
  70. }
  71. }
  72. return $capabilities;
  73. }
  74. /**
  75. * In order to improve lazy loading a closure can be registered which will be called in case
  76. * capabilities are actually requested
  77. *
  78. * $callable has to return an instance of OCP\Capabilities\ICapability
  79. *
  80. * @param \Closure $callable
  81. */
  82. public function registerCapability(\Closure $callable) {
  83. $this->capabilities[] = $callable;
  84. }
  85. }