| 1 |
<?php |
| 2 |
// phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards. |
| 3 |
|
| 4 |
declare(strict_types=1); |
| 5 |
|
| 6 |
namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Source; |
| 7 |
|
| 8 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ArrayDefinition; |
| 9 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\AutowireDefinition; |
| 10 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\DecoratorDefinition; |
| 11 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition; |
| 12 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Exception\InvalidDefinition; |
| 13 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\FactoryDefinition; |
| 14 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Helper\DefinitionHelper; |
| 15 |
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ValueDefinition; |
| 16 |
|
| 17 |
/** |
| 18 |
* Turns raw definitions/definition helpers into definitions ready |
| 19 |
* to be resolved or compiled. |
| 20 |
* |
| 21 |
* @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 22 |
*/ |
| 23 |
class DefinitionNormalizer |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @var Autowiring |
| 27 |
*/ |
| 28 |
private $autowiring; |
| 29 |
|
| 30 |
public function __construct(Autowiring $autowiring) |
| 31 |
{ |
| 32 |
$this->autowiring = $autowiring; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Normalize a definition that is *not* nested in another one. |
| 37 |
* |
| 38 |
* This is usually a definition declared at the root of a definition array. |
| 39 |
* |
| 40 |
* @param mixed $definition |
| 41 |
* @param string $name The definition name. |
| 42 |
* |
| 43 |
* @throws InvalidDefinition |
| 44 |
*/ |
| 45 |
public function normalizeRootDefinition($definition, string $name) : Definition |
| 46 |
{ |
| 47 |
if ($definition instanceof DefinitionHelper) { |
| 48 |
$definition = $definition->getDefinition($name); |
| 49 |
} elseif (is_array($definition)) { |
| 50 |
$definition = new ArrayDefinition($definition); |
| 51 |
} elseif ($definition instanceof \Closure) { |
| 52 |
$definition = new FactoryDefinition($name, $definition); |
| 53 |
} elseif (! $definition instanceof Definition) { |
| 54 |
$definition = new ValueDefinition($definition); |
| 55 |
} |
| 56 |
|
| 57 |
if ($definition instanceof AutowireDefinition) { |
| 58 |
$definition = $this->autowiring->autowire($name, $definition); |
| 59 |
} |
| 60 |
|
| 61 |
$definition->setName($name); |
| 62 |
|
| 63 |
try { |
| 64 |
$definition->replaceNestedDefinitions([$this, 'normalizeNestedDefinition']); |
| 65 |
} catch (InvalidDefinition $e) { |
| 66 |
throw InvalidDefinition::create($definition, sprintf( |
| 67 |
'Definition "%s" contains an error: %s', |
| 68 |
$definition->getName(), |
| 69 |
$e->getMessage() |
| 70 |
), $e); |
| 71 |
} |
| 72 |
|
| 73 |
return $definition; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Normalize a definition that is nested in another one. |
| 78 |
* |
| 79 |
* @param mixed $definition |
| 80 |
* @return mixed |
| 81 |
* |
| 82 |
* @throws InvalidDefinition |
| 83 |
*/ |
| 84 |
public function normalizeNestedDefinition($definition) |
| 85 |
{ |
| 86 |
$name = '<nested definition>'; |
| 87 |
|
| 88 |
if ($definition instanceof DefinitionHelper) { |
| 89 |
$definition = $definition->getDefinition($name); |
| 90 |
} elseif (is_array($definition)) { |
| 91 |
$definition = new ArrayDefinition($definition); |
| 92 |
} elseif ($definition instanceof \Closure) { |
| 93 |
$definition = new FactoryDefinition($name, $definition); |
| 94 |
} |
| 95 |
|
| 96 |
if ($definition instanceof DecoratorDefinition) { |
| 97 |
throw new InvalidDefinition('Decorators cannot be nested in another definition'); |
| 98 |
} |
| 99 |
|
| 100 |
if ($definition instanceof AutowireDefinition) { |
| 101 |
$definition = $this->autowiring->autowire($name, $definition); |
| 102 |
} |
| 103 |
|
| 104 |
if ($definition instanceof Definition) { |
| 105 |
$definition->setName($name); |
| 106 |
|
| 107 |
// Recursively traverse nested definitions |
| 108 |
$definition->replaceNestedDefinitions([$this, 'normalizeNestedDefinition']); |
| 109 |
} |
| 110 |
|
| 111 |
return $definition; |
| 112 |
} |
| 113 |
} |
| 114 |
|