Skip to content

Commit

Permalink
feat: add helper class to integrate with the container
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <ferywardiyanto@gmail.com>
  • Loading branch information
feryardiant committed Jun 28, 2020
1 parent 0b86b4f commit 249b35a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 34 deletions.
17 changes: 7 additions & 10 deletions src/Container/ArrayContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

namespace Projek\Container;

final class ArrayContainer implements \ArrayAccess
final class ArrayContainer implements \ArrayAccess, ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
use ContainerAware;

/**
* Create new instance.
Expand All @@ -18,7 +15,7 @@ final class ArrayContainer implements \ArrayAccess
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->setContainer($container);
}

/**
Expand All @@ -28,7 +25,7 @@ public function __construct(ContainerInterface $container)
*/
public function offsetSet($name, $instance)
{
$this->container->set($name, $instance);
$this->getContainer()->set($name, $instance);
}

/**
Expand All @@ -38,7 +35,7 @@ public function offsetSet($name, $instance)
public function offsetGet($name)
{
try {
return $this->container->get($name);
return $this->getContainer($name);
} catch (NotFoundException $e) {
return null;
}
Expand All @@ -50,7 +47,7 @@ public function offsetGet($name)
*/
public function offsetExists($name)
{
return $this->container->has($name);
return $this->getContainer()->has($name);
}

/**
Expand All @@ -59,6 +56,6 @@ public function offsetExists($name)
*/
public function offsetUnset($name)
{
$this->container->unset($name);
$this->getContainer()->unset($name);
}
}
33 changes: 33 additions & 0 deletions src/Container/ContainerAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Projek\Container;

trait ContainerAware
{
/**
* @var ContainerInterface
*/
private $container = null;

/**
* @see ContainerAwareInterface::setContainer()
*/
public function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}

/**
* @see ContainerAwareInterface::getContainer()
*/
public function getContainer(?string $name = null)
{
if ($name) {
return $this->container->get($name);
}

return $this->container;
}
}
24 changes: 24 additions & 0 deletions src/Container/ContainerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Projek\Container;

interface ContainerAwareInterface
{
/**
* Assign a container to the instance.
*
* @param ContainerInterface $container
* @return void
*/
public function setContainer(ContainerInterface $container): void;

/**
* Get container instance or the instance of registered service.
*
* @param null|string $name Optionally pass a container name, if needed.
* @return null|mixed|ContainerInterface
*/
public function getContainer(?string $name = null);
}
17 changes: 7 additions & 10 deletions src/Container/PropertyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

namespace Projek\Container;

final class PropertyContainer
final class PropertyContainer implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
use ContainerAware;

/**
* Create new instance.
Expand All @@ -18,7 +15,7 @@ final class PropertyContainer
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->setContainer($container);
}

/**
Expand All @@ -28,7 +25,7 @@ public function __construct(ContainerInterface $container)
*/
public function __set(string $name, $instance): void
{
$this->container->set($name, $instance);
$this->getContainer()->set($name, $instance);
}

/**
Expand All @@ -38,7 +35,7 @@ public function __set(string $name, $instance): void
public function __get(string $name)
{
try {
return $this->container->get($name);
return $this->getContainer($name);
} catch (NotFoundException $e) {
return null;
}
Expand All @@ -50,7 +47,7 @@ public function __get(string $name)
*/
public function __isset(string $name): bool
{
return $this->container->has($name);
return $this->getContainer()->has($name);
}

/**
Expand All @@ -59,6 +56,6 @@ public function __isset(string $name): bool
*/
public function __unset(string $name): void
{
$this->container->unset($name);
$this->getContainer()->unset($name);
}
}
26 changes: 12 additions & 14 deletions src/Container/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
use Psr\Container\ContainerInterface;
use ReflectionFunction;

class Resolver
class Resolver implements ContainerAwareInterface
{
/**
* PSR 11 Container Instance.
*
* @var ContainerInterface
*/
private $container;
use ContainerAware;

/**
* Create new instance.
Expand All @@ -23,7 +18,7 @@ class Resolver
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->setContainer($container);
}

/**
Expand Down Expand Up @@ -81,15 +76,18 @@ public function resolve($concrete)
return $this->createInstance($concrete);
}

if (is_string($concrete) && $this->container->has($concrete)) {
if (is_string($concrete) && $this->getContainer()->has($concrete)) {
return $concrete;
}

if ($concrete instanceof \Closure || is_object($concrete) || is_callable($concrete)) {
return $concrete;
}

throw Exception::unresolvable($concrete);
throw new Exception(sprintf(
'Couldn\'t resolve "%s" as an instance.',
! is_string($concrete) ? gettype($concrete) : $concrete
));
}

/**
Expand All @@ -101,14 +99,14 @@ public function resolve($concrete)
*/
protected function createInstance(string $className)
{
if ($this->container->has($className)) {
return $this->container->get($className);
if ($this->getContainer()->has($className)) {
return $this->getContainer($className);
}

$reflector = new \ReflectionClass($className);

if (! $reflector->isInstantiable()) {
throw Exception::notInstantiable($className);
throw new Exception(sprintf('Target "%s" is not instantiable.', $className));
}

if ($constructor = $reflector->getConstructor()) {
Expand All @@ -134,7 +132,7 @@ protected function resolveArgs(\ReflectionFunctionAbstract $callable, array $arg
}

try {
$args[$param->getPosition()] = $this->container->get(
$args[$param->getPosition()] = $this->getContainer(
($class = $param->getClass()) ? $class->getName() : $param->getName()
);
} catch (NotFoundException $e) {
Expand Down

0 comments on commit 249b35a

Please sign in to comment.