protected function setUp() { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $this->varDirectory = $objectManager->get('Magento\\Framework\\Filesystem')->getDirectoryWrite(DirectoryList::VAR_DIR); $generationDirectory = $this->varDirectory->getAbsolutePath('generation'); $this->_ioObject = new \Magento\Framework\Code\Generator\Io(new \Magento\Framework\Filesystem\Driver\File(), $generationDirectory); $this->_generator = $objectManager->create('Magento\\Framework\\Code\\Generator', ['ioObject' => $this->_ioObject, 'generatedEntities' => [DIGenerator\Factory::ENTITY_TYPE => '\\Magento\\Framework\\ObjectManager\\Code\\Generator\\Factory', DIGenerator\Proxy::ENTITY_TYPE => '\\Magento\\Framework\\ObjectManager\\Code\\Generator\\Proxy', InterceptionGenerator\Interceptor::ENTITY_TYPE => '\\Magento\\Framework\\Interception\\Code\\Generator\\Interceptor']]); $this->_generator->setObjectManager($objectManager); }
/** * @expectedException \RuntimeException */ public function testGenerateClassWithError() { $expectedEntities = array_values($this->expectedEntities); $resultClassName = self::SOURCE_CLASS . ucfirst(array_shift($expectedEntities)); $objectManagerMock = $this->getMock('Magento\\Framework\\ObjectManagerInterface'); $entityGeneratorMock = $this->getMockBuilder('\\Magento\\Framework\\Code\\Generator\\EntityAbstract')->disableOriginalConstructor()->getMock(); $objectManagerMock->expects($this->once())->method('create')->willReturn($entityGeneratorMock); $this->model->setObjectManager($objectManagerMock); $this->model->generateClass($resultClassName); }
/** * Generate Code * * @param string $generationDir * @param array $fileExcludePatterns * @param InputInterface $input * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function generateCode($generationDir, $fileExcludePatterns, $input) { // 1.1 Code scan $filePatterns = ['php' => '/.*\\.php$/', 'di' => '/\\/etc\\/([a-zA-Z_]*\\/di|di)\\.xml$/']; $directoryScanner = new Scanner\DirectoryScanner(); foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $codeScanDir) { $this->files = array_merge_recursive($this->files, $directoryScanner->scan($codeScanDir, $filePatterns, $fileExcludePatterns)); } $this->files['di'][] = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::CONFIG) . '/di.xml'; $this->files['additional'] = [$input->getOption(self::INPUT_KEY_EXTRA_CLASSES_FILE)]; $repositoryScanner = new Scanner\RepositoryScanner(); $repositories = $repositoryScanner->collectEntities($this->files['di']); $scanner = new Scanner\CompositeScanner(); $scanner->addChild(new Scanner\PhpScanner($this->log), 'php'); $scanner->addChild(new Scanner\XmlScanner($this->log), 'di'); $scanner->addChild(new Scanner\ArrayScanner(), 'additional'); $this->entities = $scanner->collectEntities($this->files); $interceptorScanner = new Scanner\XmlInterceptorScanner(); $this->entities['interceptors'] = $interceptorScanner->collectEntities($this->files['di']); // 1.2 Generation of Factory and Additional Classes $generatorIo = new \Magento\Framework\Code\Generator\Io(new \Magento\Framework\Filesystem\Driver\File(), $generationDir); $this->generator = new \Magento\Framework\Code\Generator($generatorIo, [Interceptor::ENTITY_TYPE => 'Magento\\Framework\\Interception\\Code\\Generator\\Interceptor', Proxy::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Proxy', Factory::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Factory', Mapper::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\Mapper', Persistor::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Persistor', Repository::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Repository', Converter::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Converter', SearchResults::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\SearchResults', ExtensionAttributesInterfaceGenerator::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\ExtensionAttributesInterfaceGenerator', ExtensionAttributesGenerator::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\ExtensionAttributesGenerator']); /** Initialize object manager for code generation based on configs */ $this->generator->setObjectManager($this->objectManager); $generatorAutoloader = new \Magento\Framework\Code\Generator\Autoloader($this->generator); spl_autoload_register([$generatorAutoloader, 'load']); foreach ($repositories as $entityName) { switch ($this->generator->generateClass($entityName)) { case \Magento\Framework\Code\Generator::GENERATION_SUCCESS: $this->log->add(Log::GENERATION_SUCCESS, $entityName); break; case \Magento\Framework\Code\Generator::GENERATION_ERROR: $this->log->add(Log::GENERATION_ERROR, $entityName); break; case \Magento\Framework\Code\Generator::GENERATION_SKIP: default: //no log break; } } foreach (['php', 'additional'] as $type) { sort($this->entities[$type]); foreach ($this->entities[$type] as $entityName) { switch ($this->generator->generateClass($entityName)) { case \Magento\Framework\Code\Generator::GENERATION_SUCCESS: $this->log->add(Log::GENERATION_SUCCESS, $entityName); break; case \Magento\Framework\Code\Generator::GENERATION_ERROR: $this->log->add(Log::GENERATION_ERROR, $entityName); break; case \Magento\Framework\Code\Generator::GENERATION_SKIP: default: //no log break; } } } }
if ($opt->getOption('g')) { $generationDir = $opt->getOption('g'); } AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', $generationDir . '/Magento/'); } catch (\Zend_Console_Getopt_Exception $e) { $generator = new Generator(); $entities = $generator->getGeneratedEntities(); $allowedTypes = 'Allowed entity types are: ' . implode(', ', $entities) . '.'; $example = 'Example: php -f entity_generator.php -- -t factory -c \\Magento\\Framework\\Event\\Observer ' . '-g /var/mage/m2ee/generation' . ' - will generate file /var/mage/m2ee/generation/Magento/Framework/Event/ObserverFactory.php'; echo $e->getMessage() . "\n"; echo $e->getUsageMessage() . "\n"; echo $allowedTypes . "\n"; echo 'Default generation dir is ' . $generationDir . "\n"; exit($example); } //reinit generator with correct generation path $io = new Io(new File(), $generationDir); $generator = new Generator($io, [Proxy::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Proxy', Factory::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Factory', Interceptor::ENTITY_TYPE => 'Magento\\Framework\\Interception\\Code\\Generator\\Interceptor', Mapper::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\Mapper', Repository::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Repository', Converter::ENTITY_TYPE => 'Magento\\Framework\\ObjectManager\\Code\\Generator\\Converter', SearchResults::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\SearchResults', ExtensionAttributesInterfaceGenerator::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\ExtensionAttributesInterfaceGenerator', ExtensionAttributesGenerator::ENTITY_TYPE => 'Magento\\Framework\\Api\\Code\\Generator\\ExtensionAttributesGenerator']); /** Initialize object manager for code generation based on configs */ $magentoObjectManagerFactory = \Magento\Framework\App\Bootstrap::createObjectManagerFactory(BP, $_SERVER); $objectManager = $magentoObjectManagerFactory->create($_SERVER); $generator->setObjectManager($objectManager); try { if (Generator::GENERATION_SUCCESS == $generator->generateClass($className)) { print "Class {$className} was successfully generated.\n"; } else { print "Can't generate class {$className}. This class either not generated entity, or it already exists.\n"; } } catch (Exception $e) { print "Error! {$e->getMessage()}\n"; }