/** * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject * @return void */ protected function addInitStorageObjectCalls(Model\DomainObject $domainObject) { $anyToManyRelationProperties = $domainObject->getAnyToManyRelationProperties(); if (count($anyToManyRelationProperties) > 0) { if (!$this->classObject->methodExists('__construct')) { $constructorMethod = $this->templateClassObject->getMethod('__construct'); $constructorMethod->setDescription('__construct'); $this->classObject->addMethod($constructorMethod); } else { $constructorMethod = $this->classObject->getMethod('__construct'); } if (preg_match('/\\$this->initStorageObjects()/', $this->printerService->render($constructorMethod->getBodyStmts())) < 1) { $this->classObject->setMethod($this->classObject->getMethod('__construct')); } $initStorageObjectsMethod = clone $this->templateClassObject->getMethod('initStorageObjects'); $methodBodyStmts = array(); $templateBodyStmts = $initStorageObjectsMethod->getBodyStmts(); $initStorageObjectsMethod->setModifier('protected'); foreach ($anyToManyRelationProperties as $relationProperty) { $methodBodyStmts = array_merge($methodBodyStmts, $this->parserService->replaceNodeProperty($templateBodyStmts, array('children' => $relationProperty->getName()), array('Expr_PropertyFetch', 'Expr_Variable'))); } $initStorageObjectsMethod->setBodyStmts($methodBodyStmts); $this->classObject->setMethod($initStorageObjectsMethod); } elseif ($this->classObject->methodExists('initStorageObjects')) { $this->classObject->getMethod('initStorageObjects')->setBodyStmts(array()); } }
/** * @param $fileName * @param string $subFolder * @return \EBT\ExtensionBuilder\Domain\Model\File */ protected function parseAndWrite($fileName, $subFolder = '') { $classFilePath = $this->fixturesPath . $subFolder . $fileName; $this->assertTrue(file_exists($classFilePath)); $fileHandler = fopen($classFilePath, 'r'); $classFileObject = $this->parserService->parseFile($classFilePath); $newClassFilePath = $this->tmpDir . $fileName; file_put_contents($newClassFilePath, $this->printerService->renderFileObject($classFileObject, true)); return $classFileObject; }
protected function setUp($settingFile = '') { if (!class_exists('PHPParser_Parser')) { \EBT\ExtensionBuilder\Parser\AutoLoader::register(); } if (!class_exists('PHPParser_Parser')) { die('Parser not found!!'); } $this->fixturesPath = PATH_typo3conf . 'ext/extension_builder/Tests/Fixtures/'; $this->extension = $this->getMock('EBT\\ExtensionBuilder\\Domain\\Model\\Extension', array('getExtensionDir')); $extensionKey = 'dummy'; vfsStream::setup('testDir'); $dummyExtensionDir = vfsStream::url('testDir') . '/'; $this->extension->setVendorName('EBT'); $this->extension->setExtensionKey($extensionKey); $this->extension->expects($this->any())->method('getExtensionDir')->will($this->returnValue($dummyExtensionDir)); if (is_dir($dummyExtensionDir)) { GeneralUtility::mkdir($dummyExtensionDir, TRUE); } $yamlParser = new \EBT\ExtensionBuilder\Utility\SpycYAMLParser(); $settings = $yamlParser->YAMLLoadString(file_get_contents($this->fixturesPath . 'Settings/settings1.yaml')); $this->extension->setSettings($settings); $configurationManager = GeneralUtility::makeInstance('EBT\\ExtensionBuilder\\Configuration\\ConfigurationManager'); $this->roundTripService = $this->getMock($this->buildAccessibleProxy('EBT\\ExtensionBuilder\\Service\\RoundTrip'), array('dummy')); $this->classBuilder = GeneralUtility::makeInstance('EBT\\ExtensionBuilder\\Service\\ClassBuilder'); $this->classBuilder->injectConfigurationManager($configurationManager); $this->roundTripService->injectClassBuilder($this->classBuilder); $this->roundTripService->injectConfigurationManager($configurationManager); $this->templateParser = $this->getMock($this->buildAccessibleProxy('TYPO3\\CMS\\Fluid\\Core\\Parser\\TemplateParser'), array('dummy')); $this->fileGenerator = $this->getMock($this->buildAccessibleProxy('EBT\\ExtensionBuilder\\Service\\FileGenerator'), array('dummy')); $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); $this->objectManager = clone $objectManager; $this->parserService = new \EBT\ExtensionBuilder\Service\Parser(new \PHPParser_Lexer()); $this->printerService = new \EBT\ExtensionBuilder\Service\Printer(); $this->printerService->injectNodeFactory(new \EBT\ExtensionBuilder\Parser\NodeFactory()); $localizationService = $this->objectManager->get('EBT\\ExtensionBuilder\\Service\\LocalizationService'); $this->fileGenerator->injectObjectManager($this->objectManager); $this->fileGenerator->injectPrinterService($this->printerService); $this->fileGenerator->injectLocalizationService($localizationService); $this->roundTripService->injectParserService($this->parserService); $this->roundTripService->initialize($this->extension); $this->classBuilder->injectRoundtripService($this->roundTripService); $this->classBuilder->injectParserService($this->parserService); $this->classBuilder->injectPrinterService($this->printerService); $this->classBuilder->injectClassFactory(new \EBT\ExtensionBuilder\Parser\ClassFactory()); $this->classBuilder->initialize($this->fileGenerator, $this->extension, TRUE); $this->fileGenerator->injectClassBuilder($this->classBuilder); $this->codeTemplateRootPath = PATH_typo3conf . 'ext/extension_builder/Resources/Private/CodeTemplates/Extbase/'; $this->modelClassTemplatePath = $this->codeTemplateRootPath . 'Classes/Domain/Model/Model.phpt'; $this->fileGenerator->setSettings(array('codeTemplateRootPath' => $this->codeTemplateRootPath, 'extConf' => array('enableRoundtrip' => '1'))); $this->fileGenerator->_set('codeTemplateRootPath', PATH_typo3conf . 'ext/extension_builder/Resources/Private/CodeTemplates/Extbase/'); $this->fileGenerator->_set('enableRoundtrip', true); $this->fileGenerator->_set('extension', $this->extension); }
/** * Generates the code for the repository class * Either from domainRepository template or from class partial * * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject * @param bool $mergeWithExistingClass * * @return string */ public function generateDomainRepositoryCode(\EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject) { $repositoryTemplateClassPath = $this->codeTemplateRootPath . 'Classes/Domain/Repository/Repository.phpt'; $existingClassFileObject = null; if ($this->roundTripEnabled) { $existingClassFileObject = $this->roundTripService->getRepositoryClassFile($domainObject); } $repositoryClassFileObject = $this->classBuilder->generateRepositoryClassFileObject($domainObject, $repositoryTemplateClassPath, $existingClassFileObject); if ($repositoryClassFileObject) { $this->addLicenseHeader($repositoryClassFileObject->getFirstClass()); return $this->printerService->renderFileObject($repositoryClassFileObject, true); } else { throw new \Exception('Class file for repository could not be generated'); } }