Skip to content

Commit be54491

Browse files
committed
Decouple resource provider registration
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 109aee5 commit be54491

File tree

11 files changed

+304
-31
lines changed

11 files changed

+304
-31
lines changed

apps/files/lib/AppInfo/Application.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
use OCA\Files\Notification\Notifier;
3939
use OCA\Files\Service\TagService;
4040
use OCP\AppFramework\App;
41-
use OCP\Collaboration\Resources\IManager;
41+
use OCP\Collaboration\Resources\IProviderManager;
4242
use OCP\EventDispatcher\IEventDispatcher;
4343
use OCP\IContainer;
4444

@@ -89,9 +89,9 @@ public function __construct(array $urlParams=array()) {
8989
/**
9090
* Register Collaboration ResourceProvider
9191
*/
92-
/** @var IManager $resourceManager */
93-
$resourceManager = $container->query(IManager::class);
94-
$resourceManager->registerResourceProvider(ResourceProvider::class);
92+
/** @var IProviderManager $providerManager */
93+
$providerManager = $container->query(IProviderManager::class);
94+
$providerManager->registerResourceProvider(ResourceProvider::class);
9595
Listener::register($server->getEventDispatcher());
9696

9797
/** @var IEventDispatcher $dispatcher */

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
'OCP\\Collaboration\\Resources\\ICollection' => $baseDir . '/lib/public/Collaboration/Resources/ICollection.php',
126126
'OCP\\Collaboration\\Resources\\IManager' => $baseDir . '/lib/public/Collaboration/Resources/IManager.php',
127127
'OCP\\Collaboration\\Resources\\IProvider' => $baseDir . '/lib/public/Collaboration/Resources/IProvider.php',
128+
'OCP\\Collaboration\\Resources\\IProviderManager' => $baseDir . '/lib/public/Collaboration/Resources/IProviderManager.php',
128129
'OCP\\Collaboration\\Resources\\IResource' => $baseDir . '/lib/public/Collaboration/Resources/IResource.php',
129130
'OCP\\Collaboration\\Resources\\ResourceException' => $baseDir . '/lib/public/Collaboration/Resources/ResourceException.php',
130131
'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php',
@@ -637,6 +638,7 @@
637638
'OC\\Collaboration\\Resources\\Collection' => $baseDir . '/lib/private/Collaboration/Resources/Collection.php',
638639
'OC\\Collaboration\\Resources\\Listener' => $baseDir . '/lib/private/Collaboration/Resources/Listener.php',
639640
'OC\\Collaboration\\Resources\\Manager' => $baseDir . '/lib/private/Collaboration/Resources/Manager.php',
641+
'OC\\Collaboration\\Resources\\ProviderManager' => $baseDir . '/lib/private/Collaboration/Resources/ProviderManager.php',
640642
'OC\\Collaboration\\Resources\\Resource' => $baseDir . '/lib/private/Collaboration/Resources/Resource.php',
641643
'OC\\Color' => $baseDir . '/lib/private/Color.php',
642644
'OC\\Command\\AsyncBus' => $baseDir . '/lib/private/Command/AsyncBus.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
154154
'OCP\\Collaboration\\Resources\\ICollection' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ICollection.php',
155155
'OCP\\Collaboration\\Resources\\IManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IManager.php',
156156
'OCP\\Collaboration\\Resources\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProvider.php',
157+
'OCP\\Collaboration\\Resources\\IProviderManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProviderManager.php',
157158
'OCP\\Collaboration\\Resources\\IResource' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IResource.php',
158159
'OCP\\Collaboration\\Resources\\ResourceException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ResourceException.php',
159160
'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php',
@@ -666,6 +667,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
666667
'OC\\Collaboration\\Resources\\Collection' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Collection.php',
667668
'OC\\Collaboration\\Resources\\Listener' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Listener.php',
668669
'OC\\Collaboration\\Resources\\Manager' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Manager.php',
670+
'OC\\Collaboration\\Resources\\ProviderManager' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/ProviderManager.php',
669671
'OC\\Collaboration\\Resources\\Resource' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Resource.php',
670672
'OC\\Color' => __DIR__ . '/../../..' . '/lib/private/Color.php',
671673
'OC\\Command\\AsyncBus' => __DIR__ . '/../../..' . '/lib/private/Command/AsyncBus.php',

lib/private/AppFramework/DependencyInjection/DIContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser
290290
return $dispatcher;
291291
});
292292

293+
$this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, OC\Collaboration\Resources\ProviderManager::class);
293294
$this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class);
294295
}
295296

lib/private/Collaboration/Resources/Manager.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCP\Collaboration\Resources\ICollection;
3030
use OCP\Collaboration\Resources\IManager;
3131
use OCP\Collaboration\Resources\IProvider;
32+
use OCP\Collaboration\Resources\IProviderManager;
3233
use OCP\Collaboration\Resources\IResource;
3334
use OCP\Collaboration\Resources\ResourceException;
3435
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -44,17 +45,18 @@ class Manager implements IManager {
4445

4546
/** @var IDBConnection */
4647
protected $connection;
48+
/** @var IProviderManager */
49+
protected $providerManager;
4750
/** @var ILogger */
4851
protected $logger;
4952

5053
/** @var string[] */
5154
protected $providers = [];
5255

53-
/** @var IProvider[] */
54-
protected $providerInstances = [];
5556

56-
public function __construct(IDBConnection $connection, ILogger $logger) {
57+
public function __construct(IDBConnection $connection, IProviderManager $providerManager, ILogger $logger) {
5758
$this->connection = $connection;
59+
$this->providerManager = $providerManager;
5860
$this->logger = $logger;
5961
}
6062

@@ -267,27 +269,6 @@ public function getResourcesByCollectionForUser(ICollection $collection, ?IUser
267269
return $resources;
268270
}
269271

270-
/**
271-
* @return IProvider[]
272-
* @since 16.0.0
273-
*/
274-
public function getProviders(): array {
275-
if (!empty($this->providers)) {
276-
foreach ($this->providers as $provider) {
277-
try {
278-
$this->providerInstances[] = \OC::$server->query($provider);
279-
} catch (QueryException $e) {
280-
$this->logger->logException($e, [
281-
'message' => 'Error when instantiating resource provider'
282-
]);
283-
}
284-
}
285-
$this->providers = [];
286-
}
287-
288-
return $this->providerInstances;
289-
}
290-
291272
/**
292273
* Get the rich object data of a resource
293274
*
@@ -296,7 +277,7 @@ public function getProviders(): array {
296277
* @since 16.0.0
297278
*/
298279
public function getResourceRichObject(IResource $resource): array {
299-
foreach ($this->getProviders() as $provider) {
280+
foreach ($this->providerManager->getResourceProviders() as $provider) {
300281
if ($provider->getType() === $resource->getType()) {
301282
try {
302283
return $provider->getResourceRichObject($resource);
@@ -323,7 +304,7 @@ public function canAccessResource(IResource $resource, ?IUser $user): bool {
323304
}
324305

325306
$access = false;
326-
foreach ($this->getProviders() as $provider) {
307+
foreach ($this->providerManager->getResourceProviders() as $provider) {
327308
if ($provider->getType() === $resource->getType()) {
328309
try {
329310
if ($provider->canAccessResource($resource, $user)) {
@@ -526,7 +507,8 @@ public function invalidateAccessCacheForProviderByUser(IProvider $provider, ?IUs
526507
* @param string $provider
527508
*/
528509
public function registerResourceProvider(string $provider): void {
529-
$this->providers[] = $provider;
510+
$this->logger->debug('\OC\Collaboration\Resources\Manager::registerResourceProvider is deprecated', ['provider' => $provider]);
511+
$this->providerManager->registerResourceProvider($provider);
530512
}
531513

532514
/**
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019 Daniel Kesselberg <mail@danielkesselberg.de>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace OC\Collaboration\Resources;
24+
25+
use OCP\AppFramework\QueryException;
26+
use OCP\Collaboration\Resources\IProvider;
27+
use OCP\Collaboration\Resources\IProviderManager;
28+
use OCP\ILogger;
29+
use OCP\IServerContainer;
30+
31+
class ProviderManager implements IProviderManager {
32+
33+
/** @var string[] */
34+
protected $providers = [];
35+
36+
/** @var IProvider[] */
37+
protected $providerInstances = [];
38+
39+
/** @var IServerContainer */
40+
protected $serverContainer;
41+
42+
/** @var ILogger */
43+
protected $logger;
44+
45+
public function __construct(IServerContainer $serverContainer, ILogger $logger) {
46+
$this->serverContainer = $serverContainer;
47+
$this->logger = $logger;
48+
}
49+
50+
public function getResourceProviders(): array {
51+
if ($this->providers !== []) {
52+
foreach ($this->providers as $provider) {
53+
try {
54+
$this->providerInstances[] = $this->serverContainer->query($provider);
55+
} catch (QueryException $e) {
56+
$this->logger->logException($e, [
57+
'message' => "Could not query resource provider $provider: " . $e->getMessage()
58+
]);
59+
}
60+
}
61+
$this->providers = [];
62+
}
63+
64+
return $this->providerInstances;
65+
}
66+
67+
public function registerResourceProvider(string $provider): void {
68+
$this->providers[] = $provider;
69+
}
70+
}

lib/private/Server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ public function __construct($webRoot, \OC\Config $config) {
10811081

10821082
$this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class);
10831083

1084+
$this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, \OC\Collaboration\Resources\ProviderManager::class);
10841085
$this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class);
10851086

10861087
$this->registerService('SettingsManager', function (Server $c) {

lib/public/Collaboration/Resources/IManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function getResourceForUser(string $type, string $id, ?IUser $user): IRes
116116
/**
117117
* @param string $provider
118118
* @since 16.0.0
119+
* @deprecated 18.0.0 Use IProviderManager::registerResourceProvider instead
119120
*/
120121
public function registerResourceProvider(string $provider): void;
121122
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019 Daniel Kesselberg <mail@danielkesselberg.de>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace OCP\Collaboration\Resources;
24+
25+
/**
26+
* @since 18.0.0
27+
*/
28+
interface IProviderManager {
29+
30+
/**
31+
* @return IProvider[] list of resource providers
32+
* @since 18.0.0
33+
*/
34+
public function getResourceProviders(): array;
35+
36+
/**
37+
* @param string $provider provider's class name
38+
* @since 18.0.0
39+
*/
40+
public function registerResourceProvider(string $provider): void;
41+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019 Daniel Kesselberg <mail@danielkesselberg.de>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace Test\Collaboration\Resources;
24+
25+
use OC\Collaboration\Resources\Manager;
26+
use OCP\Collaboration\Resources\IManager;
27+
use OCP\Collaboration\Resources\IProviderManager;
28+
use OCP\IDBConnection;
29+
use OCP\ILogger;
30+
use Test\TestCase;
31+
32+
class ManagerTest extends TestCase {
33+
34+
/** @var ILogger */
35+
protected $logger;
36+
/** @var IProviderManager */
37+
protected $providerManager;
38+
/** @var IManager */
39+
protected $manager;
40+
41+
protected function setUp(): void {
42+
parent::setUp();
43+
44+
$this->logger = $this->createMock(ILogger::class);
45+
$this->providerManager = $this->createMock(IProviderManager::class);
46+
47+
/** @var IDBConnection $connection */
48+
$connection = $this->createMock(IDBConnection::class);
49+
$this->manager = new Manager($connection, $this->providerManager, $this->logger);
50+
}
51+
52+
public function testRegisterResourceProvider(): void {
53+
$this->logger->expects($this->once())
54+
->method('debug')
55+
->with($this->equalTo('\OC\Collaboration\Resources\Manager::registerResourceProvider is deprecated'), $this->equalTo(['provider' => 'AwesomeResourceProvider']));
56+
$this->providerManager->expects($this->once())
57+
->method('registerResourceProvider')
58+
->with($this->equalTo('AwesomeResourceProvider'));
59+
60+
$this->manager->registerResourceProvider('AwesomeResourceProvider');
61+
}
62+
}

0 commit comments

Comments
 (0)