public function sample() { $file = File::make('Memio.php')->setStructure(Object::make('name\\space\\Sample')->addProperty(Property::make('string')->makePublic()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(VariableTag::make('string String'))))->addMethod(Method::make('get')->setPhpdoc(MethodPhpdoc::make()->setDescription(Description::make('Return string'))->setReturnTag(ReturnTag::make('string')))->setBody('return $this->string;'))->addMethod(Method::make('set')->setPhpdoc(MethodPhpdoc::make()->setDescription(Description::make('Set string'))->addParameterTag(ParameterTag::make('string', 'string', 'String'))->setReturnTag(ReturnTag::make('$this')))->addArgument(Argument::make('string', 'string'))->setBody('$this->string = $string;' . PHP_EOL . 'return $this;'))); // Generate the code and display in the console $prettyPrinter = Build::prettyPrinter(); $generatedCode = $prettyPrinter->generateCode($file); file_put_contents('tmp/origin/Memio.php', (string) $generatedCode); }
protected function generateCodeForResource(ResourceInterface $resource, array $data) { $structure = Object::make($resource->getSrcClassname()); $structure->makeFinal(); $handle = Method::make('handle'); $handle->addArgument(Argument::make($data['handles'], 'command')); $handle->setBody(" // TODO write your own implementation"); $structure->addMethod($handle); $file = File::make($resource->getSrcFilename())->setStructure($structure); $prettyPrinter = Build::prettyPrinter(); return $prettyPrinter->generateCode($file); }
protected function execute(InputInterface $input, OutputInterface $output) { $file = $input->getArgument('file'); $entity = $input->getArgument('entity'); $entityDir = $input->getArgument('entity-dir'); if (!file_exists($file)) { throw new \InvalidArgumentException("{$file} does not exist"); } if (!is_dir($entityDir)) { throw new \InvalidArgumentException("{$entityDir} is not a valid directory"); } $item = json_decode(file_get_contents($file))[0]; $file = File::make($entityDir . '/' . $entity . '.php'); $object = Object::make('Dandomain\\Api\\Entity\\' . $entity); $arrayProperties = []; foreach ($item as $key => $val) { if (gettype($val) == 'array') { $arrayProperties[] = $key; } } if (count($arrayProperties)) { $body = []; foreach ($arrayProperties as $arrayProperty) { $body[] = ' $this->' . $arrayProperty . ' = new ArrayCollection();'; } $object->addMethod(Method::make('__construct')->setBody(join("\n", $body))); $file->addFullyQualifiedName(new FullyQualifiedName('Doctrine\\Common\\Collections\\ArrayCollection')); } foreach ($item as $key => $val) { $type = gettype($val); if ($type == 'array') { $type = 'ArrayCollection'; } $property = Property::make($key)->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag($type))); $object->addProperty($property); $object->addMethod(Method::make('get' . ucfirst($key))->setPhpdoc(MethodPhpdoc::make()->setReturnTag(new ReturnTag($type)))->setBody(' return $this->' . $key . ';')); $object->addMethod(Method::make('set' . ucfirst($key))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag($type, $key))->setReturnTag(new ReturnTag($entity)))->addArgument(Argument::make('string', $key))->setBody(' $this->' . $key . ' = $' . $key . ';')); } $file->setStructure($object); $prettyPrinter = Build::prettyPrinter(); $generatedCode = $prettyPrinter->generateCode($file); file_put_contents($file->getFilename(), $generatedCode); }
protected function generateCodeForResource(ResourceInterface $resource, array $data) { $structure = Object::make($resource->getSrcClassname()); $structure->makeFinal(); $construct = Method::make('__construct'); $structure->addMethod($construct); $constructBody = []; foreach ($data['params'] as $param) { $structure->addProperty(Property::make($param)); $body = <<<METHOD return \$this->{$param}; METHOD; $construct->addArgument(Argument::make('mixed', $param)); $constructBody[] = " \$this->{$param} = \${$param};"; $method = Method::make($param)->setBody($body); $structure->addMethod($method); } $construct->setBody(implode("\n", $constructBody)); $file = File::make($resource->getSrcFilename())->setStructure($structure); $prettyPrinter = Build::prettyPrinter(); return $prettyPrinter->generateCode($file); }
public function generate($class, $param = NULL) { // Describe the code you want to generate using "Models" $this->_class = Object::make(ucfirst($class)); if ($param['property'] !== NULL) { if (is_array($param['property'])) { foreach ($param['property'] as $property) { $this->_class->addProperty(Property::make($property)); } } elseif (is_string($param['property'])) { $this->_class->addProperty(Property::make($param['property'])); } } if ($param['method'] !== NULL) { if (is_array($param['method'])) { foreach ($param['method'] as $method) { if (is_array($method)) { foreach ($method as $name => $arguments) { $this->_method = Method::make($name); foreach ($arguments as $kind => $argument) { $this->_method->addArgument(Argument::make($kind, $argument)); } } $this->_class->addMethod($this->_method); } elseif (is_string($method)) { $this->_class->addMethod(Method::make($method)); } } } elseif (is_string($param['method'])) { $this->_class->addMethod(Method::make($param['method'])); } } $file = File::make(ucfirst($class) . 'php')->setStructure($this->_class); // Generate the code and display in the console $prettyPrinter = Build::prettyPrinter(); echo '<pre>' . htmlspecialchars($prettyPrinter->generateCode($file)) . '</pre>'; }
public function testDefaultConstantValueForObject() { $argument = Argument::make('string', 'status')->setDefaultValue('self::DEFAULT_STATUS'); $generatedCode = $this->prettyPrinter->generateCode($argument); $this->assertSame('$status = self::DEFAULT_STATUS', $generatedCode); }