Beispiel #1
0
    def add(self,
            classname,
            methodname,
            scope=SCOPE_FRONTEND,
            layout_handle=None,
            reference_type=REFERENCE_CONTAINER,
            reference_name='content',
            extra_params=None):
        # Add class
        block = Phpclass('Block\\{}'.format(classname))
        type = 'frontend'
        if scope == self.SCOPE_ADMINHTML:
            block = Phpclass('Block\\Adminhtml\\{}'.format(classname))
            type = 'adminhtml'

        function_name = methodname[0] + methodname[1:]
        block.add_method(
            Phpmethod(function_name,
                      body="""//Your block code
			return 'Hello World!;""",
                      params=[],
                      docstring=['@return string']))

        # Add plug first will add the module namespace to PhpClass
        self.add_class(block)

        block_template = '{}.phtml'.format(
            classname.replace('\\', '/').lower())
        if layout_handle:
            # Layout Block XML
            xml_path = os.path.join('view', type, 'layout')
            page = Xmlnode(
                'page',
                attributes={
                    'xmlns:xsi':
                    'http://www.w3.org/2001/XMLSchema-instance',
                    'xsi:noNamespaceSchemaLocation':
                    "urn:magento:framework:View/Layout/etc/page_configuration.xsd"
                },
                nodes=[
                    Xmlnode('body',
                            attributes={},
                            nodes=[
                                Xmlnode('referenceContainer' if reference_type
                                        == self.REFERENCE_CONTAINER else
                                        'referenceBlock',
                                        attributes={
                                            'name':
                                            reference_name
                                            if reference_name else 'content'
                                        },
                                        nodes=[
                                            Xmlnode('block',
                                                    attributes={
                                                        'class':
                                                        block.class_namespace,
                                                        'name':
                                                        classname.replace(
                                                            '\\', '.').lower(),
                                                        'as':
                                                        classname.replace(
                                                            '\\', '_').lower(),
                                                        'template':
                                                        '{}::{}'.format(
                                                            self.module_name,
                                                            block_template),
                                                    })
                                        ])
                            ])
                ])
            xml_path = '{}/{}.xml'.format(xml_path, layout_handle.lower())
            self.add_xml(xml_path, page)

        # add template file
        path = os.path.join('view', type, 'templates')
        self.add_static_file(
            path,
            StaticFile(block_template,
                       body="""<?php
/**
 * @var $block \{classname}
 */
?>
<div>
	<?= $block->{function_name}() ?>
	<?= __('Hello {module_name}::{block_template}') ?>
</div>""".format(classname=block.class_namespace,
                 function_name=function_name,
                 module_name=self.module_name,
                 block_template=block_template)))
Beispiel #2
0
    def add(self,
            frontname='',
            section='index',
            action='index',
            adminhtml=False,
            ajax=False,
            extra_params=None):
        if not frontname:
            frontname = self._module.name.lower()
        file = 'etc/{}/routes.xml'.format(
            'adminhtml' if adminhtml else 'frontend')

        # Create config router
        module = Xmlnode('module', attributes={'name': self.module_name})
        if adminhtml:
            module.attributes['before'] = 'Magento_Backend'

        config = Xmlnode('config',
                         attributes={
                             'xsi:noNamespaceSchemaLocation':
                             "urn:magento:framework:App/etc/routes.xsd"
                         },
                         nodes=[
                             Xmlnode('router',
                                     attributes={
                                         'id':
                                         'admin' if adminhtml else 'standard'
                                     },
                                     nodes=[
                                         Xmlnode('route',
                                                 attributes={
                                                     'id': frontname,
                                                     'frontName': frontname
                                                 },
                                                 nodes=[module])
                                     ])
                         ])
        self.add_xml(file, config)

        # Create controller
        controller_class = ['Controller']
        if adminhtml:
            controller_class.append('Adminhtml')
        controller_class.append(section)
        controller_class.append(action)

        controller_extend = '\Magento\Backend\App\Action' if adminhtml else '\Magento\Framework\App\Action\Action'
        controller = Phpclass('\\'.join(controller_class), controller_extend)
        controller.attributes.append('protected $resultPageFactory;')

        if ajax:
            controller.attributes.append('protected $jsonHelper;')

        # generate construct
        if ajax:
            controller.add_method(
                Phpmethod(
                    '__construct',
                    params=[
                        '\Magento\Framework\App\Action\Context $context',
                        '\Magento\Framework\View\Result\PageFactory $resultPageFactory',
                        '\Magento\Framework\Json\Helper\Data $jsonHelper',
                    ],
                    body="""$this->resultPageFactory = $resultPageFactory;
					$this->jsonHelper = $jsonHelper;
					parent::__construct($context);
				"""))
        else:
            controller.add_method(
                Phpmethod(
                    '__construct',
                    params=[
                        '\Magento\Framework\App\Action\Context $context',
                        '\Magento\Framework\View\Result\PageFactory $resultPageFactory'
                    ],
                    body="""$this->resultPageFactory = $resultPageFactory;
					parent::__construct($context);
				"""))

        # generate execute method
        if ajax:
            execute_body = """try {
			    return $this->jsonResponse('your response');
			} catch (\Magento\Framework\Exception\LocalizedException $e) {
			    return $this->jsonResponse($e->getMessage());
			} catch (\Exception $e) {
			    $this->logger->critical($e);
			    return $this->jsonResponse($e->getMessage());
			}
        	"""
        else:
            execute_body = 'return $this->resultPageFactory->create();'

        controller.add_method(Phpmethod('execute', body=execute_body))

        # generate jsonResponse method
        if ajax:
            controller.add_method(
                Phpmethod('jsonResponse',
                          params=["$response = ''"],
                          body="""return $this->getResponse()->representJson(
						$this->jsonHelper->jsonEncode($response)
					);"""))

        self.add_class(controller)

        if ajax:
            return
        else:
            # create block
            block_class = ['Block']
            if adminhtml:
                block_class.append('Adminhtml')
            block_class.append(section)
            block_class.append(action)

            block = Phpclass('\\'.join(block_class),
                             '\Magento\Framework\View\Element\Template')
            self.add_class(block)

            # Add layout xml
            layout_xml = Xmlnode(
                'page',
                attributes={
                    'layout':
                    "admin-1column" if adminhtml else "1column",
                    'xsi:noNamespaceSchemaLocation':
                    "urn:magento:framework:View/Layout/etc/page_configuration.xsd"
                },
                nodes=[
                    Xmlnode('body',
                            nodes=[
                                Xmlnode('referenceContainer',
                                        attributes={'name': 'content'},
                                        nodes=[
                                            Xmlnode(
                                                'block',
                                                attributes={
                                                    'name':
                                                    "{}.{}".format(
                                                        section, action),
                                                    'class':
                                                    block.class_namespace,
                                                    'template':
                                                    "{}::{}/{}.phtml".format(
                                                        self.module_name,
                                                        section, action)
                                                })
                                        ])
                            ])
                ])
            path = os.path.join(
                'view', 'adminhtml' if adminhtml else 'frontend', 'layout',
                "{}_{}_{}.xml".format(frontname, section, action))
            self.add_xml(path, layout_xml)

            # add template file
            path = os.path.join('view',
                                'adminhtml' if adminhtml else 'frontend',
                                'templates')
            self.add_static_file(
                path,
                StaticFile("{}/{}.phtml".format(section, action),
                           body="Hello {}/{}.phtml".format(section, action)))
Beispiel #3
0
    def add(self,
            name,
            field,
            field_type='text',
            sortorder=10,
            extra_params=None):

        widget_block = Phpclass(
            '\\'.join(['Block', 'Widget', name]),
            implements=['BlockInterface'],
            dependencies=[
                'Magento\Framework\View\Element\Template',
                'Magento\Widget\Block\BlockInterface'
            ],
            extends='Template',
            attributes=[
                'protected $_template = "widget/{}.phtml";'.format(
                    name.lower())
            ])

        self.add_class(widget_block)

        parameter_attributes = {
            'name': field.lower(),
            'xsi:type': field_type,
            'visible': 'true',
            'sort_order': sortorder
        }

        if field_type == 'select' or field_type == 'multiselect':
            parameter_attributes[
                "source_model"] = "Magento\\Config\\Model\\Config\\Source\\Yesno"

        widget_file = 'etc/widget.xml'

        widget_xml = Xmlnode(
            'widgets',
            attributes={
                'xmlns:xsi':
                'http://www.w3.org/2001/XMLSchema-instance',
                'xsi:noNamespaceSchemaLocation':
                "urn:magento:module:Magento_Widget:etc/widget.xsd"
            },
            nodes=[
                Xmlnode('widget',
                        attributes={
                            'id':
                            '{}_{}_{}'.format(self._module.package.lower(),
                                              self._module.name.lower(),
                                              name.lower()),
                            'class':
                            widget_block.class_namespace
                        },
                        nodes=[
                            Xmlnode('label', node_text=name),
                            Xmlnode('description', node_text=name),
                            Xmlnode('parameters',
                                    nodes=[
                                        Xmlnode(
                                            'parameter',
                                            attributes=parameter_attributes,
                                            nodes=[
                                                Xmlnode('label',
                                                        node_text=field)
                                            ])
                                    ])
                        ])
            ])

        self.add_xml(widget_file, widget_xml)

        path = os.path.join('view', 'frontend', 'templates')
        self.add_static_file(
            path,
            StaticFile(
                "{}/{}.phtml".format('widget', name),
                body=
                "<?php if($block->getData('{name}')): ?>\n\t<h2 class='{name}'><?php echo $block->getData('{name}'); ?></h2>\n<?php endif; ?>"
                .format(name=field.lower(),
                        classname=widget_block.class_namespace)))
Beispiel #4
0
    def add(self, method_name, extra_params=None):

        payment_code = method_name.lower().replace(' ', '_')
        payment_class_name = method_name

        payment_class = Phpclass(
            'Model\\Payment\\' + payment_class_name,
            extends='\Magento\Payment\Model\Method\AbstractMethod',
            attributes=[
                'protected $_code = "' + payment_code + '";',
                'protected $_isOffline = true;'
            ])

        payment_class.add_method(
            Phpmethod(
                'isAvailable',
                params=[
                    '\\Magento\\Quote\\Api\\Data\\CartInterface $quote = null'
                ],
                body="return parent::isAvailable($quote);"))

        self.add_class(payment_class)

        payment_file = 'etc/payment.xml'

        payment_xml = Xmlnode(
            'payment',
            attributes={
                'xmlns:xsi':
                "http://www.w3.org/2001/XMLSchema-instance",
                "xsi:noNamespaceSchemaLocation":
                "urn:magento:module:Magento_Payment:etc/payment.xsd"
            },
            nodes=[
                Xmlnode('groups',
                        nodes=[
                            Xmlnode(
                                'group',
                                attributes={'id': 'offline'},
                                nodes=[
                                    Xmlnode(
                                        'label',
                                        node_text='Offline Payment Methods')
                                ])
                        ]),
                Xmlnode('methods',
                        nodes=[
                            Xmlnode('method',
                                    attributes={'name': payment_code},
                                    nodes=[
                                        Xmlnode('allow_multiple_address',
                                                node_text='1'),
                                    ])
                        ])
            ])

        self.add_xml(payment_file, payment_xml)

        config_file = 'etc/config.xml'

        config = Xmlnode(
            'config',
            attributes={
                'xsi:noNamespaceSchemaLocation':
                "urn:magento:module:Magento_Store:etc/config.xsd"
            },
            nodes=[
                Xmlnode('default',
                        nodes=[
                            Xmlnode(
                                'payment',
                                nodes=[
                                    Xmlnode(
                                        payment_code,
                                        nodes=[
                                            Xmlnode('active', node_text='1'),
                                            Xmlnode('model',
                                                    node_text=payment_class.
                                                    class_namespace),
                                            Xmlnode('order_status',
                                                    node_text='pending'),
                                            Xmlnode('title',
                                                    node_text=method_name),
                                            Xmlnode('allowspecific',
                                                    node_text='0'),
                                            Xmlnode('group',
                                                    node_text='Offline'),
                                        ])
                                ])
                        ])
            ])

        self.add_xml(config_file, config)

        system_file = 'etc/adminhtml/system.xml'

        system = Xmlnode(
            'config',
            attributes={
                'xsi:noNamespaceSchemaLocation':
                "urn:magento:module:Magento_Config:etc/system_file.xsd"
            },
            nodes=[
                Xmlnode(
                    'system',
                    nodes=[
                        Xmlnode(
                            'section',
                            attributes={
                                'id': 'payment',
                                'sortOrder': 1000,
                                'showInWebsite': 1,
                                'showInStore': 1,
                                'showInDefault': 1,
                                'translate': 'label'
                            },
                            match_attributes={'id'},
                            nodes=[
                                Xmlnode(
                                    'group',
                                    attributes={
                                        'id': payment_code,
                                        'sortOrder': 10,
                                        'showInWebsite': 1,
                                        'showInStore': 1,
                                        'showInDefault': 1,
                                        'translate': 'label'
                                    },
                                    match_attributes={'id'},
                                    nodes=[
                                        Xmlnode('label',
                                                node_text=method_name),
                                        Xmlnode(
                                            'field',
                                            attributes={
                                                'id': 'active',
                                                'type': 'select',
                                                'sortOrder': 10,
                                                'showInWebsite': 1,
                                                'showInStore': 1,
                                                'showInDefault': 1,
                                                'translate': 'label'
                                            },
                                            match_attributes={'id'},
                                            nodes=[
                                                Xmlnode('label',
                                                        node_text='Enabled'),
                                                Xmlnode(
                                                    'source_model',
                                                    node_text=
                                                    'Magento\\Config\\Model\\Config\\Source\\Yesno'
                                                ),
                                            ]),
                                        Xmlnode('field',
                                                attributes={
                                                    'id': 'title',
                                                    'type': 'text',
                                                    'sortOrder': 20,
                                                    'showInWebsite': 1,
                                                    'showInStore': 1,
                                                    'showInDefault': 1,
                                                    'translate': 'label'
                                                },
                                                match_attributes={'id'},
                                                nodes=[
                                                    Xmlnode('label',
                                                            node_text='Title'),
                                                ]),
                                        Xmlnode(
                                            'field',
                                            attributes={
                                                'id': 'order_status',
                                                'type': 'select',
                                                'sortOrder': 30,
                                                'showInWebsite': 1,
                                                'showInStore': 1,
                                                'showInDefault': 1,
                                                'translate': 'label'
                                            },
                                            match_attributes={'id'
                                                              },
                                            nodes=[
                                                Xmlnode(
                                                    'label',
                                                    node_text='New Order Status'
                                                ),
                                                Xmlnode(
                                                    'source_model',
                                                    node_text=
                                                    'Magento\\Sales\\Model\\Config\\Source\\Order\\Status\\NewStatus'
                                                ),
                                            ]),
                                        Xmlnode(
                                            'field',
                                            attributes={
                                                'id': 'allowspecific',
                                                'type': 'allowspecific',
                                                'sortOrder': 40,
                                                'showInWebsite': 1,
                                                'showInStore': 1,
                                                'showInDefault': 1,
                                                'translate': 'label'
                                            },
                                            match_attributes={'id'
                                                              },
                                            nodes=[
                                                Xmlnode(
                                                    'label',
                                                    node_text=
                                                    'Payment from Applicable Countries'
                                                ),
                                                Xmlnode(
                                                    'source_model',
                                                    node_text=
                                                    'Magento\\Payment\\Model\Config\\Source\\Allspecificcountries'
                                                ),
                                            ]),
                                        Xmlnode(
                                            'field',
                                            attributes={
                                                'id': 'specificcountry',
                                                'type': 'multiselect',
                                                'sortOrder': 50,
                                                'showInWebsite': 1,
                                                'showInStore': 1,
                                                'showInDefault': 1,
                                                'translate': 'label'
                                            },
                                            match_attributes={'id'
                                                              },
                                            nodes=[
                                                Xmlnode(
                                                    'label',
                                                    node_text=
                                                    'Payment from Applicable Countries'
                                                ),
                                                Xmlnode(
                                                    'source_model',
                                                    node_text=
                                                    'Magento\\Directory\\Model\\Config\\Source\\Country'
                                                ),
                                                Xmlnode('can_be_empty',
                                                        node_text='1'),
                                            ]),
                                        Xmlnode(
                                            'field',
                                            attributes={
                                                'id': 'sort_order',
                                                'type': 'text',
                                                'sortOrder': 60,
                                                'showInWebsite': 1,
                                                'showInStore': 1,
                                                'showInDefault': 1,
                                                'translate': 'label'
                                            },
                                            match_attributes={'id'
                                                              },
                                            nodes=[
                                                Xmlnode(
                                                    'label',
                                                    node_text='Sort Order'),
                                            ]),
                                        Xmlnode(
                                            'field',
                                            attributes={
                                                'id': 'instructions',
                                                'type': 'textarea',
                                                'sortOrder': 70,
                                                'showInWebsite': 1,
                                                'showInStore': 1,
                                                'showInDefault': 1,
                                                'translate': 'label'
                                            },
                                            match_attributes={'id'
                                                              },
                                            nodes=[
                                                Xmlnode(
                                                    'label',
                                                    node_text='Instructions'),
                                            ]),
                                    ])
                            ])
                    ])
            ])

        self.add_xml(system_file, system)

        layout_file = 'view/frontend/layout/checkout_index_index.xml'

        layout_payment = Xmlnode(
            'item',
            attributes={
                'name': payment_code,
                'xsi:type': 'array'
            },
            nodes=[
                Xmlnode('item',
                        attributes={
                            'name': 'component',
                            'xsi:type': 'string'
                        },
                        node_text='{}/js/view/payment/{}'.format(
                            self.module_name, payment_code)),
                Xmlnode('item',
                        attributes={
                            'name': 'methods',
                            'xsi:type': 'array'
                        },
                        nodes=[
                            Xmlnode('item',
                                    attributes={
                                        'name': payment_code,
                                        'xsi:type': 'array'
                                    },
                                    nodes=[
                                        Xmlnode('item',
                                                attributes={
                                                    'name':
                                                    'isBillingAddressRequired',
                                                    'xsi:type': 'boolean'
                                                },
                                                node_text='true')
                                    ])
                        ])
            ])

        layout_item = Xmlnode(
            'item',
            attributes={
                'name': 'components',
                'xsi:type': 'array'
            },
            nodes=[
                Xmlnode(
                    'item',
                    attributes={
                        'name': 'checkout',
                        'xsi:type': 'array'
                    },
                    nodes=[
                        Xmlnode(
                            'item',
                            attributes={
                                'name': 'children',
                                'xsi:type': 'array'
                            },
                            nodes=[
                                Xmlnode(
                                    'item',
                                    attributes={
                                        'name': 'steps',
                                        'xsi:type': 'array'
                                    },
                                    nodes=[
                                        Xmlnode(
                                            'item',
                                            attributes={
                                                'name': 'children',
                                                'xsi:type': 'array'
                                            },
                                            nodes=[
                                                Xmlnode(
                                                    'item',
                                                    attributes={
                                                        'name': 'billing-step',
                                                        'xsi:type': 'array'
                                                    },
                                                    nodes=[
                                                        Xmlnode(
                                                            'item',
                                                            attributes={
                                                                'name':
                                                                'children',
                                                                'xsi:type':
                                                                'array'
                                                            },
                                                            nodes=[
                                                                Xmlnode(
                                                                    'item',
                                                                    attributes={
                                                                        'name':
                                                                        'payment',
                                                                        'xsi:type':
                                                                        'array'
                                                                    },
                                                                    nodes=[
                                                                        Xmlnode(
                                                                            'item',
                                                                            attributes
                                                                            ={
                                                                                'name':
                                                                                'children',
                                                                                'xsi:type':
                                                                                'array'
                                                                            },
                                                                            nodes
                                                                            =[
                                                                                Xmlnode(
                                                                                    'item',
                                                                                    attributes
                                                                                    ={
                                                                                        'name':
                                                                                        'renders',
                                                                                        'xsi:type':
                                                                                        'array'
                                                                                    },
                                                                                    nodes
                                                                                    =[
                                                                                        Xmlnode(
                                                                                            'item',
                                                                                            attributes
                                                                                            ={
                                                                                                'name':
                                                                                                'children',
                                                                                                'xsi:type':
                                                                                                'array'
                                                                                            },
                                                                                            nodes
                                                                                            =[
                                                                                                layout_payment
                                                                                            ]
                                                                                        )
                                                                                    ]
                                                                                )
                                                                            ])
                                                                    ])
                                                            ])
                                                    ])
                                            ])
                                    ])
                            ])
                    ])
            ])

        layout_base = Xmlnode(
            'page',
            attributes={
                'xmlns:xsi':
                "http://www.w3.org/2001/XMLSchema-instance",
                'layout':
                '1column',
                'xsi:noNamespaceSchemaLocation':
                'urn:magento:framework:View/Layout/etc/page_configuration.xsd'
            },
            nodes=[
                Xmlnode('body',
                        nodes=[
                            Xmlnode('referenceBlock',
                                    attributes={'name': 'checkout.root'},
                                    nodes=[
                                        Xmlnode('arguments',
                                                nodes=[
                                                    Xmlnode(
                                                        'argument',
                                                        attributes={
                                                            'name': 'jsLayout',
                                                            'xsi:type': 'array'
                                                        },
                                                        nodes=[layout_item])
                                                ])
                                    ])
                        ])
            ])

        self.add_xml(layout_file, layout_base)

        self.add_static_file(
            'view/frontend/web/template/payment',
            StaticFile(payment_code + '.html',
                       template_file='payment/payment.tmpl',
                       context_data={
                           'module_name': self.module_name,
                           'payment_code': payment_code
                       }))
        self.add_static_file(
            'view/frontend/web/js/view/payment',
            StaticFile(payment_code + '.js',
                       template_file='payment/payment-js.tmpl',
                       context_data={
                           'module_name': self.module_name,
                           'payment_code': payment_code
                       }))
        self.add_static_file(
            'view/frontend/web/js/view/payment/method-renderer',
            StaticFile(payment_code + '-method.js',
                       template_file='payment/payment-method-js.tmpl',
                       context_data={
                           'module_name': self.module_name,
                           'payment_code': payment_code
                       }))
Beispiel #5
0
 def add(self, language='en_US', extra_params=None):
     self.add_static_file(
         'i18n', StaticFile(language + '.csv',
                            '"string","stringtranslated"'))