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 Apr 28, 2024
1 parent 6938188 commit 0a1f0ab
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 @@ -121,6 +121,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 @@ -201,6 +201,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 @@ -460,9 +476,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])) { // @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 0a1f0ab

Please sign in to comment.