Skip to content

Ability to use sub namespace for entity #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,27 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma

public function getParameters(InputInterface $input): array
{
$entityClassName = Str::asClassName($input->getArgument('entity-class'));
$classname = $input->getArgument('entity-class');

$parts = explode('\\',$classname);
$short_classname = $parts[count($parts)-1] ;
$namespace = implode('\\',array_slice($parts,0,-1));
$classname = $short_classname ;

$path = str_replace('\\','/',$namespace);

if ($namespace) {
$path = $path.'/' ;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should somehow be extracted into an external class, as we'll probably want to re-use this other places too.


$entityClassName = Str::asClassName($classname);
Validator::validateClassName($entityClassName);
$entityAlias = strtolower($entityClassName[0]);
$repositoryClassName = Str::addSuffix($entityClassName, 'Repository');

return [
'path' => $path,
'entity_namespace' => $namespace,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this be the full namespace - e.g. App\Entity\Foo or App\Entity? It would drastically simplify the template. We should also have one for repository_namespace

'entity_class_name' => $entityClassName,
'entity_alias' => $entityAlias,
'repository_class_name' => $repositoryClassName,
Expand All @@ -63,11 +78,11 @@ public function getParameters(InputInterface $input): array
public function getFiles(array $params): array
{
return [
__DIR__.'/../Resources/skeleton/doctrine/Entity.tpl.php' => 'src/Entity/'.$params['entity_class_name'].'.php',
__DIR__.'/../Resources/skeleton/doctrine/Repository.tpl.php' => 'src/Repository/'.$params['repository_class_name'].'.php',
__DIR__.'/../Resources/skeleton/doctrine/Entity.tpl.php' => 'src/Entity/'.$params['path'].$params['entity_class_name'].'.php',
__DIR__.'/../Resources/skeleton/doctrine/Repository.tpl.php' => 'src/Repository/'.$params['path'].$params['repository_class_name'].'.php',
];
}

public function writeNextStepsMessage(array $params, ConsoleStyle $io)
{
$io->text([
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/skeleton/doctrine/Entity.tpl.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?= "<?php\n" ?>

namespace App\Entity;
namespace App\Entity<?= $entity_namespace ? '\\'.$entity_namespace : '' ?>;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="App\Repository\<?= $repository_class_name ?>")
* @ORM\Entity(repositoryClass="App\Repository\<?= $entity_namespace ? $entity_namespace.'\\' : '' ?><?= $repository_class_name ?>")
*/
class <?= $entity_class_name."\n" ?>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/skeleton/doctrine/Repository.tpl.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?= "<?php\n" ?>

namespace App\Repository;
namespace App\Repository<?= $entity_namespace ? '\\'.$entity_namespace : '' ?>;

use App\Entity\<?= $entity_class_name ?>;
use App\Entity\<?= $entity_namespace ? $entity_namespace.'\\' : '' ?><?= $entity_class_name ?>;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

Expand Down