| 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 |
/** |
| 12 |
* Definition used by ContainerBuilder. |
| 13 |
*/ |
| 14 |
abstract class Definition |
| 15 |
{ |
| 16 |
use \Packetery\Nette\SmartObject; |
| 17 |
/** @var string|null */ |
| 18 |
private $name; |
| 19 |
/** @var string|null class or interface name */ |
| 20 |
private $type; |
| 21 |
/** @var array */ |
| 22 |
private $tags = []; |
| 23 |
/** @var bool|string[] */ |
| 24 |
private $autowired = \true; |
| 25 |
/** @var callable|null */ |
| 26 |
private $notifier; |
| 27 |
/** |
| 28 |
* @return static |
| 29 |
* @internal This is managed by ContainerBuilder and should not be called by user |
| 30 |
*/ |
| 31 |
public final function setName(string $name) |
| 32 |
{ |
| 33 |
if ($this->name) { |
| 34 |
throw new \Packetery\Nette\InvalidStateException('Name already has been set.'); |
| 35 |
} |
| 36 |
$this->name = $name; |
| 37 |
return $this; |
| 38 |
} |
| 39 |
public final function getName() : ?string |
| 40 |
{ |
| 41 |
return $this->name; |
| 42 |
} |
| 43 |
/** @return static */ |
| 44 |
protected function setType(?string $type) |
| 45 |
{ |
| 46 |
if ($this->autowired && $this->notifier && $this->type !== $type) { |
| 47 |
($this->notifier)(); |
| 48 |
} |
| 49 |
if ($type === null) { |
| 50 |
$this->type = null; |
| 51 |
} elseif (!\class_exists($type) && !\interface_exists($type)) { |
| 52 |
throw new \Packetery\Nette\InvalidArgumentException(\sprintf("Service '%s': Class or interface '%s' not found.", $this->name, $type)); |
| 53 |
} else { |
| 54 |
$this->type = \Packetery\Nette\DI\Helpers::normalizeClass($type); |
| 55 |
} |
| 56 |
return $this; |
| 57 |
} |
| 58 |
public final function getType() : ?string |
| 59 |
{ |
| 60 |
return $this->type; |
| 61 |
} |
| 62 |
/** @return static */ |
| 63 |
public final function setTags(array $tags) |
| 64 |
{ |
| 65 |
$this->tags = $tags; |
| 66 |
return $this; |
| 67 |
} |
| 68 |
public final function getTags() : array |
| 69 |
{ |
| 70 |
return $this->tags; |
| 71 |
} |
| 72 |
/** |
| 73 |
* @param mixed $attr |
| 74 |
* @return static |
| 75 |
*/ |
| 76 |
public final function addTag(string $tag, $attr = \true) |
| 77 |
{ |
| 78 |
$this->tags[$tag] = $attr; |
| 79 |
return $this; |
| 80 |
} |
| 81 |
/** @return mixed */ |
| 82 |
public final function getTag(string $tag) |
| 83 |
{ |
| 84 |
return $this->tags[$tag] ?? null; |
| 85 |
} |
| 86 |
/** |
| 87 |
* @param bool|string|string[] $state |
| 88 |
* @return static |
| 89 |
*/ |
| 90 |
public final function setAutowired($state = \true) |
| 91 |
{ |
| 92 |
if ($this->notifier && $this->autowired !== $state) { |
| 93 |
($this->notifier)(); |
| 94 |
} |
| 95 |
$this->autowired = \is_string($state) || \is_array($state) ? (array) $state : (bool) $state; |
| 96 |
return $this; |
| 97 |
} |
| 98 |
/** @return bool|string[] */ |
| 99 |
public final function getAutowired() |
| 100 |
{ |
| 101 |
return $this->autowired; |
| 102 |
} |
| 103 |
/** @return static */ |
| 104 |
public function setExported(bool $state = \true) |
| 105 |
{ |
| 106 |
return $this->addTag('nette.exported', $state); |
| 107 |
} |
| 108 |
public function isExported() : bool |
| 109 |
{ |
| 110 |
return (bool) $this->getTag('nette.exported'); |
| 111 |
} |
| 112 |
public function __clone() |
| 113 |
{ |
| 114 |
$this->notifier = $this->name = null; |
| 115 |
} |
| 116 |
/********************* life cycle ****************d*g**/ |
| 117 |
public abstract function resolveType(\Packetery\Nette\DI\Resolver $resolver) : void; |
| 118 |
public abstract function complete(\Packetery\Nette\DI\Resolver $resolver) : void; |
| 119 |
public abstract function generateMethod(\Packetery\Nette\PhpGenerator\Method $method, \Packetery\Nette\DI\PhpGenerator $generator) : void; |
| 120 |
public final function setNotifier(?callable $notifier) : void |
| 121 |
{ |
| 122 |
$this->notifier = $notifier; |
| 123 |
} |
| 124 |
/********************* deprecated stuff from former ServiceDefinition ****************d*g**/ |
| 125 |
/** @deprecated Use setType() */ |
| 126 |
public function setClass(?string $type) |
| 127 |
{ |
| 128 |
return $this->setType($type); |
| 129 |
} |
| 130 |
/** @deprecated Use getType() */ |
| 131 |
public function getClass() : ?string |
| 132 |
{ |
| 133 |
return $this->getType(); |
| 134 |
} |
| 135 |
/** @deprecated Use '$def instanceof \Packetery\Nette\DI\Definitions\ImportedDefinition' */ |
| 136 |
public function isDynamic() : bool |
| 137 |
{ |
| 138 |
\trigger_error(\sprintf('Service %s: %s() is deprecated, use "instanceof ImportedDefinition".', $this->getName(), __METHOD__), \E_USER_DEPRECATED); |
| 139 |
return \false; |
| 140 |
} |
| 141 |
/** @deprecated Use \Packetery\Nette\DI\Definitions\FactoryDefinition or AccessorDefinition */ |
| 142 |
public function getImplement() : ?string |
| 143 |
{ |
| 144 |
\trigger_error(\sprintf('Service %s: %s() is deprecated.', $this->getName(), __METHOD__), \E_USER_DEPRECATED); |
| 145 |
return null; |
| 146 |
} |
| 147 |
/** @deprecated Use getAutowired() */ |
| 148 |
public function isAutowired() |
| 149 |
{ |
| 150 |
return $this->autowired; |
| 151 |
} |
| 152 |
} |
| 153 |
|