chore: lots of changes
This commit is contained in:
47
src/Framework/DI/Container.php
Normal file
47
src/Framework/DI/Container.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\DI;
|
||||
|
||||
use App\Framework\Attributes\Singleton;
|
||||
|
||||
class Container
|
||||
{
|
||||
private array $singletons = [];
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user