enh(TextProcessing): Allow providers and task types to declare a dynamic ID instead of using className

this allows AppAPI to register anonymous classes as TextProcessing providers and task types

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
pull/41088/head
Marcel Klehr 2023-10-24 14:41:24 +07:00
parent 7a55ea7823
commit 3dcedb7900
5 changed files with 86 additions and 4 deletions

@ -28,6 +28,7 @@ namespace OC\TextProcessing;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\TextProcessing\Db\Task as DbTask;
use OCP\IConfig;
use OCP\TextProcessing\IProviderWithId;
use OCP\TextProcessing\Task;
use OCP\TextProcessing\Task as OCPTask;
use OC\TextProcessing\Db\TaskMapper;
@ -120,7 +121,12 @@ class Manager implements IManager {
$preferences = json_decode($json, true);
if (isset($preferences[$task->getType()])) {
// If a preference for this task type is set, move the preferred provider to the start
$provider = current(array_filter($providers, fn ($provider) => $provider::class === $preferences[$task->getType()]));
$provider = current(array_filter($providers, function ($provider) use ($preferences, $task) {
if ($provider instanceof IProviderWithId) {
return $provider->getId() === $preferences[$task->getType()];
}
return $provider::class === $preferences[$task->getType()];
}));
if ($provider !== false) {
$providers = array_filter($providers, fn ($p) => $p !== $provider);
array_unshift($providers, $provider);

@ -56,7 +56,7 @@ interface IProvider {
* provider handles
*
* @since 27.1.0
* @return class-string<T>
* @return class-string<T>|string
*/
public function getTaskType(): string;
}

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
*
* @author Marcel Klehr <mklehr@gmx.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCP\TextProcessing;
/**
* @since 28.0.0
*/
interface IProviderWithId extends IProvider {
/**
* The id of this provider
* @since 28.0.0
*/
public function getId(): string;
}

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
*
* @author Marcel Klehr <mklehr@gmx.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCP\TextProcessing;
/**
* @since 28.0.0
*/
interface ITaskTypeWithId {
/**
* The id of this provider
* @since 28.0.0
*/
public function getId(): string;
}

@ -29,8 +29,8 @@ namespace OCP\TextProcessing;
* This is a text processing task
* @since 27.1.0
* @psalm-template T of ITaskType
* @psalm-template S as class-string<T>
* @psalm-template P as IProvider<T>
* @psalm-template S as class-string<T> | string
* @psalm-template P as IProvider<T> | IProvider
*/
final class Task implements \JsonSerializable {
protected ?int $id = null;