/** * @param Database $database */ public function generate(Database $database) { foreach ($database->getTables() as $table) { // Create namespace and inner class $namespace = new PhpNamespace($this->resolver->resolveRepositoryNamespace($table)); $class = $namespace->addClass($this->resolver->resolveRepositoryName($table)); // Detect extends class if (($extends = $this->config->get('repository.extends')) !== NULL) { $namespace->addUse($extends); $class->setExtends($extends); } // Save file $this->generateFile($this->resolver->resolveRepositoryFilename($table), (string) $namespace); } // Generate abstract base class if ($this->config->get('repository.extends') !== NULL) { // Create abstract class $namespace = new PhpNamespace($this->config->get('repository.namespace')); $class = $namespace->addClass(Helpers::extractShortName($this->config->get('repository.extends'))); $class->setAbstract(TRUE); // Add extends from ORM/Repository $extends = $this->config->get('nextras.orm.class.repository'); $namespace->addUse($extends); $class->setExtends($extends); // Save file $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->get('repository.extends')), $this->config->get('repository.folder')), (string) $namespace); } }
protected function execute(InputInterface $input, OutputInterface $output) { $config = $this->getApplication()->getConfig(); $dialog = $this->getHelper('dialog'); $className = $input->getArgument('className'); $modelName = $input->getArgument('modelName'); $endPoint = $input->getArgument('endPoint'); $model = $this->getModel($modelName); $buildDirectory = $config['build']['classes']; $buildPath = $buildDirectory . '/' . $className . '.php'; if (file_exists($buildPath)) { if (!$dialog->askConfirmation($output, sprintf('<question>Class file "%s" exists, overwrite?</question>', $buildPath), false)) { return; } } $modelConfig = ['properties' => $model->properties]; $configsDirectory = $config['build']['configs']; $configPath = realpath($configsDirectory . '/' . $modelName . '.json'); if (file_exists($configPath)) { $modelConfig = json_decode(file_get_contents($configPath), true); } $namespace = new PhpNamespace($config['namespace']); $namespace->addUse($config['extends']); $class = new ClassType($className, $namespace); $class->addExtend($config['extends']); if (!empty($endPoint)) { $class->addConst("ENDPOINT", $endPoint); } foreach ($model->properties as $propertyName => $propertyDef) { if (in_array($propertyName, $modelConfig['properties'], true)) { $property = $class->addProperty($propertyName)->setVisibility('public'); $accessorMethod = $class->addMethod($this->toCamelCase("get_" . $propertyName)); $accessorMethod->setBody('return $this->' . $propertyName . ';'); $mutatorMethod = $class->addMethod($this->toCamelCase("set_" . $propertyName)); $mutatorMethod->addParameter($propertyName); $mutatorMethod->setBody('$this->' . $propertyName . ' = $' . $propertyName . ';'); if (is_string($propertyDef['type'])) { $property->addDocument("@var {$propertyDef['type']}"); } else { $property->addDocument("@var mixed"); } } else { $output->writeln(sprintf("<info>Skipped property %s</info>", $propertyName)); } } file_put_contents($buildPath, str_replace("\t", " ", "<?php\n{$namespace}{$class}")); // TODO: replace with PHP_CodeSniffer library exec(sprintf('vendor/bin/phpcbf --standard=PSR2 --encoding=utf-8 "%s"', $buildPath)); $output->writeln(sprintf("<info>Class %s created</info>", $buildPath)); }
/** * @param Database $database */ public function generate(Database $database) { foreach ($database->getTables() as $table) { // Create namespace and inner class $namespace = new PhpNamespace($this->resolver->resolveEntityNamespace($table)); $class = $namespace->addClass($this->resolver->resolveEntityName($table)); // Detect extends class if (($extends = $this->config->get('entity.extends')) === NULL) { $extends = $this->config->get('nextras.orm.class.entity'); } // Add namespace and extends class $namespace->addUse($extends); $class->setExtends($extends); // Add table columns foreach ($table->getColumns() as $column) { if ($this->config->get('generator.entity.exclude.primary')) { if ($column->isPrimary()) { continue; } } foreach ($this->decorators as $decorator) { $decorator->doDecorate($column, $class, $namespace); } } // Save file $this->generateFile($this->resolver->resolveEntityFilename($table), (string) $namespace); } // Generate abstract base class if ($this->config->get('entity.extends') !== NULL) { // Create abstract class $namespace = new PhpNamespace($this->config->get('entity.namespace')); $class = $namespace->addClass(Helpers::extractShortName($this->config->get('entity.extends'))); $class->setAbstract(TRUE); // Add extends from ORM/Entity $extends = $this->config->get('nextras.orm.class.entity'); $namespace->addUse($extends); $class->setExtends($extends); // Save file $this->generateFile($this->resolver->resolveFilename(Helpers::extractShortName($this->config->get('entity.extends')), $this->config->get('entity.folder')), (string) $namespace); } }
/** * @param PhpNamespace $namespace * @param ClassType $class * @param Column $column * @return void */ public function doDecorate(Column $column, ClassType $class, PhpNamespace $namespace) { switch ($column->getType()) { // Map: DateTime case ColumnTypes::TYPE_DATETIME: $column->setType('DateTime'); if ($column->getDefault() !== NULL) { $column->setDefault('now'); } $namespace->addUse('Nette\\Utils\\DateTime'); break; // Map: Enum // Map: Enum case ColumnTypes::TYPE_ENUM: foreach ($column->getEnum() as $enum) { $name = Strings::upper($column->getName()) . '_' . $enum; $class->addConst($name, $enum); } if ($column->getDefault() !== NULL) { $column->setDefault(Strings::upper($column->getName()) . '_' . $column->getDefault()); } break; } }
/** * */ public function build() { foreach ($this->metaDataFactory->getAllMetaData() as $meta_data) { $class_name = $meta_data->getName(); $space_name = $this->parseNamespace($class_name); printf('Generated classes related to %s\\%s' . PHP_EOL, $space_name, $class_name); $base_space = new PhpNamespace(ltrim($space_name . '\\' . $this->baseNamespace, '\\')); $base_class = $base_space->addClass($class_name); $constructor = $base_class->addMethod('__construct')->setVisibility("public")->addComment("Instantiate a new " . $base_class->getName()); if ($this->entityParent) { $base_space->addUse($this->entityParent); $base_class->setExtends($this->entityParent); } $base_space->addUse('Doctrine\\Common\\Collections\\ArrayCollection'); foreach ($meta_data->getFieldNames() as $field) { $type = $this->translateType($meta_data->getTypeOfField($field)); $base_class->addProperty($field)->setVisibility("protected")->addComment("")->addComment("@access protected")->addComment("@var {$type}"); $base_class->addMethod('get' . ucfirst($field))->setVisibility("public")->addComment("Get the value of {$field}")->addComment("")->addComment("@access public")->addComment("@return {$type} The value of {$field}")->addBody("return \$this->{$field};"); $base_class->addMethod('set' . ucfirst($field))->setVisibility("public")->addComment("Set the value of {$field}")->addComment("")->addComment("@access public")->addComment("@param {$type} \$value The value to set to {$field}")->addComment("@return " . $base_class->getName() . " The object instance for method chaining")->addBody("\$this->{$field} = \$value;")->addBody("")->addBody("return \$this;")->addParameter("value"); } foreach ($meta_data->getAssociationMappings() as $mapping) { $field = $mapping['fieldName']; $type = in_array($mapping['type'], $this->toOneTypes) ? $mapping['targetEntity'] : 'ArrayCollection'; $base_class->addProperty($field)->setVisibility("protected")->addComment("")->addComment("@access protected")->addComment("@var {$type}"); $base_class->addMethod('get' . ucfirst($field))->setVisibility("public")->addComment("Get the value of {$field}")->addComment("")->addComment("@access public")->addComment("@return {$type} The value of {$field}")->addBody("return \$this->{$field};"); if ($type == 'ArrayCollection') { $constructor->addBody("\$this->{$field} = new ArrayCollection();"); // // hasRelatedEntities() // addRelatedEntities() // removeRelatedEntities() // } else { // // hasRelatedEntity() // // On set, if value is set to null, check if bi-directional and remove // $parameter = $base_class->addMethod('set' . ucfirst($field))->setVisibility("public")->addComment("Set the value of {$field}")->addComment("")->addComment("@access public")->addComment("@param {$type} \$value The value to set to {$field}")->addComment("@return " . $base_class->getName() . " The object instance for method chaining")->addBody("\$this->{$field} = \$value;")->addBody("")->addBody("return \$this;")->addParameter("value")->setTypeHint($type); if ($mapping['inversedBy']) { $inverse = $this->metaDataFactory->getMetadataFor($mapping['targetEntity'])->getAssociationMapping($mapping['inversedBy']); if ($inverse['orphanRemoval']) { $parameter->setOptional(TRUE); } } if (isset($mapping['joinColumns'][0]['nullable']) && $mapping['joinColumns'][0]['nullable']) { $parameter->setOptional(TRUE); } } } $this->sortMethods($base_class); $this->sortProperties($base_class); $this->write($this->entityRoot, $base_space, $base_class, TRUE); $space = new PhpNamespace($space_name); $class = $space->addClass($class_name)->setExtends(ltrim($space_name . '\\' . $this->baseNamespace . '\\' . $class_name, '\\')); $class->addMethod('__construct')->setVisibility("public")->addComment("Instantiate a new " . $class->getName())->addBody("return parent::__construct();"); $this->write($this->entityRoot, $space, $class); if ($meta_data->customRepositoryClassName) { $repo_class_name = $meta_data->customRepositoryClassName; $repo_space_name = $this->parseNamespace($repo_class_name); $repo_space = new PhpNamespace($repo_space_name); $repo_class = $repo_space->addClass($repo_class_name); $repo_space->addUse($this->repositoryParent); $repo_class->setExtends($this->repositoryParent); $this->write($this->repositoryRoot, $repo_space, $repo_class); } } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $root_class = $this->app['engine']->fetch('doctrine/entities', 'root_class'); $base_namespace = $this->app['engine']->fetch('doctrine/entities', 'base_namespace'); $entity_root = $this->app['engine']->fetch('doctrine/entities', 'entity_root'); $entity_root = $this->app->getDirectory($entity_root); $repo_root = $this->app['engine']->fetch('doctrine/entities', 'repository_root'); $repo_root = $this->app->getDirectory($repo_root); foreach ($this->metaData as $meta_data) { $class_name = $meta_data->getName(); $space_name = $this->parseNamespace($class_name); printf('Generated classes related to %s\\%s', $space_name, $class_name); $base_space = new PhpNamespace(ltrim($space_name . '\\' . $base_namespace, '\\')); $base_class = $base_space->addClass($class_name); $constructor = $base_class->addMethod('__construct')->setVisibility("public")->addComment("Instantiate a new " . $base_class->getName()); if ($root_class) { $base_space->addUse($root_class); $base_class->setExtends($root_class); } $base_space->setBracketedSyntax(TRUE); $base_space->addUse('Doctrine\\Common\\Collections\\ArrayCollection'); foreach ($meta_data->getFieldNames() as $field) { $type = $this->translateType($meta_data->getTypeOfField($field)); $base_class->addProperty($field)->setVisibility("protected")->addComment("")->addComment("@access protected")->addComment("@var {$type}"); $base_class->addMethod('get' . ucfirst($field))->setVisibility("public")->addComment("Get the value of {$field}")->addComment("")->addComment("@access public")->addComment("@return {$type} The value of {$field}")->addBody("return \$this->{$field};"); $base_class->addMethod('set' . ucfirst($field))->setVisibility("public")->addComment("Set the value of {$field}")->addComment("")->addComment("@access public")->addComment("@param {$type} \$value The value to set to {$field}")->addComment("@return " . $base_class->getName() . " The object instance for method chaining")->addBody("\$this->{$field} = \$value;")->addBody("")->addBody("return \$this;")->addParameter("value"); } foreach ($meta_data->getAssociationMappings() as $mapping) { $field = $mapping['fieldName']; $type = in_array($mapping['type'], $this->toOneTypes) ? $mapping['targetEntity'] : 'ArrayCollection'; $base_class->addProperty($field)->setVisibility("protected")->addComment("")->addComment("@access protected")->addComment("@var {$type}"); $base_class->addMethod('get' . ucfirst($field))->setVisibility("public")->addComment("Get the value of {$field}")->addComment("")->addComment("@access public")->addComment("@return {$type} The value of {$field}")->addBody("return \$this->{$field};"); if ($type == 'ArrayCollection') { $constructor->addBody("\$this->{$field} = new ArrayCollection();"); // // hasRelatedEntities() // addRelatedEntities() // removeRelatedEntities() // } else { // // hasRelatedEntity() // // On set, if value is set to null, check if bi-directional and remove // $base_class->addMethod('set' . ucfirst($field))->setVisibility("public")->addComment("Set the value of {$field}")->addComment("")->addComment("@access public")->addComment("@param {$type} \$value The value to set to {$field}")->addComment("@return " . $base_class->getName() . " The object instance for method chaining")->addBody("\$this->{$field} = \$value;")->addBody("")->addBody("return \$this;")->addParameter("value")->setTypeHint($type); } } $this->sortMethods($base_class); $this->sortProperties($base_class); $this->write($entity_root, $base_space, $base_class, TRUE); $space = new PhpNamespace($space_name); $class = $space->addClass($class_name)->setExtends($space_name . '\\' . $base_namespace . '\\' . $class_name); $class->addMethod('__construct')->setVisibility("public")->addComment("Instantiate a new " . $class->getName())->addBody("return parent::__construct();"); $space->setBracketedSyntax(TRUE); $this->write($entity_root, $space, $class); if ($meta_data->customRepositoryClassName) { $repo_class_name = $meta_data->customRepositoryClassName; $repo_space_name = $this->parseNamespace($repo_class_name); $repo_space = new PhpNamespace($repo_space_name); $repo_class = $repo_space->addClass($repo_class_name); $repo_space->setBracketedSyntax(TRUE); $repo_space->addUse('Inkwell\\Doctrine\\Repository'); $repo_class->setExtends('Inkwell\\Doctrine\\Repository'); $this->write($repo_root, $repo_space, $repo_class); } echo PHP_EOL; } }