Skip to content

Commit 3f20653

Browse files
kesselbskjnldsv
authored andcommitted
Decouple resource provider registration
Signed-off-by: Daniel Kesselberg <[email protected]>
1 parent 2959487 commit 3f20653

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
@@ -41,7 +41,7 @@
4141
use OCA\Files\Notification\Notifier;
4242
use OCA\Files\Service\TagService;
4343
use OCP\AppFramework\App;
44-
use OCP\Collaboration\Resources\IManager;
44+
use OCP\Collaboration\Resources\IProviderManager;
4545
use OCP\EventDispatcher\IEventDispatcher;
4646
use OCP\IContainer;
4747

@@ -92,9 +92,9 @@ public function __construct(array $urlParams=array()) {
9292
/**
9393
* Register Collaboration ResourceProvider
9494
*/
95-
/** @var IManager $resourceManager */
96-
$resourceManager = $container->query(IManager::class);
97-
$resourceManager->registerResourceProvider(ResourceProvider::class);
95+
/** @var IProviderManager $providerManager */
96+
$providerManager = $container->query(IProviderManager::class);
97+
$providerManager->registerResourceProvider(ResourceProvider::class);
9898
Listener::register($server->getEventDispatcher());
9999

100100
/** @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
@@ -35,6 +35,7 @@
3535
use OCP\Collaboration\Resources\ICollection;
3636
use OCP\Collaboration\Resources\IManager;
3737
use OCP\Collaboration\Resources\IProvider;
38+
use OCP\Collaboration\Resources\IProviderManager;
3839
use OCP\Collaboration\Resources\IResource;
3940
use OCP\Collaboration\Resources\ResourceException;
4041
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -50,17 +51,18 @@ class Manager implements IManager {
5051

5152
/** @var IDBConnection */
5253
protected $connection;
54+
/** @var IProviderManager */
55+
protected $providerManager;
5356
/** @var ILogger */
5457
protected $logger;
5558

5659
/** @var string[] */
5760
protected $providers = [];
5861

59-
/** @var IProvider[] */
60-
protected $providerInstances = [];
6162

62-
public function __construct(IDBConnection $connection, ILogger $logger) {
63+
public function __construct(IDBConnection $connection, IProviderManager $providerManager, ILogger $logger) {
6364
$this->connection = $connection;
65+
$this->providerManager = $providerManager;
6466
$this->logger = $logger;
6567
}
6668

@@ -273,27 +275,6 @@ public function getResourcesByCollectionForUser(ICollection $collection, ?IUser
273275
return $resources;
274276
}
275277

276-
/**
277-
* @return IProvider[]
278-
* @since 16.0.0
279-
*/
280-
public function getProviders(): array {
281-
if (!empty($this->providers)) {
282-
foreach ($this->providers as $provider) {
283-
try {
284-
$this->providerInstances[] = \OC::$server->query($provider);
285-
} catch (QueryException $e) {
286-
$this->logger->logException($e, [
287-
'message' => 'Error when instantiating resource provider'
288-
]);
289-
}
290-
}
291-
$this->providers = [];
292-
}
293-
294-
return $this->providerInstances;
295-
}
296-
297278
/**
298279
* Get the rich object data of a resource
299280
*
@@ -302,7 +283,7 @@ public function getProviders(): array {
302283
* @since 16.0.0
303284
*/
304285
public function getResourceRichObject(IResource $resource): array {
305-
foreach ($this->getProviders() as $provider) {
286+
foreach ($this->providerManager->getResourceProviders() as $provider) {
306287
if ($provider->getType() === $resource->getType()) {
307288
try {
308289
return $provider->getResourceRichObject($resource);
@@ -329,7 +310,7 @@ public function canAccessResource(IResource $resource, ?IUser $user): bool {
329310
}
330311

331312
$access = false;
332-
foreach ($this->getProviders() as $provider) {
313+
foreach ($this->providerManager->getResourceProviders() as $provider) {
333314
if ($provider->getType() === $resource->getType()) {
334315
try {
335316
if ($provider->canAccessResource($resource, $user)) {
@@ -532,7 +513,8 @@ public function invalidateAccessCacheForProviderByUser(IProvider $provider, ?IUs
532513
* @param string $provider
533514
*/
534515
public function registerResourceProvider(string $provider): void {
535-
$this->providers[] = $provider;
516+
$this->logger->debug('\OC\Collaboration\Resources\Manager::registerResourceProvider is deprecated', ['provider' => $provider]);
517+
$this->providerManager->registerResourceProvider($provider);
536518
}
537519

538520
/**
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 <[email protected]>
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
@@ -1086,6 +1086,7 @@ public function __construct($webRoot, \OC\Config $config) {
10861086

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

1089+
$this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, \OC\Collaboration\Resources\ProviderManager::class);
10891090
$this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class);
10901091

10911092
$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
@@ -121,6 +121,7 @@ public function getResourceForUser(string $type, string $id, ?IUser $user): IRes
121121
/**
122122
* @param string $provider
123123
* @since 16.0.0
124+
* @deprecated 18.0.0 Use IProviderManager::registerResourceProvider instead
124125
*/
125126
public function registerResourceProvider(string $provider): void;
126127
}
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 <[email protected]>
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 <[email protected]>
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)