Skip to content

Commit

Permalink
Add the doctrine entity config when adding an entity
Browse files Browse the repository at this point in the history
  • Loading branch information
carakas committed Jul 24, 2018
1 parent d5fb4e0 commit 59552ce
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
9 changes: 9 additions & 0 deletions features/generate/domain/entity.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ Feature: It is possible to generate a class for an entity
"""
Then the command has finished successfully
And the file "src/Standalone/MyEntityWithMultipleParameters.php" should be dumped and look like "../generate/domain/resources/php71/Standalone/MyEntityWithMultipleParameters.php"

Scenario:
When I run the command "generate:domain:entity" and I provide as input
"""
MyTestEntity[enter]Backend\Modules\TestModule\Domain\MyTestEntity[enter]MyTestEntity[enter][enter]
"""
Then the command has finished successfully
And the file "src/Backend/Modules/TestModule/Domain/MyTestEntity/MyTestEntity.php" should be dumped and look like "../generate/domain/resources/php71/Backend/Modules/TestModule/Domain/MyTestEntity/MyTestEntity.php"
And the file "src/Backend/Modules/TestModule/Resources/config/doctrine.yml" should be dumped and look like "../generate/domain/resources/php71/Backend/Modules/TestModule/Resources/config/doctrine.yml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Backend\Modules\TestModule\Domain\MyTestEntity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name="MyTestEntity")
* @ORM\Entity
*/
final class MyTestEntity
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;

public function getId(): int
{
return $this->id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

MyTestEntity:
type: annotation
is_bundle: false
dir: "%kernel.project_dir%/src/Backend/Modules/TestModule/Domain/MyTestEntity/MyTestEntity"
alias: MyTestEntity
prefix: Backend\Modules\TestModule\Domain\MyTestEntity
14 changes: 14 additions & 0 deletions src/CLI/Console/Generate/Domain/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ModuleGenerator\CLI\Console\GenerateCommand;
use ModuleGenerator\Domain\Entity\Entity as EntityClass;
use ModuleGenerator\Domain\Entity\EntityType;
use ModuleGenerator\Module\Backend\Resources\Config\Doctrine\Doctrine;
use ModuleGenerator\PhpGenerator\ModuleName\ModuleName;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -31,5 +33,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
);

$this->generateService->generateClass($entity, $this->getTargetPhpVersion());

$moduleName = $this->extractModuleName($entity->getClassName()->getNamespace());
if ($moduleName instanceof ModuleName) {
$this->generateService->generateFile(
new Doctrine(
$moduleName,
[$entity],
true
),
$this->getTargetPhpVersion()
);
}
}
}
17 changes: 17 additions & 0 deletions src/Module/Backend/Resources/Config/Doctrine/Doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
namespace ModuleGenerator\Module\Backend\Resources\Config\Doctrine;

use ModuleGenerator\CLI\Service\Generate\GeneratableModuleFile;
use ModuleGenerator\Domain\Entity\Entity;
use ModuleGenerator\PhpGenerator\ModuleName\ModuleName;

final class Doctrine extends GeneratableModuleFile
{
/** @var Entity[] */
private $entities;

public function __construct(ModuleName $moduleName, array $entities = [], bool $append = false)
{
parent::__construct($moduleName, $append);

$this->entities = $entities;
}

public function getFilePath(float $targetPhpVersion): string
{
return 'Backend/Modules/' . $this->getModuleName() . '/Resources/config/doctrine.yml';
Expand All @@ -15,4 +27,9 @@ public function getTemplatePath(float $targetPhpVersion): string
{
return __DIR__ . '/doctrine.yml.twig';
}

public function getEntities(): array
{
return $this->entities;
}
}
12 changes: 11 additions & 1 deletion src/Module/Backend/Resources/Config/Doctrine/doctrine.yml.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
doctrine:
{% if not file.append %}doctrine:
dbal:
types:
orm:
mappings:
{% endif %}
{% for entity in file.entities %}

{{ entity.tableName }}:
type: annotation
is_bundle: false
dir: "%kernel.project_dir%/src{{ entity.className.fullyQualifiedName|replace({'\\': '/'}) }}"
alias: {{ entity.tableName }}
prefix: {{ entity.className.namespace }}
{% endfor %}

0 comments on commit 59552ce

Please sign in to comment.