/** * Generates plugin XML * This command generates the necessary files and configuration * to "plugin" to a preexisting Magento 2 object manager object. * * pestle.phar generate_plugin_xml Pulsestorm_Helloworld 'Magento\Framework\Logger\Monolog' 'Pulsestorm\Helloworld\Plugin\Magento\Framework\Logger\Monolog' * * @argument module_name Create in which module? [Pulsestorm_Helloworld] * @argument class Which class are you plugging into? [Magento\Framework\Logger\Monolog] * @argument class_plugin What's your plugin class name? [<$module_name$>\Plugin\<$class$>] * @command generate_plugin_xml */ function pestle_cli($argv) { // $module_info = askForModuleAndReturnInfo($argv); $module_info = getModuleInformation($argv['module_name']); $class = $argv['class']; $class_plugin = $argv['class_plugin']; $path_di = $module_info->folder . '/etc/di.xml'; if (!file_exists($path_di)) { $xml = simplexml_load_string(getBlankXml('di')); writeStringToFile($path_di, $xml->asXml()); output("Created new {$path_di}"); } $xml = simplexml_load_file($path_di); // $plugin_name = strToLower($module_info->name) . '_' . underscoreClass($class); // simpleXmlAddNodesXpath($xml, // "/type[@name=$class]/plugin[@name=$plugin_name,@type=$class_plugin]"); $type = $xml->addChild('type'); $type->addAttribute('name', $class); $plugin = $type->addChild('plugin'); $plugin->addAttribute('name', strToLower($module_info->name) . '_' . underscoreClass($class)); $plugin->addAttribute('type', $class_plugin); writeStringToFile($path_di, formatXmlString($xml->asXml())); output("Added nodes to {$path_di}"); $path_plugin = getPathFromClass($class_plugin); $body = implode("\n", [' //function beforeMETHOD($subject, $arg1, $arg2){}', ' //function aroundMETHOD($subject, $procede, $arg1, $arg2){return $proceed($arg1, $arg2);}', ' //function afterMETHOD($subject, $result){return $result;}']); $class_definition = str_replace('<$body$>', "\n{$body}\n", createClassTemplate($class_plugin)); writeStringToFile($path_plugin, $class_definition); output("Created file {$path_plugin}"); }
/** * Generates bin/magento command files * This command generates the necessary files and configuration * for a new command for Magento 2's bin/magento command line program. * * pestle.phar Pulsestorm_Generate Example * * Creates * app/code/Pulsestorm/Generate/Command/Example.php * app/code/Pulsestorm/Generate/etc/di.xml * * @command generate_command * @argument module_name In which module? [Pulsestorm_Helloworld] * @argument command_name Command Name? [Testbed] */ function pestle_cli($argv) { $module_info = getModuleInformation($argv['module_name']); $namespace = $module_info->vendor; $module_name = $module_info->name; $module_shortname = $module_info->short_name; $module_dir = $module_info->folder; $command_name = $argv['command_name']; // $command_name = input("Command Name?", 'Testbed'); output($module_dir); createPhpClass($module_dir, $namespace, $module_shortname, $command_name); $path_di_xml = createDiIfNeeded($module_dir); $xml_di = simplexml_load_file($path_di_xml); //get commandlist node $nodes = $xml_di->xpath('/config/type[@name="Magento\\Framework\\Console\\CommandList"]'); $xml_type_commandlist = array_shift($nodes); if (!$xml_type_commandlist) { throw new Exception("Could not find CommandList node"); } $argument = simpleXmlAddNodesXpath($xml_type_commandlist, '/arguments/argument[@name=commands,@xsi:type=array]'); $full_class = $namespace . '\\' . $module_shortname . '\\Command\\' . $command_name; $item_name = str_replace('\\', '_', strToLower($full_class)); $item = $argument->addChild('item', $full_class); $item->addAttribute('name', $item_name); $item->addAttribute('xsi:type', 'object', 'http://www.w3.org/2001/XMLSchema-instance'); $xml_di = formatXmlString($xml_di->asXml()); writeStringToFile($path_di_xml, $xml_di); }
function generateDiConfiguration($argv) { $moduleInfo = getModuleInformation($argv['module']); $pathAndXml = loadOrCreateDiXml($moduleInfo); $path = $pathAndXml['path']; $di_xml = $pathAndXml['xml']; $preference = $di_xml->addChild('preference'); $preference['for'] = $argv['for']; $preference['type'] = $argv['type']; writeStringToFile($path, formatXmlString($di_xml->asXml())); }
/** * One Line Description * * @command generate_view * @argument module_name Which Module? [Pulsestorm_HelloGenerate] * @argument area Which Area? [frontend] * @argument handle Which Handle? [<$module_name$>_index_index] * @argument block_name Block Name? [Main] * @argument template Template File? [content.phtml] */ function pestle_cli($argv) { $module_name = $argv['module_name']; $area = $argv['area']; $handle = $argv['handle']; $block_name = $argv['block_name']; $template = $argv['template']; $module_info = getModuleInformation($module_name); createTemplateFile($module_info, $area, $template); $class = createBlockClass($module_info, $block_name); createHandleFile($module_info, $area, $template, $class, $handle); }
/** * Creates a Route XML * generate_route module area id * @command generate_route * @argument module_name Which Module? [Pulsestorm_HelloWorld] * @argument area Which Area (frontend, adminhtml)? [frontend] * @argument frontname Frontname/Route ID? [pulsestorm_helloworld] */ function pestle_cli($argv) { $module = $argv['module_name']; $area = $argv['area']; $frontname = $argv['frontname']; $module_info = getModuleInformation($module); $router_id = getRouterIdFromArea($area); $route_id = $frontname; $xml = createRoutesXmlFile($module_info, $area, $frontname, $router_id, $route_id); $acl = $module . '::' . $frontname . '_menu'; createControllerClassForRoute($module, $area, $acl); if ($area === 'adminhtml') { output(" Don't forget your menu.xml and acl.xml"); output(' action="' . $frontname . '/index/index"'); output(' id="' . $acl); } }
/** * One Line Description * * @command generate_crud_model * @argument module_name Which module? [Pulsestorm_HelloGenerate] * @argument model_name What model name? [Thing] */ function pestle_cli($argv) { $module_name = $argv['module_name']; $module_info = getModuleInformation($argv['module_name']); $model_name = $argv['model_name']; createModelInterface($module_info, $model_name); createCollectionClass($module_info, $model_name); createResourceModelClass($module_info, $model_name); createModelClass($module_info, $model_name); createSchemaClass($module_info, $model_name); createDataClass($module_info, $model_name); }
/** * One Line Description * * @command generate_menu * @argument module_name Module Name? [Pulsestorm_HelloGenerate] * @argument parent @callback selectParentMenu * @argument id Menu Link ID [<$module_name$>::unique_identifier] * @argument resource ACL Resource [<$id$>] * @argument title Link Title [My Link Title] * @argument action Three Segment Action [frontname/index/index] * @argument sortOrder Sort Order? [10] */ function pestle_cli($argv) { extract($argv); $path = getModuleInformation($module_name)->folder . '/etc/adminhtml/menu.xml'; $xml = loadOrCreateMenuXml($path); $xml = addAttributesToXml($argv, $xml); writeStringToFile($path, $xml->asXml()); output("Writing: {$path}"); output("Done."); }
/** * Generates a Magento 2.1 ui grid listing and support classes. * * @command magento2:generate:ui:grid * @argument module Which Module? [Pulsestorm_Gridexample] * @argument grid_id Create a unique ID for your Listing/Grid! [pulsestorm_gridexample_log] * @argument collection_resource What Resource Collection Model should your listing use? [Magento\Cms\Model\ResourceModel\Page\Collection] * @argument db_id_column What's the ID field for you model? [pulsestorm_gridexample_log_id] */ function pestle_cli($argv) { $module_info = getModuleInformation($argv['module']); generateUiComponentXmlFile($argv['grid_id'], $argv['db_id_column'], $module_info); generateDataProviderClass($module_info, $argv['grid_id'], $argv['collection_resource'] . 'Factory'); generatePageActionClass($module_info, $argv['grid_id'], $argv['db_id_column']); output("Don't forget to add this to your layout XML with <uiComponent name=\"{$argv['grid_id']}\"/> "); }