/** * Build a Code Generation Php Object from a Class Reflection * * @param ClassReflection $classReflection * @return InterfaceGenerator */ public static function fromReflection(ClassReflection $classReflection) { if (!$classReflection->isInterface()) { throw new Exception\InvalidArgumentException(sprintf('Class %s is not a interface', $classReflection->getName())); } // class generator $cg = new static($classReflection->getName()); $methods = []; $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock())); } // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } foreach ($classReflection->getConstants() as $name => $value) { $cg->addConstant($name, $value); } $cg->addMethods($methods); return $cg; }
/** * Build a Code Generation Php Object from a Class Reflection * * @param ClassReflection $classReflection * @return TraitGenerator */ public static function fromReflection(ClassReflection $classReflection) { // class generator $cg = new static($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock())); } // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $properties[] = PropertyGenerator::fromReflection($reflectionProperty); } } $cg->addProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } $cg->addMethods($methods); return $cg; }
/** * Generate code to cache from class reflection. * This is a total mess, I know. Just wanted to flesh out the logic. * * @todo Refactor into a class, clean up logic, DRY it up, maybe move * some of this into Zend\Code * @param ClassReflection $r * @return string */ protected static function getCacheCode(ClassReflection $r) { $useString = ''; $usesNames = array(); if (count($uses = $r->getDeclaringFile()->getUses())) { foreach ($uses as $use) { $usesNames[$use['use']] = $use['as']; $useString .= "use {$use['use']}"; if ($use['as']) { $useString .= " as {$use['as']}"; } $useString .= ";\n"; } } $declaration = ''; if ($r->isAbstract() && !$r->isInterface()) { $declaration .= 'abstract '; } if ($r->isFinal()) { $declaration .= 'final '; } if ($r->isInterface()) { $declaration .= 'interface '; } if (!$r->isInterface()) { $declaration .= 'class '; } $declaration .= $r->getShortName(); $parentName = false; if (($parent = $r->getParentClass()) && $r->getNamespaceName()) { $parentName = array_key_exists($parent->getName(), $usesNames) ? $usesNames[$parent->getName()] ?: $parent->getShortName() : (0 === strpos($parent->getName(), $r->getNamespaceName()) ? substr($parent->getName(), strlen($r->getNamespaceName()) + 1) : '\\' . $parent->getName()); } else { if ($parent && !$r->getNamespaceName()) { $parentName = '\\' . $parent->getName(); } } if ($parentName) { $declaration .= " extends {$parentName}"; } $interfaces = array_diff($r->getInterfaceNames(), $parent ? $parent->getInterfaceNames() : array()); if (count($interfaces)) { foreach ($interfaces as $interface) { $iReflection = new ClassReflection($interface); $interfaces = array_diff($interfaces, $iReflection->getInterfaceNames()); } $declaration .= $r->isInterface() ? ' extends ' : ' implements '; $declaration .= implode(', ', array_map(function ($interface) use($usesNames, $r) { $iReflection = new ClassReflection($interface); return array_key_exists($iReflection->getName(), $usesNames) ? $usesNames[$iReflection->getName()] ?: $iReflection->getShortName() : (0 === strpos($iReflection->getName(), $r->getNamespaceName()) ? substr($iReflection->getName(), strlen($r->getNamespaceName()) + 1) : '\\' . $iReflection->getName()); }, $interfaces)); } $classContents = $r->getContents(false); $classFileDir = dirname($r->getFileName()); $classContents = trim(str_replace('__DIR__', sprintf("'%s'", $classFileDir), $classContents)); $return = "\nnamespace " . $r->getNamespaceName() . " {\n" . $useString . $declaration . "\n" . $classContents . "\n}\n"; return $return; }
/** * Returns class' code inside namespace * * @param ClassReflection $class */ private function dumpClass(ClassReflection $class) { if (array_search($class->getName(), $this->cachedClasses) !== false) { return; } if ($class->isInternal()) { return; } if ($class->getParentClass()) { $this->dumpClass($class->getParentClass()); } foreach ($class->getInterfaces() as $interface) { $this->dumpClass($interface); } if ($class->getTraits()) { foreach ($class->getTraits() as $trait) { $this->dumpClass($trait); } } $classContents = $class->getContents(false); $classFileDir = dirname($class->getFileName()); $classContents = trim(str_replace('__DIR__', sprintf("'%s'", $classFileDir), $classContents)); $uses = implode("\n", $this->codeGenerator->getUseLines($class)); $this->cache[] = "namespace " . $class->getNamespaceName() . " {\n" . ($uses ? $uses . "\n" : '') . $this->codeGenerator->getDeclarationLine($class) . "\n" . $classContents . "\n}\n"; $this->cachedClasses[] = $class->getName(); }
/** * Generate view content * * @param string $moduleName * @param ClassReflection $loadedEntity */ protected function addContent($moduleName, ClassReflection $loadedEntity) { // prepare some params $moduleIdentifier = $this->filterCamelCaseToUnderscore($moduleName); $entityName = $loadedEntity->getShortName(); $entityParam = lcfirst($entityName); $formParam = lcfirst(str_replace('Entity', '', $loadedEntity->getShortName())) . 'DataForm'; $moduleRoute = $this->filterCamelCaseToDash($moduleName); // set action body $body = []; $body[] = 'use ' . $loadedEntity->getName() . ';'; $body[] = ''; $body[] = '/** @var ' . $entityName . ' $' . $entityParam . ' */'; $body[] = '$' . $entityParam . ' = $this->' . $entityParam . ';'; $body[] = ''; $body[] = '$this->h1(\'' . $moduleIdentifier . '_title_update\');'; $body[] = ''; $body[] = '$this->' . $formParam . '->setAttribute(\'action\', $this->url(\'' . $moduleRoute . '/update\', [\'id\' => $' . $entityParam . '->getIdentifier()]));'; $body[] = ''; $body[] = 'echo $this->bootstrapForm($this->' . $formParam . ');'; $body[] = '?>'; $body[] = '<p>'; $body[] = ' <a class="btn btn-primary" href="<?php echo $this->url(\'' . $moduleRoute . '\'); ?>">'; $body[] = ' <i class="fa fa-table"></i>'; $body[] = ' <?php echo $this->translate(\'' . $moduleIdentifier . '_action_index\'); ?>'; $body[] = ' </a>'; $body[] = '</p>'; $body = implode(AbstractGenerator::LINE_FEED, $body); // add method $this->setContent($body); }
/** * Generate view content * * @param string $moduleName * @param ClassReflection $loadedEntity */ protected function addContent($moduleName, ClassReflection $loadedEntity) { // prepare some params $moduleIdentifier = $this->filterCamelCaseToUnderscore($moduleName); $entityName = $loadedEntity->getShortName(); $entityParam = lcfirst($entityName); $listParam = lcfirst(str_replace('Entity', '', $entityName)) . 'List'; $moduleRoute = $this->filterCamelCaseToDash($moduleName); // set action body $body = []; $body[] = 'use ' . $loadedEntity->getName() . ';'; $body[] = ''; $body[] = '$this->h1(\'' . $moduleIdentifier . '_title_index\');'; $body[] = '?>'; $body[] = '<table class="table table-bordered table-striped">'; $body[] = ' <thead>'; $body[] = ' <tr>'; foreach ($loadedEntity->getProperties() as $property) { $body[] = ' <th><?php echo $this->translate(\'' . $moduleIdentifier . '_label_' . $this->filterCamelCaseToUnderscore($property->getName()) . '\'); ?></th>'; } $body[] = ' <th width="95">'; $body[] = ' <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/create\'); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_create\'); ?>">'; $body[] = ' <i class="fa fa-plus"></i>'; $body[] = ' </a>'; $body[] = ' </th>'; $body[] = ' </tr>'; $body[] = ' </thead>'; $body[] = ' <tbody>'; $body[] = ' <?php /** @var ' . $entityName . ' $' . $entityParam . ' */ ?>'; $body[] = ' <?php foreach ($this->' . $listParam . ' as $' . $entityParam . '): ?>'; $body[] = ' <tr>'; foreach ($loadedEntity->getProperties() as $property) { $methodName = 'get' . ucfirst($property->getName()); $body[] = ' <td><?php echo $' . $entityParam . '->' . $methodName . '() ?></td>'; } $body[] = ' <td>'; $body[] = ' <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/show\', [\'id\' => $' . $entityParam . '->getIdentifier()]); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_show\'); ?>">'; $body[] = ' <i class="fa fa-search"></i>'; $body[] = ' </a>'; $body[] = ' <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/update\', [\'id\' => $' . $entityParam . '->getIdentifier()]); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_update\'); ?>">'; $body[] = ' <i class="fa fa-pencil"></i>'; $body[] = ' </a>'; $body[] = ' <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/delete\', [\'id\' => $' . $entityParam . '->getIdentifier()]); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_delete\'); ?>">'; $body[] = ' <i class="fa fa-trash"></i>'; $body[] = ' </a>'; $body[] = ' </td>'; $body[] = ' </tr>'; $body[] = ' <?php endforeach; ?>'; $body[] = ' </tbody>'; $body[] = '</table>'; $body = implode(AbstractGenerator::LINE_FEED, $body); // add method $this->setContent($body); }
private function shouldSkip(ClassReflection $class) { if ($class->isInternal()) { return true; } foreach ($this->skip as $prefix) { if (strpos($class->getName(), $prefix) === 0) { return true; } } return false; }
/** * @param ClassReflection $class * @param string $namespaceName * @param array $useArray * @return string */ private function getClassNameInContext(ClassReflection $class, $namespaceName, $useArray) { if (!$namespaceName) { return '\\' . $class->getName(); } return array_key_exists($class->getName(), $useArray) ? $useArray[$class->getName()] ?: $class->getShortName() : (0 === strpos($class->getName(), $namespaceName) ? substr($class->getName(), strlen($namespaceName) + 1) : '\\' . $class->getName()); }
/** * Generate view content * * @param string $moduleName * @param ClassReflection $loadedEntity */ protected function addContent($moduleName, ClassReflection $loadedEntity) { // prepare some params $moduleIdentifier = $this->filterCamelCaseToUnderscore($moduleName); $entityName = $loadedEntity->getShortName(); $entityParam = lcfirst($entityName); $formParam = lcfirst(str_replace('Entity', '', $loadedEntity->getShortName())) . 'DeleteForm'; $moduleRoute = $this->filterCamelCaseToDash($moduleName); $deleteMessage = $moduleRoute . '_message_' . $moduleRoute . '_deleting_possible'; // set action body $body = []; $body[] = 'use ' . $loadedEntity->getName() . ';'; $body[] = ''; $body[] = '/** @var ' . $entityName . ' $' . $entityParam . ' */'; $body[] = '$' . $entityParam . ' = $this->' . $entityParam . ';'; $body[] = ''; $body[] = '$this->h1(\'' . $moduleIdentifier . '_title_delete\');'; $body[] = ''; $body[] = '$this->' . $formParam . '->setAttribute(\'action\', $this->url(\'' . $moduleRoute . '/delete\', [\'id\' => $' . $entityParam . '->getIdentifier()]));'; $body[] = ''; $body[] = '?>'; $body[] = '<div class="well">'; $body[] = ' <table class="table">'; $body[] = ' <tbody>'; foreach ($loadedEntity->getProperties() as $property) { $methodName = 'get' . ucfirst($property->getName()); $body[] = ' <tr>'; $body[] = ' <th class="col-sm-2 text-right"><?php echo $this->translate(\'' . $moduleIdentifier . '_label_' . $this->filterCamelCaseToUnderscore($property->getName()) . '\'); ?></th>'; $body[] = ' <td class="col-sm-10"><?php echo $' . $entityParam . '->' . $methodName . '() ?></td>'; $body[] = ' </tr>'; } $body[] = ' <tr>'; $body[] = ' <th class="col-sm-2 text-right"> </th>'; $body[] = ' <td class="col-sm-10"><?php echo $this->form($this->' . $formParam . '); ?></td>'; $body[] = ' </tr>'; $body[] = ' </tbody>'; $body[] = ' </table>'; $body[] = '</div>'; $body[] = '<p>'; $body[] = ' <a class="btn btn-primary" href="<?php echo $this->url(\'' . $moduleRoute . '\'); ?>">'; $body[] = ' <i class="fa fa-table"></i>'; $body[] = ' <?php echo $this->translate(\'' . $moduleIdentifier . '_action_index\'); ?>'; $body[] = ' </a>'; $body[] = '</p>'; $body = implode(AbstractGenerator::LINE_FEED, $body); // add method $this->setContent($body); }
/** * Build a Code Generation Php Object from a Class Reflection * * @param ClassReflection $classReflection * @return ClassGenerator */ public static function fromReflection(ClassReflection $classReflection) { $cg = new static($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock())); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var \Zend\Code\Reflection\ClassReflection $parentClass */ $parentClass = $classReflection->getParentClass(); $interfaces = $classReflection->getInterfaces(); if ($parentClass) { $cg->setExtendedClass($parentClass->getName()); $interfaces = array_diff($interfaces, $parentClass->getInterfaces()); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var \Zend\Code\Reflection\ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $properties[] = PropertyGenerator::fromReflection($reflectionProperty); } } $cg->addProperties($properties); $constants = array(); foreach ($classReflection->getConstants() as $name => $value) { $constants[] = array('name' => $name, 'value' => $value); } $cg->addConstants($constants); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } $cg->addMethods($methods); return $cg; }
public function getClassUseName(ClassReflection $currentClass, ClassReflection $useClass) { $useNames = $this->fileReflectionUseStatementService->getUseNames($currentClass->getDeclaringFile()); $fullUseClassName = $useClass->getName(); $classUseName = null; if (array_key_exists($fullUseClassName, $useNames)) { $classUseName = $useNames[$fullUseClassName] ?: $useClass->getShortName(); } else { if (0 === strpos($fullUseClassName, $currentClass->getNamespaceName())) { $classUseName = substr($fullUseClassName, strlen($currentClass->getNamespaceName()) + 1); } else { $classUseName = '\\' . $fullUseClassName; } } return $classUseName; }
/** * fromReflection() - build a Code Generation Php Object from a Class Reflection * * @param ReflectionClass $classReflection * @return ClassGenerator */ public static function fromReflection(ClassReflection $classReflection) { // class generator $cg = new static($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocblock(DocblockGenerator::fromReflection($classReflection->getDocblock())); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var $parentClass \Zend\Code\Reflection\ReflectionClass */ if ($parentClass = $classReflection->getParentClass()) { $cg->setExtendedClass($parentClass->getName()); $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces()); } else { $interfaces = $classReflection->getInterfaces(); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var $interface \Zend\Code\Reflection\ReflectionClass */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { /* @var $reflectionProperty \PropertyReflection\Code\Reflection\ReflectionProperty */ if ($reflectionProperty->getDeclaringClass()->getName() == $cg->getName()) { $properties[] = PropertyGenerator::fromReflection($reflectionProperty); } } $cg->setProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { /* @var $reflectionMethod \MethodReflection\Code\Reflection\ReflectionMethod */ if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getName()) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } $cg->setMethods($methods); return $cg; }
/** * Process method parameters * * @param array $def * @param Reflection\ClassReflection $rClass * @param Reflection\MethodReflection $rMethod */ protected function processParams(&$def, Reflection\ClassReflection $rClass, Reflection\MethodReflection $rMethod) { if (count($rMethod->getParameters()) === 0) { return; } $methodName = $rMethod->getName(); // @todo annotations here for alternate names? $def['parameters'][$methodName] = array(); foreach ($rMethod->getParameters() as $p) { /** @var $p \ReflectionParameter */ $actualParamName = $p->getName(); $fqName = $rClass->getName() . '::' . $rMethod->getName() . ':' . $p->getPosition(); $def['parameters'][$methodName][$fqName] = array(); // set the class name, if it exists $def['parameters'][$methodName][$fqName][] = $actualParamName; $def['parameters'][$methodName][$fqName][] = $p->getClass() !== null ? $p->getClass()->getName() : null; $def['parameters'][$methodName][$fqName][] = !$p->isOptional(); $def['parameters'][$methodName][$fqName][] = $p->isOptional() && $p->isDefaultValueAvailable() ? $p->getDefaultValue() : null; } }
/** * Generate view content * * @param string $moduleName * @param ClassReflection $loadedEntity */ protected function addContent($moduleName, ClassReflection $loadedEntity) { // prepare some params $moduleIdentifier = $this->filterCamelCaseToUnderscore($moduleName); $entityName = $loadedEntity->getShortName(); $entityParam = lcfirst($entityName); $moduleRoute = $this->filterCamelCaseToDash($moduleName); // set action body $body = []; $body[] = 'use ' . $loadedEntity->getName() . ';'; $body[] = ''; $body[] = '/** @var ' . $entityName . ' $' . $entityParam . ' */'; $body[] = '$' . $entityParam . ' = $this->' . $entityParam . ';'; $body[] = ''; $body[] = '$this->h1(\'' . $moduleIdentifier . '_title_show\');'; $body[] = '?>'; $body[] = '<table class="table table-bordered">'; $body[] = ' <tbody>'; foreach ($loadedEntity->getProperties() as $property) { $methodName = 'get' . ucfirst($property->getName()); $body[] = ' <tr>'; $body[] = ' <th><?php echo $this->translate(\'' . $moduleIdentifier . '_label_' . $this->filterCamelCaseToUnderscore($property->getName()) . '\'); ?></th>'; $body[] = ' <td><?php echo $' . $entityParam . '->' . $methodName . '() ?></td>'; $body[] = ' </tr>'; } $body[] = ' </tbody>'; $body[] = '</table>'; $body[] = '<p>'; $body[] = ' <a class="btn btn-primary" href="<?php echo $this->url(\'' . $moduleRoute . '\'); ?>">'; $body[] = ' <i class="fa fa-table"></i>'; $body[] = ' <?php echo $this->translate(\'' . $moduleIdentifier . '_action_index\'); ?>'; $body[] = ' </a>'; $body[] = '</p>'; $body = implode(AbstractGenerator::LINE_FEED, $body); // add method $this->setContent($body); }
/** * @param ClassReflection $class * @return bool */ public function isSatisfiedBy(ClassReflection $class) { $className = $class->getName(); return $className === 'Zend\\Loader\\AutoloaderFactory' || $className === 'Zend\\Loader\\SplAutoloader'; }
/** * Makes several checks. * * 1. Exclude blacklisted namespaces * 2. Exclude blacklisted classes * 3. Skip Internal classes * * @param Reflection\ClassReflection $class */ protected function processClassIntoCacheFile(Reflection\ClassReflection $class) { if ($this->classList[$class->getName()] === true) { return; } if ($class->isInternal() === true) { return; } // Make the string regex compactible $excludeNamespaces = array_map(function ($namespace) { return str_replace('\\', '[\\\\]', $namespace); }, $this->getConfig()->getIgnoreNamespaces()); // Make the regex $excludeNamespaceRegex = '/^(' . implode('|', $excludeNamespaces) . ')(.*)/'; if (preg_match($excludeNamespaceRegex, $class->getName())) { return; } $this->classList[$class->getName()] = true; $this->buildNamespace($class); }
/** * @param ClassReflection $class * @return bool */ public function isSatisfiedBy(ClassReflection $class) { return 0 !== strpos($class->getName(), 'Zend'); }
/** * Find the getter method for a given property in the Data Object class * * @param ClassReflection $class * @param string $camelCaseProperty * @return string processed method name * @throws \Exception If $camelCaseProperty has no corresponding getter method */ protected function _processGetterMethod(ClassReflection $class, $camelCaseProperty) { $getterName = 'get' . $camelCaseProperty; $boolGetterName = 'is' . $camelCaseProperty; if ($class->hasMethod($getterName)) { $methodName = $getterName; } elseif ($class->hasMethod($boolGetterName)) { $methodName = $boolGetterName; } else { throw new \Exception(sprintf('Property :"%s" does not exist in the Data Object class: "%s".', $camelCaseProperty, $class->getName())); } return $methodName; }
/** * Copied from ClassGenerator::fromReflection and tweaked slightly * @param ClassReflection $classReflection * * @return ClassGenerator */ public function getGeneratorFromReflection(ClassReflection $classReflection) { // class generator $cg = new ClassGenerator($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $docblock = DocBlockGenerator::fromReflection($classReflection->getDocBlock()); $docblock->setIndentation(Generator::$indentation); $cg->setDocBlock($docblock); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var \Zend\Code\Reflection\ClassReflection $parentClass */ $parentClass = $classReflection->getParentClass(); if ($parentClass) { $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\')); $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces()); } else { $interfaces = $classReflection->getInterfaces(); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var \Zend\Code\Reflection\ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $property = PropertyGenerator::fromReflection($reflectionProperty); $property->setIndentation(Generator::$indentation); $properties[] = $property; } } $cg->addProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $method = MethodGenerator::fromReflection($reflectionMethod); $method->setBody(preg_replace("/^\\s+/m", '', $method->getBody())); $method->setIndentation(Generator::$indentation); $methods[] = $method; } } $cg->addMethods($methods); return $cg; }
/** * @param AnnotationCollection $annotations * @param ClassReflection $reflection * @param ArrayObject $spec */ protected function configureEntity($annotations, $reflection, $spec) { $name = $reflection->getName(); $spec['entity'] = $name; $events = $this->getEventManager(); foreach ($annotations as $annotation) { $events->trigger(__FUNCTION__, $this, ['annotation' => $annotation, 'spec' => $spec]); } }
/** * Checks if the object can be set in this fieldset * * @param object $object * @return bool */ public function allowObjectBinding($object) { $validBindingClass = false; if (is_object($object) && $this->allowedObjectBindingClass()) { $objectClass = ltrim($this->allowedObjectBindingClass(), '\\'); $reflection = new ClassReflection($object); $validBindingClass = $reflection->getName() == $objectClass || $reflection->isSubclassOf($this->allowedObjectBindingClass()); } return $validBindingClass || $this->object && $object instanceof $this->object; }
/** * Find the setter method name for a property from the given class * * @param ClassReflection $class * @param string $camelCaseProperty * @return string processed method name * @throws \Exception If $camelCaseProperty has no corresponding setter method */ public function findSetterMethodName(ClassReflection $class, $camelCaseProperty) { $setterName = 'set' . $camelCaseProperty; $boolSetterName = 'setIs' . $camelCaseProperty; if ($class->hasMethod($setterName)) { $methodName = $setterName; } elseif ($class->hasMethod($boolSetterName)) { $methodName = $boolSetterName; } else { throw new \Exception(sprintf('Property :"%s" does not exist in the provided class: "%s".', $camelCaseProperty, $class->getName())); } return $methodName; }
/** * * @param ClassReflection $class * @return ClassReflection[] */ private function getInterfaces(ClassReflection $class) { $classes = []; foreach ($class->getInterfaces() as $interface) { $classes = array_merge($classes, $this->getInterfaces($interface)); } if ($class->isUserDefined() && $class->isInterface() && !in_array($class->getName(), $this->seen)) { $this->seen[] = $class->getName(); $classes[] = $class; } return $classes; }
/** * Builds and populates Action object based on the provided class name * * @param string $className * @return Action */ public function buildAction($className) { $classReflection = new ClassReflection($className); $scanner = new FileScanner($classReflection->getFileName(), $this->annotationManager); $classScanner = $scanner->getClass($classReflection->getName()); $action = new Action($classScanner->getName()); foreach ($classScanner->getMethods() as $classMethod) { if ($classMethod->isPublic() && $classMethod->getName() != '__construct') { $action->addMethod($this->buildMethod($classMethod)); } } return $action; }
/** * Get class name * * @return string */ public function getName() { return $this->reflection->getName(); }
/** * @param ClassReflection $classReflection * @param $parent * @param $usesNames * @return string */ protected function extractInterfaceStatement(ClassReflection $classReflection, $parent, $usesNames) { $parentInterfaceNames = $parent instanceof ClassReflection ? $parent->getInterfaceNames() : array(); $interfaceNames = array_diff($classReflection->getInterfaceNames(), $parentInterfaceNames); $interfaceStatement = ''; if (!count($interfaceNames)) { return $interfaceStatement; } foreach ($interfaceNames as $interface) { $iReflection = new ClassReflection($interface); $interfaceNames = array_diff($interfaceNames, $iReflection->getInterfaceNames()); } $interfaceStatement .= $classReflection->isInterface() ? ' extends ' : ' implements '; $interfaceStatement .= implode(', ', array_map(function ($interface) use($usesNames, $classReflection) { $iReflection = new ClassReflection($interface); return array_key_exists($iReflection->getName(), $usesNames) ? $usesNames[$iReflection->getName()] ?: $iReflection->getShortName() : (0 === strpos($iReflection->getName(), $classReflection->getNamespaceName()) ? substr($iReflection->getName(), strlen($classReflection->getNamespaceName()) + 1) : '\\' . $iReflection->getName()); }, $interfaceNames)); return $interfaceStatement; }