public function sample() { $file = new PhpFile(); $class = $file->addClass('name\\space\\Sample'); $class->addProperty('string')->addComment('@var string String'); $class->addMethod('get')->addComment('Return string')->addComment('')->addComment('@return string')->setBody('return $this->?;', ['string']); $class->addMethod('set')->addComment('Set string')->addComment('')->addComment('@param string $string String')->addComment('@return $this')->setBody('$this->string = $string;' . PHP_EOL . 'return $this;')->addParameter('string'); file_put_contents('tmp/origin/Nette.php', (string) $file); }
public function generateContent() { $file = new PhpFile(); $class = $file->addClass($this->getFullName()); $class->setExtends('Joseki\\Migration\\DefaultMigration'); $class->addMethod('beforeMigrate')->addBody('parent::beforeMigrate();'); $m = $class->addMethod('migrate'); if ($this->migrateBody) { $m->addBody($this->migrateBody, [$this->migrateBodyParameters]); } $class->addMethod('afterMigrate')->addBody('parent::afterMigrate();'); return $file; }
private function generateMessage($className, DescriptorProto $message) { $file = new PhpFile(); $class = $file->addClass($className); $ns = $class->getNamespace(); $ns->addUse("Skrz\\Meta\\Protobuf\\ProtobufField", null, $protobufFieldAlias); if (($info = $this->getSourceCodeInfo($className)) && $info->getLeadingComments()) { $class->addComment(trim($info->getLeadingComments())); } $this->addAutoGeneratedWarning($class); foreach ((array) $message->getField() as $i => $field) { /** @var FieldDescriptorProto $field */ $propertyName = lcfirst(implode("", array_map(function ($s) { return ucfirst($s); }, explode("_", $field->getName())))); $property = $class->addProperty($propertyName); $property->setVisibility("protected"); if (($info = $this->getSourceCodeInfo($className, array_merge($this->paths[$className], [DescriptorProtoMeta::FIELD_PROTOBUF_FIELD, $i]))) && $info->getLeadingComments()) { $property->addComment(str_replace("*/", "* /", trim($info->getLeadingComments()))); $property->addComment(""); } switch ($field->getType()) { case FieldDescriptorProto\TypeEnum::TYPE_DOUBLE: $wireType = WireTypeEnum::FIXED64; $phpType = "float"; break; case FieldDescriptorProto\TypeEnum::TYPE_FLOAT: $wireType = WireTypeEnum::FIXED32; $phpType = "float"; break; case FieldDescriptorProto\TypeEnum::TYPE_UINT64: case FieldDescriptorProto\TypeEnum::TYPE_UINT32: $unsigned = true; case FieldDescriptorProto\TypeEnum::TYPE_INT64: case FieldDescriptorProto\TypeEnum::TYPE_INT32: $wireType = WireTypeEnum::VARINT; $phpType = "int"; break; case FieldDescriptorProto\TypeEnum::TYPE_FIXED64: $unsigned = true; case FieldDescriptorProto\TypeEnum::TYPE_SFIXED64: $wireType = WireTypeEnum::FIXED64; $phpType = "int"; break; case FieldDescriptorProto\TypeEnum::TYPE_FIXED32: $unsigned = true; case FieldDescriptorProto\TypeEnum::TYPE_SFIXED32: $wireType = WireTypeEnum::FIXED32; $phpType = "int"; break; case FieldDescriptorProto\TypeEnum::TYPE_BOOL: $wireType = WireTypeEnum::VARINT; $phpType = "bool"; break; case FieldDescriptorProto\TypeEnum::TYPE_STRING: case FieldDescriptorProto\TypeEnum::TYPE_BYTES: $wireType = WireTypeEnum::STRING; $phpType = "string"; break; case FieldDescriptorProto\TypeEnum::TYPE_MESSAGE: $wireType = WireTypeEnum::STRING; $fieldClassName = $this->convertPackageToNamespace($field->getTypeName()); $ns->addUse($fieldClassName, null, $phpType); break; case FieldDescriptorProto\TypeEnum::TYPE_ENUM: $wireType = WireTypeEnum::VARINT; $fieldClassName = $this->convertPackageToNamespace($field->getTypeName() . "Enum"); $ns->addUse($fieldClassName, null, $see); $phpType = "int"; break; case FieldDescriptorProto\TypeEnum::TYPE_SINT32: case FieldDescriptorProto\TypeEnum::TYPE_SINT64: $wireType = WireTypeEnum::ZIGZAG; $phpType = "int"; break; default: throw new \LogicException("Unhandled type '{$field->getType()}'."); } if ($field->getLabel() === FieldDescriptorProto\LabelEnum::LABEL_REPEATED) { $phpType .= "[]"; } $property->addComment("@var {$phpType}"); if (isset($see)) { $property->addComment("@see {$see}"); unset($see); } $property->addComment("")->addComment("@Skrz\\Meta\\Protobuf\\ProtobufField(" . "number={$field->getNumber()}" . ", wireType=\"{$wireType}\"" . ", unsigned=" . var_export(isset($unsigned) ? $unsigned : false, true) . ", packed=" . var_export($field->getOptions() ? $field->getOptions()->getPacked() : false, true) . ")"); $getter = $class->addMethod("get" . ucfirst($propertyName)); $getter->addComment("@return {$phpType}"); $getter->addBody("return \$this->{$propertyName};"); $setter = $class->addMethod("set" . ucfirst($propertyName)); $setter->addParameter($propertyName); $setter->addComment("@param {$phpType} \${$propertyName}")->addComment("")->addComment("@return self"); $setter->addBody("\$this->{$propertyName} = \${$propertyName};")->addBody("return \$this;"); } return new Result($file, $class); }
public function createMetaClass(Type $type, PhpFile $file) { return $file->addClass($this->createMetaClassName($type)); }
use Nette\PhpGenerator\PhpFile; use Skrz\Meta\Reflection\Method; use Skrz\Meta\Reflection\MixedType; use Skrz\Meta\Reflection\ObjectType; use Skrz\Meta\Reflection\Parameter; use Skrz\Meta\Reflection\Property; use Skrz\Meta\Reflection\Type; use Skrz\Meta\Reflection\VoidType; $classes = array("ReflectionClass" => "Skrz\\Meta\\Reflection\\Type", "ReflectionMethod" => "Skrz\\Meta\\Reflection\\Method", "ReflectionProperty" => "Skrz\\Meta\\Reflection\\Property", "ReflectionParameter" => "Skrz\\Meta\\Reflection\\Parameter"); $someInstance = array("ReflectionClass" => new \ReflectionClass("Skrz\\Meta\\AClass"), "ReflectionMethod" => $rm = new \ReflectionMethod("Skrz\\Meta\\AClass", "aMethod"), "ReflectionProperty" => new \ReflectionProperty("Skrz\\Meta\\AClass", "aProperty"), "ReflectionParameter" => $rm->getParameters()[1]); $annotationReaderMethod = array("ReflectionClass" => "getClassAnnotations", "ReflectionMethod" => "getMethodAnnotations", "ReflectionProperty" => "getPropertyAnnotations", "ReflectionParameter" => null); $stackExpression = array("ReflectionClass" => "\$reflection->getName()", "ReflectionMethod" => "\$reflection->getDeclaringClass()->getName() . '::' . \$reflection->getName()", "ReflectionProperty" => "\$reflection->getDeclaringClass()->getName() . '::'. \$reflection->getName()", "ReflectionParameter" => "\$reflection->getDeclaringClass()->getName() . '::' . \$reflection->getDeclaringFunction()->getName() . '(' . \$reflection->getPosition() . ')'"); $outputDirectory = __DIR__ . "/../src"; foreach ($classes as $className => $discoveryClassName) { $file = new PhpFile(); $class = $file->addClass($discoveryClassName); $ns = $class->getNamespace(); if ($className === "ReflectionClass") { $ns->addUse("Skrz\\Meta\\Reflection\\ObjectType", null, $mixedTypeAlias); $class->addExtend("Skrz\\Meta\\Reflection\\ObjectType"); } $rc = new \ReflectionClass($className); $someInstanceOfClassName = $someInstance[$className]; $currentAnnotationReaderMethod = $annotationReaderMethod[$className]; foreach ($rc->getProperties() as $property) { $class->addProperty($property->getName())->addComment("@var string"); } $constructor = $class->addMethod("__construct"); $constructor->addParameter("reflection")->setTypeHint("\\" . $className); $constructor->addBody("\$this->reflection = \$reflection;"); $fromReflection = $class->addMethod("fromReflection");