Skip to content

Commit

Permalink
Force the regeneration of the container cache file before compiling t…
Browse files Browse the repository at this point in the history
…he phar file
  • Loading branch information
guvra committed Nov 25, 2019
1 parent 40952d2 commit 631096d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
3 changes: 2 additions & 1 deletion bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ $fileName = $basePath . '/build/dist/gdpr-dump-' . Application::VERSION . '.phar
try {
// Force the generation of the container cache
$application = new AppKernel();
$application->initialize();
$application->setDebug(true);
$application->boot();

// Create the phar file
$compiler = new Compiler();
Expand Down
62 changes: 43 additions & 19 deletions src/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,38 @@ class AppKernel
/**
* @var bool
*/
private $initialized = false;
private $booted = false;

/**
* Initialize the application dependencies.
* @var bool
*/
public function initialize()
private $debug = false;

/**
* Initialize the application.
*/
public function boot()
{
if ($this->initialized) {
if ($this->booted) {
return;
}

// Convert notices/warnings into exceptions
$this->initErrorHandler();

// Build the service container
$this->container = $this->buildServiceContainer();
$this->container = $this->buildContainer();

$this->initialized = true;
$this->booted = true;
}

/**
* Run the console application.
* Run the application.
*/
public function run()
{
// Initialize the application dependencies
$this->initialize();
$this->boot();

// Configure and run the application
$application = new Application();
Expand All @@ -66,6 +71,26 @@ public function run()
$application->run();
}

/**
* Toggle the debug mode.
*
* @param bool $value
*/
public function setDebug(bool $value)
{
$this->debug = $value;
}

/**
* Check whether debug mode is enabled.
*
* @return bool
*/
public function isDebug(): bool
{
return $this->debug;
}

/**
* Set the error handler.
*/
Expand All @@ -86,32 +111,31 @@ private function initErrorHandler()
*
* @return ContainerInterface
*/
private function buildServiceContainer(): ContainerInterface
private function buildContainer(): ContainerInterface
{
$basePath = dirname(__DIR__);

// Initialize the config cache
$file = $basePath . '/app/container.php';
$configCache = new ConfigCache($file, false);
$configCache = new ConfigCache($file, $this->isDebug());

// Load the container from the cache if it exists
if ($configCache->isFresh()) {
require_once $file;
return new CachedContainer();
}

// Otherwise, create the container
$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator($basePath . '/app/config'));
// Build the container
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator($basePath . '/app/config'));
$loader->load('services.yaml');
$container->setParameter('app_root', $basePath);
$container->compile();

$containerBuilder->setParameter('app_root', $basePath);
$containerBuilder->compile();

// Save the container contents to the cache
$dumper = new PhpDumper($containerBuilder);
// Dump the container contents to the cache
$dumper = new PhpDumper($container);
$configCache->write($dumper->dump(['class' => 'CachedContainer']));

return $containerBuilder;
return $container;
}
}

0 comments on commit 631096d

Please sign in to comment.