Skip to content

Commit

Permalink
Backport #415
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Mar 9, 2024
1 parent c56f4f6 commit 0ff769f
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Queue/ServicesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@
namespace Queue\Queue;

use Cake\Core\ContainerInterface;
use LogicException;

trait ServicesTrait {

/**
* @var \Cake\Core\ContainerInterface
* @var \Cake\Core\ContainerInterface|null
*/
protected ContainerInterface $container;
protected ?ContainerInterface $container = null;

/**
* @param string $id Classname or identifier of the service you want to retrieve
* @template T
*
* @param class-string<T> $id Classname or identifier of the service you want to retrieve
*
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*
* @return mixed
* @return T
*/
protected function getService(string $id): mixed {
protected function getService(string $id) {
if ($this->container === null) {
throw new LogicException(
"The Container has not been set. Hint: getService() must not be called in the Task's constructor.",
);
}

return $this->container->get($id);
}

Expand Down

0 comments on commit 0ff769f

Please sign in to comment.