def __init__(self, name, description='', required=False, default=None, choises=None, yes_no=False, regex_validator='', error_message='', depend=None, label=None, multiple_choices=False, repeat=False): self.name = name self.description = description self.required = required self.default = default self.choises = choises self.yes_no = yes_no self.regex_validator = regex_validator self.error_message = error_message self.depend = depend self.label = label if label else upperfirst(name.replace('_', ' ')) self.multiple_choices = multiple_choices self.repeat = repeat
def add(self, event, scope=SCOPE_ALL, extra_params=None): split_event = event.split('_') observer = Phpclass( '\\'.join([ 'Observer', split_event[0], ''.join(upperfirst(item) for item in split_event[1:]) ]), implements=['\Magento\Framework\Event\ObserverInterface']) observer.add_method( Phpmethod( 'execute', params=['\\Magento\\Framework\\Event\\Observer $observer'], body="//Your observer code", docstring=[ 'Execute observer', '', '@param \\Magento\\Framework\\Event\\Observer $observer', '@return void' ])) self.add_class(observer) config = Xmlnode( 'config', attributes={ 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation': "urn:magento:framework:Event/etc/events.xsd" }, nodes=[ Xmlnode('event', attributes={'name': event}, nodes=[ Xmlnode('observer', attributes={ 'name': '{}_{}'.format( observer.class_namespace.replace( '\\', '_').lower(), event), 'instance': observer.class_namespace, }) ]) ]) xml_path = ['etc'] if scope == self.SCOPE_FRONTEND: xml_path.append('frontend') elif scope == self.SCOPE_ADMINHTML: xml_path.append('adminhtml') elif scope == self.SCOPE_WEBAPI: soap_xml_path = ['etc'] xml_path.append('webapi_rest') soap_xml_path.append('webapi_soap') soap_xml_path.append('events.xml') self.add_xml(os.path.join(*soap_xml_path), config) xml_path.append('events.xml') self.add_xml(os.path.join(*xml_path), config)
def add(self, event, scope=SCOPE_ALL, extra_params=None): split_event = event.split('_') observer = Phpclass( '\\'.join(['Observer', split_event[0], ''.join(upperfirst(item) for item in split_event[1:])]), implements=['\Magento\Framework\Event\ObserverInterface']) observer.add_method(Phpmethod( 'execute', params=['\Magento\Framework\Event\Observer $observer'] )) self.add_class(observer) config = Xmlnode('config', attributes={'xmlns:xsi':'http://www.w3.org/2001/XMLSchema-instance','xsi:noNamespaceSchemaLocation':"urn:magento:framework:Event/etc/events.xsd"}, nodes=[ Xmlnode('event', attributes={'name': event}, nodes=[ Xmlnode('observer', attributes={ 'name': '{}_{}'.format(observer.class_namespace.replace('\\', '_').lower(), event), 'instance':observer.class_namespace, }) ]) ]) xml_path = ['etc'] if scope == self.SCOPE_FRONTEND: xml_path.append('frontend') elif scope == self.SCOPE_ADMINHTML: xml_path.append('adminhtml') xml_path.append('events.xml') self.add_xml(os.path.join(*xml_path), config)
def __init__(self, package, name, description='', license=None): self.package = upperfirst(package) self.name = upperfirst(name) self.description = description self.license = license self._xmls = {} self._classes = {} self._static_files = {} # minimum requirements for Magento2 module etc_module = Xmlnode('config', attributes={ 'xsi:noNamespaceSchemaLocation': "urn:magento:framework:Module/etc/module.xsd" }, nodes=[ Xmlnode('module', attributes={ 'name': self.module_name, 'setup_version': '1.0.0' }) ]) self.add_xml('etc/module.xml', etc_module) self.add_static_file( '.', StaticFile('registration.php', template_file='registration.tmpl', context_data={'module_name': self.module_name})) self._composer = OrderedDict() self._composer['name'] = '{}/module-{}'.format(self.package.lower(), self.name.lower()) self._composer['description'] = self.description self._composer['type'] = 'magento2-module' self._composer['license'] = 'proprietary' self._composer['authors'] = [{ 'name': 'Mage2Gen', 'email': '*****@*****.**' }] self._composer['minimum-stability'] = 'dev' self._composer['require'] = {} self._composer['autoload'] = { 'files': ['registration.php'], 'psr-4': { "{}\\{}\\".format(self.package, self.name): "" } }
def add(self, test_suite, test_name, extra_params=None): class_name = "\\Test\\Unit\\{}Test".format(test_suite) unittest_class = Phpclass(class_name,extends='\\PHPUnit_Framework_TestCase') # Adding class setup and teardown unittest_class.add_method( Phpmethod( 'setUpBeforeClass', access='public static', body="//setup", docstring=['Is called once before running all test in class'] ) ) unittest_class.add_method( Phpmethod( 'tearDownAfterClass', access='public static', body="//teardown", docstring=['Is called once after running all test in class'] ) ) # Adding test setup and teardown unittest_class.add_method( Phpmethod( 'setUp', access='protected', body="//setup", docstring=['Is called before running a test'] ) ) unittest_class.add_method( Phpmethod( 'tearDown', access='protected', body="//teardown", docstring=['Is called after running a test'] ) ) # Adding test unittest_class.add_method( Phpmethod( 'test{}'.format(upperfirst(test_name)), access='public', body='$this->assertTrue(false);', docstring=["The test itself, every test function must start with 'test'"] ) ) self.add_class(unittest_class)
def add(self, test_suite, test_name, extra_params=None): class_name = "\\Test\\Unit\\{}Test".format(test_suite) unittest_class = Phpclass(class_name,extends='\\PHPUnit_Framework_TestCase') # Adding class setup and teardown unittest_class.add_method( Phpmethod( 'setUpBeforeClass', access='public static', body="//setup", docstring=['Is called once before running all test in class'] ) ) unittest_class.add_method( Phpmethod( 'tearDownAfterClass', access='public static', body="//teardown", docstring=['Is called once after running all test in class'] ) ) # Adding test setup and teardown unittest_class.add_method( Phpmethod( 'setUp', access='protected', body="//setup", docstring=['Is called before running a test'] ) ) unittest_class.add_method( Phpmethod( 'tearDown', access='protected', body="//teardown", docstring=['Is called after running a test'] ) ) # Adding test unittest_class.add_method( Phpmethod( 'test{}'.format(upperfirst(test_name)), access='public', body='$this->assertTrue(false);', docstring=["The test itself, every test function must start with 'test'"] ) ) self.add_class(unittest_class)
def __init__(self, package, name, description='', license=None): self.package = upperfirst(package) self.name = upperfirst(name) self.description = description self.license = license self._xmls = {} self._classes = {} self._static_files = {} # minimum requirements for Magento2 module etc_module = Xmlnode('config', attributes={'xsi:noNamespaceSchemaLocation':"urn:magento:framework:Module/etc/module.xsd"}, nodes=[ Xmlnode('module', attributes={'name': self.module_name, 'setup_version': '1.0.0'}) ]) self.add_xml('etc/module.xml', etc_module) self.add_static_file('.', StaticFile('registration.php', template_file='registration.tmpl', context_data={'module_name':self.module_name})) self._composer = OrderedDict() self._composer['name'] = '{}/module-{}'.format(self.package.lower(), self.name.lower()) self._composer['description'] = self.description self._composer['license'] = 'proprietary' self._composer['authors'] = [ { 'name': 'Mage2Gen', 'email': '*****@*****.**' } ] self._composer['minimum-stability'] = 'dev' self._composer['require'] = {} self._composer['autoload'] = { 'files': [ 'registration.php' ], 'psr-4': { "{}\\{}\\".format(self.package, self.name): "" } }
def __init__( self, name, description='', required=False, default=None, choises=None, yes_no=False, regex_validator='', error_message='', depend=None, label=None, multiple_choices=False, repeat=False ): self.name = name self.description = description self.required = required self.default = default self.choises = choises self.yes_no = yes_no self.regex_validator = regex_validator self.error_message = error_message self.depend = depend self.label = label if label else upperfirst(name.replace('_', ' ')) self.multiple_choices = multiple_choices self.repeat = repeat
def add(self, cronjob_class, schedule='*/5 * * * *', extra_params=None): crontab_class = Phpclass('Cron\\{}'.format(upperfirst(cronjob_class)), attributes=[ 'protected $logger;' ]) crontab_class.add_method(Phpmethod( '__construct', params=[ '\Psr\Log\LoggerInterface $logger', ], body="$this->logger = $logger;", docstring=[ 'Constructor', '', '@param \\Psr\\Log\\LoggerInterface $logger', ] )) crontab_class.add_method(Phpmethod('execute', body='$this->logger->addInfo("Cronjob '+cronjob_class+' is executed.");', docstring=[ 'Execute the cron', '', '@return void', ] )) self.add_class(crontab_class) crontab_xml = Xmlnode('config',attributes={'xmlns:xsi':'http://www.w3.org/2001/XMLSchema-instance','xsi:noNamespaceSchemaLocation':"urn:magento:module:Magento_Cron:etc/crontab.xsd"},nodes=[ Xmlnode('group',attributes={'id':'default'},nodes=[ Xmlnode( 'job', attributes={ 'name': "{}_{}".format(self.module_name, cronjob_class).lower(), 'instance': crontab_class.class_namespace, 'method': "execute", }, nodes=[ Xmlnode('schedule',node_text=schedule) ] ) ]) ]); self.add_xml('etc/crontab.xml', crontab_xml)
def upper_class_namespace(self, class_namespace): return '\\'.join( upperfirst(n) for n in class_namespace.strip('\\').split('\\'))
def name_label(self): return upperfirst(self.name.replace('_', ' '))
def name_label(self): return upperfirst(self.name.replace('_', ' '))
def add(self, api_name, api_method='GET', extra_params=None): methodname = api_name url = '/V1/' + self._module.package.lower( ) + '-' + self._module.name.lower() + '/' + api_name.lower() resource = 'anonymous' description = api_method + ' for ' + api_name + ' api' management_interface = InterfaceClass('Api\\' + methodname + 'ManagementInterface') management_interface.add_method( InterfaceMethod(api_method.lower() + upperfirst(api_name), params=['$param'], docstring=[ description, '@param string $param', '@return string' ])) self.add_class(management_interface) api_classname = management_interface.class_namespace model = Phpclass('\\'.join(['Model', methodname + 'Management'])) model.add_method( Phpmethod(api_method.lower() + upperfirst(api_name), params=['$param'], docstring=['{@inheritdoc}'], body="return 'hello api " + api_method + " return the $param ' . $param;")) self.add_class(model) model_classname = model.class_namespace di_xml = Xmlnode( 'config', attributes={ 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation': "urn:magento:framework:ObjectManager/etc/config.xsd" }, nodes=[ Xmlnode('preference', attributes={ 'for': api_classname, 'type': model_classname }) ]) self.add_xml('etc/di.xml', di_xml) webapi_xml = Xmlnode( 'routes', attributes={ 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation': "urn:magento:module:Magento_Webapi:etc/webapi.xsd" }, nodes=[ Xmlnode('route', attributes={ 'url': url, 'method': api_method }, match_attributes={'url', 'method'}, nodes=[ Xmlnode('service', attributes={ 'class': api_classname, 'method': api_method.lower() + upperfirst(api_name) }), Xmlnode('resources', nodes=[ Xmlnode('resource', attributes={'ref': resource}) ]) ]) ]) self.add_xml('etc/webapi.xml', webapi_xml)
def upper_class_namespace(self, class_namespace): return '\\'.join(upperfirst(n) for n in class_namespace.strip('\\').split('\\'))
def add(self, config_name, node_name, field_name, extra_params=None): config_class_name = ''.join( utils.upperfirst(w) for w in config_name.split('_')) # Create XSD config_xsd = Xmlnode( 'xs:schema', xsd=True, attributes={ 'attributeFormDefault': "unqualified", "elementFormDefault": "qualified", "xmlns:xs": "http://www.w3.org/2001/XMLSchema" }, nodes=[ Xmlnode('xs:element', attributes={'name': 'config'}, nodes=[ Xmlnode( 'xs:complexType', nodes=[ Xmlnode( 'xs:choice', attributes={'maxOccurs': 'unbounded'}, nodes=[ Xmlnode( 'xs:element', attributes={ 'name': node_name, 'type': '{}Type'.format(node_name), 'maxOccurs': 'unbounded', 'minOccurs': '0' }) ]) ]) ]), Xmlnode('xs:complexType', attributes={'type': '{}Type'.format(node_name)}, nodes=[ Xmlnode('xs:sequence', nodes=[ Xmlnode('xs:element', attributes={ 'name': field_name, 'type': 'xs:string' }) ]) ]) ]) self.add_xml('etc/{}.xsd'.format(config_name), config_xsd) # create merge XSD config_merged_xsd = Xmlnode( 'xs:schema', attributes={'xmlns:xs': 'http://www.w3.org/2001/XMLSchema'}, nodes=[ Xmlnode('xs:include', attributes={ 'schemaLocation': 'urn:magento:module:{}:etc/{}.xsd'.format( self.module_name, config_name) }) ]) self.add_xml('etc/{}_merged.xsd'.format(config_name), config_merged_xsd) # Create SchemaLocator schema_locator_class = Phpclass( 'Config\\{}\\SchemaLocator'.format(config_class_name), implements=[ '\\Magento\\Framework\\Config\\SchemaLocatorInterface' ], attributes=[ 'protected $_schema = null;', 'protected $_perFileSchema = null;' ], dependencies=['Magento\\Framework\\Module\\Dir']) schema_locator_class.add_method( Phpmethod( '__construct', params=[ '\\Magento\\Framework\\Module\\Dir\\Reader $moduleReader' ], body=""" $etcDir = $moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, '{module_name}'); $this->_schema = $etcDir . '/{config_name}_merged.xsd'; $this->_perFileSchema = $etcDir . '/{config_name}.xsd'; """.format(module_name=self.module_name, config_name=config_name), docstring=[ '@param \Magento\Framework\Module\Dir\Reader $moduleReader' ])) schema_locator_class.add_method( Phpmethod('getSchema', body="return $this->_schema;", docstring=[ 'Get path to merged config schema', '', '@return string|null' ])) schema_locator_class.add_method( Phpmethod('getPerFileSchema', body="return $this->_perFileSchema;", docstring=[ 'Get path to pre file validation schema', '', '@return string|null' ])) self.add_class(schema_locator_class) # Create Converter converter_class = Phpclass( 'Config\\{}\\Converter'.format(config_class_name), implements=['\\Magento\\Framework\\Config\\ConverterInterface']) converter_class.add_method( Phpmethod('convert', params=['$source'], docstring=[ 'Convert dom node tree to array', '', '@param \DOMDocument $source', '@return array', ], body=""" $output = []; $xpath = new \DOMXPath($source); $nodes = $xpath->evaluate('/config/{node_name}'); /** @var $node \DOMNode */ foreach ($nodes as $node) {{ $nodeId = $node->attributes->getNamedItem('id'); $data = []; $data['id'] = $nodeId; foreach ($node->childNodes as $childNode) {{ if ($childNode->nodeType != XML_ELEMENT_NODE) {{ continue; }} $data[$childNode->nodeName] = $childNode->nodeValue; }} $output['{node_name}'][$nodeId] = $data; }} return $output; """.format(node_name=node_name))) self.add_class(converter_class) # Create Reader reader_class = Phpclass( 'Config\\{}\\Reader'.format(config_class_name), extends='\\Magento\\Framework\\Config\\Reader\\Filesystem', attributes=[ "protected $_idAttributes = [\n '/config/{}' => 'id',\n ];" .format(node_name) ]) reader_class.add_method( Phpmethod( '__construct', params=[ '\\Magento\\Framework\\Config\\FileResolverInterface $fileResolver', 'Converter $converter', 'SchemaLocator $schemaLocator', '\\Magento\\Framework\\Config\\ValidationStateInterface $validationState', "$fileName = '{}.xml'".format(config_name), '$idAttributes = []', "$domDocumentClass = 'Magento\\Framework\\Config\\Dom'", "$defaultScope = 'global'", ], body=""" parent::__construct( $fileResolver, $converter, $schemaLocator, $validationState, $fileName, $idAttributes, $domDocumentClass, $defaultScope ); """)) self.add_class(reader_class)