chore: complete update
This commit is contained in:
@@ -1,47 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\DI;
|
||||
|
||||
use App\Framework\Attributes\Singleton;
|
||||
|
||||
class Container
|
||||
interface Container
|
||||
{
|
||||
private array $singletons = [];
|
||||
public MethodInvoker $invoker {get;}
|
||||
|
||||
public function __construct(private array $definitions)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function get(string $class): object
|
||||
{
|
||||
if (isset($this->singletons[$class])) {
|
||||
return $this->singletons[$class];
|
||||
}
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
$constructor = $reflection->getConstructor();
|
||||
$dependencies = [];
|
||||
|
||||
if ($constructor !== null) {
|
||||
foreach ($constructor->getParameters() as $param) {
|
||||
$type = $param->getType();
|
||||
if (!$type || $type->isBuiltin()) {
|
||||
throw new \RuntimeException("Cannot resolve parameter {$param->getName()}");
|
||||
}
|
||||
$dependencies[] = $this->get($type->getName());
|
||||
}
|
||||
}
|
||||
|
||||
$instance = $reflection->newInstanceArgs($dependencies);
|
||||
|
||||
if ($reflection->getAttributes(Singleton::class)) {
|
||||
$this->singletons[$class] = $instance;
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
/**
|
||||
* @template T of object
|
||||
* @param class-string<T> $class
|
||||
* @return T
|
||||
*/
|
||||
public function get(string $class): object;
|
||||
public function has(string $class): bool;
|
||||
public function bind(string $abstract, callable|string|object $concrete): void;
|
||||
public function singleton(string $abstract, callable|string|object $concrete): void;
|
||||
public function instance(string $abstract, object $instance): void;
|
||||
public function forget(string $class): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user