Skip to content

Commit 0ff769f

Browse files
committed
Backport #415
1 parent c56f4f6 commit 0ff769f

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Queue/ServicesTrait.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,32 @@
44
namespace Queue\Queue;
55

66
use Cake\Core\ContainerInterface;
7+
use LogicException;
78

89
trait ServicesTrait {
910

1011
/**
11-
* @var \Cake\Core\ContainerInterface
12+
* @var \Cake\Core\ContainerInterface|null
1213
*/
13-
protected ContainerInterface $container;
14+
protected ?ContainerInterface $container = null;
1415

1516
/**
16-
* @param string $id Classname or identifier of the service you want to retrieve
17+
* @template T
18+
*
19+
* @param class-string<T> $id Classname or identifier of the service you want to retrieve
1720
*
18-
* @throws \Psr\Container\NotFoundExceptionInterface
1921
* @throws \Psr\Container\ContainerExceptionInterface
22+
* @throws \Psr\Container\NotFoundExceptionInterface
2023
*
21-
* @return mixed
24+
* @return T
2225
*/
23-
protected function getService(string $id): mixed {
26+
protected function getService(string $id) {
27+
if ($this->container === null) {
28+
throw new LogicException(
29+
"The Container has not been set. Hint: getService() must not be called in the Task's constructor.",
30+
);
31+
}
32+
2433
return $this->container->get($id);
2534
}
2635

0 commit comments

Comments
 (0)