diff --git a/app/src/Bootstrap/BootManager.php b/app/src/Bootstrap/BootManager.php new file mode 100644 index 0000000..dd1069a --- /dev/null +++ b/app/src/Bootstrap/BootManager.php @@ -0,0 +1,37 @@ +addDefinitions( + ...glob($configDirectory . '/*.php') + ); + + if (self::enableContainerCompilation()) { + $container->enableCompilation(dirname(__DIR__, 2) . '/cache'); + } + + return $container->build(); + } + + /** Determine if container compilation should be enabled. */ + protected static function enableContainerCompilation(): bool + { + if (filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOL)) { + return false; + } + + if (! filter_var(getenv('COMPILE_CONTAINER'), FILTER_VALIDATE_BOOL)) { + return false; + } + + return true; + } +} diff --git a/index.php b/index.php index f0a8e8c..1bacb91 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ safeLoad(); // Initialize the container -$container = (new ContainerBuilder)->addDefinitions( - ...glob(__DIR__ . '/app/config/*.php') -); - -// Compile the container -if (! filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOL)) { - $container->enableCompilation(__DIR__ . '/app/cache'); -} +$container = BootManager::createContainer(__DIR__ . '/app/config'); // Initialize the application -$app = $container->build()->call(AppManager::class); +$app = $container->call(AppManager::class); // Engage! $app->run(); diff --git a/tests/.env b/tests/.env index 78c9542..4f3a136 100644 --- a/tests/.env +++ b/tests/.env @@ -21,6 +21,7 @@ TIMEZONE="America/Phoenix" MAX_HASH_SIZE=1000000000 +COMPILE_CONTAINER=false CACHE_DRIVER=array CACHE_LIFETIME=0 diff --git a/tests/TestCase.php b/tests/TestCase.php index d2ea616..0a5dfec 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,9 @@ namespace Tests; +use App\Bootstrap\BootManager; use App\Config; use DI\Container; -use DI\ContainerBuilder; use Dotenv\Dotenv; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Contracts\Cache\CacheInterface; @@ -26,9 +26,9 @@ class TestCase extends BaseTestCase { Dotenv::createUnsafeImmutable(__DIR__)->safeLoad(); - $this->container = (new ContainerBuilder)->addDefinitions( - ...glob(dirname(__DIR__) . '/app/config/*.php') - )->build(); + $this->container = BootManager::createContainer( + dirname(__DIR__) . '/app/config' + ); $this->config = new Config($this->container); $this->cache = new ArrayAdapter($this->config->get('cache_lifetime'));