| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Imagify\Dependencies\League\Container\Definition; |
| 6 |
|
| 7 |
use Generator; |
| 8 |
use Imagify\Dependencies\League\Container\ContainerAwareTrait; |
| 9 |
use Imagify\Dependencies\League\Container\Exception\NotFoundException; |
| 10 |
|
| 11 |
class DefinitionAggregate implements DefinitionAggregateInterface |
| 12 |
{ |
| 13 |
use ContainerAwareTrait; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var DefinitionInterface[] |
| 17 |
*/ |
| 18 |
protected $definitions = []; |
| 19 |
|
| 20 |
public function __construct(array $definitions = []) |
| 21 |
{ |
| 22 |
$this->definitions = array_filter($definitions, static function ($definition) { |
| 23 |
return ($definition instanceof DefinitionInterface); |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
public function add(string $id, $definition): DefinitionInterface |
| 28 |
{ |
| 29 |
if (false === ($definition instanceof DefinitionInterface)) { |
| 30 |
$definition = new Definition($id, $definition); |
| 31 |
} |
| 32 |
|
| 33 |
$this->definitions[] = $definition->setAlias($id); |
| 34 |
|
| 35 |
return $definition; |
| 36 |
} |
| 37 |
|
| 38 |
public function addShared(string $id, $definition): DefinitionInterface |
| 39 |
{ |
| 40 |
$definition = $this->add($id, $definition); |
| 41 |
return $definition->setShared(true); |
| 42 |
} |
| 43 |
|
| 44 |
public function has(string $id): bool |
| 45 |
{ |
| 46 |
$id = Definition::normaliseAlias($id); |
| 47 |
|
| 48 |
foreach ($this->getIterator() as $definition) { |
| 49 |
if ($id === $definition->getAlias()) { |
| 50 |
return true; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
public function hasTag(string $tag): bool |
| 58 |
{ |
| 59 |
foreach ($this->getIterator() as $definition) { |
| 60 |
if ($definition->hasTag($tag)) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
public function getDefinition(string $id): DefinitionInterface |
| 69 |
{ |
| 70 |
$id = Definition::normaliseAlias($id); |
| 71 |
|
| 72 |
foreach ($this->getIterator() as $definition) { |
| 73 |
if ($id === $definition->getAlias()) { |
| 74 |
return $definition->setContainer($this->getContainer()); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id)); |
| 79 |
} |
| 80 |
|
| 81 |
public function resolve(string $id) |
| 82 |
{ |
| 83 |
return $this->getDefinition($id)->resolve(); |
| 84 |
} |
| 85 |
|
| 86 |
public function resolveNew(string $id) |
| 87 |
{ |
| 88 |
return $this->getDefinition($id)->resolveNew(); |
| 89 |
} |
| 90 |
|
| 91 |
public function resolveTagged(string $tag): array |
| 92 |
{ |
| 93 |
$arrayOf = []; |
| 94 |
|
| 95 |
foreach ($this->getIterator() as $definition) { |
| 96 |
if ($definition->hasTag($tag)) { |
| 97 |
$arrayOf[] = $definition->setContainer($this->getContainer())->resolve(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $arrayOf; |
| 102 |
} |
| 103 |
|
| 104 |
public function resolveTaggedNew(string $tag): array |
| 105 |
{ |
| 106 |
$arrayOf = []; |
| 107 |
|
| 108 |
foreach ($this->getIterator() as $definition) { |
| 109 |
if ($definition->hasTag($tag)) { |
| 110 |
$arrayOf[] = $definition->setContainer($this->getContainer())->resolveNew(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $arrayOf; |
| 115 |
} |
| 116 |
|
| 117 |
public function getIterator(): Generator |
| 118 |
{ |
| 119 |
yield from $this->definitions; |
| 120 |
} |
| 121 |
} |
| 122 |
|