8000 use trait for handing uuid io · symfony/maker-bundle@7594d97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7594d97

Browse files
committed
use trait for handing uuid io
1 parent 4d865a7 commit 7594d97

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

src/Doctrine/EntityClassGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636
) {
3737
}
3838

39-
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $useUUIDIdentifier = false, bool $broadcast = false): string
39+
public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $apiResource, bool $withPasswordUpgrade = false, bool $generateRepositoryClass = true, bool $broadcast = false, bool $useUuidIdentifier = false): string
4040
{
4141
$repoClassDetails = $this->generator->createClassNameDetails(
4242
$entityClassDetails->getRelativeName(),
@@ -59,7 +59,7 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
5959
$useStatements->addUseStatement(ApiResource::class);
6060
}
6161

62-
if ($useUUIDIdentifier) {
62+
if ($useUuidIdentifier) {
6363
$useStatements->addUseStatement(Uuid::class);
6464
}
6565

@@ -73,7 +73,7 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
7373
'broadcast' => $broadcast,
7474
'should_escape_table_name' => $this->doctrineHelper->isKeyword($tableName),
7575
'table_name' => $tableName,
76-
'uuid_id_enabled' => $useUUIDIdentifier,
76+
'uuid_id_enabled' => $useUuidIdentifier,
7777
]
7878
);
7979

src/Maker/Common/UidTrait.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Maker\Common;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Uid\Uuid;
16+
17+
/**
18+
* @author Jesse Rushlow<jr@rushlow.dev>
19+
*
20+
* @internal
21+
*/
22+
trait UidTrait
23+
{
24+
protected bool $usesUid = false;
25+
26+
protected function checkIsUsingUid(InputInterface $input): void
27+
{
28+
if ($this->usesUid = $input->getOption('uuid_id') && !class_exists(Uuid::class)) {
29+
throw new \RuntimeException('You must install symfony/uid to use Uuid\'s as "id" (composer require symfony/uid)');
30+
}
31+
}
32+
}

src/Maker/MakeEntity.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Bundle\MakerBundle\Generator;
2626
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
2727
use Symfony\Bundle\MakerBundle\InputConfiguration;
28+
use Symfony\Bundle\MakerBundle\Maker\Common\UidTrait;
2829
use Symfony\Bundle\MakerBundle\Str;
2930
use Symfony\Bundle\MakerBundle\Util\ClassDetails;
3031
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassProperty;
@@ -47,6 +48,8 @@
4748
*/
4849
final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface
4950
{
51+
use UidTrait;
52+
5053
private Generator $generator;
5154
private EntityClassGenerator $entityClassGenerator;
5255

@@ -123,6 +126,8 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
123126
return;
124127
}
125128

129+
$this->checkIsUsingUid($input);
130+
126131
$argument = $command->getDefinition()->getArgument('name');
127132
$question = $this->createEntityClassQuestion($argument->getDescription());
128133
$entityClassName = $io->askQuestion($question);
@@ -166,12 +171,6 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
166171

167172
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
168173
{
169-
if ($input->getOption('uuid_id')) {
170-
if (!class_exists('Symfony\Component\Uid\Uuid')) {
171-
$io->warning('The symfony/uid package is not installed');
172-
}
173-
}
174-
175174
$overwrite = $input->getOption('overwrite');
176175

177176
// the regenerate option has entirely custom behavior
@@ -195,8 +194,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
195194
$input->getOption('api-resource'),
196195
false,
197196
true,
198-
$input->getOption('uuid_id'),
199-
$broadcast
197+
$broadcast,
198+
$this->usesUid
200199
);
201200

202201
if ($broadcast) {

src/Maker/MakeResetPassword.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Bundle\MakerBundle\FileManager;
2727
use Symfony\Bundle\MakerBundle\Generator;
2828
use Symfony\Bundle\MakerBundle\InputConfiguration;
29+
use Symfony\Bundle\MakerBundle\Maker\Common\UidTrait;
2930
use Symfony\Bundle\MakerBundle\Security\InteractiveSecurityHelper;
3031
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
3132
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
@@ -81,6 +82,8 @@
8182
*/
8283
class MakeResetPassword extends AbstractMaker
8384
{
85+
use UidTrait;
86+
8487
private string $fromEmailAddress;
8588
private string $fromEmailName;
8689
private string $controllerResetSuccessRedirect;
@@ -136,6 +139,8 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
136139
{
137140
$io->title('Let\'s make a password reset feature!');
138141

142+
$this->checkIsUsingUid($input);
143+
139144
$interactiveSecurityHelper = new InteractiveSecurityHelper();
140145

141146
if (!$this->fileManager->fileExists($path = 'config/packages/security.yaml')) {
@@ -273,7 +278,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
273278
]
274279
);
275280

276-
$this->generateRequestEntity($generator, $requestClassNameDetails, $repositoryClassNameDetails, $input->getOption('uuid_id'));
281+
$this->generateRequestEntity($generator, $requestClassNameDetails, $repositoryClassNameDetails);
277282

278283
$this->setBundleConfig($io, $generator, $repositoryClassNameDetails->getFullName());
279284

@@ -406,9 +411,9 @@ private function successMessage(ConsoleStyle $io, string $requestClassName): voi
406411
$io->newLine();
407412
}
408413

409-
private function generateRequestEntity(Generator $generator, ClassNameDetails $requestClassNameDetails, ClassNameDetails $repositoryClassNameDetails, $useUUIDIdentifier = false): void
414+
private function generateRequestEntity(Generator $generator, ClassNameDetails $requestClassNameDetails, ClassNameDetails $repositoryClassNameDetails): void
410415
{
411-
$requestEntityPath = $this->entityClassGenerator->generateEntityClass($requestClassNameDetails, false, false, false, $useUUIDIdentifier);
416+
$requestEntityPath = $this->entityClassGenerator->generateEntityClass($requestClassNameDetails, false, false, false, $this->usesUid);
412417

413418
$generator->writeChanges();
414419

src/Maker/MakeUser.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Bundle\MakerBundle\FileManager;
2222
use Symfony\Bundle\MakerBundle\Generator;
2323
use Symfony\Bundle\MakerBundle\InputConfiguration;
24+
use Symfony\Bundle\MakerBundle\Maker\Common\UidTrait;
2425
use Symfony\Bundle\MakerBundle\Security\SecurityConfigUpdater;
2526
use Symfony\Bundle\MakerBundle\Security\UserClassBuilder;
2627
use Symfony\Bundle\MakerBundle\Security\UserClassConfiguration;
@@ -48,6 +49,8 @@
4849
*/
4950
final class MakeUser extends AbstractMaker
5051
{
52+
use UidTrait;
53+
5154
public function __construct(
5255
private FileManager $fileManager,
5356
private UserClassBuilder $userClassBuilder,
@@ -82,6 +85,8 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
8285

8386
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
8487
{
88+
$this->checkIsUsingUid($input);
89+
8590
if (null === $input->getArgument('name')) {
8691
$name = $io->ask(
8792
$command->getDefinition()->getArgument('name')->getDescription(),
@@ -138,8 +143,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
138143
$userClassNameDetails,
139144
false, // api resource
140145
$userClassConfiguration->hasPassword(), // security user
141-
true,
142-
$input->getOption('uuid_id')
146+
useUuidIdentifier: $this->usesUid
143147
);
144148
} else {
145149
$classPath = $generator->generateClass($userClassNameDetails->getFullName(), 'Class.tpl.php');

0 commit comments

Comments
 (0)
0