Skip to content

Commit

Permalink
Resolver: added new special functions constant() and getProperty()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2023
1 parent f76f9f1 commit b24f58f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/DI/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public function formatStatement(Statement $statement): string
case $entity === 'string':
return $this->formatPhp('?::?(?, ?)', [Helpers::class, 'convertType', $arguments[0], $entity]);

case $entity === 'getProperty':
return $arguments[0] instanceof Reference
? $this->formatPhp('?->?', $arguments)
: $this->formatPhp('?::$?', $arguments);

case $entity === 'constant':
return implode('::', $arguments);

case is_string($entity): // create class
return $arguments
? $this->formatPhp("new $entity(...?:)", [$arguments])
Expand Down
20 changes: 18 additions & 2 deletions src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo
}
break;

case $entity === 'getProperty':
if (count($arguments) !== 2) {
throw new ServiceCreationException(sprintf('Function %s() expects 2 parameter, %s given.', $entity, count($arguments)));
}

break;

case $entity === 'constant':
if (count($arguments) !== 2) {
throw new ServiceCreationException(sprintf('Function %s() expects 2 parameter, %s given.', $entity, count($arguments)));
}

$arguments[0] = $this->resolveReferenceType($arguments[0]);
break;


case is_string($entity): // create class
if (!class_exists($entity)) {
throw new ServiceCreationException(sprintf("Class '%s' not found.", $entity));
Expand Down Expand Up @@ -454,9 +470,9 @@ private function convertReferences(array $arguments): array
if (!isset($pair[1])) { // @service
$val = new Reference($pair[0]);
} elseif (preg_match('#^[A-Z][a-zA-Z0-9_]*$#D', $pair[1], $m)) { // @service::CONSTANT
$val = ContainerBuilder::literal($this->resolveReferenceType(new Reference($pair[0])) . '::' . $pair[1]);
$val = new Statement('constant', [new Reference($pair[0]), $pair[1]]);
} else { // @service::property
$val = new Statement([new Reference($pair[0]), '$' . $pair[1]]);
$val = new Statement('getProperty', [new Reference($pair[0]), $pair[1]]);
}
} elseif (is_string($val) && str_starts_with($val, '@@')) { // escaped text @@
$val = substr($val, 1);
Expand Down

0 comments on commit b24f58f

Please sign in to comment.