From 98589cc2b62aebec43376bae3f241b4ba189d321 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 11 Jul 2013 16:44:18 +0200 Subject: [PATCH] generate Model using Propel command in Thelia app --- core/bootstrap.php | 1 + .../lib/Thelia/Command/BaseModuleGenerate.php | 10 ++- .../Command/ModuleGenerateModelCommand.php | 88 +++++++++++++++++++ core/lib/Thelia/Config/Resources/config.xml | 1 + 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 core/lib/Thelia/Command/ModuleGenerateModelCommand.php diff --git a/core/bootstrap.php b/core/bootstrap.php index fd0dd80e9..8b53e72e5 100755 --- a/core/bootstrap.php +++ b/core/bootstrap.php @@ -11,6 +11,7 @@ define('THELIA_CONF_DIR' , THELIA_LOCAL_DIR . 'config/'); define('THELIA_MODULE_DIR' , THELIA_LOCAL_DIR . 'modules/'); define('THELIA_WEB_DIR' , THELIA_ROOT . '/web/'); define('THELIA_TEMPLATE_DIR' , THELIA_ROOT . '/templates/'); +define('DS', DIRECTORY_SEPARATOR); $loader = require __DIR__ . "/vendor/autoload.php"; diff --git a/core/lib/Thelia/Command/BaseModuleGenerate.php b/core/lib/Thelia/Command/BaseModuleGenerate.php index e160f9dd2..50938587d 100644 --- a/core/lib/Thelia/Command/BaseModuleGenerate.php +++ b/core/lib/Thelia/Command/BaseModuleGenerate.php @@ -22,8 +22,11 @@ /*************************************************************************************/ namespace Thelia\Command; +use Propel\Runtime\Propel; +use Symfony\Component\Console\Application; - abstract class BaseModuleGenerate extends ContainerAwareCommand { + +abstract class BaseModuleGenerate extends ContainerAwareCommand { protected $module; protected $moduleDirectory; @@ -52,4 +55,9 @@ namespace Thelia\Command; } return ucfirst($name); } + + protected function getPropelApplication() + { + return new Application("Propel", Propel::VERSION); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Command/ModuleGenerateModelCommand.php b/core/lib/Thelia/Command/ModuleGenerateModelCommand.php new file mode 100644 index 000000000..a29002b82 --- /dev/null +++ b/core/lib/Thelia/Command/ModuleGenerateModelCommand.php @@ -0,0 +1,88 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Command; + + +use Propel\Generator\Command\ModelBuildCommand; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\StreamOutput; +use Symfony\Component\Filesystem\Filesystem; + +class ModuleGenerateModelCommand extends BaseModuleGenerate { + + protected function configure() + { + $this + ->setName("module:generate:model") + ->setDescription("generate model for a specific module") + ->addArgument( + "name", + InputArgument::REQUIRED, + "module name" + ) + ; + + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $this->module = $this->formatModuleName($input->getArgument("name")); + $this->moduleDirectory = THELIA_MODULE_DIR . DS . $this->module; + + $fs = new Filesystem(); + + if ($fs->exists($this->moduleDirectory) === false) { + throw new \RuntimeException(sprintf("%s module does not exists")); + } + + if ($fs->exists($this->moduleDirectory . DS . "Config" . DS . "schema.xml") === false) { + throw new \RuntimeException("schema.xml not found in Config directory. Needed file for generating model"); + } + + + $propelApp = $this->getPropelApplication(); + + $moduleBuildPropel = new ModelBuildCommand(); + $moduleBuildPropel->setApplication($propelApp); + + $moduleBuildPropel->run( + new ArrayInput(array( + "command" => $moduleBuildPropel->getName(), + "--output-dir" => THELIA_MODULE_DIR, + "--input-dir" => $this->moduleDirectory . DS ."Config" + )), + new StreamOutput(fopen('php://memory', 'w', false)) + ); + + if ($fs->exists(THELIA_MODULE_DIR . DS . "Thelia")) { + $fs->remove(THELIA_MODULE_DIR . DS . "Thelia"); + } + + + } + +} \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index e70756adb..c3efac45d 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -35,6 +35,7 @@ +