Skip to content
Snippets Groups Projects
Verified Commit fee1879b authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat: allow to compile cache in working environment

parent 57f2cb77
Branches
Tags
2 merge requests!5feat: remove compilation command,!4feat: remove compilation command
...@@ -22,6 +22,7 @@ use WPDesk\Init\HookDriver\CompositeDriver; ...@@ -22,6 +22,7 @@ use WPDesk\Init\HookDriver\CompositeDriver;
use WPDesk\Init\HookDriver\GenericDriver; use WPDesk\Init\HookDriver\GenericDriver;
use WPDesk\Init\HookDriver\HookDriver; use WPDesk\Init\HookDriver\HookDriver;
use WPDesk\Init\HookDriver\LegacyDriver; use WPDesk\Init\HookDriver\LegacyDriver;
use WPDesk\Init\Util\PhpFileDumper;
use WPDesk\Init\Util\PhpFileLoader; use WPDesk\Init\Util\PhpFileLoader;
use WPDesk\Init\Plugin\Header; use WPDesk\Init\Plugin\Header;
use WPDesk\Init\Util\Path; use WPDesk\Init\Util\Path;
...@@ -46,6 +47,8 @@ final class Kernel { ...@@ -46,6 +47,8 @@ final class Kernel {
/** @var ExtensionsSet */ /** @var ExtensionsSet */
private $extensions; private $extensions;
private PhpFileDumper $dumper;
public function __construct( public function __construct(
string $filename, string $filename,
Configuration $config, Configuration $config,
...@@ -56,6 +59,7 @@ final class Kernel { ...@@ -56,6 +59,7 @@ final class Kernel {
$this->extensions = $extensions; $this->extensions = $extensions;
$this->loader = new PhpFileLoader(); $this->loader = new PhpFileLoader();
$this->parser = new DefaultHeaderParser(); $this->parser = new DefaultHeaderParser();
$this->dumper = new PhpFileDumper();
} }
public function boot(): void { public function boot(): void {
...@@ -63,11 +67,16 @@ final class Kernel { ...@@ -63,11 +67,16 @@ final class Kernel {
try { try {
$plugin_data = $this->loader->load( $cache_path ); $plugin_data = $this->loader->load( $cache_path );
} catch ( \Exception $e ) { } catch ( \Exception $e ) {
// If cache not found, load data from memory. try {
// Avoid writing files on host environment. $this->dumper->dump(
// Generate cache with command instead. $this->parser->parse( $this->filename ),
$cache_path
);
$plugin_data = $this->loader->load( $cache_path );
} catch ( \Exception $e ) {
$plugin_data = $this->parser->parse( $this->filename ); $plugin_data = $this->parser->parse( $this->filename );
} }
}
$plugin = new Plugin( $this->filename, new Header( $plugin_data ) ); $plugin = new Plugin( $this->filename, new Header( $plugin_data ) );
...@@ -93,12 +102,13 @@ final class Kernel { ...@@ -93,12 +102,13 @@ final class Kernel {
// If there's a cache file, use it as we are in production environment. // If there's a cache file, use it as we are in production environment.
// Otherwise, build the container from scratch and use live version, without compilation. // Otherwise, build the container from scratch and use live version, without compilation.
if ( file_exists( $this->get_cache_path( $this->get_container_name( $plugin ) . '.php' ) ) ) { //
// On failure, restart container compilation, without cache.
if ( $this->config->get( 'debug', false ) === false ) {
$original_builder->enableCompilation( $original_builder->enableCompilation(
$this->get_cache_path(), $this->get_cache_path(),
$this->get_container_name( $plugin ) $this->get_container_name( $plugin )
); );
return $original_builder->build();
} }
$builder = new ContainerBuilder( $original_builder ); $builder = new ContainerBuilder( $original_builder );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment