diff --git a/src/InitCompat.php b/src/InitCompat.php index 43496fb32c1ae2e8692e89e71637cdb1698a1004..a273ec181d0f4490935ea11e6546f52c8e666ef0 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 fe1869e04ed9cc7561bdc6e9b389d2406fa971cb..f24a679bf085d2e2b9570c2f3bed2589436971eb 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 c317fe5c28969bfe0bc456f9111b1e91658c0131..a94e90989c54cdcc011dbd4b3722b498c31087b0 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; } }