/** * Creates a Route XML * generate_route module area id * @command generate_route */ function pestle_cli($argv) { $module_info = askForModuleAndReturnInfo($argv); $module = $module_info->name; $legend = ['frontend' => 'standard', 'adminhtml' => 'admin']; $areas = array_keys($legend); $area = inputOrIndex('Which area? [frontend, adminhtml]', 'frontend', $argv, 1); $router_id = $legend[$area]; if (!in_array($area, $areas)) { throw new Exception("Invalid areas"); } $frontname = inputOrIndex('Frontname/Route ID?', null, $argv, 2); $route_id = $frontname; $path = $module_info->folder . '/etc/' . $area . '/routes.xml'; if (!file_exists($path)) { $xml = simplexml_load_string(getBlankXml('routes')); writeStringToFile($path, $xml->asXml()); } $xml = simplexml_load_file($path); simpleXmlAddNodesXpath($xml, "router[@id={$router_id}]/" . "route[@id={$route_id},@frontName={$frontname}]/" . "module[@name={$module}]"); writeStringToFile($path, formatXmlString($xml->asXml())); $class = str_replace('_', '\\', $module) . '\\Controller\\Index\\Index'; $controllerClass = createControllerClass($class, $area); $path_controller = getPathFromClass($class); writeStringToFile($path_controller, $controllerClass); output($path); output($path_controller); }
/** * 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); }
public function testBaseline() { $xml = simplexml_load_string('<root/>'); $node = simpleXmlAddNodesXpath($xml, 'foo/bar[@baz=hello]/science'); $this->assertTrue(strpos($xml->asXml(), '<root><foo><bar baz="hello"><science/></bar></foo></root>') !== false); $this->assertEquals($node->getName(), 'science'); }
function createViewXmlFile($base_folder, $package, $theme, $area) { $path = $base_folder . '/etc/view.xml'; $xml = simplexml_load_string(getBlankXml('view')); $media = simpleXmlAddNodesXpath($xml, 'media'); output("Creating: {$path}"); writeStringToFile($path, formatXmlString($xml->asXml())); }
function createHandleFile($module_info, $area, $template, $class, $handle) { $xml = simplexml_load_string(getBlankXml('layout_handle')); $name = strToLower($module_info->name) . '_block_' . strToLower(getShortClassNameFromClass($class)); simpleXmlAddNodesXpath($xml, 'referenceBlock[@name=content]/block[' . '@template=' . $template . ',' . '@class=' . $class . ',' . '@name=' . $name . ']'); $path = $module_info->folder . '/view/' . $area . '/layout/' . $handle . '.xml'; output("Creating: {$path}"); writeStringToFile($path, $xml->asXml()); }
function createRoutesXmlFile($module_info, $area, $frontname, $router_id, $route_id) { $module = $module_info->name; $path = $module_info->folder . '/etc/' . $area . '/routes.xml'; if (!file_exists($path)) { $xml = simplexml_load_string(getBlankXml('routes')); writeStringToFile($path, $xml->asXml()); } $xml = simplexml_load_file($path); simpleXmlAddNodesXpath($xml, "router[@id={$router_id}]/" . "route[@id={$route_id},@frontName={$frontname}]/" . "module[@name={$module}]"); writeStringToFile($path, formatXmlString($xml->asXml())); output($path); return $xml; }
/** * One Line Description * * @command generate_acl * @argument module_name Which Module? [Pulsestorm_HelloWorld] * @argument rule_ids Rule IDs? [<$module_name$>::top,<$module_name$>::config,] */ function pestle_cli($argv) { extract($argv); $rule_ids = explode(',', $rule_ids); $rule_ids = array_filter($rule_ids); $path = getBaseModuleDir($module_name) . '/etc/acl.xml'; if (!file_exists($path)) { $xml = simplexml_load_string(getBlankXml('acl')); writeStringToFile($path, $xml->asXml()); } $xml = simplexml_load_file($path); $xpath = 'acl/resources/resource[@id=Magento_Backend::admin]'; foreach ($rule_ids as $id) { $id = trim($id); $xpath .= '/resource[@id=' . $id . ',@title=TITLE HERE FOR]'; } simpleXmlAddNodesXpath($xml, $xpath); writeStringToFile($path, formatXmlString($xml->asXml())); output("Created {$path}"); }
function generateDiXml($module_info) { $path_di = $module_info->folder . '/etc/adminhtml/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); $item = simpleXmlAddNodesXpath($xml, 'type[@name=Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\CollectionFactory]/' . 'arguments/argument[@name=collections,@xsi:type=array]/' . 'item[@name=pulsestorm_commercebug_log_data_source,@xsi:type=string]'); $item[0] = 'Pulsestorm\\Commercebug\\Model\\ResourceModel\\Log\\Grid\\Collection'; $virtualType = addVirtualType($xml, 'Pulsestorm\\Commercebug\\Model\\ResourceModel\\Log\\Grid\\Collection', 'Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\SearchResult'); $arguments = $virtualType->addChild('arguments'); $argument = addArgument($arguments, 'mainTable', 'string', 'pulsestorm_commercebug_log'); $argument = addArgument($arguments, 'resourceModel', 'string', 'Pulsestorm\\Commercebug\\Model\\ResourceModel\\Log'); return $xml; }