2026-03-22 02:40:42 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Pronchev\Pinecore;
|
|
|
|
|
|
|
|
|
|
use DI\Container;
|
|
|
|
|
use DI\ContainerBuilder;
|
2026-04-06 16:00:19 +03:00
|
|
|
use Pronchev\Pinecore\Http\Router;
|
2026-03-22 02:40:42 +03:00
|
|
|
|
|
|
|
|
class ContainerFactory
|
|
|
|
|
{
|
|
|
|
|
public static function build(Environment $env, Config $config, string $basePath): Container
|
|
|
|
|
{
|
|
|
|
|
$builder = new ContainerBuilder();
|
|
|
|
|
|
|
|
|
|
if ($env->isProd()) {
|
|
|
|
|
$cacheDir = $basePath . '/var/cache/prod';
|
|
|
|
|
if (!is_dir($cacheDir)) {
|
|
|
|
|
mkdir($cacheDir, 0755, true);
|
|
|
|
|
}
|
|
|
|
|
$builder->enableCompilation($cacheDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$builder->useAutowiring(true);
|
|
|
|
|
|
2026-04-06 16:00:19 +03:00
|
|
|
$routesFile = $basePath . '/config/routes.php';
|
|
|
|
|
if (file_exists($routesFile)) {
|
|
|
|
|
$routes = require $routesFile;
|
|
|
|
|
$builder->addDefinitions([
|
|
|
|
|
Router::class => fn() => new Router($routes),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 02:40:42 +03:00
|
|
|
$servicesFile = $basePath . '/config/services.php';
|
|
|
|
|
if (file_exists($servicesFile)) {
|
|
|
|
|
$definitions = require $servicesFile;
|
|
|
|
|
$definitions($builder, $config, $basePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $builder->build();
|
|
|
|
|
}
|
|
|
|
|
}
|