Manager.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 $sorters = [];
  31. /** @var ISorter[] */
  32. protected $sorterInstances = [];
  33. /** @var IServerContainer */
  34. private $c;
  35. public function __construct(IServerContainer $container) {
  36. $this->c = $container;
  37. }
  38. public function runSorters(array $sorters, array &$sortArray, array $context) {
  39. $sorterInstances = $this->getSorters();
  40. while ($sorter = array_shift($sorters)) {
  41. if (isset($sorterInstances[$sorter])) {
  42. $sorterInstances[$sorter]->sort($sortArray, $context);
  43. } else {
  44. $this->c->getLogger()->warning('No sorter for ID "{id}", skipping', [
  45. 'app' => 'core', 'id' => $sorter
  46. ]);
  47. }
  48. }
  49. }
  50. public function registerSorter($className) {
  51. $this->sorters[] = $className;
  52. }
  53. protected function getSorters() {
  54. if (count($this->sorterInstances) === 0) {
  55. foreach ($this->sorters as $sorter) {
  56. /** @var ISorter $instance */
  57. $instance = $this->c->resolve($sorter);
  58. if (!$instance instanceof ISorter) {
  59. $this->c->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
  60. ['app' => 'core', 'class' => $sorter]);
  61. continue;
  62. }
  63. $sorterId = trim($instance->getId());
  64. if (trim($sorterId) === '') {
  65. $this->c->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
  66. ['app' => 'core', 'class' => $sorter]);
  67. continue;
  68. }
  69. $this->sorterInstances[$sorterId] = $instance;
  70. }
  71. }
  72. return $this->sorterInstances;
  73. }
  74. }