Added COMPILE_CONTAINER environment variable option for independantly controlling cotainer compilation

This commit is contained in:
Chris Kankiewicz
2021-04-25 14:00:24 -07:00
parent 4f73a5244d
commit f5e7ad82cd
4 changed files with 45 additions and 13 deletions

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Bootstrap;
use DI\Container;
use DI\ContainerBuilder;
class BootManager
{
/** Create the application service container. */
public static function createContainer(string $configDirectory): Container
{
$container = (new ContainerBuilder)->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;
}
}

View File

@@ -1,6 +1,7 @@
<?php
use App\Bootstrap\AppManager;
use App\Bootstrap\BootManager;
use DI\ContainerBuilder;
use Dotenv\Dotenv;
@@ -13,17 +14,10 @@ ini_set('open_basedir', __DIR__);
Dotenv::createUnsafeImmutable(__DIR__)->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();

View File

@@ -21,6 +21,7 @@ TIMEZONE="America/Phoenix"
MAX_HASH_SIZE=1000000000
COMPILE_CONTAINER=false
CACHE_DRIVER=array
CACHE_LIFETIME=0

View File

@@ -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'));