Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Command To Generate Magento 1 Module #523

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions modules/pulsestorm/magento1/generate/controller/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Pulsestorm\Magento1\Generate\Controller;
use function Pulsestorm\Pestle\Importer\pestle_import;
pestle_import('Pulsestorm\Pestle\Library\output');

pestle_import('Pulsestorm\Pestle\Library\exitWithErrorMessage');
pestle_import('Pulsestorm\Magento1\Generate\Library\createFrontendController');

/**
* Creates a new controller in a module (presumes module is already configured)
*
* @command magento1:generate:controller
* @argument full_module_name Full Module Name [Pulsestorm_Helloworld]
* @argument url_path Url Path [pulsestorm_helloworld/index/index]
*/
function pestle_cli($argv)
{
createFrontendController(
$argv['full_module_name'],
$argv['url_path']
);
output("Done");
}
91 changes: 91 additions & 0 deletions modules/pulsestorm/magento1/generate/library/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
namespace Pulsestorm\Magento1\Generate\Library;
use function Pulsestorm\Pestle\Importer\pestle_import;
pestle_import('Pulsestorm\Pestle\Library\isAboveRoot');
pestle_import('Pulsestorm\Pestle\Library\getBaseDir');
pestle_import('Pulsestorm\Cli\Code_Generation\createClassTemplate');
pestle_import('Pulsestorm\Pestle\Library\writeStringToFile');

function getBaseMagentoDir($path=false)
{
return getBaseDir($path, 'app/Mage.php');
}

function getModuleDeclarationFile($fullName) {
return getBaseMagentoDir() . '/app/etc/modules/' . $fullName . '.xml';
}

function getModuleCodePool($fullName) {
$pathDeclare = getModuleDeclarationFile($fullName);
$contents = simplexml_load_file($pathDeclare);
return $contents->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');
$parts = explode('_', $camelCasedController);
$last = array_pop($parts);
$camelCasedControllerPath = implode('_', $parts);
$camelCasedControllerPath .= ('/' . $last);

writeStringToFile(
$pathControllers . "/${camelCasedControllerPath}Controller.php",
$classContents,
false
);
};

/**
* Not a command, just library functions
* @command library
*/
function pestle_cli($argv)
{
}
55 changes: 55 additions & 0 deletions modules/pulsestorm/magento1/generate/module/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Pulsestorm\Magento1\Generate\Module;
use function Pulsestorm\Pestle\Importer\pestle_import;
pestle_import('Pulsestorm\Pestle\Library\output');
pestle_import('Pulsestorm\Pestle\Library\exitWithErrorMessage');
pestle_import('Pulsestorm\Pestle\Library\writeStringToFile');
pestle_import('Pulsestorm\Magento1\Generate\Library\getBaseMagentoDir');

function generateConfigXml($fullModuleName) {
return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
"<config>
<modules>
<$fullModuleName>
<version>0.1.0</version>
</$fullModuleName>
</modules>
</config>";
}

function generateModuleDeclarationFile($fullModuleName, $pool) {
return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
"<config>
<modules>
<$fullModuleName>
<active>true</active>
<codePool>$pool</codePool>
<depends />
</$fullModuleName>
</modules>
</config>";
}

/**
* 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($argv['full_module_name'], $argv['code_pool']));
writeStringToFile($pathModule, generateConfigXml($argv['full_module_name']));

output('');
output('Generated Base Module Files:');
output(' ' . $pathEtc);
output(' ' . $pathModule);
}
53 changes: 53 additions & 0 deletions modules/pulsestorm/magento1/generate/route/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace Pulsestorm\Magento1\Generate\Route;
use function Pulsestorm\Pestle\Importer\pestle_import;
pestle_import('Pulsestorm\Pestle\Library\output');
pestle_import('Pulsestorm\Pestle\Library\exitWithErrorMessage');
pestle_import('Pulsestorm\Magento1\Generate\Library\getPathModule');
pestle_import('Pulsestorm\Magento1\Generate\Library\getPathModuleConfigFile');
pestle_import('Pulsestorm\Magento1\Generate\Library\createFrontendController');
pestle_import('Pulsestorm\Xml_Library\formatXmlString');
pestle_import('Pulsestorm\Xml_Library\simpleXmlAddNodesXpath');
pestle_import('Pulsestorm\Pestle\Library\writeStringToFile');
pestle_import('Pulsestorm\Cli\Code_Generation\createClassTemplate');

/**
* Generates frontend route configuration and index controller
*
* @command magento1:generate:route
* @argument full_module_name Full Module Name [Pulsestorm_Helloworld]
*/
function pestle_cli($argv)
{
$pathConfig = getPathModuleConfigFile($argv['full_module_name']);
$config = simplexml_load_file($pathConfig);

$fullNameLc = strToLower($argv['full_module_name']);
if(isset($config->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'
);

output('Done');
}
25 changes: 23 additions & 2 deletions modules/pulsestorm/pestle/library/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -166,6 +172,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);
Expand Down
2 changes: 1 addition & 1 deletion modules/pulsestorm/pestle/runner/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down