public function generate(Generator\ClassGenerator $class, PHPClass $type) { if (!($extends = $type->getExtends()) && class_exists($type->getNamespace())) { $extendNamespace = $type->getNamespace(); $extendNamespace = explode('\\', $extendNamespace); $extendClass = array_pop($extendNamespace); $extendNamespace = implode('\\', $extendNamespace); $extends = new PHPClass(); $extends->setName($extendClass); $extends->setNamespace($extendNamespace); $class->setExtendedClass($extends); } if ($extends->getName() == "string" && $extends->getNamespace() == "" && class_exists($type->getNamespace() . '\\String')) { $extends->setName('String'); $extends->setNamespace($type->getNamespace()); } elseif ($extends->getName() == "string" && $extends->getNamespace() == "" && class_exists($type->getNamespace())) { $extendNamespace = $type->getNamespace(); $extendNamespace = explode('\\', $extendNamespace); $extendClass = array_pop($extendNamespace); $extendNamespace = implode('\\', $extendNamespace); $extends = new PHPClass(); $extends->setName($extendClass); $extends->setNamespace($extendNamespace); $class->setExtendedClass($extends); } $docblock = new DocBlockGenerator("Class representing " . $type->getName()); if ($type->getDoc()) { $docblock->setLongDescription($type->getDoc()); } $class->setNamespaceName($type->getNamespace()); $class->setName($type->getName()); $class->setDocblock($docblock); $class->setExtendedClass($extends->getName()); if ($extends->getNamespace() != $type->getNamespace()) { if ($extends->getName() == $type->getName()) { $class->addUse($type->getExtends()->getFullName(), $extends->getName() . "Base"); $class->setExtendedClass($extends->getName() . "Base"); } else { $class->addUse($extends->getFullName()); } } if ($this->handleBody($class, $type)) { return true; } }
public function controllerAction() { $config = $this->getServiceLocator()->get('config'); $moduleName = $config['VisioCrudModeler']['params']['moduleName']; $modulePath = $config['VisioCrudModeler']['params']['modulesDirectory']; $db = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'); $dataSourceDescriptor = new DbDataSourceDescriptor($db, 'K08_www_biedronka_pl'); $listDataSets = $dataSourceDescriptor->listDataSets(); $filter = new \Zend\Filter\Word\UnderscoreToCamelCase(); foreach ($listDataSets as $d) { $className = $filter->filter($d) . 'Controller'; $file = new FileGenerator(); $file->setFilename($className); $file->setNamespace($moduleName)->setUse('Zend\\Mvc\\Controller\\AbstractActionController'); $foo = new ClassGenerator(); $docblock = DocBlockGenerator::fromArray(array('shortDescription' => 'Sample generated class', 'longDescription' => 'This is a class generated with Zend\\Code\\Generator.', 'tags' => array(array('name' => 'version', 'description' => '$Rev:$'), array('name' => 'license', 'description' => 'New BSD')))); $foo->setName($className); $foo->setExtendedClass('Base' . $className); $foo->setDocblock($docblock); $file->setClass($foo); echo '<pre>'; echo htmlentities($file->generate()); echo '</pre>'; $fileView = new FileGenerator(); $body = "echo '{$className}';"; $fileView->setBody($body); echo '<pre>'; echo htmlentities($fileView->generate()); echo '</pre>'; } echo '<hr />'; foreach ($listDataSets as $d) { $className = 'Base' . $filter->filter($d) . 'Controller'; $fileBase = new FileGenerator(); $fileBase->setFilename($className); $fileBase->setNamespace($moduleName)->setUse('Zend\\Mvc\\Controller\\AbstractActionController'); $fooBase = new ClassGenerator(); $docblockBase = DocBlockGenerator::fromArray(array('shortDescription' => 'Sample generated class', 'longDescription' => 'This is a class generated with Zend\\Code\\Generator.', 'tags' => array(array('name' => 'version', 'description' => '$Rev:$'), array('name' => 'license', 'description' => 'New BSD')))); $fooBase->setName($className); $index = new MethodGenerator(); $index->setName('indexAction'); $create = new MethodGenerator(); $create->setName('createAction'); $read = new MethodGenerator(); $read->setName('readAction'); $update = new MethodGenerator(); $update->setName('updateAction'); $delete = new MethodGenerator(); $delete->setName('deleteAction'); $fooBase->setExtendedClass('AbstractActionController'); //$fooBase->set $fooBase->setDocblock($docblock); $fooBase->addMethodFromGenerator($index); $fooBase->addMethodFromGenerator($create); $fooBase->addMethodFromGenerator($read); $fooBase->addMethodFromGenerator($update); $fooBase->addMethodFromGenerator($delete); $fileBase->setClass($fooBase); echo '<pre>'; echo htmlentities($fileBase->generate()); echo '</pre>'; } exit; }
public function generate(Generator\ClassGenerator $class, PHPClass $type) { $docblock = new DocBlockGenerator("Class representing " . $type->getName()); if ($type->getDoc()) { $docblock->setLongDescription($type->getDoc()); } $class->setNamespaceName($type->getNamespace() ?: NULL); $class->setName($type->getName()); $class->setDocblock($docblock); if ($extends = $type->getExtends()) { if ($p = $this->isOneType($extends)) { $this->handleProperty($class, $p); $this->handleValueMethod($class, $p, $extends); } else { $class->setExtendedClass($extends->getName()); if ($extends->getNamespace() != $type->getNamespace()) { if ($extends->getName() == $type->getName()) { $class->addUse($type->getExtends()->getFullName(), $extends->getName() . "Base"); $class->setExtendedClass($extends->getName() . "Base"); } else { $class->addUse($extends->getFullName()); } } } } if ($this->handleBody($class, $type)) { return true; } }
public function buildModel($model) { $class = new ClassGenerator($model->config->name); $class->setExtendedClass('\\app\\extensions\\data\\Model'); $class->setNamespaceName('app\\models'); $repository = "{$model->config->repository}" == "true" ? "(repositoryClass=\"app\\resources\\repository\\{$model->config->name}Repository\")\n" : null; $callback = "{$model->config->callback}" == "true" ? "@HasLifecycleCallbacks\n" : null; $table = "@Table(name=\"{$model->config->table}\")"; $docblock = new DocblockGenerator("@Entity" . $repository . $callback . $table); $class->setDocblock($docblock); $validations = array(); foreach ($model->fields->field as $field) { $type = "{$field->type}"; $nullable = isset($field->nullable) ? ", nullable=\"{$field->nullable}\"" : null; $length = isset($field->length) ? ", length=\"{$field->length}\"" : null; $default = isset($field->default) ? "{$field->default}" : null; if ($type == "boolean" && $default) { $default = $default == "true" ? true : false; } $property = new PropertyGenerator("{$field->name}", $default, PropertyGenerator::FLAG_PROTECTED); switch ($type) { case "index": $docblock = '@Id @Column(type="integer") @GeneratedValue'; break; case "relation": $mappedBy = isset($field->mappedBy) ? ", mappedBy=\"{$field->mappedBy}\"" : ""; $inversedBy = isset($field->inversedBy) ? ", inversedBy=\"{$field->inversedBy}\"" : ""; $cascades = ''; if (isset($field->cascades->cascade[0])) { $cascades = ", cascade={"; foreach ($field->cascades->cascade as $cascade) { $cascades .= "\"{$cascade}\","; } $cascades = substr($cascades, 0, -1) . "}"; } $docblock = "@{$field->relation}(targetEntity=\"{$field->targetEntity}\"{$mappedBy}{$inversedBy}{$cascades})"; break; default: $docblock = "@Column(type=\"{$type}\"{$nullable}{$length})"; } $property->setDocblock($docblock); $class->setProperty($property); } foreach ($model->validations->validation as $validation) { $validations["{$validation->field}"] = array(); foreach ($validation->rules->rule as $rule) { $validations["{$validation->field}"][] = array("{$rule->type}", "message" => "{$rule->message}"); } } if (!empty($validations)) { $class->setProperty(new PropertyGenerator("validations", $validations, PropertyGenerator::FLAG_PROTECTED)); } if ($callback) { $beforePersist = new MethodGenerator('beforePersist'); $beforeUpdate = new MethodGenerator('beforeUpdate'); $beforePersist->setDocblock("@PrePersist"); $beforeUpdate->setDocblock("@PreUpdate"); $beforePersist->setBody("\$this->created = new \\DateTime();"); $beforeUpdate->setBody("\$this->modified = new \\DateTime();"); $class->setMethods(array($beforePersist, $beforeUpdate)); } return $class; }
$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());