/** * 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 Magento 2 Observer * This command generates the necessary files and configuration to add * an event observer to a Magento 2 system. * * pestle.phar generate_observer Pulsestorm_Generate controller_action_predispatch pulsestorm_generate_listener3 'Pulsestorm\Generate\Model\Observer3' * * @command generate_observer * @argument module Full Module Name? [Pulsestorm_Generate] * @argument event_name Event Name? [controller_action_predispatch] * @argument observer_name Observer Name? [<$module$>_listener] * @argument model_name Class Name? [<$module$>\Model\Observer] */ function pestle_cli($argv) { // $module = input("Full Module Name?", 'Pulsestorm_Helloworld'); // $event_name = input('Event Name?', 'controller_action_predispatch'); // $observer_name = input('Observer Name?', strToLower($module . '_listener')); // $model_name = input('Class Name?', str_replace('_', '\\', $module) . '\\Model\\Observer'); // $method_name = input('Method Name?', 'myMethodName'); $module = $argv['module']; $event_name = $argv['event_name']; $observer_name = $argv['observer_name']; $model_name = $argv['model_name']; $method_name = 'execute'; $path_xml_event = initilizeModuleConfig($module, 'events.xml', '../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd'); $xml = simplexml_load_file($path_xml_event); $nodes = $xml->xpath('//event[@name="' . $event_name . '"]'); $node = array_shift($nodes); $event = $node; if (!$node) { $event = $node ? $node : $xml->addChild('event'); $event->addAttribute('name', $event_name); } $observer = $event->addChild('observer'); $observer->addAttribute('name', $observer_name); $observer->addAttribute('instance', $model_name); // $observer->addAttribute('method', $method_name); output("Creating: {$path_xml_event}"); $path = writeStringToFile($path_xml_event, $xml->asXml()); output("Creating: {$model_name}"); $contents = createClassTemplate($model_name, false, '\\Magento\\Framework\\Event\\ObserverInterface'); $contents = str_replace('<$body$>', "\n" . ' public function execute(\\Magento\\Framework\\Event\\Observer $observer){exit(__FILE__);}' . "\n", $contents); // $contents = createBasicClassContents($model_name, $method_name); createClassFile($model_name, $contents); }
/** * Generates Magento 2 Observer * This command generates the necessary files and configuration to add * an event observer to a Magento 2 system. * * pestle.phar generate_observer Pulsestorm_Generate controller_action_predispatch pulsestorm_generate_listener3 'Pulsestorm\Generate\Model\Observer3' * * @command generate_observer * @argument module Full Module Name? [Pulsestorm_Generate] * @argument event_name Event Name? [controller_action_predispatch] * @argument observer_name Observer Name? [<$module$>_listener] * @argument model_name Class Name? [<$module$>\Model\Observer] */ function pestle_cli($argv) { $module = $argv['module']; $event_name = $argv['event_name']; $observer_name = $argv['observer_name']; $model_name = $argv['model_name']; $method_name = 'execute'; $path_xml_event = initilizeModuleConfig($module, 'events.xml', 'urn:magento:framework:Event/etc/events.xsd'); $xml = simplexml_load_file($path_xml_event); $nodes = $xml->xpath('//event[@name="' . $event_name . '"]'); $node = array_shift($nodes); $event = $node; if (!$node) { $event = $node ? $node : $xml->addChild('event'); $event->addAttribute('name', $event_name); } $observer = $event->addChild('observer'); $observer->addAttribute('name', $observer_name); $observer->addAttribute('instance', $model_name); // $observer->addAttribute('method', $method_name); output("Creating: {$path_xml_event}"); $path = writeStringToFile($path_xml_event, $xml->asXml()); output("Creating: {$model_name}"); $contents = createClassTemplate($model_name, false, '\\Magento\\Framework\\Event\\ObserverInterface'); $contents = str_replace('<$body$>', "\n" . ' public function execute(\\Magento\\Framework\\Event\\Observer $observer){exit(__FILE__);}' . "\n", $contents); createClassFile($model_name, $contents); }
function createBlockClass($module_info, $block_name) { $class_name = str_replace('_', '\\', $module_info->name) . '\\Block\\' . ucwords($block_name); output("Creating: " . $class_name); $contents = createClassTemplate($class_name, '\\Magento\\Framework\\View\\Element\\Template'); $contents = str_replace('<$body$>', "\n" . ' function _prepareLayout(){}' . "\n", $contents); createClassFile($class_name, $contents); return $class_name; }
public function testCreateClassTemplate() { $fixture = '<' . '?php namespace ; class Foo {<$body$>}' . "\n"; $template = createClassTemplate('Foo'); $this->assertEquals($template, $fixture); }
function generateNewClass($argv) { $pathType = getPathFromClass($argv['type']); $typeGlobalNs = '\\' . trim($argv['for'], '\\'); $classContents = createClassTemplate($argv['type'], $typeGlobalNs); if (isTypeInterface($typeGlobalNs)) { $classContents = createClassTemplate($argv['type'], null, $typeGlobalNs); } $classContents = str_replace('<$body$>', '', $classContents); if (!file_exists($pathType)) { output("Creating {$pathType}"); writeStringToFile($pathType, $classContents); } else { output("{$pathType} already exists, skipping creation"); } }
function createBlockClass($module_info, $block_name, $area = 'frontname') { $class_name = str_replace('_', '\\', $module_info->name) . '\\Block\\'; if ($area === 'adminhtml') { $class_name .= 'Adminhtml\\'; } $class_name .= ucwords($block_name); output("Creating: " . $class_name); $baseClass = '\\Magento\\Framework\\View\\Element\\Template'; if ($area === 'adminhtml') { $baseClass = '\\Magento\\Backend\\Block\\Template'; } $contents = createClassTemplate($class_name, $baseClass); $contents = str_replace('<$body$>', "\n" . ' function _prepareLayout(){}' . "\n", $contents); createClassFile($class_name, $contents); return $class_name; }
function createDataClass($module_info, $model_name) { $className = str_replace('_', '\\', $module_info->name) . '\\Setup\\InstallData'; $path = getPathFromClass($className); $template = createClassTemplate($className, false, '\\Magento\\Framework\\Setup\\InstallDataInterface'); $contents = str_replace('<$body$>', templateInstallDataFunction(), $template); if (!file_exists($path)) { output("Creating: " . $path); writeStringToFile($path, $contents); } else { output("Data Installer Already Exists: " . $path); } }