1
0

Manager.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Collaboration\AutoComplete;
  7. use OCP\Collaboration\AutoComplete\IManager;
  8. use OCP\Collaboration\AutoComplete\ISorter;
  9. use OCP\IServerContainer;
  10. class Manager implements IManager {
  11. /** @var string[] */
  12. protected array $sorters = [];
  13. /** @var ISorter[] */
  14. protected array $sorterInstances = [];
  15. public function __construct(
  16. private IServerContainer $container,
  17. ) {
  18. }
  19. public function runSorters(array $sorters, array &$sortArray, array $context): void {
  20. $sorterInstances = $this->getSorters();
  21. while ($sorter = array_shift($sorters)) {
  22. if (isset($sorterInstances[$sorter])) {
  23. $sorterInstances[$sorter]->sort($sortArray, $context);
  24. } else {
  25. $this->container->getLogger()->warning('No sorter for ID "{id}", skipping', [
  26. 'app' => 'core', 'id' => $sorter
  27. ]);
  28. }
  29. }
  30. }
  31. public function registerSorter($className): void {
  32. $this->sorters[] = $className;
  33. }
  34. protected function getSorters(): array {
  35. if (count($this->sorterInstances) === 0) {
  36. foreach ($this->sorters as $sorter) {
  37. /** @var ISorter $instance */
  38. $instance = $this->container->resolve($sorter);
  39. if (!$instance instanceof ISorter) {
  40. $this->container->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
  41. ['app' => 'core', 'class' => $sorter]);
  42. continue;
  43. }
  44. $sorterId = trim($instance->getId());
  45. if (trim($sorterId) === '') {
  46. $this->container->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
  47. ['app' => 'core', 'class' => $sorter]);
  48. continue;
  49. }
  50. $this->sorterInstances[$sorterId] = $instance;
  51. }
  52. }
  53. return $this->sorterInstances;
  54. }
  55. }