Streamline view rendering

This commit is contained in:
Tim
2023-08-15 13:37:03 +02:00
parent 45797ba20a
commit 24683e70a8
6 changed files with 37 additions and 9 deletions

View File

@ -3,6 +3,8 @@
namespace Ardent\Undercurrent\Container;
use Ardent\Undercurrent\Http\RouteConfig;
use Ardent\Undercurrent\Logger\LogContainer;
use Ardent\Undercurrent\Logger\LoggerInterface;
use Exception;
use ReflectionClass;
use ReflectionMethod;
@ -15,12 +17,20 @@ class GenericContainer implements ContainerInterface
private array $aliases = [];
public function add(string $className, ?callable $definition = null, bool $singleton = true): self
public function add(
string $className,
?callable $definition = null,
bool $singleton = true
): self
{
if (!$definition) {
$definition = fn() => $this->autowire($className);
}
if (isset($this->definitions[$className])) {
throw new Exception(sprintf('Class %s already defined', $className));
}
$this->definitions[$className] = [
'definition' => $definition,
'singleton' => $singleton,
@ -55,6 +65,10 @@ class GenericContainer implements ContainerInterface
return $this->instances[$className];
}
$instance = $definition($this);
if ($className !== LogContainer::class) {
$logger = $this->get(LoggerInterface::class);
$logger->add(sprintf('Created singleton instance of %s', $className));
}
$this->instances[$className] = $instance;
} else {
$instance = $definition($this);