Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Jun 30, 2023
1 parent 3359f18 commit 943d7c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
27 changes: 17 additions & 10 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ class Container implements ContainerInterface

protected ?ContainerInterface $delegate = null;

public function __construct()
{
$this->set(ContainerInterface::class, $this);
}

/**
* @template T
*
* @param class-string<T> $id
* @param class-string<T> $id
* @return T
*
* @inheritDoc
Expand Down Expand Up @@ -107,13 +112,14 @@ public function call(callable|string|array $callable, array $arguments = []): mi
} elseif ($callable instanceof Closure || (is_string($callable) && function_exists($callable))) {
// handles closures and plain functions
$method = new ReflectionFunction($callable);
} elseif (is_string($callable) && class_exists($callable)) {
} elseif (is_callable($callable) || (is_string($callable) && class_exists($callable))) {
// handles class-string case
$callable = $this->get($callable);
}

if (is_object($callable) && method_exists($callable, '__invoke')) {
$method = new ReflectionMethod($callable, '__invoke');
if (is_string($callable) && class_exists($callable)) {
$callable = $this->get($callable);
}
if (method_exists($callable, '__invoke')) {
$method = new ReflectionMethod($callable, '__invoke');
}
}

if ($method === null) {
Expand All @@ -126,7 +132,7 @@ public function call(callable|string|array $callable, array $arguments = []): mi
}

/**
* @param string $class
* @param string $class
* @return object|string|null
*
* @throws ContainerException
Expand All @@ -149,7 +155,7 @@ protected function resolve(string $class): object|string|null
}

/**
* @param ReflectionParameter[] $parameters
* @param ReflectionParameter[] $parameters
* @return array|null[]|object[]|string[]
*
* @throws ContainerException
Expand All @@ -166,7 +172,8 @@ protected function getArguments(array $parameters, $additional = []): array

return match (true) {
$type !== null && $this->has($type) => $this->get($type), // via definitions
array_key_exists($param->getName(), $additional) => $additional[$param->getName()], // defined by the user
array_key_exists($param->getName(),
$additional) => $additional[$param->getName()], // defined by the user
!empty($positionalArgs) => array_shift($positionalArgs),
$param->isOptional() => $param->getDefaultValue(), // use default when available
$type !== null && class_exists($type) && !enum_exists($type) => $this->resolve($type), // via reflection
Expand Down
32 changes: 13 additions & 19 deletions src/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,30 @@ public function singleton(): static
return $this;
}

public function hasInstance(): bool
{
return $this->instance !== null;
}

public function getInstance(): object|null
{
return $this->instance;
}

/**
* @throws ContainerException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws NotFoundExceptionInterface|\ReflectionException
*/
public function make(ContainerInterface $container): mixed
{
if ($this->hasInstance()) {
return $this->getInstance();
if ($this->instance !== null) {
return $this->instance;
}

$resolved = $this->resolver;

if (is_callable($this->resolver)) {
// resolve the callable, if the resolver is a callable
$resolved = ($this->resolver)($container);
} elseif (is_string($resolved) && class_exists($resolved) && !enum_exists($resolved)) {
// if is a string (class concrete) and can be resolved via container
// resolve the callable, if the resolver is a callable
if (is_callable($resolved)) {
$resolved = call_user_func($resolved, $container);
}

// if is a string (class concrete) and can be resolved via container
if (is_string($resolved) && class_exists($resolved) && !enum_exists($resolved)) {
$resolved = $container->get($resolved);
} else {
}

if (!is_object($resolved)) {
throw ContainerException::invalidDefinition($this->id);
}

Expand Down

0 comments on commit 943d7c4

Please sign in to comment.