2017-08-30 04:56:02 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-08-30 04:56:02 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Collaboration\AutoComplete;
|
|
|
|
|
|
|
|
|
|
use OCP\Collaboration\AutoComplete\IManager;
|
|
|
|
|
use OCP\Collaboration\AutoComplete\ISorter;
|
|
|
|
|
use OCP\IServerContainer;
|
|
|
|
|
|
|
|
|
|
class Manager implements IManager {
|
|
|
|
|
/** @var string[] */
|
2023-07-03 03:36:55 -04:00
|
|
|
protected array $sorters = [];
|
2017-08-30 04:56:02 -04:00
|
|
|
|
|
|
|
|
/** @var ISorter[] */
|
2023-07-03 03:36:55 -04:00
|
|
|
protected array $sorterInstances = [];
|
2017-08-30 04:56:02 -04:00
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private IServerContainer $container,
|
|
|
|
|
) {
|
2017-08-30 04:56:02 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
public function runSorters(array $sorters, array &$sortArray, array $context): void {
|
2017-08-30 04:56:02 -04:00
|
|
|
$sorterInstances = $this->getSorters();
|
2020-04-10 08:19:56 -04:00
|
|
|
while ($sorter = array_shift($sorters)) {
|
|
|
|
|
if (isset($sorterInstances[$sorter])) {
|
2017-08-30 04:56:02 -04:00
|
|
|
$sorterInstances[$sorter]->sort($sortArray, $context);
|
|
|
|
|
} else {
|
2023-07-03 03:36:55 -04:00
|
|
|
$this->container->getLogger()->warning('No sorter for ID "{id}", skipping', [
|
2017-08-30 04:56:02 -04:00
|
|
|
'app' => 'core', 'id' => $sorter
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
public function registerSorter($className): void {
|
2017-10-26 10:23:41 -04:00
|
|
|
$this->sorters[] = $className;
|
2017-08-30 04:56:02 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
protected function getSorters(): array {
|
2020-04-10 08:19:56 -04:00
|
|
|
if (count($this->sorterInstances) === 0) {
|
2017-08-30 04:56:02 -04:00
|
|
|
foreach ($this->sorters as $sorter) {
|
|
|
|
|
/** @var ISorter $instance */
|
2023-07-03 03:36:55 -04:00
|
|
|
$instance = $this->container->resolve($sorter);
|
2020-04-10 08:19:56 -04:00
|
|
|
if (!$instance instanceof ISorter) {
|
2023-07-03 03:36:55 -04:00
|
|
|
$this->container->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
|
2017-08-30 04:56:02 -04:00
|
|
|
['app' => 'core', 'class' => $sorter]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$sorterId = trim($instance->getId());
|
2020-04-10 08:19:56 -04:00
|
|
|
if (trim($sorterId) === '') {
|
2023-07-03 03:36:55 -04:00
|
|
|
$this->container->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
|
2017-08-30 04:56:02 -04:00
|
|
|
['app' => 'core', 'class' => $sorter]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->sorterInstances[$sorterId] = $instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->sorterInstances;
|
|
|
|
|
}
|
|
|
|
|
}
|