| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\DI\Definitions; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\DI\ServiceCreationException; |
| 12 |
/** |
| 13 |
* Definition of standard service. |
| 14 |
* |
| 15 |
* @property string|null $class |
| 16 |
* @property Statement $factory |
| 17 |
* @property Statement[] $setup |
| 18 |
*/ |
| 19 |
final class ServiceDefinition extends Definition |
| 20 |
{ |
| 21 |
/** @var Statement */ |
| 22 |
private $creator; |
| 23 |
/** @var Statement[] */ |
| 24 |
private $setup = []; |
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
$this->creator = new Statement(null); |
| 28 |
} |
| 29 |
/** @return static */ |
| 30 |
public function setType(?string $type) |
| 31 |
{ |
| 32 |
return parent::setType($type); |
| 33 |
} |
| 34 |
/** |
| 35 |
* Alias for setCreator() |
| 36 |
* @param string|array|Definition|Reference|Statement $factory |
| 37 |
* @return static |
| 38 |
*/ |
| 39 |
public function setFactory($factory, array $args = []) |
| 40 |
{ |
| 41 |
return $this->setCreator($factory, $args); |
| 42 |
} |
| 43 |
/** |
| 44 |
* Alias for getCreator() |
| 45 |
*/ |
| 46 |
public function getFactory() : Statement |
| 47 |
{ |
| 48 |
return $this->getCreator(); |
| 49 |
} |
| 50 |
/** |
| 51 |
* @param string|array|Definition|Reference|Statement $creator |
| 52 |
* @return static |
| 53 |
*/ |
| 54 |
public function setCreator($creator, array $args = []) |
| 55 |
{ |
| 56 |
$this->creator = $creator instanceof Statement ? $creator : new Statement($creator, $args); |
| 57 |
return $this; |
| 58 |
} |
| 59 |
public function getCreator() : Statement |
| 60 |
{ |
| 61 |
return $this->creator; |
| 62 |
} |
| 63 |
/** @return string|array|Definition|Reference|null */ |
| 64 |
public function getEntity() |
| 65 |
{ |
| 66 |
return $this->creator->getEntity(); |
| 67 |
} |
| 68 |
/** @return static */ |
| 69 |
public function setArguments(array $args = []) |
| 70 |
{ |
| 71 |
$this->creator->arguments = $args; |
| 72 |
return $this; |
| 73 |
} |
| 74 |
/** @return static */ |
| 75 |
public function setArgument($key, $value) |
| 76 |
{ |
| 77 |
$this->creator->arguments[$key] = $value; |
| 78 |
return $this; |
| 79 |
} |
| 80 |
/** |
| 81 |
* @param Statement[] $setup |
| 82 |
* @return static |
| 83 |
*/ |
| 84 |
public function setSetup(array $setup) |
| 85 |
{ |
| 86 |
foreach ($setup as &$entity) { |
| 87 |
if (!$entity instanceof Statement) { |
| 88 |
throw new \Packetery\Nette\InvalidArgumentException('Argument must be \\Packetery\\Nette\\DI\\Definitions\\Statement[].'); |
| 89 |
} |
| 90 |
$entity = $this->prependSelf($entity); |
| 91 |
} |
| 92 |
$this->setup = $setup; |
| 93 |
return $this; |
| 94 |
} |
| 95 |
/** @return Statement[] */ |
| 96 |
public function getSetup() : array |
| 97 |
{ |
| 98 |
return $this->setup; |
| 99 |
} |
| 100 |
/** |
| 101 |
* @param string|array|Definition|Reference|Statement $entity |
| 102 |
* @return static |
| 103 |
*/ |
| 104 |
public function addSetup($entity, array $args = []) |
| 105 |
{ |
| 106 |
$entity = $entity instanceof Statement ? $entity : new Statement($entity, $args); |
| 107 |
$this->setup[] = $this->prependSelf($entity); |
| 108 |
return $this; |
| 109 |
} |
| 110 |
public function resolveType(\Packetery\Nette\DI\Resolver $resolver) : void |
| 111 |
{ |
| 112 |
if (!$this->getEntity()) { |
| 113 |
if (!$this->getType()) { |
| 114 |
throw new ServiceCreationException('Factory and type are missing in definition of service.'); |
| 115 |
} |
| 116 |
$this->setCreator($this->getType(), $this->creator->arguments ?? []); |
| 117 |
} elseif (!$this->getType()) { |
| 118 |
$type = $resolver->resolveEntityType($this->creator); |
| 119 |
if (!$type) { |
| 120 |
throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory method.'); |
| 121 |
} |
| 122 |
$this->setType($type); |
| 123 |
$resolver->addDependency(new \ReflectionClass($type)); |
| 124 |
} |
| 125 |
// auto-disable autowiring for aliases |
| 126 |
if ($this->getAutowired() === \true && $this->getEntity() instanceof Reference) { |
| 127 |
$this->setAutowired(\false); |
| 128 |
} |
| 129 |
} |
| 130 |
public function complete(\Packetery\Nette\DI\Resolver $resolver) : void |
| 131 |
{ |
| 132 |
$entity = $this->creator->getEntity(); |
| 133 |
if ($entity instanceof Reference && !$this->creator->arguments && !$this->setup) { |
| 134 |
$ref = $resolver->normalizeReference($entity); |
| 135 |
$this->setCreator([new Reference(\Packetery\Nette\DI\ContainerBuilder::ThisContainer), 'getService'], [$ref->getValue()]); |
| 136 |
} |
| 137 |
$this->creator = $resolver->completeStatement($this->creator); |
| 138 |
foreach ($this->setup as &$setup) { |
| 139 |
$setup = $resolver->completeStatement($setup, \true); |
| 140 |
} |
| 141 |
} |
| 142 |
private function prependSelf(Statement $setup) : Statement |
| 143 |
{ |
| 144 |
return \is_string($setup->getEntity()) && \strpbrk($setup->getEntity(), ':@?\\') === \false ? new Statement([new Reference(Reference::Self), $setup->getEntity()], $setup->arguments) : $setup; |
| 145 |
} |
| 146 |
public function generateMethod(\Packetery\Nette\PhpGenerator\Method $method, \Packetery\Nette\DI\PhpGenerator $generator) : void |
| 147 |
{ |
| 148 |
$code = $generator->formatStatement($this->creator) . ";\n"; |
| 149 |
if (!$this->setup) { |
| 150 |
$method->setBody('return ' . $code); |
| 151 |
return; |
| 152 |
} |
| 153 |
$code = '$service = ' . $code; |
| 154 |
foreach ($this->setup as $setup) { |
| 155 |
$code .= $generator->formatStatement($setup) . ";\n"; |
| 156 |
} |
| 157 |
$code .= 'return $service;'; |
| 158 |
$method->setBody($code); |
| 159 |
} |
| 160 |
public function __clone() |
| 161 |
{ |
| 162 |
parent::__clone(); |
| 163 |
$this->creator = \unserialize(\serialize($this->creator)); |
| 164 |
$this->setup = \unserialize(\serialize($this->setup)); |
| 165 |
} |
| 166 |
} |
| 167 |
\class_exists(\Packetery\Nette\DI\ServiceDefinition::class); |
| 168 |
|