diff --git a/src/ActivationReminder/Composer/InvalidSettingValue.php b/src/ActivationReminder/Composer/InvalidSettingValue.php new file mode 100644 index 0000000000000000000000000000000000000000..12e6ca6b30106436044089440c1a8e72fc78875d --- /dev/null +++ b/src/ActivationReminder/Composer/InvalidSettingValue.php @@ -0,0 +1,12 @@ +<?php + +namespace WPDesk\ActivationReminder\Composer; + +class InvalidSettingValue extends \RuntimeException { + + public function __construct( $field, $value ) { + $message = sprintf( 'Invalid Activation Reminder setting value for field %1$a: %2$s!', $field, isset( $value ) ? $value : ' not set' ); + parent::__construct( $message ); + } + +} \ No newline at end of file diff --git a/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php b/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php index 59e9f129b01228809970f93645c1656176ca16ee..35b631da9c8c9d0cc717eb4a06bc9a227c3f706a 100644 --- a/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php +++ b/src/ActivationReminder/Composer/PrepareActivationReminderCommand.php @@ -33,6 +33,8 @@ class PrepareActivationReminderCommand extends BaseCommand { $output->writeln("Creating activation reminder."); + $settings = Settings::create_from_composer_settings( $this->getComposer()->getPackage()->getExtra() ); + if ( $this->package_has_activation_remider() ) { $classLoader = require( 'vendor/autoload.php' ); @@ -97,6 +99,7 @@ class PrepareActivationReminderCommand extends BaseCommand $autoload['files'][] = $class_file; $package->setAutoload( $autoload ); + $composer->getAutoloadGenerator()->setDevMode(true); $composer->getAutoloadGenerator()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize ); } diff --git a/src/ActivationReminder/Composer/Settings.php b/src/ActivationReminder/Composer/Settings.php new file mode 100644 index 0000000000000000000000000000000000000000..d101e6c6f3ab92990d525147924d4ba0ea3e9392 --- /dev/null +++ b/src/ActivationReminder/Composer/Settings.php @@ -0,0 +1,146 @@ +<?php + +namespace WPDesk\ActivationReminder\Composer; + +class Settings { + const TARGET_DIR = 'target-dir'; + const TARGET_CLASS = 'target-class'; + const PLUGIN_TITLE = 'plugin-title'; + const PLUGIN_DIR = 'plugin-dir'; + const LOGO_URL = 'logo-url'; + const BUY_PLUGIN_URL = 'buy-plugin-url'; + const ACTIVATION_REMINDER = 'activation-reminder'; + + /** + * @var string + */ + private $target_dir; + + /** + * @var string + */ + private $target_class; + + /** + * @var string + */ + private $plugin_title; + + /** + * @var string + */ + private $plugin_dir; + + /** + * @var string + */ + private $logo_url; + + /** + * @var string + */ + private $buy_plugin_url; + + /** + * @param string $target_dir + * @param string $target_class + * @param string $plugin_title + * @param string $plugin_dir + * @param string $logo_url + * @param string $buy_plugin_url + */ + public function __construct( string $target_dir, string $target_class, string $plugin_title, string $plugin_dir, string $logo_url, string $buy_plugin_url ) { + $this->target_dir = $target_dir; + $this->target_class = $target_class; + $this->plugin_title = $plugin_title; + $this->plugin_dir = $plugin_dir; + $this->logo_url = $logo_url; + $this->buy_plugin_url = $buy_plugin_url; + } + + /** + * @return string + */ + public function get_target_dir(): string { + return $this->target_dir; + } + + /** + * @return string + */ + public function get_target_class(): string { + return $this->target_class; + } + + /** + * @return string + */ + public function get_plugin_title(): string { + return $this->plugin_title; + } + + /** + * @return string + */ + public function get_plugin_dir(): string { + return $this->plugin_dir; + } + + /** + * @return string + */ + public function get_logo_url(): string { + return $this->logo_url; + } + + /** + * @return string + */ + public function get_buy_plugin_url(): string { + return $this->buy_plugin_url; + } + + /** + * + */ + public static function validate_settings( $settings ) { + if ( ! isset( $settings[ self::ACTIVATION_REMINDER ] ) ) { + throw new InvalidSettingValue( self::ACTIVATION_REMINDER, null ); + } + if ( ! is_array( $settings[ self::ACTIVATION_REMINDER ] ) ) { + throw new InvalidSettingValue( self::ACTIVATION_REMINDER, 'should be array' ); + } + $settings_fields = [ + self::TARGET_DIR, + self::TARGET_CLASS, + self::PLUGIN_TITLE, + self::PLUGIN_DIR, + self::LOGO_URL, + self::BUY_PLUGIN_URL, + ]; + foreach ( $settings_fields as $field ) { + if ( ! isset( $settings[ self::ACTIVATION_REMINDER ][ $field ] ) ) { + throw new InvalidSettingValue( $field, null ); + } + } + } + + + /** + * @param array $settings + * + * @return self + */ + public static function create_from_composer_settings( array $settings ) { + self::validate_settings( $settings ); + return new self( + $settings[ self::ACTIVATION_REMINDER ][ self::TARGET_DIR ], + $settings[ self::ACTIVATION_REMINDER ][ self::TARGET_CLASS ], + $settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_TITLE ], + $settings[ self::ACTIVATION_REMINDER ][ self::PLUGIN_DIR ], + $settings[ self::ACTIVATION_REMINDER ][ self::LOGO_URL ], + $settings[ self::ACTIVATION_REMINDER ][ self::BUY_PLUGIN_URL ] + ); + } + +} \ No newline at end of file