Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
/ container Public archive
generated from spaceonfire/skeleton

Commit

Permalink
Merge pull request #1 from spaceonfire/develop
Browse files Browse the repository at this point in the history
Split container responsibility
  • Loading branch information
tntrex authored Jun 21, 2020
2 parents c5f198b + dc8ea4f commit f8ccf9d
Show file tree
Hide file tree
Showing 19 changed files with 711 additions and 408 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Nothing
-->

## [2.0.0] - 2020-06-21
### Added
- `ArgumentResolver` class as default implementation of `ResolverInterface`
- `ReflectionFactory` class creates instance of any existing class and resolve constructor arguments
- `ReflectionInvoker` class calls any given callable with resolved arguments
- `ReflectionContainer` class acts like factory for any existing class

### Removed
- `Container` class does not manage any existing class no more
- `Container` class does not implements `ResolverInterface` no more
- Removed `AbstractContainerDecorator` class

## [1.0.0] - 2020-06-11
### Added
- First release
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
run:
start:
docker-compose up -d
docker-compose exec app bash

stop:
docker-compose down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ $ composer require spaceonfire/container
## Usage

```php
$container = new spaceonfire\Container\Container();
use spaceonfire\Container\Container;
use spaceonfire\Container\ReflectionContainer;
use spaceonfire\Container\ContainerChain;

$container = new ContainerChain([
new Container(),
new ReflectionContainer(),
]);
```

## Change log
Expand Down
2 changes: 2 additions & 0 deletions ecs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ parameters:
skip:
Unused variable $_.: ~
Unused parameter $_.: ~
SlevomatCodingStandard\Sniffs\Functions\UnusedParameterSniff.UnusedParameter:
- src/ReflectionContainer.php

services:
Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer: ~
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ parameters:
- src/
ignoreErrors:
- '/expects callable(.*) array(.*) given\.$/'
- '/expects class-string(.*)string given\.$/'
- '/no value type specified in iterable type(.*)spaceonfire\\Container\\Definition\\DefinitionAggregate/'
- '/no value type specified in iterable type(.*)spaceonfire\\Container\\ServiceProvider\\ServiceProviderAggregate/'
86 changes: 0 additions & 86 deletions src/AbstractContainerDecorator.php

This file was deleted.

72 changes: 72 additions & 0 deletions src/Argument/ArgumentResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace spaceonfire\Container\Argument;

use ReflectionFunctionAbstract;
use ReflectionMethod;
use spaceonfire\Container\ContainerAwareTrait;
use spaceonfire\Container\ContainerInterface;
use spaceonfire\Container\Exception\ContainerException;
use Throwable;

final class ArgumentResolver implements ResolverInterface
{
use ContainerAwareTrait;

/**
* ArgumentResolver constructor.
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
}

/**
* @inheritDoc
*/
public function resolveArguments(ReflectionFunctionAbstract $reflection, array $arguments = []): array
{
$result = [];

foreach ($reflection->getParameters() as $parameter) {
try {
$name = $parameter->getName();

if (array_key_exists($name, $arguments)) {
$v = $arguments[$name];
if ($v instanceof Argument) {
$v = $v->resolve($this->getContainer());
}
$result[$name] = $v;
continue;
}

$class = $parameter->getClass();
$defaultValue = $parameter->isDefaultValueAvailable()
? new ArgumentValue($parameter->getDefaultValue())
: null;

$argument = new Argument($name, $class === null ? null : $class->getName(), $defaultValue);

$result[$name] = $argument->resolve($this->getContainer());
} catch (Throwable $e) {
$location = $reflection->getName();

if ($reflection instanceof ReflectionMethod) {
$location = $reflection->getDeclaringClass()->getName() . '::' . $location;
}

throw new ContainerException(
sprintf('Unable to resolve `%s` in {%s}: %s', $parameter->getName(), $location, $e->getMessage()),
$e->getCode(),
$e
);
}
}

return array_values($result);
}
}
Loading

0 comments on commit f8ccf9d

Please sign in to comment.