1
0

Manager.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Collaboration\AutoComplete;
  25. use OCP\Collaboration\AutoComplete\IManager;
  26. use OCP\Collaboration\AutoComplete\ISorter;
  27. use OCP\IServerContainer;
  28. class Manager implements IManager {
  29. /** @var string[] */
  30. protected array $sorters = [];
  31. /** @var ISorter[] */
  32. protected array $sorterInstances = [];
  33. public function __construct(
  34. private IServerContainer $container,
  35. ) {
  36. }
  37. public function runSorters(array $sorters, array &$sortArray, array $context): void {
  38. $sorterInstances = $this->getSorters();
  39. while ($sorter = array_shift($sorters)) {
  40. if (isset($sorterInstances[$sorter])) {
  41. $sorterInstances[$sorter]->sort($sortArray, $context);
  42. } else {
  43. $this->container->getLogger()->warning('No sorter for ID "{id}", skipping', [
  44. 'app' => 'core', 'id' => $sorter
  45. ]);
  46. }
  47. }
  48. }
  49. public function registerSorter($className): void {
  50. $this->sorters[] = $className;
  51. }
  52. protected function getSorters(): array {
  53. if (count($this->sorterInstances) === 0) {
  54. foreach ($this->sorters as $sorter) {
  55. /** @var ISorter $instance */
  56. $instance = $this->container->resolve($sorter);
  57. if (!$instance instanceof ISorter) {
  58. $this->container->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
  59. ['app' => 'core', 'class' => $sorter]);
  60. continue;
  61. }
  62. $sorterId = trim($instance->getId());
  63. if (trim($sorterId) === '') {
  64. $this->container->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
  65. ['app' => 'core', 'class' => $sorter]);
  66. continue;
  67. }
  68. $this->sorterInstances[$sorterId] = $instance;
  69. }
  70. }
  71. return $this->sorterInstances;
  72. }
  73. }