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 #8 from spaceonfire/development
Browse files Browse the repository at this point in the history
2.4.0
  • Loading branch information
tntrex authored Dec 19, 2020
2 parents ad6c687 + 8a4dfbb commit b5a0bb0
Show file tree
Hide file tree
Showing 33 changed files with 306 additions and 70 deletions.
41 changes: 38 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,76 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Nothing
-->

## [2.4.0] - 2020-12-19

### Added

- `spaceonfire\Container\RawValueHolder` class added and can be used for definition of concrete value.
- `null` used as default value for parameters that allows it.

### Deprecated

- Class `spaceonfire\Container\Argument\ArgumentValue` replaced with `spaceonfire\Container\RawValueHolder`. Class alias
provided for backwards compatibility, but will be removed in next major release.
- `ContainerAwareInterface::setContainer()` should not be considered to return `$this`. It will be void in next major
release.

### Fixed

- Reflection factory now does not try to instantiate abstract classes.
`spaceonfire\Container\Exception\CannotInstantiateAbstractClassException` threw instead.
- Argument resolves with default value for abstract classes when available.

## [2.3.0] - 2020-10-24

### Added

- Added priority option for containers in `spaceonfire\Container\CompositeContainer`.

## [2.2.0] - 2020-10-22

### Deprecated
- Class `spaceonfire\Container\ContainerChain` renamed to `spaceonfire\Container\CompositeContainer`.
This name clearly describes what this class does and just fits best.
Class alias provided for backwards compatibility, but will be removed in next major release.

- Class `spaceonfire\Container\ContainerChain` renamed to `spaceonfire\Container\CompositeContainer`. This name clearly
describes what this class does and just fits best. Class alias provided for backwards compatibility, but will be
removed in next major release.

## [2.1.1] - 2020-09-26

### Fixed

- Development config updates
- Fix PhpDoc comment in service provider aggregate

## [2.1.0] - 2020-09-23

### Added

- Support definition tags

## [2.0.1] - 2020-06-21

### Fixed

- Resolve definition in `Container` class using parent container

## [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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.3-dev"
"dev-master": "2.4-dev"
}
},
"config": {
Expand Down
14 changes: 7 additions & 7 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use SlevomatCodingStandard\Sniffs\Functions\UnusedInheritedVariablePassedToClosureSniff;
use SlevomatCodingStandard\Sniffs\Functions\UnusedParameterSniff;
use SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
Expand All @@ -29,16 +28,17 @@

$parameters->set('skip', [
'Unused variable $_.' => null,
'Unused parameter $_.' => null,
UnusedParameterSniff::class => [
__DIR__ . '/src/ReflectionContainer.php',
],
]);

$services = $containerConfigurator->services();

$services->set(LineLengthFixer::class);
$services->set(LineLengthFixer::class)
->call('configure', [
[
LineLengthFixer::LINE_LENGTH => 120,
LineLengthFixer::INLINE_SHORT_LINES => false,
],
]);
$services->set(UnusedInheritedVariablePassedToClosureSniff::class);
$services->set(UnusedParameterSniff::class);
$services->set(UnusedVariableSniff::class);
};
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ parameters:
message: '/^If condition is always false\.$/'
paths:
- src/ContainerChain.php
- src/Argument/ArgumentValue.php
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<directory suffix=".php">src/</directory>
<exclude>
<file>src/ContainerChain.php</file>
<file>src/Argument/ArgumentValue.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
26 changes: 18 additions & 8 deletions src/Argument/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace spaceonfire\Container\Argument;

use spaceonfire\Container\ContainerInterface;
use spaceonfire\Container\Exception\CannotInstantiateAbstractClassException;
use spaceonfire\Container\Exception\ContainerException;
use spaceonfire\Container\RawValueHolder;

final class Argument
{
Expand All @@ -18,25 +20,25 @@ final class Argument
*/
private $className;
/**
* @var ArgumentValue|null
* @var RawValueHolder |null
*/
private $defaultValue;

/**
* Argument constructor.
* @param string $name
* @param string|null $className
* @param ArgumentValue|null $defaultValue
* @param RawValueHolder |null $defaultValue
*/
public function __construct(string $name, ?string $className = null, ?ArgumentValue $defaultValue = null)
public function __construct(string $name, ?string $className = null, ?RawValueHolder $defaultValue = null)
{
$this->name = $name;
$this->className = $className;
$this->defaultValue = $defaultValue;
}

/**
* Getter for `name` property
* Getter for `name` property.
* @return string
*/
public function getName(): string
Expand All @@ -45,17 +47,25 @@ public function getName(): string
}

/**
* Resolve argument using container
* Resolve argument using container.
* @param ContainerInterface $container
* @return mixed
*/
public function resolve(ContainerInterface $container)
{
if ($this->className !== null && $container->has($this->className)) {
return $container->get($this->className);
if (null !== $this->className && $container->has($this->className)) {
try {
return $container->get($this->className);
} catch (CannotInstantiateAbstractClassException $exception) {
if (null !== $this->defaultValue) {
return $this->defaultValue->getValue();
}

throw $exception;
}
}

if ($this->defaultValue !== null) {
if (null !== $this->defaultValue) {
return $this->defaultValue->getValue();
}

Expand Down
21 changes: 16 additions & 5 deletions src/Argument/ArgumentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

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

final class ArgumentResolver implements ResolverInterface
Expand Down Expand Up @@ -44,12 +46,21 @@ public function resolveArguments(ReflectionFunctionAbstract $reflection, array $
continue;
}

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

$argument = new Argument($name, $class === null ? null : $class->getName(), $defaultValue);
if ($parameter->isDefaultValueAvailable()) {
$defaultValue = new RawValueHolder($parameter->getDefaultValue());
} elseif (null !== $type && $type->allowsNull()) {
$defaultValue = new RawValueHolder(null);
} else {
$defaultValue = null;
}

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

$result[$name] = $argument->resolve($this->getContainer());
} catch (Throwable $e) {
Expand Down
25 changes: 7 additions & 18 deletions src/Argument/ArgumentValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,17 @@

namespace spaceonfire\Container\Argument;

final class ArgumentValue
{
/**
* @var mixed
*/
private $value;
use spaceonfire\Container\RawValueHolder;
use function class_alias;

/**
* ArgumentValue constructor.
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}
class_alias(RawValueHolder::class, __NAMESPACE__ . '\ArgumentValue');

if (false) {
/**
* Getter for `value` property
* @return mixed
* @deprecated Will be dropped in next major release.
* Use \spaceonfire\Container\RawValueHolder instead.
*/
public function getValue()
final class ArgumentValue extends RawValueHolder
{
return $this->value;
}
}
2 changes: 1 addition & 1 deletion src/Argument/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
interface ResolverInterface extends ContainerAwareInterface
{
/**
* Resolves function arguments
* Resolve function arguments.
* @param ReflectionFunctionAbstract $reflection
* @param array<string,mixed> $arguments
* @return array<mixed>
Expand Down
3 changes: 1 addition & 2 deletions src/CompositeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* Attention: You should not extend this class because it will become final in the next major release
* after the backward compatibility aliases are removed.
*
* @package spaceonfire\Container
* @final
*/
class CompositeContainer implements ContainerWithServiceProvidersInterface, ContainerAwareInterface, IteratorAggregate
Expand Down Expand Up @@ -52,7 +51,7 @@ public function __construct(iterable $containers = [])
/**
* @inheritDoc
*/
public function setContainer(ContainerInterface $container): ContainerAwareInterface
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
foreach ($this->getIterator() as $delegate) {
Expand Down
2 changes: 1 addition & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(
/**
* @inheritDoc
*/
public function setContainer(ContainerInterface $container): ContainerAwareInterface
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
$this->argumentResolver->setContainer($container);
Expand Down
8 changes: 4 additions & 4 deletions src/ContainerAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
interface ContainerAwareInterface
{
/**
* Set a container
* Set a container.
* @param ContainerInterface $container
* @return $this|ContainerAwareInterface
* @return void|$this returning $this is deprecated for this method. It would be void in next release.
*/
public function setContainer(ContainerInterface $container): self;
public function setContainer(ContainerInterface $container);

/**
* Get the container
* Get the container.
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait ContainerAwareTrait
/**
* @inheritDoc
*/
public function setContainer(ContainerInterface $container): ContainerAwareInterface
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
return $this;
Expand Down
9 changes: 6 additions & 3 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use spaceonfire\Container\Argument\Argument;
use spaceonfire\Container\ContainerInterface;
use spaceonfire\Container\Exception\ContainerException;
use spaceonfire\Container\RawValueHolder;

final class Definition implements DefinitionInterface
{
Expand Down Expand Up @@ -128,14 +129,16 @@ public function addMethodCalls(array $methods = []): DefinitionInterface
*/
public function resolve(ContainerInterface $container)
{
if ($this->resolved !== null && $this->isShared()) {
if (null !== $this->resolved && $this->isShared()) {
return $this->resolved;
}

if (is_callable($this->concrete)) {
if ($this->concrete instanceof RawValueHolder) {
$resolved = $this->concrete->getValue();
} elseif (is_callable($this->concrete)) {
$resolved = $container->invoke($this->concrete, $this->arguments);
} elseif (is_string($this->concrete)) {
$buildAnyway = $this->abstract === $this->concrete || $this->arguments !== [];
$buildAnyway = $this->abstract === $this->concrete || [] !== $this->arguments;

$resolved = $buildAnyway
? $container->make($this->concrete, $this->arguments)
Expand Down
4 changes: 2 additions & 2 deletions src/Definition/DefinitionAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function newStatic($items): CollectionInterface
}

/**
* Returns index for definition
* Returns index for definition.
* @param DefinitionInterface $value
* @return string
*/
Expand All @@ -65,7 +65,7 @@ public function indexer(DefinitionInterface $value): string
* @inheritDoc
* @param DefinitionInterface $value
*/
public function offsetSet($_, $value): void
public function offsetSet($offset, $value): void
{
$alias = $this->indexer($value);

Expand Down
2 changes: 1 addition & 1 deletion src/Definition/DefinitionAggregateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function hasDefinition(string $id): bool;
public function getDefinition(string $id): DefinitionInterface;

/**
* Make definition
* Make definition.
* @param string $abstract
* @param mixed $concrete
* @param bool $shared
Expand Down
Loading

0 comments on commit b5a0bb0

Please sign in to comment.