| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Imagify\Dependencies\League\Container; |
| 6 |
|
| 7 |
use BadMethodCallException; |
| 8 |
use Imagify\Dependencies\League\Container\Exception\ContainerException; |
| 9 |
|
| 10 |
trait ContainerAwareTrait |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var ?DefinitionContainerInterface |
| 14 |
*/ |
| 15 |
protected $container; |
| 16 |
|
| 17 |
public function setContainer(DefinitionContainerInterface $container): ContainerAwareInterface |
| 18 |
{ |
| 19 |
$this->container = $container; |
| 20 |
|
| 21 |
if ($this instanceof ContainerAwareInterface) { |
| 22 |
return $this; |
| 23 |
} |
| 24 |
|
| 25 |
throw new BadMethodCallException(sprintf( |
| 26 |
'Attempt to use (%s) while not implementing (%s)', |
| 27 |
ContainerAwareTrait::class, |
| 28 |
ContainerAwareInterface::class |
| 29 |
)); |
| 30 |
} |
| 31 |
|
| 32 |
public function getContainer(): DefinitionContainerInterface |
| 33 |
{ |
| 34 |
if ($this->container instanceof DefinitionContainerInterface) { |
| 35 |
return $this->container; |
| 36 |
} |
| 37 |
|
| 38 |
throw new ContainerException('No container implementation has been set.'); |
| 39 |
} |
| 40 |
} |
| 41 |
|