/** * @param ClassManager $class * @return string * @throws RendererException */ protected function renderClass(ClassManager $class) { $template = $class->getTemplate(); $tags = $class->getTemplateTags(); $properties = []; $methods = []; $interfacePart = ''; $extendsPart = ''; foreach ($class->getProperties() as $property) { $properties[] = $this->render($property); } foreach ($class->getMethods() as $method) { $methods[] = $this->render($method); } if ($class->hasExtends()) { $extendsPart = sprintf(" extends %s", $class->getExtends()); } if ($class->hasInterface()) { $interfacePart = sprintf(" implements %s", $class->getInterface()->getNamespace()); } $args[RenderableInterface::TAG_NAMESPACE] = $class->getNamespaceWithoutNameAndBackslashPrefix(); $args[RenderableInterface::TAG_COMMENT] = empty($class->getComment()) ? "" : sprintf(" %s", $class->getComment()); $args[RenderableInterface::TAG_NAME] = $class->getName(); $args[RenderableInterface::TAG_EXTENDS] = $extendsPart; $args[RenderableInterface::TAG_INTERFACE] = $interfacePart; $args[RenderableInterface::TAG_CONSTRUCTOR] = $this->render($class->getConstructor()); $args[RenderableInterface::TAG_PROPERTIES] = Tools::implodeArrayToTemplate($properties); $args[RenderableInterface::TAG_METHODS] = Tools::implodeArrayToTemplate($methods); $args[RenderableInterface::TAG_MULTILINE_COMMENT] = $this->prepareMultilineCommentForElement($class); return $this->addNewLineAfter($this->replace($tags, $args, $template)); }
/** * @param string $content * @param ClassManager $item * @param ReflectionClass $reflectionClass * @return ClassConstructorManager */ protected function resolveConstructorToRender($content, ClassManager $item, ReflectionClass $reflectionClass) { $constructor = $item->getConstructor(); $constructorFromFile = $reflectionClass->getConstructor(); $source = Tools::explodeTemplateStringToArray($content); $constructorBody = implode("", array_slice($source, $constructorFromFile->getStartLine(), $constructorFromFile->getEndLine() - $constructorFromFile->getStartLine())); $matches = []; preg_match_all("/this->[\\S]+/", $constructorBody, $matches); $initPropertiesFromConstructor = []; if (count($matches) > 0) { foreach ($matches[0] as $match) { $initPropertiesFromConstructor[] = str_replace("this->", "", trim($match)); } } $newInitProperties = new ArrayCollection(); foreach ($constructor->getInitProperties() as $initProperty) { if (in_array($initProperty->getProperty()->getPreparedName(), $initPropertiesFromConstructor)) { continue; } $newInitProperties->add($initProperty); } $constructor->setInitProperties($newInitProperties); $item->setConstructor($constructor); return $constructor; }