-
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.
Add league/container, event-manager to composer and mozart (#782)
- Loading branch information
Showing
33 changed files
with
2,135 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
classes/Dependencies/League/Container/Argument/ArgumentResolverInterface.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
use Imagify\Dependencies\League\Container\ContainerAwareInterface; | ||
use ReflectionFunctionAbstract; | ||
|
||
interface ArgumentResolverInterface extends ContainerAwareInterface | ||
{ | ||
/** | ||
* Resolve an array of arguments to their concrete implementations. | ||
* | ||
* @param array $arguments | ||
* | ||
* @return array | ||
*/ | ||
public function resolveArguments(array $arguments) : array; | ||
|
||
/** | ||
* Resolves the correct arguments to be passed to a method. | ||
* | ||
* @param ReflectionFunctionAbstract $method | ||
* @param array $args | ||
* | ||
* @return array | ||
*/ | ||
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array; | ||
} |
120 changes: 120 additions & 0 deletions
120
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
use Imagify\Dependencies\League\Container\Container; | ||
use Imagify\Dependencies\League\Container\Exception\{ContainerException, NotFoundException}; | ||
use Imagify\Dependencies\League\Container\ReflectionContainer; | ||
use Imagify\Dependencies\Psr\Container\ContainerInterface; | ||
use ReflectionFunctionAbstract; | ||
use ReflectionParameter; | ||
|
||
trait ArgumentResolverTrait | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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; | ||
} | ||
|
||
$container = null; | ||
|
||
try { | ||
$container = $this->getLeagueContainer(); | ||
} catch (ContainerException $e) { | ||
if ($this instanceof ReflectionContainer) { | ||
$container = $this; | ||
} | ||
} | ||
|
||
if ($container !== null) { | ||
try { | ||
return $container->get($id); | ||
} catch (NotFoundException $exception) { | ||
if ($argument instanceof ClassNameWithOptionalValue) { | ||
return $argument->getOptionalValue(); | ||
} | ||
|
||
if ($justStringValue) { | ||
return $id; | ||
} | ||
|
||
throw $exception; | ||
} | ||
} | ||
|
||
if ($argument instanceof ClassNameWithOptionalValue) { | ||
return $argument->getOptionalValue(); | ||
} | ||
|
||
// Just a string value. | ||
return $id; | ||
}, $arguments); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array | ||
{ | ||
$arguments = array_map(function (ReflectionParameter $param) use ($method, $args) { | ||
$name = $param->getName(); | ||
$type = $param->getType(); | ||
|
||
if (array_key_exists($name, $args)) { | ||
return new RawArgument($args[$name]); | ||
} | ||
|
||
if ($type) { | ||
if (PHP_VERSION_ID >= 70100) { | ||
$typeName = $type->getName(); | ||
} else { | ||
$typeName = (string) $type; | ||
} | ||
|
||
$typeName = ltrim($typeName, '?'); | ||
|
||
if ($param->isDefaultValueAvailable()) { | ||
return new ClassNameWithOptionalValue($typeName, $param->getDefaultValue()); | ||
} | ||
|
||
return new ClassName($typeName); | ||
} | ||
|
||
if ($param->isDefaultValueAvailable()) { | ||
return new RawArgument($param->getDefaultValue()); | ||
} | ||
|
||
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; | ||
} |
29 changes: 29 additions & 0 deletions
29
classes/Dependencies/League/Container/Argument/ClassName.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
class ClassName implements ClassNameInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $value; | ||
|
||
/** | ||
* Construct. | ||
* | ||
* @param string $value | ||
*/ | ||
public function __construct(string $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getClassName() : string | ||
{ | ||
return $this->value; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
classes/Dependencies/League/Container/Argument/ClassNameInterface.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
interface ClassNameInterface | ||
{ | ||
/** | ||
* Return the class name. | ||
* | ||
* @return string | ||
*/ | ||
public function getClassName() : string; | ||
} |
39 changes: 39 additions & 0 deletions
39
classes/Dependencies/League/Container/Argument/ClassNameWithOptionalValue.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
class ClassNameWithOptionalValue implements ClassNameInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $className; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $optionalValue; | ||
|
||
/** | ||
* @param string $className | ||
* @param mixed $optionalValue | ||
*/ | ||
public function __construct(string $className, $optionalValue) | ||
{ | ||
$this->className = $className; | ||
$this->optionalValue = $optionalValue; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getClassName(): string | ||
{ | ||
return $this->className; | ||
} | ||
|
||
public function getOptionalValue() | ||
{ | ||
return $this->optionalValue; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
classes/Dependencies/League/Container/Argument/RawArgument.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
class RawArgument implements RawArgumentInterface | ||
{ | ||
/** | ||
* @var mixed | ||
*/ | ||
protected $value; | ||
|
||
/** | ||
* Construct. | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function __construct($value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
classes/Dependencies/League/Container/Argument/RawArgumentInterface.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Imagify\Dependencies\League\Container\Argument; | ||
|
||
interface RawArgumentInterface | ||
{ | ||
/** | ||
* Return the value of the raw argument. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getValue(); | ||
} |
Oops, something went wrong.