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. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Collaboration\AutoComplete;
  24. use OCP\Collaboration\AutoComplete\IManager;
  25. use OCP\Collaboration\AutoComplete\ISorter;
  26. use OCP\IServerContainer;
  27. class Manager implements IManager {
  28. /** @var string[] */
  29. protected $sorters =[];
  30. /** @var ISorter[] */
  31. protected $sorterInstances = [];
  32. /** @var IServerContainer */
  33. private $c;
  34. public function __construct(IServerContainer $container) {
  35. $this->c = $container;
  36. }
  37. public function runSorters(array $sorters, array &$sortArray, array $context) {
  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->c->getLogger()->warning('No sorter for ID "{id}", skipping', [
  44. 'app' => 'core', 'id' => $sorter
  45. ]);
  46. }
  47. }
  48. }
  49. public function registerSorter($className) {
  50. $this->sorters[] = $className;
  51. }
  52. protected function getSorters() {
  53. if(count($this->sorterInstances) === 0) {
  54. foreach ($this->sorters as $sorter) {
  55. /** @var ISorter $instance */
  56. $instance = $this->c->resolve($sorter);
  57. if(!$instance instanceof ISorter) {
  58. $this->c->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->c->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. }