Beispiel #1
0
 /**
  * @param \PhpParser\Node\Stmt $node
  * @param \EBT\ExtensionBuilder\Domain\Model\FunctionObject $object
  * @return \EBT\ExtensionBuilder\Domain\Model\AbstractObject
  */
 protected function setFunctionProperties(\PhpParser\Node\Stmt $node, Model\FunctionObject $object)
 {
     if (property_exists($node, 'type')) {
         $object->setModifiers($node->type);
     }
     $object->setBodyStmts($node->stmts);
     $position = 0;
     $object->setStartLine($node->getAttribute('startLine'));
     $object->setEndLine($node->getAttribute('endLine'));
     $getVarTypeFromParamTag = false;
     $paramTags = array();
     if ($object->isTaggedWith('param') && is_array($object->getTagValues('param'))) {
         $paramTags = $object->getTagValues('param');
         if (count($paramTags) == count($node->params)) {
             $getVarTypeFromParamTag = true;
         }
     }
     /** @var $param \PhpParser\NodeAbstract */
     foreach ($node->params as $param) {
         $parameter = new Model\ClassObject\MethodParameter($param->name);
         $parameter->setPosition($position);
         $parameter->setStartLine($param->getAttribute('startLine'));
         $parameter->setEndLine($param->getAttribute('endLine'));
         if (!is_null($param->type)) {
             $parameter->setTypeHint(NodeConverter::getValueFromNode($param->type));
             if (!$getVarTypeFromParamTag) {
                 $parameter->setVarType(NodeConverter::getValueFromNode($param->type));
             }
         } elseif ($getVarTypeFromParamTag) {
             // if there is not type hint but a varType in the param tag,
             // we set the varType of the parameter
             $paramTag = explode(' ', $paramTags[$position]);
             if ($paramTag[0] !== '$' . $param->name) {
                 $parameter->setVarType($paramTag[0]);
                 $parameter->setTypeForParamTag($paramTag[0]);
             }
         }
         if (!is_null($param->default)) {
             $parameter->setDefaultValue($param->default);
         }
         $object->setParameter($parameter);
         $position++;
     }
     $object->updateParamTags();
     return $object;
 }
Beispiel #2
0
 /**
  * replace a single parameter, depending on position
  *
  * @param \EBT\ExtensionBuilder\Domain\Model\ClassObject\MethodParameter $parameter
  * @return void
  */
 public function replaceParameter(MethodParameter $parameter)
 {
     $this->parameters[$parameter->getPosition()] = $parameter;
 }
Beispiel #3
0
 /**
  *
  * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject\Relation\AbstractRelation $domainProperty
  *
  * @return \EBT\ExtensionBuilder\Domain\Model\ClassObject\Method
  */
 protected function buildRemoveMethod($domainProperty)
 {
     $propertyName = $domainProperty->getName();
     $removeMethodName = self::getMethodName($domainProperty, 'remove');
     $parameterName = \EBT\ExtensionBuilder\Utility\Tools::getParameterName($domainProperty, 'remove');
     if ($this->classObject->methodExists($removeMethodName)) {
         $removeMethod = $this->classObject->getMethod($removeMethodName);
     } else {
         $removeMethod = clone $this->templateClassObject->getMethod('removeChild');
         $removeMethod->setName('remove' . ucfirst(\EBT\ExtensionBuilder\Utility\Inflector::singularize($propertyName)));
         $removeMethod->setTag('param', \EBT\ExtensionBuilder\Utility\Tools::getParamTag($domainProperty, 'remove'), true);
         $removeMethod->setTag('return', 'void');
         $removeMethod->addModifier('public');
         $removeMethod->getParameterByPosition(0)->setName($parameterName)->setVarType($domainProperty->getForeignClassName())->setTypeHint($domainProperty->getForeignClassName());
         $removeMethod->updateParamTags();
         $this->updateMethodBody($removeMethod, array('childToRemove' => $parameterName, 'child' => $domainProperty->getForeignModelName(), 'children' => $propertyName, 'Child' => $domainProperty->getForeignModelName()));
         $this->updateDocComment($removeMethod, array('\\bchildToRemove\\b' => $parameterName, '\\bChild\\b' => $domainProperty->getForeignModelName()));
     }
     $removeParameters = $removeMethod->getParameterNames();
     if (!in_array(\EBT\ExtensionBuilder\Utility\Tools::getParameterName($domainProperty, 'remove'), $removeParameters)) {
         $removeParameter = new Model\ClassObject\MethodParameter(\EBT\ExtensionBuilder\Utility\Tools::getParameterName($domainProperty, 'remove'), true);
         $removeParameter->setName(\EBT\ExtensionBuilder\Utility\Tools::getParameterName($domainProperty, 'remove'))->setVarType($domainProperty->getForeignClassName())->setTypeHint($domainProperty->getForeignClassName())->setTypeForParamTag($domainProperty->getTypeForComment());
         $removeMethod->setParameter($removeParameter);
     }
     if (!$removeMethod->hasDescription()) {
         $removeMethod->setDescription('Removes a ' . $domainProperty->getForeignModelName());
     }
     return $removeMethod;
 }
Beispiel #4
0
 /**
  * @param \EBT\ExtensionBuilder\Domain\Model\ClassObject\MethodParameter $parameter
  * @return \PhpParser\Node\Param
  */
 public function buildParameterNode(\EBT\ExtensionBuilder\Domain\Model\ClassObject\MethodParameter $parameter)
 {
     $factory = new \PhpParser\BuilderFactory();
     $paramNodeBuilder = $factory->param($parameter->getName());
     if ($parameter->hasTypeHint()) {
         $paramNodeBuilder->setTypeHint($parameter->getTypeHint());
     }
     if ($parameter->isPassedByReference()) {
         $paramNodeBuilder->makeByRef();
     }
     if (!is_null($parameter->getDefaultValue())) {
         $paramNodeBuilder->setDefault($parameter->getDefaultValue());
     }
     $parameterNode = $paramNodeBuilder->getNode();
     $parameterNode->setAttribute('startLine', $parameter->getStartLine());
     $parameterNode->setAttribute('endLine', $parameter->getEndLine());
     return $parameterNode;
 }
Beispiel #5
0
 /**
  * If a domainObject was renamed
  *
  * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $oldDomainObject
  * @param \EBT\ExtensionBuilder\Domain\Model\DomainObject $currentDomainObject
  * @return void
  */
 protected function mapOldControllerToCurrentClassObject(Model\DomainObject $oldDomainObject, Model\DomainObject $currentDomainObject)
 {
     $extensionDir = $this->previousExtensionDirectory;
     $newClassName = $currentDomainObject->getName() . 'Controller';
     $newName = $currentDomainObject->getName();
     $oldName = $oldDomainObject->getName();
     $this->classObject->setName($newClassName);
     $this->classObject->setDescription($this->replaceUpperAndLowerCase($oldName, $newName, $this->classObject->getDescription()));
     if ($oldDomainObject->isAggregateRoot()) {
         // should we keep the old properties comments and tags?
         $this->classObject->removeProperty(GeneralUtility::lcfirst($oldName) . 'Repository');
         $injectMethodName = 'inject' . $oldName . 'Repository';
         if ($currentDomainObject->isAggregateRoot()) {
             // update the initializeAction method body
             $initializeMethod = $this->classObject->getMethod('initializeAction');
             if ($initializeMethod != null) {
                 $initializeMethodBodyStmts = $initializeMethod->getBodyStmts();
                 $initializeMethodBodyStmts = str_replace(GeneralUtility::lcfirst($oldName) . 'Repository', GeneralUtility::lcfirst($newName) . 'Repository', $initializeMethodBodyStmts);
                 $initializeMethod->setBodyStmts($initializeMethodBodyStmts);
                 $this->classObject->setMethod($initializeMethod);
             }
             $injectMethod = $this->classObject->getMethod($injectMethodName);
             if ($injectMethod != null) {
                 $this->classObject->removeMethod($injectMethodName);
                 $initializeMethodBodyStmts = str_replace(GeneralUtility::lcfirst($oldName), GeneralUtility::lcfirst($newName), $injectMethod->getBodyStmts());
                 $injectMethod->setBodyStmts($initializeMethodBodyStmts);
                 $injectMethod->setTag('param', $currentDomainObject->getFullyQualifiedDomainRepositoryClassName() . ' $' . $newName . 'Repository');
                 $injectMethod->setName('inject' . $newName . 'Repository');
                 $parameter = new Model\ClassObject\MethodParameter(GeneralUtility::lcfirst($newName) . 'Repository');
                 $parameter->setTypeHint($currentDomainObject->getFullyQualifiedDomainRepositoryClassName());
                 $parameter->setPosition(0);
                 $injectMethod->replaceParameter($parameter);
                 $this->classObject->setMethod($injectMethod);
             }
             foreach ($oldDomainObject->getActions() as $action) {
                 // here we have to update all the occurences of domain object names in action methods
                 $actionMethod = $this->classObject->getMethod($action->getName() . 'Action');
                 if ($actionMethod != null) {
                     $actionMethodBody = $actionMethod->getBodyStmts();
                     $newActionMethodBody = str_replace(GeneralUtility::lcfirst($oldName) . 'Repository', GeneralUtility::lcfirst($newName) . 'Repository', $actionMethodBody);
                     $actionMethod->setBodyStmts($newActionMethodBody);
                     $actionMethod->setTag('param', $currentDomainObject->getQualifiedClassName());
                     $parameters = $actionMethod->getParameters();
                     foreach ($parameters as &$parameter) {
                         if (strpos($parameter->getTypeHint(), $oldDomainObject->getFullQualifiedClassName()) > -1) {
                             $parameter->setTypeHint($currentDomainObject->getFullQualifiedClassName());
                             $parameter->setName($this->replaceUpperAndLowerCase($oldName, $newName, $parameter->getName()));
                             $actionMethod->replaceParameter($parameter);
                         }
                     }
                     $tags = $actionMethod->getTags();
                     foreach ($tags as $tagName => $tagValue) {
                         $tags[$tagName] = $this->replaceUpperAndLowerCase($oldName, $newName, $tagValue);
                     }
                     $actionMethod->setTags($tags);
                     $actionMethod->setDescription($this->replaceUpperAndLowerCase($oldName, $newName, $actionMethod->getDescription()));
                     //TODO: this is not safe. Could rename unwanted variables
                     $actionMethod->setBodyStmts($this->replaceUpperAndLowerCase($oldName, $newName, $actionMethod->getBodyStmts()));
                     $this->classObject->setMethod($actionMethod);
                 }
             }
         } else {
             $this->classObject->removeMethod('initializeAction');
             $this->classObject->removeMethod($injectMethodName);
             $this->cleanUp(FileGenerator::getFolderForClassFile($extensionDir, 'Repository'), $oldName . 'Repository.php');
         }
     }
     $this->classObject->setFileName($newName . 'Controller.php');
     $this->cleanUp(FileGenerator::getFolderForClassFile($extensionDir, 'Controller'), $oldName . 'Controller.php');
 }