-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
49c5593
commit 6f204e3
Showing
51 changed files
with
798 additions
and
1,098 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...ntainer/Argument/RawArgumentInterface.php → .../Container/Argument/ArgumentInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 58 additions & 67 deletions
125
classes/Dependencies/League/Container/Argument/ArgumentResolverTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,111 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
use Imagify\Dependencies\League\Container\Container; | ||
use Imagify\Dependencies\League\Container\DefinitionContainerInterface; | ||
use Imagify\Dependencies\League\Container\Exception\{ContainerException, NotFoundException}; | ||
use Imagify\Dependencies\League\Container\ReflectionContainer; | ||
use Imagify\Dependencies\Psr\Container\ContainerInterface; | ||
use ReflectionFunctionAbstract; | ||
use ReflectionParameter; | ||
use ReflectionNamedType; | ||
|
||
trait ArgumentResolverTrait | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolveArguments(array $arguments) : array | ||
public function resolveArguments(array $arguments): array | ||
{ | ||
return array_map(function ($argument) { | ||
$justStringValue = false; | ||
|
||
if ($argument instanceof RawArgumentInterface) { | ||
return $argument->getValue(); | ||
} elseif ($argument instanceof ClassNameInterface) { | ||
$id = $argument->getClassName(); | ||
} elseif (!is_string($argument)) { | ||
return $argument; | ||
} else { | ||
$justStringValue = true; | ||
$id = $argument; | ||
try { | ||
$container = $this->getContainer(); | ||
} catch (ContainerException $e) { | ||
$container = ($this instanceof ReflectionContainer) ? $this : null; | ||
} | ||
|
||
foreach ($arguments as &$arg) { | ||
// if we have a literal, we don't want to do anything more with it | ||
if ($arg instanceof LiteralArgumentInterface) { | ||
$arg = $arg->getValue(); | ||
continue; | ||
} | ||
|
||
$container = null; | ||
if ($arg instanceof ArgumentInterface) { | ||
$argValue = $arg->getValue(); | ||
} else { | ||
$argValue = $arg; | ||
} | ||
|
||
try { | ||
$container = $this->getLeagueContainer(); | ||
} catch (ContainerException $e) { | ||
if ($this instanceof ReflectionContainer) { | ||
$container = $this; | ||
} | ||
if (!is_string($argValue)) { | ||
continue; | ||
} | ||
|
||
if ($container !== null) { | ||
// resolve the argument from the container, if it happens to be another | ||
// argument wrapper, use that value | ||
if ($container instanceof ContainerInterface && $container->has($argValue)) { | ||
try { | ||
return $container->get($id); | ||
} catch (NotFoundException $exception) { | ||
if ($argument instanceof ClassNameWithOptionalValue) { | ||
return $argument->getOptionalValue(); | ||
} | ||
$arg = $container->get($argValue); | ||
|
||
if ($justStringValue) { | ||
return $id; | ||
if ($arg instanceof ArgumentInterface) { | ||
$arg = $arg->getValue(); | ||
} | ||
|
||
throw $exception; | ||
continue; | ||
} catch (NotFoundException $e) { | ||
} | ||
} | ||
|
||
if ($argument instanceof ClassNameWithOptionalValue) { | ||
return $argument->getOptionalValue(); | ||
// if we have a default value, we use that, no more resolution as | ||
// we expect a default/optional argument value to be literal | ||
if ($arg instanceof DefaultValueInterface) { | ||
$arg = $arg->getDefaultValue(); | ||
} | ||
} | ||
|
||
// Just a string value. | ||
return $id; | ||
}, $arguments); | ||
return $arguments; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array | ||
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []): array | ||
{ | ||
$arguments = array_map(function (ReflectionParameter $param) use ($method, $args) { | ||
$params = $method->getParameters(); | ||
$arguments = []; | ||
|
||
foreach ($params as $param) { | ||
$name = $param->getName(); | ||
$type = $param->getType(); | ||
|
||
// if we've been given a value for the argument, treat as literal | ||
if (array_key_exists($name, $args)) { | ||
return new RawArgument($args[$name]); | ||
$arguments[] = new LiteralArgument($args[$name]); | ||
continue; | ||
} | ||
|
||
if ($type) { | ||
if (PHP_VERSION_ID >= 70100) { | ||
$typeName = $type->getName(); | ||
} else { | ||
$typeName = (string) $type; | ||
} | ||
$type = $param->getType(); | ||
|
||
$typeName = ltrim($typeName, '?'); | ||
if ($type instanceof ReflectionNamedType) { | ||
// in PHP 8, nullable arguments have "?" prefix | ||
$typeHint = ltrim($type->getName(), '?'); | ||
|
||
if ($param->isDefaultValueAvailable()) { | ||
return new ClassNameWithOptionalValue($typeName, $param->getDefaultValue()); | ||
$arguments[] = new DefaultValueArgument($typeHint, $param->getDefaultValue()); | ||
continue; | ||
} | ||
|
||
return new ClassName($typeName); | ||
$arguments[] = new ResolvableArgument($typeHint); | ||
continue; | ||
} | ||
|
||
if ($param->isDefaultValueAvailable()) { | ||
return new RawArgument($param->getDefaultValue()); | ||
$arguments[] = new LiteralArgument($param->getDefaultValue()); | ||
continue; | ||
} | ||
|
||
throw new NotFoundException(sprintf( | ||
'Unable to resolve a value for parameter (%s) in the function/method (%s)', | ||
$name, | ||
$method->getName() | ||
)); | ||
}, $method->getParameters()); | ||
} | ||
|
||
return $this->resolveArguments($arguments); | ||
} | ||
|
||
/** | ||
* @return ContainerInterface | ||
*/ | ||
abstract public function getContainer() : ContainerInterface; | ||
|
||
/** | ||
* @return Container | ||
*/ | ||
abstract public function getLeagueContainer() : Container; | ||
abstract public function getContainer(): DefinitionContainerInterface; | ||
} |
Oops, something went wrong.