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