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

refactor: simplify package


Take a step back and revisit some ideas by removing previous code. Get
rid of container caching, support for bundles and plugin environment
data. Focus on bringing working alpha version, with missing features to
be rolled out closer to stable representation.

Signed-off-by: default avatarBart Jaskulski <bjaskulski@protonmail.com>
parent 3b388b3a
No related branches found
No related tags found
3 merge requests!3improve into wp-hook and some additional unfinished things,!21.x,!1Draft: Basic implementation of plugin initializer
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
......@@ -7,10 +7,10 @@ namespace WPDesk\Init;
class InitCompat {
public static function from_config( $config_path, $environment = null ) {
public static function from_config( $config_path ) {
require __DIR__ . '/platform_check.php';
$init = new PluginInit( $config_path, $environment );
$init = new PluginInit( $config_path );
return $init->init();
}
......
......@@ -56,24 +56,15 @@ final class Plugin {
*/
private $version;
/**
* Current plugin execution environment.
*
* @var string
*/
private $environment;
public function __construct(
string $file,
string $name,
string $version,
?string $slug = null,
string $environment = 'prod'
) {
$this->file = $file;
$this->name = $name;
$this->version = $version;
$this->environment = $environment;
$this->basename = plugin_basename( $file );
$this->directory = rtrim( plugin_dir_path( $file ), '/' ) . '/';
$this->url = rtrim( plugin_dir_url( $file ), '/' ) . '/';
......@@ -130,14 +121,6 @@ final class Plugin {
return $this->url . ltrim( $path, '/' );
}
public function get_environment(): string {
return $this->environment;
}
public function is_environment( string $env ): bool {
return $this->environment === $env;
}
public function get_version(): string {
return $this->version;
}
......
......@@ -15,11 +15,10 @@ use WPDesk\Init\Loader\PhpFileLoader;
/**
* Plugin builder class responsible for our initialization system.
*
* @api
*/
final class PluginInit {
private const ENV_PRODUCTION = 'prod';
private const ENV_DEVELOPMENT = 'dev';
/** @var Bundle[] */
private $bundles = [];
......@@ -29,9 +28,6 @@ final class PluginInit {
/** @var Configuration */
private $config;
/** @var string */
private $env;
/** @var PhpFileLoader */
private $loader;
......@@ -43,11 +39,9 @@ final class PluginInit {
/**
* @param string|array|Configuration $config
* @param string $environment
*/
public function __construct(
$config,
string $environment = self::ENV_PRODUCTION,
?HookDriver $driver = null,
?HeaderParser $parser = null
) {
......@@ -59,12 +53,11 @@ final class PluginInit {
} elseif ( \is_string( $config ) ) {
$this->config = new Configuration( $this->loader->load( $config ) );
} else {
throw new \InvalidArgumentException( 'Invalid configuration' );
throw new \InvalidArgumentException( sprintf( 'Configuration must be either path to configuration file, array of configuration data or %s instance', Configuration::class ) );
}
$this->env = $environment;
$this->driver = $driver ?? new CallbackDriver();
$this->parser = $parser ?? new PluginHeaderParser();
$this->parser = $parser ?? new DefaultHeaderParser();
}
/**
......@@ -94,25 +87,7 @@ final class PluginInit {
$plugin = $this->create_plugin( $plugin_data );
$requirements = \array_merge(
[
// Prepend requirements from plugin header.
'wp' => $plugin_data['RequiresWP'] ?? null,
'php' => $plugin_data['RequiresPHP'] ?? null,
],
$this->config->get( 'require', [] )
);
if ( ! $this->check_requirements( $plugin, $requirements ) ) {
return null;
}
foreach ( $this->config->get( 'bundles', [] ) as $bundle ) {
$this->bundles[ $bundle ] = new $bundle();
}
$container = $this->initialize_container( $plugin );
$container->set( Plugin::class, $plugin );
$this->driver->register_hooks( $this->config, $this->bundles, $container );
......@@ -120,68 +95,25 @@ final class PluginInit {
return $plugin;
}
private function get_container_class( Plugin $plugin ): string {
return \str_replace( '-', '_', $plugin->get_slug() ) . '_container';
}
private function initialize_container( Plugin $plugin ): Container {
$original_builder = new DiBuilder();
$cache_path = $plugin->get_path( $this->config->get( 'cache_path', 'generated' ) . '/container' );
if ( $plugin->is_environment( self::ENV_PRODUCTION ) ) {
// Skip calling build() on bundles, if we've already compiled the container.
if ( file_exists( $cache_path . '/' . $this->get_container_class( $plugin ) . '.php' ) ) {
return $original_builder->build();
}
$original_builder->enableCompilation(
$cache_path,
$this->get_container_class( $plugin )
);
}
$builder = new ContainerBuilder( $original_builder );
$builder->add_definitions( $this->config->get( 'container_definitions', [] ) );
$builder->add_definitions( __DIR__ . '/Resources/services.inc.php' );
foreach ( $this->bundles as $bundle ) {
$bundle->build( $builder, $this->config );
}
return $builder->build();
}
/**
* @param array{Name: string, Version?: string, TextDomain: string} $plugin_data
*/
private function create_plugin( array $plugin_data ): Plugin {
return new Plugin(
$this->filename,
$plugin_data['Name'],
$plugin_data['Version'] ?? '0.0.0',
$plugin_data['TextDomain'] ?? null,
$this->env
);
}
/**
* @param Plugin $plugin
*
* @return bool
*/
private function check_requirements( Plugin $plugin, array $requirements ): bool {
$checker_factory = new \WPDesk_Basic_Requirement_Checker_Factory();
$checker = $checker_factory->create_from_requirement_array(
$plugin->get_basename(),
$plugin->get_name(),
array_filter( $requirements ),
$plugin->get_slug()
$plugin_data['TextDomain'],
);
if ( ! $checker->are_requirements_met() ) {
$checker->render_notices();
return false;
}
return true;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment