Generates code for any generateable model
Author: Thomas Gossmann
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     // recuperation du controller
     $controller = $this->argument('controller');
     // création de la méthode
     $method = camel_case($this->argument('name'));
     $method = PhpMethod::create($method);
     $method->setStatic(true);
     // Gestion du body
     $body = file_get_contents(__DIR__ . '/stubs/table.stub');
     $method->setBody($body);
     // block de commentaire
     $dockblock = new Docblock();
     $dockblock->appendTag(TagFactory::create('name', 'Artisan'));
     $dockblock->appendTag(TagFactory::create('see', 'php artisan ffmake:table'));
     $dockblock->appendTag(TagFactory::create('generated', Carbon::now()));
     $method->setDocblock($dockblock);
     // Récupération du controller à mettre à jour
     $controller = ucfirst(camel_case($controller . '_controller'));
     $controller = new \ReflectionClass('App\\Http\\Controllers\\' . $controller);
     $class = PhpClass::fromReflection($controller)->setMethod($method);
     $class->setParentClassName('Controller');
     // fix la gestion des namespaec pour la parent class
     // Génration du code
     $generator = new CodeGenerator();
     $class = '<?php ' . $generator->generate($class);
     // inscription du code dans la classe
     file_put_contents($controller->getFileName(), $class);
     $this->info('Action generated dans le fichier : ' . $controller->getFileName());
 }
 public function testSignature()
 {
     $expected = 'interface MyInterface {' . "\n" . '}';
     $trait = PhpInterface::create('MyInterface');
     $codegen = new CodeGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false]);
     $code = $codegen->generate($trait);
     $this->assertEquals($expected, $code);
 }
 public function testConfig()
 {
     $generator = new CodeGenerator(null);
     $this->assertTrue($generator->getConfig() instanceof CodeGeneratorConfig);
     $config = new CodeGeneratorConfig();
     $generator = new CodeGenerator($config);
     $this->assertSame($config, $generator->getConfig());
 }
 /**
  * {@inheritDoc}
  */
 public function generate(GenerateableInterface $model)
 {
     $content = "<?php\n";
     $comment = $this->config->getHeaderComment();
     if ($comment !== null && !$comment->isEmpty()) {
         $content .= str_replace('/**', '/*', $comment->toString()) . "\n";
     }
     $docblock = $this->config->getHeaderDocblock();
     if ($docblock !== null && !$docblock->isEmpty()) {
         $content .= $docblock->toString() . "\n";
     }
     if ($this->config->getDeclareStrictTypes()) {
         $content .= "declare(strict_types=1);\n\n";
     }
     $content .= parent::generate($model);
     if ($this->config->getBlankLineAtEnd() && !Text::create($content)->endsWith("\n")) {
         $content .= "\n";
     }
     return $content;
 }
 /**
  * {@inheritDoc}
  */
 public function generate(GenerateableInterface $model)
 {
     $content = "<?php\n";
     $comment = $this->config->getHeaderComment();
     if (!empty($comment)) {
         $docblock = new Docblock();
         $docblock->setLongDescription($comment);
         $content .= str_replace('/**', '/*', $docblock->toString()) . "\n";
     }
     if ($this->config->getHeaderDocblock() instanceof Docblock) {
         $content .= $this->config->getHeaderDocblock()->toString() . "\n";
     }
     if ($this->config->getDeclareStrictTypes()) {
         $content .= "declare(strict_types=1);\n\n";
     }
     $content .= parent::generate($model);
     if ($this->config->getBlankLineAtEnd()) {
         $content .= "\n";
     }
     return $content;
 }
 public function testABClass()
 {
     $class = Fixtures::createABClass();
     $modelGenerator = new ModelGenerator();
     $modelCode = $modelGenerator->generate($class);
     $this->assertEquals($this->getGeneratedContent('ABClass.php'), $modelCode);
     $generator = new CodeGenerator(['generateDocblock' => false]);
     $code = $generator->generate($class);
     $this->assertEquals($modelCode, $code);
     $modelGenerator = new ModelGenerator(['generateDocblock' => true]);
     $modelCode = $modelGenerator->generate($class);
     $this->assertEquals($this->getGeneratedContent('ABClassWithComments.php'), $modelCode);
     $generator = new CodeGenerator(['generateDocblock' => true]);
     $code = $generator->generate($class);
     $this->assertEquals($modelCode, $code);
 }
 public function testMethodReturnTypeHinting()
 {
     $class = PhpClass::create()->setName('GenerationTestClass')->setMethod(PhpMethod::create('a')->addParameter(PhpParameter::create('b'))->setType('int'));
     $expected = "class GenerationTestClass {\n\n\tpublic function a(\$b): int {\n\t}\n}";
     $codegen = new CodeGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false, 'generateReturnTypeHints' => true]);
     $code = $codegen->generate($class);
     $this->assertEquals($expected, $code);
 }
    $class = new gossi\codegen\model\PhpClass($classDescription['identifier']);
    $class->setFinal(true);
    $escapedClassName = str_replace("\\", "\\\\", $class->getQualifiedName());
    $class->setProperty(PhpProperty::create("connection")->setType('\\PDO'));
    $constructor = PhpMethod::create("__construct");
    $constructor->addSimpleParameter("connection", '\\PDO');
    $constructor->setBody('$this->connection = $connection;');
    $class->setMethod($constructor);
    foreach ($classDescription['properties'] as $propertyIdentifier => $value) {
        $class->setProperty(PhpProperty::create($propertyIdentifier));
    }
    $querybuilder = $conn->createQueryBuilder();
    $foreignKeys = $table->getForeignKeys();
    foreach ($classDescription['methods'] as $methodIdentifier => $method) {
        $foreignKeyMethod = PhpMethod::create($methodIdentifier);
        $foreignKeyMapParameters = [];
        $foreignKeyMethod->setParameters(array_map(function ($methodParameter) {
            return PhpParameter::create($methodParameter);
        }, $method['parameters']));
        $query = $querybuilder->select($method['query'][1]['fields'])->from($method['query'][1]['from']);
        if (strlen($method['query'][1]['where']) > 0) {
            $query->where($method['query'][1]['where']);
        }
        $foreignKeyMethod->setBody('$statement = $this->connection->prepare("' . $query . '", \\PDO::FETCH_CLASS, "' . str_replace("\\", "\\\\", $escapedClassName) . '", [$connection]);' . PHP_EOL . join(PHP_EOL, array_map(function ($methodParameter) {
            return '$statement->bindParam(":' . $methodParameter . '", $' . $methodParameter . ', \\PDO::PARAM_STR);';
        }, $method['parameters'])) . PHP_EOL . 'return $statement->fetchAll();');
        $class->setMethod($foreignKeyMethod);
    }
    $generator = new CodeGenerator();
    file_put_contents($tablesDirectory . DIRECTORY_SEPARATOR . $tableName . '.php', '<?php' . PHP_EOL . $generator->generate($class));
}
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $controller = $this->argument('controller');
     // création de la méthode
     $method = camel_case($this->argument('method') . '_' . $this->argument('name'));
     $method = PhpMethod::create($method);
     // PARAMS
     $this->generateParams($method);
     // BODY
     $body = '';
     $body .= $this->generateAcl();
     // TEMPLATE
     $template = camel_case('template_' . $this->option('template'));
     if (!method_exists($this, $template)) {
         exc('Impossible de trouver le template : ' . $template);
     }
     $body .= call_user_func([$this, $template]);
     $method->setBody($body);
     // DOCKBOCK
     $dockblock = new Docblock();
     $dockblock->appendTag(TagFactory::create('name', 'Artisan'));
     $dockblock->appendTag(TagFactory::create('see', 'php artisan ffmake:action'));
     $dockblock->appendTag(TagFactory::create('generated', Carbon::now()));
     $method->setDocblock($dockblock);
     // CONTROLLER
     $controller = ucfirst(camel_case($controller . '_controller'));
     $controller = new \ReflectionClass('App\\Http\\Controllers\\' . $controller);
     $class = PhpClass::fromReflection($controller)->setMethod($method);
     $class->setParentClassName('Controller');
     // fix la gestion des namespaec pour la parent class
     // GENERATION
     $generator = new CodeGenerator();
     $class = '<?php ' . $generator->generate($class);
     // inscription du code dans la classe
     file_put_contents($controller->getFileName(), $class);
     $this->info('Action generated dans le fichier : ' . $controller->getFileName());
 }