slickframework /
di
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | /** |
||||||
| 4 | * This file is part of slick/di package |
||||||
| 5 | * |
||||||
| 6 | * For the full copyright and license information, please view the LICENSE.md |
||||||
| 7 | * file that was distributed with this source code. |
||||||
| 8 | */ |
||||||
| 9 | |||||||
| 10 | namespace Slick\Di; |
||||||
| 11 | |||||||
| 12 | use Exception; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 13 | use Slick\Di\DefinitionLoader\DirectoryDefinitionLoader; |
||||||
| 14 | |||||||
| 15 | /** |
||||||
| 16 | * Container Builder |
||||||
| 17 | * |
||||||
| 18 | * @package Slick\Di |
||||||
| 19 | * @author Filipe Silva <[email protected]> |
||||||
| 20 | */ |
||||||
| 21 | final class ContainerBuilder implements ContainerBuilderInterface |
||||||
| 22 | { |
||||||
| 23 | |||||||
| 24 | private ?ContainerInterface $container = null; |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * @var array|string |
||||||
| 28 | */ |
||||||
| 29 | private string|array $definitions = []; |
||||||
| 30 | |||||||
| 31 | /** |
||||||
| 32 | * @throws Exception|\Slick\Di\Exception |
||||||
| 33 | */ |
||||||
| 34 | public function __construct($definitions = []) |
||||||
| 35 | { |
||||||
| 36 | if (is_array($definitions)) { |
||||||
| 37 | $this->definitions = $definitions; |
||||||
| 38 | return; |
||||||
| 39 | } |
||||||
| 40 | |||||||
| 41 | $this->load(new DirectoryDefinitionLoader($definitions)); |
||||||
| 42 | } |
||||||
| 43 | |||||||
| 44 | /** |
||||||
| 45 | * @inheritdoc |
||||||
| 46 | * @throws Exception |
||||||
| 47 | */ |
||||||
| 48 | public function load(DefinitionLoaderInterface $loader): ContainerBuilder |
||||||
| 49 | { |
||||||
| 50 | if ($loader instanceof ContainerAwareInterface) { |
||||||
| 51 | $loader->setContainer($this->getContainer()); |
||||||
| 52 | } |
||||||
| 53 | $definitions = (array) $loader->getIterator(); |
||||||
| 54 | $this->definitions = array_merge($this->definitions, $definitions); |
||||||
|
0 ignored issues
–
show
It seems like
$this->definitions can also be of type string; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 55 | $this->setContainer(new Container()); |
||||||
| 56 | return $this; |
||||||
| 57 | } |
||||||
| 58 | 8 | ||||||
| 59 | /** |
||||||
| 60 | 8 | * Get current container |
|||||
| 61 | 8 | * |
|||||
| 62 | 8 | * If no container was created a new, empty container will be created. |
|||||
| 63 | * |
||||||
| 64 | * @return ContainerInterface |
||||||
| 65 | */ |
||||||
| 66 | public function getContainer(): ContainerInterface |
||||||
| 67 | { |
||||||
| 68 | if (!$this->container) { |
||||||
| 69 | 6 | $this->setContainer(new Container()); |
|||||
| 70 | } |
||||||
| 71 | 6 | return $this->container; |
|||||
| 72 | 6 | } |
|||||
| 73 | 6 | ||||||
| 74 | 6 | ||||||
| 75 | 6 | /** |
|||||
| 76 | * Set the dependency container |
||||||
| 77 | * |
||||||
| 78 | * @param ContainerInterface $container |
||||||
| 79 | * |
||||||
| 80 | * @return ContainerBuilder |
||||||
| 81 | 6 | */ |
|||||
| 82 | public function setContainer(ContainerInterface $container): ContainerBuilder |
||||||
| 83 | 6 | { |
|||||
| 84 | $this->container = $container; |
||||||
| 85 | 6 | $this->hydrateContainer($this->definitions); |
|||||
|
0 ignored issues
–
show
It seems like
$this->definitions can also be of type string; however, parameter $definitions of Slick\Di\ContainerBuilder::hydrateContainer() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 86 | 6 | return $this; |
|||||
| 87 | 6 | } |
|||||
| 88 | 6 | ||||||
| 89 | 6 | /** |
|||||
| 90 | * Hydrates the container with provided definitions |
||||||
| 91 | 6 | * |
|||||
| 92 | 2 | * @param array $definitions |
|||||
| 93 | 2 | */ |
|||||
| 94 | 6 | protected function hydrateContainer(array $definitions): void |
|||||
| 95 | 6 | { |
|||||
| 96 | foreach ($definitions as $name => $definition) { |
||||||
| 97 | $this->container->register($name, $definition); |
||||||
|
0 ignored issues
–
show
The method
register() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 98 | } |
||||||
| 99 | } |
||||||
| 100 | } |
||||||
| 101 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: