Skip to content

Commit 35f24bc

Browse files
Merge pull request #58355 from nextcloud/fix/ocp/container
fix(IRegistrationContext): Use IContainer in registerService factory
2 parents f06133b + d92c2de commit 35f24bc

File tree

8 files changed

+46
-47
lines changed

8 files changed

+46
-47
lines changed

build/rector-strict.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
->withPaths([
1212
$nextcloudDir . '/build/rector-strict.php',
1313
$nextcloudDir . '/core/BackgroundJobs/ExpirePreviewsJob.php',
14+
$nextcloudDir . '/lib/public/IContainer.php',
1415
])
1516
->withPreparedSets(
1617
deadCode: true,

lib/private/AppFramework/DependencyInjection/DIContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public function has($id): bool {
326326
* @inheritDoc
327327
* @param list<class-string> $chain
328328
*/
329-
public function query(string $name, bool $autoload = true, array $chain = []) {
329+
public function query(string $name, bool $autoload = true, array $chain = []): mixed {
330330
if ($name === 'AppName' || $name === 'appName') {
331331
return $this->appName;
332332
}

lib/private/AppFramework/Utility/SimpleContainer.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private function buildClassConstructorParameters(\ReflectionMethod $constructor,
120120
* @inheritDoc
121121
* @param list<class-string> $chain
122122
*/
123-
public function resolve($name, array $chain = []) {
123+
public function resolve(string $name, array $chain = []): mixed {
124124
$baseMsg = 'Could not resolve ' . $name . '!';
125125
try {
126126
$class = new ReflectionClass($name);
@@ -140,7 +140,7 @@ public function resolve($name, array $chain = []) {
140140
* @inheritDoc
141141
* @param list<class-string> $chain
142142
*/
143-
public function query(string $name, bool $autoload = true, array $chain = []) {
143+
public function query(string $name, bool $autoload = true, array $chain = []): mixed {
144144
$name = $this->sanitizeName($name);
145145
if (isset($this->container[$name])) {
146146
return $this->container[$name];
@@ -161,15 +161,11 @@ public function query(string $name, bool $autoload = true, array $chain = []) {
161161
throw new QueryNotFoundException('Could not resolve ' . $name . '!');
162162
}
163163

164-
/**
165-
* @param string $name
166-
* @param mixed $value
167-
*/
168-
public function registerParameter($name, $value) {
164+
public function registerParameter(string $name, mixed $value): void {
169165
$this[$name] = $value;
170166
}
171167

172-
public function registerService($name, Closure $closure, $shared = true) {
168+
public function registerService(string $name, Closure $closure, bool $shared = true): void {
173169
$wrapped = function () use ($closure) {
174170
return $closure($this);
175171
};
@@ -191,7 +187,7 @@ public function registerService($name, Closure $closure, $shared = true) {
191187
* @param string $alias the alias that should be registered
192188
* @param string $target the target that should be resolved instead
193189
*/
194-
public function registerAlias($alias, $target): void {
190+
public function registerAlias(string $alias, string $target): void {
195191
$this->registerService($alias, function (ContainerInterface $container) use ($target): mixed {
196192
return $container->get($target);
197193
}, false);

lib/private/ServerContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function has($id, bool $noRecursion = false): bool {
119119
* @throws QueryException
120120
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
121121
*/
122-
public function query(string $name, bool $autoload = true, array $chain = []) {
122+
public function query(string $name, bool $autoload = true, array $chain = []): mixed {
123123
$name = $this->sanitizeName($name);
124124

125125
if (str_starts_with($name, 'OCA\\')) {

lib/public/AppFramework/Bootstrap/IRegistrationContext.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace OCP\AppFramework\Bootstrap;
1111

12-
use OC\AppFramework\Utility\SimpleContainer;
1312
use OCP\AppFramework\IAppContainer;
1413
use OCP\Authentication\TwoFactorAuth\IProvider;
1514
use OCP\Calendar\ICalendarProvider;
@@ -69,7 +68,7 @@ public function registerDashboardWidget(string $widgetClass): void;
6968
*
7069
* @param string $name
7170
* @param callable $factory
72-
* @psalm-param callable(SimpleContainer): mixed $factory
71+
* @psalm-param callable(IContainer): mixed $factory
7372
* @param bool $shared If set to true the factory result will be cached otherwise every query will call the factory again
7473
*
7574
* @return void

lib/public/IContainer.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
68
* SPDX-License-Identifier: AGPL-3.0-only
79
*/
810
// use OCP namespace for all classes that are considered public.
911
// This means that they should be used by apps instead of the internal Nextcloud classes
10-
1112
namespace OCP;
1213

1314
use Closure;
14-
use OC\AppFramework\Utility\SimpleContainer;
1515
use Psr\Container\ContainerExceptionInterface;
1616
use Psr\Container\ContainerInterface;
1717
use Psr\Container\NotFoundExceptionInterface;
@@ -22,51 +22,56 @@
2222
* IContainer is the basic interface to be used for any internal dependency injection mechanism
2323
*
2424
* @since 6.0.0
25-
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface
2625
*/
2726
interface IContainer extends ContainerInterface {
27+
/**
28+
* Finds an entry of the container by its identifier and returns it.
29+
*
30+
* @template T
31+
* @param class-string<T>|string $id Identifier of the entry to look for.
32+
*
33+
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
34+
* @throws ContainerExceptionInterface Error while retrieving the entry.
35+
*
36+
* @return ($id is class-string<T> ? T : mixed) Entry.
37+
* @since 34.0.0
38+
*/
39+
public function get(string $id);
40+
2841
/**
2942
* @template T
3043
*
3144
* If a parameter is not registered in the container try to instantiate it
3245
* by using reflection to find out how to build the class
33-
* @param string $name the class name to resolve
34-
* @psalm-param string|class-string<T> $name
35-
* @return \stdClass
36-
* @psalm-return ($name is class-string ? T : mixed)
46+
* @param class-string<T>|string $name
47+
* @return ($name is class-string<T> ? T : mixed)
3748
* @since 8.2.0
38-
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
49+
* @deprecated 20.0.0 use {@see self::get()}
3950
* @throws ContainerExceptionInterface if the class could not be found or instantiated
4051
*/
41-
public function resolve($name);
52+
public function resolve(string $name): mixed;
4253

4354
/**
4455
* Look up a service for a given name in the container.
4556
*
4657
* @template T
47-
*
48-
* @param string $name
49-
* @psalm-param string|class-string<T> $name
58+
* @param class-string<T>|string $name
5059
* @param bool $autoload Should we try to autoload the service. If we are trying to resolve built in types this makes no sense for example
51-
* @return mixed
52-
* @psalm-return ($name is class-string ? T : mixed)
60+
* @return ($name is class-string<T> ? T : mixed)
5361
* @throws ContainerExceptionInterface if the query could not be resolved
5462
* @throws NotFoundExceptionInterface if the name could not be found within the container
5563
* @since 6.0.0
56-
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
64+
* @deprecated 20.0.0 use {@see self::get()}
5765
*/
58-
public function query(string $name, bool $autoload = true);
66+
public function query(string $name, bool $autoload = true): mixed;
5967

6068
/**
6169
* A value is stored in the container with it's corresponding name
6270
*
63-
* @param string $name
64-
* @param mixed $value
65-
* @return void
6671
* @since 6.0.0
6772
* @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerParameter
6873
*/
69-
public function registerParameter($name, $value);
74+
public function registerParameter(string $name, mixed $value): void;
7075

7176
/**
7277
* A service is registered in the container where a closure is passed in which will actually
@@ -75,14 +80,11 @@ public function registerParameter($name, $value);
7580
* memory and be reused on subsequent calls.
7681
* In case the parameter is false the service will be recreated on every call.
7782
*
78-
* @param string $name
79-
* @param \Closure(SimpleContainer): mixed $closure
80-
* @param bool $shared
81-
* @return void
83+
* @param \Closure(IContainer): mixed $closure
8284
* @since 6.0.0
8385
* @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerService
8486
*/
85-
public function registerService($name, Closure $closure, $shared = true);
87+
public function registerService(string $name, Closure $closure, bool $shared = true): void;
8688

8789
/**
8890
* Shortcut for returning a service from a service under a different key,
@@ -93,5 +95,5 @@ public function registerService($name, Closure $closure, $shared = true);
9395
* @since 8.2.0
9496
* @deprecated 20.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerServiceAlias
9597
*/
96-
public function registerAlias($alias, $target);
98+
public function registerAlias(string $alias, string $target): void;
9799
}

psalm-strict.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
>
1818
<projectFiles>
1919
<file name="core/BackgroundJobs/ExpirePreviewsJob.php"/>
20+
<file name="lib/public/IContainer.php"/>
2021
<ignoreFiles>
2122
<directory name="apps/**/composer"/>
2223
<directory name="apps/**/tests"/>

tests/lib/Collaboration/Collaborators/SearchTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,22 @@ public function testSearch(
9191
->willReturnCallback(function ($class) use ($searchResult, $userPlugin, $groupPlugin, $remotePlugin, $mailPlugin) {
9292
if ($class === SearchResult::class) {
9393
return $searchResult;
94-
} elseif ($class === $userPlugin) {
94+
} elseif ($class === 'user') {
9595
return $userPlugin;
96-
} elseif ($class === $groupPlugin) {
96+
} elseif ($class === 'group') {
9797
return $groupPlugin;
98-
} elseif ($class === $remotePlugin) {
98+
} elseif ($class === 'remote') {
9999
return $remotePlugin;
100-
} elseif ($class === $mailPlugin) {
100+
} elseif ($class === 'mail') {
101101
return $mailPlugin;
102102
}
103103
return null;
104104
});
105105

106-
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => $userPlugin]);
107-
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => $groupPlugin]);
108-
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => $remotePlugin]);
109-
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => $mailPlugin]);
106+
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => 'user']);
107+
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => 'group']);
108+
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => 'remote']);
109+
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => 'mail']);
110110

111111
[$results, $moreResults] = $this->search->search($searchTerm, $shareTypes, false, $perPage, $perPage * ($page - 1));
112112

0 commit comments

Comments
 (0)