addClass() public method

public addClass ( $name ) : ClassType
return ClassType
Example #1
0
 /**
  * @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);
     }
 }
Example #2
0
 /**
  * @param string $className
  * @return string
  */
 public function generate($className)
 {
     if (!class_exists($className)) {
         throw new InvalidArgumentException("Unknown class {$className}. Check for typos in class name and make sure" . " that given class is properly loaded.");
     }
     $origin = new ReflectionClass($className);
     $interface = ClassType::from(self::ACCESSOR_INTERFACE);
     $namespace = new PhpNamespace($this->naming->getNamespace());
     $class = $namespace->addClass($this->naming->deriveClassName($className));
     $class->addDocument("This class was automatically generated by Markatom/Accessor library.");
     $class->addImplement(self::ACCESSOR_INTERFACE);
     $class->addConst('TARGET', $origin->getName());
     $class->addProperty('writer')->setVisibility('private');
     $class->setMethods($interface->getMethods());
     $class->getMethod('read')->setVisibility('public')->setBody($this->generateReadBody($origin));
     $class->getMethod('write')->setVisibility('public')->setBody($this->generateWriteBody($origin));
     return (string) $namespace;
 }
Example #3
0
 /**
  * @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);
     }
 }
Example #4
0
 /**
  * @param PhpNamespace $namespace
  * @param string       $class_name
  * @param object       $object
  */
 public function object($namespace, $class_name, $object)
 {
     $class_name = ucfirst($class_name);
     $class = $namespace->addClass($class_name);
     $class->addDocument('Class ' . $class_name);
     foreach (get_object_vars($object) as $name => $value) {
         $property = $class->addProperty($name);
         $property->setVisibility('public');
         switch (gettype($value)) {
             case 'object':
                 $class_name = ucfirst($name);
                 $property->addDocument('@var ' . $class_name . PHP_EOL);
                 $this->object($namespace, $class_name, $value);
                 break;
             case 'array':
                 $type_doc = isset($value[0]) ? gettype($value[0]) . '[]' : 'array';
                 $property->addDocument('@var ' . $type_doc . PHP_EOL);
                 break;
             default:
                 $property->addDocument('@var ' . gettype($value) . PHP_EOL);
         }
     }
 }
Example #5
0
 /**
  *
  */
 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;
     }
 }