Skip to content

Commit

Permalink
Change details in Container package
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 28, 2023
1 parent 47da365 commit e823052
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 116 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to `Slytherin` will be documented in this file.

### Added
- `preferred` property in integrations
- `ContainerException` in `Container`

### Fixed
- Type hinting of all classes using `PHPStan` (up to `level 9`)
Expand Down
47 changes: 14 additions & 33 deletions src/Container/AurynContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AurynContainer extends Injector implements ContainerInterface
/**
* @var array<string, mixed>
*/
protected $instances = array();
protected $items = array();

/**
* Initializes the container instance.
Expand Down Expand Up @@ -80,17 +80,19 @@ public function add($id, $concrete = null)
*/
public function get($id)
{
$entry = null;

$this->has($id) && $entry = $this->resolve($id);

if (! is_null($entry)) return $entry;
if (! $this->has($id))
{
$message = 'Alias (%s) is not being managed by the container';

$message = 'Alias (%s) is not being managed by the container';
throw new Exception\NotFoundException(sprintf($message, $id));
}

$message = (string) sprintf($message, $id);
if (array_key_exists($id, $this->items))
{
return $this->items[$id];
}

throw new Exception\NotFoundException($message);
return $this->injector->make($id);
}

/**
Expand All @@ -101,9 +103,7 @@ public function get($id)
*/
public function has($id)
{
$exists = isset($this->has[$id]) ? $this->has[$id] : false;

if (isset($this->has[$id])) return $exists;
if (array_key_exists($id, $this->has)) return $this->has[$id];

$filter = Injector::I_BINDINGS | Injector::I_DELEGATES;

Expand All @@ -115,9 +115,7 @@ public function has($id)

$definitions = array_filter($definitions);

$exists = ! empty($definitions) ?: class_exists($id);

return $exists;
return ! empty($definitions) ?: class_exists($id);
}

/**
Expand All @@ -129,25 +127,8 @@ public function has($id)
*/
public function set($id, $concrete)
{
$this->instances[$id] = $concrete;
$this->items[$id] = $concrete;

return $this;
}

/**
* Resolves the specified identifier to an instance.
*
* @param string $id
* @return mixed
*/
protected function resolve($id)
{
$entry = null;

isset($this->instances[$id]) && $entry = $this->instances[$id];

isset($entry) === false && $entry = $this->injector->make($id);

return $entry;
}
}
68 changes: 38 additions & 30 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ class Container implements ContainerInterface
public function __construct(array $instances = array(), PsrContainerInterface $container = null)
{
$this->instances = $instances;

if (! $container)
{
$container = new ReflectionContainer;
}

$this->extra = $container ?: new ReflectionContainer;
$this->extra = $container;
}

/**
Expand Down Expand Up @@ -77,18 +82,25 @@ public function alias($id, $original)
*/
public function arguments(\ReflectionFunctionAbstract $reflector, $parameters = array())
{
$arguments = array();
$items = $reflector->getParameters();

foreach ($reflector->getParameters() as $key => $parameter)
$result = array();

foreach ($items as $key => $item)
{
$argument = $this->argument($parameter);
$argument = $this->argument($item);

$name = $parameter->getName();
$name = (string) $item->getName();

$arguments[$key] = $argument ?: $parameters[$name];
if (array_key_exists($name, $parameters))
{
$result[$key] = $parameters[$name];
}

if ($argument) $result[$key] = $argument;
}

return $arguments;
return $result;
}

/**
Expand Down Expand Up @@ -155,16 +167,16 @@ public function resolve($id, ServerRequestInterface $request = null)
return $this->extra->get($id);
}

$arguments = array();
$result = array();

foreach ($constructor->getParameters() as $parameter)
{
$argument = $this->argument($parameter);

$arguments[] = $this->request($argument, $request);
$result[] = $this->handle($argument, $request);
}

return $reflection->newInstanceArgs($arguments);
return $reflection->newInstanceArgs($result);
}

/**
Expand Down Expand Up @@ -210,19 +222,15 @@ protected function argument(\ReflectionParameter $parameter)
}

/**
* Returns the manipulated ServerRequest (from middleware) to an argument.
* Handles the manipulated ServerRequest (from middleware) to an argument.
*
* @param mixed $argument
* @param \Psr\Http\Message\ServerRequestInterface|null $request
* @return mixed
*/
protected function request($argument, ServerRequestInterface $request = null)
protected function handle($argument, ServerRequestInterface $request = null)
{
$instanceof = $argument instanceof ServerRequestInterface;

$instanceof === true && $argument = $request ?: $argument;

return $argument;
return $argument instanceof ServerRequestInterface && $request ? $request : $argument;
}

/**
Expand All @@ -240,21 +248,21 @@ protected function value($name)
$object = $this->get($name);
}

if (! $object && $this->extra->has($name))
if ($object || ! $this->extra->has($name))
{
// If the identifier does not exists from extra, ---
// Try to get again from the parent container
try
{
return $this->extra->get($name);
}
catch (NotFoundExceptionInterface $error)
{
return $this->get($name);
}
// -------------------------------------------------
return $object;
}

return $object;
// If the identifier does not exists from extra, ---
// Try to get again from the parent container
try
{
return $this->extra->get($name);
}
catch (NotFoundExceptionInterface $error)
{
return $this->get($name);
}
// -------------------------------------------------
}
}
17 changes: 17 additions & 0 deletions src/Container/ContainerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rougin\Slytherin\Container;

use Psr\Container\ContainerExceptionInterface;

/**
* Container Exception
*
* A specified exception in handling errors in containers.
*
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class ContainerException extends \Exception implements ContainerExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Container/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ interface ContainerInterface extends PsrContainerInterface
* @return self
*/
public function set($id, $concrete);
}
}
8 changes: 4 additions & 4 deletions src/Container/Exception/ContainerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Rougin\Slytherin\Container\Exception;

use Psr\Container\ContainerExceptionInterface;
use Rougin\Slytherin\Container\ContainerException as BaseException;

/**
* Container Exception
*
* A specified exception in handling errors in containers.
* NOTE: To be removed in v1.0.0. Use NotFoundException instead.
* NOTE: To be removed in v1.0.0. Use Container\ContainerException instead.
*
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class ContainerException extends \Exception implements ContainerExceptionInterface
class ContainerException extends BaseException
{
}
}
2 changes: 1 addition & 1 deletion src/Container/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
*/
class NotFoundException extends BaseException
{
}
}
13 changes: 1 addition & 12 deletions src/Container/LeagueContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ public function get($id, array $arguments = array())
return parent::get($id, $arguments);
}

/**
* Returns true if the container can return an entry for the given identifier.
*
* @param string $id
* @return boolean
*/
public function has($id)
{
return parent::has($id);
}

/**
* Sets a new instance to the container.
*
Expand All @@ -56,4 +45,4 @@ public function set($id, $concrete, $share = false)

return $this;
}
}
}
2 changes: 1 addition & 1 deletion src/Container/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
*/
class NotFoundException extends \InvalidArgumentException implements NotFoundExceptionInterface
{
}
}
16 changes: 4 additions & 12 deletions src/Container/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,23 @@ public function getClass()

if (! $php8)
{
$method = array($this->param, 'getClass');

return call_user_func((array) $method);
return call_user_func(array($this->param, 'getClass'));
}

$method = array($this->param, 'getType');

$type = call_user_func((array) $method);
$type = call_user_func(array($this->param, 'getType'));

$builtIn = true;

if ($type)
{
$method = array($type, 'isBuiltin');

$builtIn = call_user_func((array) $method);
$builtIn = call_user_func(array($type, 'isBuiltin'));
}

if ($builtIn) return null;

/** @var callable */
$class = array($type, 'getName');

$class = call_user_func($class);

return new \ReflectionClass($class);
return new \ReflectionClass(call_user_func($class));
}
}
Loading

0 comments on commit e823052

Please sign in to comment.