/** * Make a word human readable * * @param string $string The string to make human readable * @return string The human readable string */ public function render($string = NULL) { if ($string === NULL) { $string = $this->renderChildren(); } return $this->inflector->humanize($string); }
/** * Pluralize a word * * @return string The pluralized string * @author Christopher Hlubek <*****@*****.**> */ public function render() { $content = $this->renderChildren(); $pluralizedContent = $this->inflector->pluralize($content); if ($pluralizedContent == $content) { $pluralizedContent .= 's'; } return $pluralizedContent; }
/** * * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject\AbstractProperty $property * @param string $methodType (set,add,remove) * @return string method body */ public static function getParameterName(AbstractProperty $domainProperty, $methodType) { $propertyName = $domainProperty->getName(); switch ($methodType) { case 'set': return $propertyName; case 'add': return \EBT\ExtensionBuilder\Utility\Inflector::singularize($propertyName); case 'remove': return \EBT\ExtensionBuilder\Utility\Inflector::singularize($propertyName) . 'ToRemove'; } }
/** * @param \EBT\ExtensionBuilder\Domain\Model\Extension $extension * @param string $type * @throws \InvalidArgumentException */ public function prepareLabelArray($extension, $type = 'locallang') { $labelArray = array(); foreach ($extension->getDomainObjects() as $domainObject) { /* @var \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject */ $labelArray[$domainObject->getLabelNamespace()] = $this->inflector->humanize($domainObject->getName()); foreach ($domainObject->getProperties() as $property) { $labelArray[$property->getLabelNamespace()] = $this->inflector->humanize($property->getName()); } if ($type == 'locallang_db') { $tableToMapTo = $domainObject->getMapToTable(); if (!empty($tableToMapTo)) { $labelArray[$tableToMapTo . '.tx_extbase_type.' . $domainObject->getRecordType()] = $extension->getName() . ' ' . $domainObject->getName(); } if (count($domainObject->getChildObjects()) > 0) { $labelArray[$extension->getShortExtensionKey() . '.tx_extbase_type'] = 'Record Type'; $labelArray[$extension->getShortExtensionKey() . '.tx_extbase_type.0'] = 'Default'; $labelArray[$domainObject->getLabelNamespace() . '.tx_extbase_type.' . $domainObject->getRecordType()] = $extension->getName() . ' ' . $domainObject->getName(); } } } return $labelArray; }
/** * Write a simple model class for a non aggregate root domain object with one to one relation * * @test */ function writeModelClassWithManyToManyRelation() { $modelName = 'ModelCgt5'; $relatedModelName = 'RelatedModel'; $propertyName = 'relNames'; $domainObject = $this->buildDomainObject($modelName); $relatedDomainObject = $this->buildDomainObject($relatedModelName); $relation = new Relation\ManyToManyRelation($propertyName); $relation->setForeignModel($relatedDomainObject); $relation->setInlineEditing(false); $domainObject->addProperty($relation); $classFileContent = $this->fileGenerator->generateDomainObjectCode($domainObject, false); $modelClassDir = 'Classes/Domain/Model/'; \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($this->extension->getExtensionDir(), $modelClassDir); $absModelClassDir = $this->extension->getExtensionDir() . $modelClassDir; $this->assertTrue(is_dir($absModelClassDir), 'Directory ' . $absModelClassDir . ' was not created'); $modelClassPath = $absModelClassDir . $domainObject->getName() . '.php'; \TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($modelClassPath, $classFileContent); $this->assertFileExists($modelClassPath, 'File was not generated: ' . $modelClassPath); $className = $domainObject->getFullQualifiedClassName(); if (!class_exists($className)) { include $modelClassPath; } $this->assertTrue(class_exists($className), 'Class was not generated:' . $className); $reflection = new \TYPO3\CMS\Extbase\Reflection\ClassReflection($className); $this->assertTrue($reflection->hasMethod('add' . ucfirst(Inflector::singularize($propertyName))), 'Add method was not generated'); $this->assertTrue($reflection->hasMethod('remove' . ucfirst(Inflector::singularize($propertyName))), 'Remove method was not generated'); $this->assertTrue($reflection->hasMethod('get' . ucfirst($propertyName)), 'Getter was not generated'); $this->assertTrue($reflection->hasMethod('set' . ucfirst($propertyName)), 'Setter was not generated'); $this->assertTrue($reflection->hasMethod('initStorageObjects'), 'initStorageObjects was not generated'); //checking methods $setterMethod = $reflection->getMethod('set' . ucfirst($propertyName)); $this->assertTrue($setterMethod->isTaggedWith('param'), 'No param tag set for setter method'); $paramTagValues = $setterMethod->getTagValues('param'); $this->assertEquals(0, strpos($paramTagValues[0], '\\TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage<' . $relatedDomainObject->getFullQualifiedClassName()), 'Wrong param tag:' . $paramTagValues[0]); $parameters = $setterMethod->getParameters(); $this->assertEquals(1, count($parameters), 'Wrong parameter count in setter method'); $parameter = current($parameters); $this->assertEquals($parameter->getName(), $propertyName, 'Wrong parameter name in setter method'); $addMethod = $reflection->getMethod('add' . ucfirst(Inflector::singularize($propertyName))); $this->assertTrue($addMethod->isTaggedWith('param'), 'No param tag set for setter method'); $paramTagValues = $addMethod->getTagValues('param'); $this->assertEquals(0, strpos($paramTagValues[0], $relatedDomainObject->getFullQualifiedClassName()), 'Wrong param tag:' . $paramTagValues[0]); $parameters = $addMethod->getParameters(); $this->assertEquals(1, count($parameters), 'Wrong parameter count in add method'); $parameter = current($parameters); $this->assertEquals($parameter->getName(), Inflector::singularize($propertyName), 'Wrong parameter name in add method'); $removeMethod = $reflection->getMethod('remove' . ucfirst(Inflector::singularize($propertyName))); $this->assertTrue($removeMethod->isTaggedWith('param'), 'No param tag set for remove method'); $paramTagValues = $removeMethod->getTagValues('param'); $this->assertEquals(0, strpos($paramTagValues[0], $relatedDomainObject->getFullQualifiedClassName()), 'Wrong param tag:' . $paramTagValues[0]); $parameters = $removeMethod->getParameters(); $this->assertEquals(1, count($parameters), 'Wrong parameter count in remove method'); $parameter = current($parameters); $this->assertEquals($parameter->getName(), Inflector::singularize($propertyName) . 'ToRemove', 'Wrong parameter name in remove method'); }
/** * Singularize a word * * @return string The pluralized string * @author Sebastian Kurfürst <*****@*****.**> */ public function render() { $content = $this->renderChildren(); return $this->inflector->singularize($content); }
/** * @test */ public function classBuilderGeneratesMethodsForRelationProperty() { $modelName2 = 'Model2'; $propertyName = 'relNames'; $domainObject1 = $this->buildDomainObject($this->modelName, true, true); $relatedDomainObject = $this->buildDomainObject($modelName2); $relationProperty = new \EBT\ExtensionBuilder\Domain\Model\DomainObject\Relation\ManyToManyRelation($propertyName); $relationProperty->setForeignModel($relatedDomainObject); $domainObject1->addProperty($relationProperty); $modelClassObject = $this->classBuilder->generateModelClassFileObject($domainObject1, $this->modelClassTemplatePath, FALSE)->getFirstClass(); $this->assertTrue($modelClassObject->methodExists('add' . ucfirst(Inflector::singularize($propertyName))), 'Add method was not generated'); $this->assertTrue($modelClassObject->methodExists('remove' . ucfirst(Inflector::singularize($propertyName))), 'Remove method was not generated'); $this->assertTrue($modelClassObject->methodExists('set' . ucfirst($propertyName)), 'Setter was not generated'); $this->assertTrue($modelClassObject->methodExists('set' . ucfirst($propertyName)), 'Setter was not generated'); $addMethod = $modelClassObject->getMethod('add' . ucfirst(Inflector::singularize($propertyName))); $this->assertTrue($addMethod->isTaggedWith('param'), 'No param tag set for setter method'); $paramTagValues = $addMethod->getTagValues('param'); $this->assertTrue(strpos($paramTagValues, $relatedDomainObject->getFullQualifiedClassName()) === 0, 'Wrong param tag:' . $paramTagValues); $parameters = $addMethod->getParameters(); $this->assertTrue(count($parameters) == 1, 'Wrong parameter count in add method'); $parameter = current($parameters); $this->assertTrue($parameter->getName() == Inflector::singularize($propertyName), 'Wrong parameter name in add method'); $this->assertTrue($parameter->getTypeHint() == $relatedDomainObject->getFullQualifiedClassName(), 'Wrong type hint for add method parameter:' . $parameter->getTypeHint()); }
/** * This method generates the repository class object, * which is passed to the template * it keeps all methods and properties including * user modified method bodies and comments * needed to create a repository class file * * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject * @param \EBT\ExtensionBuilder\Domain\Model\File $existingClassFileObject * * @return \EBT\ExtensionBuilder\Domain\Model\File */ public function generateRepositoryClassFileObject($domainObject, $repositoryTemplateClassPath, $existingClassFileObject = null) { $this->classObject = null; $className = $domainObject->getName() . 'Repository'; $this->templateFileObject = $this->parserService->parseFile($repositoryTemplateClassPath); $this->templateClassObject = $this->templateFileObject->getFirstClass(); if ($existingClassFileObject) { $this->classFileObject = $existingClassFileObject; $this->classObject = $existingClassFileObject->getFirstClass(); if ($this->classFileObject->getNamespace() === false) { $nameSpace = new \EBT\ExtensionBuilder\Domain\Model\NamespaceObject('dummy'); $this->classFileObject->addNamespace($nameSpace); } } if ($this->classObject == null) { $this->classFileObject = clone $this->templateFileObject; $this->classObject = clone $this->templateClassObject; $this->classObject->resetAll(); $this->classObject->setName($className); $this->classObject->setDescription('The repository for ' . Inflector::pluralize($domainObject->getName())); if (isset($this->settings['Repository']['parentClass'])) { $parentClass = $this->settings['Repository']['parentClass']; } else { $parentClass = '\\TYPO3\\CMS\\Extbase\\Persistence\\Repository'; } $this->classObject->setParentClassName($parentClass); } if ($domainObject->getSorting() && is_null($this->classObject->getProperty('defaultOrderings'))) { $defaultOrderings = $this->templateClassObject->getProperty('defaultOrderings'); $this->classObject->addProperty($defaultOrderings); } $this->classFileObject->getNamespace()->setName($this->extension->getNamespaceName() . '\\Domain\\Repository')->setClasses(array($this->classObject)); return $this->classFileObject; }
/** * This method generates the repository class object, * which is passed to the template * it keeps all methods and properties including * user modified method bodies and comments * needed to create a repository class file * * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $domainObject * @param boolean $mergeWithExistingClass * * @return \EBT\ExtensionBuilder\Domain\Model\File */ public function generateRepositoryClassFileObject($domainObject, $repositoryTemplateClassPath, $mergeWithExistingClass) { $this->classObject = NULL; $className = $domainObject->getName() . 'Repository'; $this->templateFileObject = $this->parserService->parseFile($repositoryTemplateClassPath); $this->templateClassObject = $this->templateFileObject->getFirstClass(); if ($mergeWithExistingClass) { try { $this->classFileObject = $this->roundTripService->getRepositoryClassFile($domainObject); if ($this->classFileObject instanceof Model\File) { $this->classObject = $this->classFileObject->getFirstClass(); } } catch (\Exception $e) { \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Class ' . $className . ' could not be imported: ' . $e->getMessage(), 'extension_builder'); } } if ($this->classObject == NULL) { $this->classFileObject = clone $this->templateFileObject; $this->classObject = clone $this->templateClassObject; $this->classObject->resetAll(); $this->classObject->setName($className); $this->classObject->setNamespaceName($this->extension->getNamespaceName() . '\\Domain\\Repository'); $this->classObject->setDescription('The repository for ' . Inflector::pluralize($domainObject->getName())); if (isset($this->settings['Repository']['parentClass'])) { $parentClass = $this->settings['Repository']['parentClass']; } else { $parentClass = '\\TYPO3\\CMS\\Extbase\\Persistence\\Repository'; } $this->classObject->setParentClassName($parentClass); } $this->classFileObject->getNamespace()->setName($this->extension->getNamespaceName() . '\\Domain\\Repository')->setClasses(array($this->classObject)); return $this->classFileObject; }
/** * Removes all related methods, if a property was removed * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject\AbstractProperty $propertyToRemove * * @return void */ protected function removePropertyAndRelatedMethods($propertyToRemove) { $propertyName = $propertyToRemove->getName(); $this->classObject->removeProperty($propertyName); if ($propertyToRemove->isAnyToManyRelation()) { $this->classObject->removeMethod('add' . ucfirst(Inflector::singularize($propertyName))); $this->classObject->removeMethod('remove' . ucfirst(Inflector::singularize($propertyName))); GeneralUtility::devLog('Methods removed: ' . 'add' . ucfirst(Inflector::singularize($propertyName)), 'extension_builder'); } $this->classObject->removeMethod('get' . ucfirst($propertyName)); $this->classObject->removeMethod('set' . ucfirst($propertyName)); if ($propertyToRemove->isBoolean()) { $this->classObject->removeMethod('is' . ucfirst($propertyName)); } GeneralUtility::devLog('Methods removed: ' . 'get' . ucfirst($propertyName), 'extension_builder'); }