Build instances of the calendar providers before using them

What we get from the registration context are only the class names. We
still have to load the classes before we can use them.

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2021-10-14 11:12:55 +02:00
parent ed533bd128
commit c75a12c154
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8
2 changed files with 44 additions and 5 deletions

View File

@ -31,6 +31,11 @@ use OCP\Calendar\ICalendar;
use OCP\Calendar\ICalendarProvider;
use OCP\Calendar\ICalendarQuery;
use OCP\Calendar\IManager;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Throwable;
use function array_map;
use function array_merge;
class Manager implements IManager {
@ -47,8 +52,18 @@ class Manager implements IManager {
/** @var Coordinator */
private $coordinator;
public function __construct(Coordinator $coordinator) {
/** @var ContainerInterface */
private $container;
/** @var LoggerInterface */
private $logger;
public function __construct(Coordinator $coordinator,
ContainerInterface $container,
LoggerInterface $logger) {
$this->coordinator = $coordinator;
$this->container = $container;
$this->logger = $logger;
}
/**
@ -159,9 +174,21 @@ class Manager implements IManager {
}
/** @var CalendarQuery $query */
$calendars = array_merge(...array_map(static function (ICalendarProvider $p) use ($query) {
return $p->getCalendars($query->getPrincipalUri(), $query->getCalendarUris());
}, $context->getCalendarProviders()));
$calendars = array_merge(
...array_map(function ($registration) use ($query) {
try {
/** @var ICalendarProvider $provider */
$provider = $this->container->get($registration->getService());
} catch (Throwable $e) {
$this->logger->error('Could not load calendar provider ' . $registration->getService() . ': ' . $e->getMessage(), [
'exception' => $e,
]);
return [];
}
return $provider->getCalendars($query->getPrincipalUri(), $query->getCalendarUris());
}, $context->getCalendarProviders())
);
$results = [];
/** @var ICalendar $calendar */

View File

@ -27,6 +27,8 @@ use OC\AppFramework\Bootstrap\Coordinator;
use OC\Calendar\Manager;
use OCP\Calendar\ICalendar;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class ManagerTest extends TestCase {
@ -34,6 +36,12 @@ class ManagerTest extends TestCase {
/** @var Coordinator|MockObject */
private $coordinator;
/** @var MockObject|ContainerInterface */
private $container;
/** @var MockObject|LoggerInterface */
private $logger;
/** @var Manager */
private $manager;
@ -41,9 +49,13 @@ class ManagerTest extends TestCase {
parent::setUp();
$this->coordinator = $this->createMock(Coordinator::class);
$this->container = $this->createMock(ContainerInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->manager = new Manager(
$this->coordinator
$this->coordinator,
$this->container,
$this->logger
);
}