From b6d626a450fe6c89b863e3e9f2de1ba9b7a26c81 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Fri, 17 Nov 2023 00:48:13 +0100 Subject: [PATCH] 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: Bart Jaskulski <bjaskulski@protonmail.com> --- src/InitCompat.php | 4 +-- src/Plugin.php | 17 ---------- src/PluginInit.php | 84 +++++----------------------------------------- 3 files changed, 10 insertions(+), 95 deletions(-) diff --git a/src/InitCompat.php b/src/InitCompat.php index 43496fb..a273ec1 100644 --- a/src/InitCompat.php +++ b/src/InitCompat.php @@ -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(); } diff --git a/src/Plugin.php b/src/Plugin.php index fe1869e..f24a679 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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; } diff --git a/src/PluginInit.php b/src/PluginInit.php index c317fe5..a94e909 100644 --- a/src/PluginInit.php +++ b/src/PluginInit.php @@ -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; } } -- GitLab