From 16d09857186e4a1a2da81852db7a1112ee6544d2 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Wed, 18 Sep 2013 17:06:08 +0200 Subject: [PATCH] payment modules --- .../Thelia/Command/ModuleActivateCommand.php | 93 +++++++++++++++++++ core/lib/Thelia/Config/Resources/config.xml | 1 + core/lib/Thelia/Module/BaseModule.php | 15 ++- install/insert.sql | 6 +- local/modules/Cheque/Cheque.php | 6 ++ local/modules/Colissimo/Colissimo.php | 5 + local/modules/DebugBar/DebugBar.php | 5 + local/modules/FakeCB/FakeCB.php | 5 + reset_install.sh | 7 ++ 9 files changed, 137 insertions(+), 6 deletions(-) create mode 100755 core/lib/Thelia/Command/ModuleActivateCommand.php diff --git a/core/lib/Thelia/Command/ModuleActivateCommand.php b/core/lib/Thelia/Command/ModuleActivateCommand.php new file mode 100755 index 000000000..71177c02d --- /dev/null +++ b/core/lib/Thelia/Command/ModuleActivateCommand.php @@ -0,0 +1,93 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Command; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Filesystem\Exception\IOException; + +use Thelia\Command\ContainerAwareCommand; +use Thelia\Model\ModuleQuery; + +/** + * activates a module + * + * Class ModuleActivateCommand + * @package Thelia\Command + * @author Etienne Roudeix + * + */ +class ModuleActivateCommand extends BaseModuleGenerate +{ + protected function configure() + { + $this + ->setName("module:activate") + ->setDescription("Activates a module") + ->addArgument( + "module" , + InputArgument::REQUIRED, + "module to activate" + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $moduleCode = $this->formatModuleName($input->getArgument("module")); + + $module = ModuleQuery::create()->findOneByCode($moduleCode); + + if(null === $module) { + $output->renderBlock(array( + '', + sprintf("module %s not found", $moduleCode), + '' + ), "bg=red;fg=white"); + } + + try { + $moduleReflection = new \ReflectionClass($module->getFullNamespace()); + + $moduleInstance = $moduleReflection->newInstance(); + + $moduleInstance->activate(); + } catch(\Exception $e) { + $output->renderBlock(array( + '', + sprintf("Activation fail with Exception : [%d] %s", $e->getCode(), $e->getMessage()), + '' + ), "bg=red;fg=white"); + } + + $output->renderBlock(array( + '', + sprintf("Activation succeed for module %s", $moduleCode), + '' + ), "bg=green;fg=black"); + } +} diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index faf3b4241..5380f9d3f 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -105,6 +105,7 @@ + diff --git a/core/lib/Thelia/Module/BaseModule.php b/core/lib/Thelia/Module/BaseModule.php index faae1e9ce..dd57ea531 100755 --- a/core/lib/Thelia/Module/BaseModule.php +++ b/core/lib/Thelia/Module/BaseModule.php @@ -38,14 +38,22 @@ abstract class BaseModule extends ContainerAware const DELIVERY_MODULE_TYPE = 2; const PAYMENT_MODULE_TYPE = 3; + const IS_ACTIVATED = 1; + const IS_NOT_ACTIVATED = 0; + public function __construct() { } - protected function activate() + public function activate() { - + $moduleModel = $this->getModuleModel(); + if($moduleModel->getActivate() == self::IS_NOT_ACTIVATED) { + $moduleModel->setActivate(self::IS_ACTIVATED); + $moduleModel->save(); + $this->afterActivation(); + } } public function hasContainer() @@ -127,7 +135,7 @@ abstract class BaseModule extends ContainerAware } /** - * @return ChildModule + * @return Module * @throws \Thelia\Exception\ModuleException */ public function getModuleModel() @@ -143,6 +151,7 @@ abstract class BaseModule extends ContainerAware abstract public function getCode(); abstract public function install(); + abstract public function afterActivation(); abstract public function destroy(); } diff --git a/install/insert.sql b/install/insert.sql index 9ca1d3ba2..2689c8f70 100755 --- a/install/insert.sql +++ b/install/insert.sql @@ -32,9 +32,9 @@ INSERT INTO `config` (`name`, `value`, `secured`, `hidden`, `created_at`, `updat INSERT INTO `module` (`id`, `code`, `type`, `activate`, `position`, `full_namespace`, `created_at`, `updated_at`) VALUES (1, 'DebugBar', 1, 1, 1, 'DebugBar\\DebugBar', NOW(), NOW()), -(2, 'Colissimo', 2, 1, 1, 'Colissimo\\Colissimo', NOW(), NOW()), -(3, 'Cheque', 3, 1, 1, 'Cheque\\Cheque', NOW(), NOW()), -(4, 'FakeCB', 3, 1, 2, 'FakeCB\\FakeCB', NOW(), NOW()); +(2, 'Colissimo', 2, 0, 1, 'Colissimo\\Colissimo', NOW(), NOW()), +(3, 'Cheque', 3, 0, 1, 'Cheque\\Cheque', NOW(), NOW()), +(4, 'FakeCB', 3, 0, 2, 'FakeCB\\FakeCB', NOW(), NOW()); INSERT INTO `module_i18n` (`id`, `locale`, `title`, `description`, `chapo`, `postscriptum`) VALUES ('2', 'en_US', '72h delivery', NULL, NULL, NULL), diff --git a/local/modules/Cheque/Cheque.php b/local/modules/Cheque/Cheque.php index 1dd915936..d6b462194 100755 --- a/local/modules/Cheque/Cheque.php +++ b/local/modules/Cheque/Cheque.php @@ -25,6 +25,7 @@ namespace Cheque; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; +use Thelia\Model\ModuleImageQuery; use Thelia\Module\BaseModule; use Thelia\Module\PaymentModuleInterface; @@ -59,6 +60,11 @@ class Cheque extends BaseModule implements PaymentModuleInterface } public function install() + { + + } + + public function afterActivation() { /* insert the images from image folder if first module activation */ $module = $this->getModuleModel(); diff --git a/local/modules/Colissimo/Colissimo.php b/local/modules/Colissimo/Colissimo.php index cfeaab994..7ff972feb 100755 --- a/local/modules/Colissimo/Colissimo.php +++ b/local/modules/Colissimo/Colissimo.php @@ -67,6 +67,11 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface return 2; } + public function afterActivation() + { + + } + /** * YOU HAVE TO IMPLEMENT HERE ABSTRACT METHODD FROM BaseModule Class * Like install and destroy diff --git a/local/modules/DebugBar/DebugBar.php b/local/modules/DebugBar/DebugBar.php index dcdf91313..c6339bb9b 100755 --- a/local/modules/DebugBar/DebugBar.php +++ b/local/modules/DebugBar/DebugBar.php @@ -32,6 +32,11 @@ class DebugBar extends BaseModule * Like install and destroy */ + public function afterActivation() + { + + } + public function install() { // TODO: Implement install() method. diff --git a/local/modules/FakeCB/FakeCB.php b/local/modules/FakeCB/FakeCB.php index e59651a47..7b304420c 100755 --- a/local/modules/FakeCB/FakeCB.php +++ b/local/modules/FakeCB/FakeCB.php @@ -60,6 +60,11 @@ class FakeCB extends BaseModule implements PaymentModuleInterface } public function install() + { + + } + + public function afterActivation() { /* insert the images from image folder if first module activation */ $module = $this->getModuleModel(); diff --git a/reset_install.sh b/reset_install.sh index 348927a54..f5244b0c9 100755 --- a/reset_install.sh +++ b/reset_install.sh @@ -32,4 +32,11 @@ php Thelia thelia:create-admin --login_name thelia2 --password thelia2 --last_na echo -e "\n\e[01;34m[INFO] Clearing caches\e[00m\n" php Thelia cache:clear +echo -e "\n\e[01;34m[INFO] Activating Delivery Module(s)\e[00m\n" +php Thelia module:activate Colissimo + +echo -e "\n\e[01;34m[INFO] Activating Payment Module(s)\e[00m\n" +php Thelia module:activate Cheque +php Thelia module:activate FakeCB + echo -e "\n\e[00;32m[SUCCESS] Reset done\e[00m\n" \ No newline at end of file