From 259bc0e80cb331de73ee89bd809fc936f1f69a9d Mon Sep 17 00:00:00 2001 From: Alan Storm Date: Thu, 20 Aug 2020 00:38:51 -0700 Subject: [PATCH 1/4] generate magento 1 module --- .../magento1/generate/module/module.php | 62 +++++++++++++++++++ modules/pulsestorm/pestle/library/module.php | 15 +++++ modules/pulsestorm/pestle/runner/module.php | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 modules/pulsestorm/magento1/generate/module/module.php diff --git a/modules/pulsestorm/magento1/generate/module/module.php b/modules/pulsestorm/magento1/generate/module/module.php new file mode 100644 index 0000000..e0ae03a --- /dev/null +++ b/modules/pulsestorm/magento1/generate/module/module.php @@ -0,0 +1,62 @@ + + + + + 0.1.0 + + +'; +} + +function generateModuleDeclarationFile() { + return ' + + + + true + local + + + +'; +} + +/** +* One Line Description +* +* @command magento1:generate:module +* @argument code_pool Code Pool [community] +* @argument full_module_name Full Module Name [Pulsestorm_Helloworld] +*/ +function pestle_cli($argv) +{ + list($package, $module) = explode('_', $argv['full_module_name']); + + $pathEtc = getBaseMagentoDir() . '/app/etc/modules/' . $argv['full_module_name'] . '.xml'; + $pathModule = getBaseMagentoDir() . '/app/code/' . $argv['code_pool'] . '/' . $package . + '/' . $module . '/etc/config.xml'; + + writeStringToFile($pathEtc, generateModuleDeclarationFile()); + writeStringToFile($pathModule, generateConfigXml()); + + output(''); + output('Generated Base Module Files:'); + output(' ' . $pathEtc); + output(' ' . $pathModule); +} diff --git a/modules/pulsestorm/pestle/library/module.php b/modules/pulsestorm/pestle/library/module.php index 23e2935..c6f724f 100644 --- a/modules/pulsestorm/pestle/library/module.php +++ b/modules/pulsestorm/pestle/library/module.php @@ -166,6 +166,21 @@ function getDocCommentAsString($function) return trim( implode("\n", $lines) ); } +function getBaseDir($path, $fileToTest) { + if($path && isAboveRoot($path)) + { + output("Could not find base Magento directory"); + exit; + } + + $path = $path ? $path : getcwd(); + if(file_exists($path . '/' . $fileToTest)) + { + return realpath($path); + } + return getBaseDir($path . '/..', $fileToTest); +} + function isAboveRoot($path) { $parts = explode('..', $path); diff --git a/modules/pulsestorm/pestle/runner/module.php b/modules/pulsestorm/pestle/runner/module.php index c68e9ae..b6c9a04 100644 --- a/modules/pulsestorm/pestle/runner/module.php +++ b/modules/pulsestorm/pestle/runner/module.php @@ -153,7 +153,7 @@ function getListOfDefinedCliFunctions() } $main = $namespace . '\\pestle_cli'; if(!function_exists($main)) { - if(!in_array($main, ['zend\diactoros\pestle_cli'])) { + if(!in_array($main, ['zend\diactoros\pestle_cli','swoole\coroutine\pestle_cli','co\pestle_cli'])) { output("Skipping $main -- no such function"); } continue; From 56e15572a9440957e110f56dbc88251e597d561f Mon Sep 17 00:00:00 2001 From: Alan Storm Date: Thu, 20 Aug 2020 00:45:34 -0700 Subject: [PATCH 2/4] actually generate file --- .../magento1/generate/module/module.php | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/modules/pulsestorm/magento1/generate/module/module.php b/modules/pulsestorm/magento1/generate/module/module.php index e0ae03a..e93c9ff 100644 --- a/modules/pulsestorm/magento1/generate/module/module.php +++ b/modules/pulsestorm/magento1/generate/module/module.php @@ -13,28 +13,28 @@ function getBaseMagentoDir($path=false) return getBaseDir($path, 'app/Mage.php'); } -function generateConfigXml() { - return ' - +function generateConfigXml($fullModuleName) { + return '' . "\n" . +" - + <$fullModuleName> 0.1.0 - + -'; +"; } -function generateModuleDeclarationFile() { - return ' - +function generateModuleDeclarationFile($fullModuleName, $pool) { + return '' . "\n" . +" - + <$fullModuleName> true - local + $pool - + -'; +"; } /** @@ -52,8 +52,8 @@ function pestle_cli($argv) $pathModule = getBaseMagentoDir() . '/app/code/' . $argv['code_pool'] . '/' . $package . '/' . $module . '/etc/config.xml'; - writeStringToFile($pathEtc, generateModuleDeclarationFile()); - writeStringToFile($pathModule, generateConfigXml()); + writeStringToFile($pathEtc, generateModuleDeclarationFile($argv['full_module_name'], $argv['code_pool'])); + writeStringToFile($pathModule, generateConfigXml($argv['full_module_name'])); output(''); output('Generated Base Module Files:'); From 787fa4be4c5d36ab674dfa87310250c9d20f45c7 Mon Sep 17 00:00:00 2001 From: Alan Storm Date: Thu, 20 Aug 2020 13:34:48 -0700 Subject: [PATCH 3/4] generating a route --- .../magento1/generate/library/module.php | 86 +++++++++++++++++++ .../magento1/generate/module/module.php | 9 +- .../magento1/generate/route/module.php | 51 +++++++++++ modules/pulsestorm/pestle/library/module.php | 10 ++- 4 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 modules/pulsestorm/magento1/generate/library/module.php create mode 100644 modules/pulsestorm/magento1/generate/route/module.php diff --git a/modules/pulsestorm/magento1/generate/library/module.php b/modules/pulsestorm/magento1/generate/library/module.php new file mode 100644 index 0000000..f10b5f5 --- /dev/null +++ b/modules/pulsestorm/magento1/generate/library/module.php @@ -0,0 +1,86 @@ +modules->{$fullName}->codePool; +} + +function getBaseModuleDir($fullName) { + list($package, $module) = explode('_', $fullName); + $codePool = getModuleCodePool($fullName); + return getBaseMagentoDir() . '/app/code/' . $codePool . '/' . + $package . '/' . $module; +} + +function getPathModuleConfigFile($fullName, $file='config.xml') { + return getBaseModuleDir($fullName) . '/etc/' . $file; +} + +function getPathModule($fullName, $folder='') { + return getBaseModuleDir($fullName) . '/' . $folder; +} + +function m1CreateClassTemplate($class, $extends=false, $implements=false, $includeUse=false) { + $contents = createClassTemplate($class, $extends, $implements, $includeUse); + return str_replace("namespace ;\n\n", '', $contents); +} + +function createFrontendController($fullName, $path) { + $parts = explode('/',$path); + if(count($parts) !== 3) { + exitWithErrorMessage('please use full/three/part URL path'); + } + list($module, $controller, $action) = $parts; + + $parts = explode('_',$controller); + $parts = array_map(function($part){ + return ucwords($part); + }, $parts); + $camelCasedController = implode('', $parts); + $classContents = m1CreateClassTemplate( + $fullName . "_${camelCasedController}Controller", + 'Mage_Core_Controller_Front_Action' + ); + + $classContents = str_replace( + '<$body$>', + "\n + public function ${action}Action() + { + \$this->loadLayout(); + \$this->renderLayout(); + } +", + $classContents + ); + $pathControllers = getPathModule($fullName, 'controllers'); + writeStringToFile( + $pathControllers . "/${camelCasedController}Controller.php", + $classContents, + false + ); +}; + +/** +* Not a command, just library functions +* @command library +*/ +function pestle_cli($argv) +{ +} diff --git a/modules/pulsestorm/magento1/generate/module/module.php b/modules/pulsestorm/magento1/generate/module/module.php index e93c9ff..90c679e 100644 --- a/modules/pulsestorm/magento1/generate/module/module.php +++ b/modules/pulsestorm/magento1/generate/module/module.php @@ -1,17 +1,10 @@ ' . "\n" . diff --git a/modules/pulsestorm/magento1/generate/route/module.php b/modules/pulsestorm/magento1/generate/route/module.php new file mode 100644 index 0000000..fa4d0cf --- /dev/null +++ b/modules/pulsestorm/magento1/generate/route/module.php @@ -0,0 +1,51 @@ +frontend->routers->{$fullNameLc})) { + output('frontend/routers/' . $fullNameLc . ' already exists'); + exit; + } + + $xpathModuleRouters = 'frontend/routers/' . strToLower($argv['full_module_name']); + $xmlModuleRouters = simpleXmlAddNodesXpath($config, $xpathModuleRouters); + $xmlUse = simpleXmlAddNodesXpath($config, $xpathModuleRouters . '/use'); + simpleXmlAddNodesXpath($config, $xpathModuleRouters . '/args'); + $xmlModule = simpleXmlAddNodesXpath($config, $xpathModuleRouters . '/args/module'); + $xmlFrontName = simpleXmlAddNodesXpath($config, $xpathModuleRouters . '/args/frontName'); + + $xmlUse[0] = 'standard'; + $xmlModule[0] = $argv['full_module_name']; + $xmlFrontName[0] = strToLower($argv['full_module_name']); + + writeStringToFile( + $pathConfig, + formatXmlString($config->asXml()) + ); + + createFrontendController( + $argv['full_module_name'], + 'pulsestorm_simplerest/index/index' + ); +} diff --git a/modules/pulsestorm/pestle/library/module.php b/modules/pulsestorm/pestle/library/module.php index c6f724f..01a8720 100644 --- a/modules/pulsestorm/pestle/library/module.php +++ b/modules/pulsestorm/pestle/library/module.php @@ -123,16 +123,22 @@ function getPartFromDeclaration($class, $part) return null; } -function writeStringToFile($path, $contents) +function writeStringToFile($path, $contents, $overwrite=true) { if(!is_dir(dirname($path))) { mkdir(dirname($path),0755,true); } + + if(file_exists($path) && !$overwrite) { + output('SKIPPING: file already exists ' . $path); + return; + } + $path_backup = $path . '.' . uniqid() . '.bak.php'; if(file_exists($path)) { - output('Backing existing file: ' . $path_backup); + output('Backing up existing file: ' . $path_backup); copy($path, $path_backup); } file_put_contents($path, $contents); From de4cd5b55f89f7456346a04b22071ad51b9eef66 Mon Sep 17 00:00:00 2001 From: Alan Storm Date: Fri, 21 Aug 2020 00:22:09 -0700 Subject: [PATCH 4/4] controllers command --- .../magento1/generate/controller/module.php | 23 +++++++++++++++++++ .../magento1/generate/library/module.php | 9 ++++++-- .../magento1/generate/route/module.php | 4 +++- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 modules/pulsestorm/magento1/generate/controller/module.php diff --git a/modules/pulsestorm/magento1/generate/controller/module.php b/modules/pulsestorm/magento1/generate/controller/module.php new file mode 100644 index 0000000..6bc856d --- /dev/null +++ b/modules/pulsestorm/magento1/generate/controller/module.php @@ -0,0 +1,23 @@ +