/** * {@inheritdoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $classGenerator->setExtendedClass($originalClass->getName()); $additionalInterfaces = ['OpenClassrooms\\ServiceProxy\\ServiceProxyInterface']; $additionalProperties['proxy_realSubject'] = new PropertyGenerator('proxy_realSubject', null, PropertyGenerator::FLAG_PRIVATE); $additionalMethods['setProxy_realSubject'] = new MethodGenerator('setProxy_realSubject', [['name' => 'realSubject']], MethodGenerator::FLAG_PUBLIC, '$this->proxy_realSubject = $realSubject;'); $methods = $originalClass->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { $preSource = ''; $postSource = ''; $exceptionSource = ''; $methodAnnotations = $this->annotationReader->getMethodAnnotations($method); foreach ($methodAnnotations as $methodAnnotation) { if ($methodAnnotation instanceof Cache) { $additionalInterfaces['cache'] = 'OpenClassrooms\\ServiceProxy\\ServiceProxyCacheInterface'; $response = $this->cacheStrategy->execute($this->serviceProxyStrategyRequestBuilder->create()->withAnnotation($methodAnnotation)->withClass($originalClass)->withMethod($method)->build()); foreach ($response->getMethods() as $methodToAdd) { $additionalMethods[$methodToAdd->getName()] = $methodToAdd; } foreach ($response->getProperties() as $propertyToAdd) { $additionalProperties[$propertyToAdd->getName()] = $propertyToAdd; } $preSource .= $response->getPreSource(); $postSource .= $response->getPostSource(); $exceptionSource .= $response->getExceptionSource(); } } $classGenerator->addMethodFromGenerator($this->generateProxyMethod($method, $preSource, $postSource, $exceptionSource)); } $classGenerator->setImplementedInterfaces($additionalInterfaces); $classGenerator->addProperties($additionalProperties); $classGenerator->addMethods($additionalMethods); }
private function generateAutoLoaderClass() { $class = new ClassGenerator(); $class->setName('AutoLoader'); $class->setFinal(true); $class->setExtendedClass('BaseAutoLoader'); $class->addProperties([$this->generatePSR4MapProp(), $this->generatePSR0MapProp(), $this->generateClassMapProp(), $this->generateFileIncludesProp(), $this->generateProxiesMapProp()]); return $class->generate(); }
public function testPropertyAccessors() { $classGenerator = new ClassGenerator(); $classGenerator->addProperties(array('propOne', new PropertyGenerator('propTwo'))); $properties = $classGenerator->getProperties(); $this->assertEquals(count($properties), 2); $this->assertInstanceOf('Zend\\Code\\Generator\\PropertyGenerator', current($properties)); $property = $classGenerator->getProperty('propTwo'); $this->assertInstanceOf('Zend\\Code\\Generator\\PropertyGenerator', $property); $this->assertEquals($property->getName(), 'propTwo'); // add a new property $classGenerator->addProperty('prop3'); $this->assertEquals(count($classGenerator->getProperties()), 3); }
private function buildClass($replacement) { $type = $this->splitNsandClass($replacement['originalFullyQualifiedType']); $class = new ClassGenerator(); $class->setName($type['class']); $class->setNamespaceName($type['ns']); $class->setExtendedClass('\\Brads\\Ppm\\Proxy'); $properties = []; $methods = []; $implementedInterfaces = []; foreach ($versions as $version => $info) { foreach ($info['files'] as $file) { echo "Parsing: " . $this->vendorDir . '/' . $package . '/' . $version . '/' . $file . "\n"; $parsedFile = new ReflectionFile($this->vendorDir . '/' . $package . '/' . $version . '/' . $file); $parsedClass = $parsedFile->getFileNamespace($info['toNs'])->getClass($info['toNs'] . '\\' . $type['class']); if ($parsedClass->getInterfaceNames() != null) { $implementedInterfaces = array_merge($implementedInterfaces, $parsedClass->getInterfaceNames()); } foreach ($parsedClass->getMethods() as $method) { if ($method->isPublic()) { $generatedMethod = new MethodGenerator(); $generatedMethod->setName($method->name); $generatedMethod->setBody('echo "Hello world!";'); $generatedMethod->setAbstract($method->isAbstract()); $generatedMethod->setFinal($method->isFinal()); $generatedMethod->setStatic($method->isStatic()); $generatedMethod->setVisibility(MethodGenerator::VISIBILITY_PUBLIC); foreach ($method->getParameters() as $param) { $generatedParam = new ParameterGenerator(); $generatedParam->setName($param->name); if ($param->hasType()) { $generatedParam->setType($param->getType()); } //$generatedParam->setDefaultValue($param->getDefaultValue()); $generatedParam->setPosition($param->getPosition()); $generatedParam->setVariadic($param->isVariadic()); $generatedParam->setPassedByReference($param->isPassedByReference()); $generatedMethod->setParameter($generatedParam); } $existingMethod = Linq::from($methods)->firstOrDefault(null, function (MethodGenerator $v) use($method) { return $v->getName() == $method->name; }); if ($existingMethod != null) { $existingParams = $existingMethod->getParameters(); foreach ($generatedMethod->getParameters() as $newParam) { $existingParam = Linq::from($existingParams)->firstOrDefault(function (ParameterGenerator $v) { return $v->getName() == $newParam->getName(); }); if ($existingParam == null) { $existingMethod->setParameter($newParam); } } } else { $methods[] = $generatedMethod; } } } foreach ($parsedClass->getProperties() as $prop) { //$properties[] = PropertyGenerator::fromReflection($prop); } } } $class->setImplementedInterfaces($implementedInterfaces); $class->addMethods($methods); $class->addProperties($properties); return (new FileGenerator(['classes' => [$class]]))->generate(); }
/** * Handle an associative property field * @param string $name - name of the field * @param \Zend\Code\Generator\ClassGenerator $cg - The class generator object to attach to * @param \Doctrine\ORM\Mapping\ClassMetadata $ormClassMetaData - The ORM class meta data */ private function handleAssocProperty($name, Generator\ClassGenerator &$cg, ORMClassMetadata $ormClassMetaData) { $assocMapping = $ormClassMetaData->getAssociationMapping($name); $property = $this->createProperty($name); if ($assocMapping['type'] & $ormClassMetaData::TO_MANY) { // This is a collection (should be an Array) $property->setDocBlock('@var array $' . $name); $property->setDefaultValue(array()); $paramType = self::PARAM_TYPE_RELATION_COLLECTION; } else { // This is a single relation $property->setDocBlock('@var ' . $assocMapping['targetEntity'] . ' $' . $name); $paramType = self::PARAM_TYPE_RELATION_SINGLE; } if (!$cg->hasProperty($name)) { $cg->addProperties(array($property)); $cg->addMethods($this->getSetterMethods($cg, $name, $paramType, $assocMapping['targetEntity'])); } }
/** * 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; }
$dirName = dirname($filename); if (!is_dir($dirName)) { mkdir($dirName . '/', 0777, true); } return file_put_contents($filename, $data, $flags); } /********** FILE UTILS ************/ public function generate() { // Definitions $classGenerator = new ClassGenerator(); $classGenerator->setName($this->getClassName()); $classGenerator->setDocblock($this->getDocBlock()); $classGenerator->setNamespaceName($this->getClassNameSpace()); $classGenerator->setExtendedClass($this->extendedClass); // Properties $classGenerator->addProperties($this->getProperties());