Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare for testing #3

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 1
level: 2
paths:
- src
48 changes: 42 additions & 6 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace Danu\PhpDi;

use Danu\PhpDi\Contracts\ContainerContract;
use Danu\PhpDi\Exception\ContainerException;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;

final class Container
class Container implements ContainerContract
{
/**
* @var array
*/
private array $services = [];

/**
* The container's instance.
*
Expand All @@ -27,13 +35,32 @@ final class Container
public static function instance(): static
{

if (is_null(static::$instance)) {
static::$instance = new static;
if (!isset(self::$instance)) {
self::$instance = new self();
}

return static::$instance;
return self::$instance;
}

/**
* @param string $id
* @return object
*/
public function get(string $id): object
{
if(!isset($this->services[$id])) throw ContainerException::notFoundContainer($id);

return $this->make($this->services[$id]);
}

/**
* @param string $id
* @return bool
*/
public function has(string $id): bool
{
return isset($this->services[$id]);
}

/**
* @param $class
Expand All @@ -42,10 +69,15 @@ public static function instance(): static
*/
public function make($class, array $parameters = []): mixed
{
$classReflection = new ReflectionClass($class);
try {
$classReflection = new ReflectionClass($class);
} catch (ReflectionException $e) {
throw ContainerException::classDoesNotExist($class::class);
}

$constructorParams = $classReflection->getConstructor()->getParameters();

$dependencies = $dependencies = $this->resolveParams($constructorParams, $parameters);
$dependencies = $this->resolveParams($constructorParams, $parameters);

return $classReflection->newInstance(...$dependencies);
}
Expand Down Expand Up @@ -121,4 +153,8 @@ private function __construct()
private function __clone(): void
{
}

private function __wakeup()
{
}
}
8 changes: 1 addition & 7 deletions src/Contracts/ContainerContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ interface ContainerContract
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get(string $id): mixed;

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
**
* @param string $id Identifier of the entry to look for.
*
* @return bool
Expand Down
7 changes: 6 additions & 1 deletion src/Exception/ContainerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ class ContainerException extends InternalException
* @param string $id
* @return static
*/
public static function NotFoundContainer(string $id): static
public static function notFoundContainer(string $id): static
{
return self::make("No entry was found for **this** identifier: {$id}");
}

public static function classDoesNotExist(string $className): static
{
return self::make("Class does not exist: {$className}");
}
}
Loading