Skip to content

Commit

Permalink
[make:user] Legacy <= 5.3 & Doctrine Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow authored and weaverryan committed May 3, 2022
1 parent 9a61b81 commit e0c5f2b
Show file tree
Hide file tree
Showing 27 changed files with 159 additions and 929 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require-dev": {
"composer/semver": "^3.0",
"doctrine/doctrine-bundle": "^2.4",
"doctrine/orm": "^2.3",
"doctrine/orm": "^2.10.0",
"symfony/http-client": "^5.4.7|^6.0",
"symfony/phpunit-bridge": "^5.4.7|^6.0",
"symfony/polyfill-php80": "^1.16.0",
Expand All @@ -41,6 +41,9 @@
"preferred-install": "dist",
"sort-packages": true
},
"conflict": {
"doctrine/orm": "<2.10"
},
"autoload": {
"psr-4": { "Symfony\\Bundle\\MakerBundle\\": "src/" }
},
Expand Down

This file was deleted.

57 changes: 43 additions & 14 deletions src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@

namespace Symfony\Bundle\MakerBundle\Doctrine;

use Doctrine\Common\Persistence\ManagerRegistry as LegacyManagerRegistry;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Mapping;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\UX\Turbo\Attribute\Broadcast;

/**
* @internal
Expand All @@ -25,7 +34,6 @@ final class EntityClassGenerator
{
private $generator;
private $doctrineHelper;
private $managerRegistryClassName = LegacyManagerRegistry::class;

public function __construct(Generator $generator, DoctrineHelper $doctrineHelper)
{
Expand All @@ -43,11 +51,25 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $

$tableName = $this->doctrineHelper->getPotentialTableName($entityClassDetails->getFullName());

$useStatements = new UseStatementGenerator([
$repoClassDetails->getFullName(),
[Mapping::class => 'ORM'],
]);

if ($broadcast) {
$useStatements->addUseStatement(Broadcast::class);
}

if ($apiResource) {
// @legacy Drop annotation class when annotations are no longer supported.
$useStatements->addUseStatement(class_exists(ApiResource::class) ? ApiResource::class : \ApiPlatform\Core\Annotation\ApiResource::class);
}

$entityPath = $this->generator->generateClass(
$entityClassDetails->getFullName(),
'doctrine/Entity.tpl.php',
[
'repository_full_class_name' => $repoClassDetails->getFullName(),
'use_statements' => $useStatements,
'repository_class_name' => $repoClassDetails->getShortName(),
'api_resource' => $apiResource,
'broadcast' => $broadcast,
Expand All @@ -69,7 +91,7 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $
return $entityPath;
}

public function generateRepositoryClass(string $repositoryClass, string $entityClass, bool $withPasswordUpgrade, bool $includeExampleComments = true)
public function generateRepositoryClass(string $repositoryClass, string $entityClass, bool $withPasswordUpgrade, bool $includeExampleComments = true): void
{
$shortEntityClass = Str::getShortClassName($entityClass);
$entityAlias = strtolower($shortEntityClass[0]);
Expand All @@ -82,26 +104,33 @@ public function generateRepositoryClass(string $repositoryClass, string $entityC

$interfaceClassNameDetails = new ClassNameDetails($passwordUserInterfaceName, 'Symfony\Component\Security\Core\User');

$useStatements = new UseStatementGenerator([
$entityClass,
ManagerRegistry::class,
ServiceEntityRepository::class,
OptimisticLockException::class,
ORMException::class,
]);

if ($withPasswordUpgrade) {
$useStatements->addUseStatement([
$interfaceClassNameDetails->getFullName(),
PasswordUpgraderInterface::class,
UnsupportedUserException::class,
]);
}

$this->generator->generateClass(
$repositoryClass,
'doctrine/Repository.tpl.php',
[
'entity_full_class_name' => $entityClass,
'use_statements' => $useStatements,
'entity_class_name' => $shortEntityClass,
'entity_alias' => $entityAlias,
'with_password_upgrade' => $withPasswordUpgrade,
'password_upgrade_user_interface' => $interfaceClassNameDetails,
'doctrine_registry_class' => $this->managerRegistryClassName,
'include_example_comments' => $includeExampleComments,
]
);
}

/**
* Called by a compiler pass to inject the non-legacy value if available.
*/
public function setMangerRegistryClassName(string $managerRegistryClassName)
{
$this->managerRegistryClassName = $managerRegistryClassName;
}
}
48 changes: 33 additions & 15 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassDetails;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -48,12 +49,22 @@ final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface
private $doctrineHelper;
private $generator;
private $entityClassGenerator;

public function __construct(FileManager $fileManager, DoctrineHelper $doctrineHelper, string $projectDirectory, Generator $generator = null, EntityClassGenerator $entityClassGenerator = null)
{
private $phpCompatUtil;

public function __construct(
FileManager $fileManager,
DoctrineHelper $doctrineHelper,
string $projectDirectory = null,
Generator $generator = null,
EntityClassGenerator $entityClassGenerator = null,
PhpCompatUtil $phpCompatUtil = null
) {
$this->fileManager = $fileManager;
$this->doctrineHelper = $doctrineHelper;
// $projectDirectory is unused, argument kept for BC

if (null !== $projectDirectory) {
@trigger_error('The $projectDirectory constructor argument is no longer used since 1.41.0', \E_USER_DEPRECATED);
}

if (null === $generator) {
@trigger_error(sprintf('Passing a "%s" instance as 4th argument is mandatory since version 1.5.', Generator::class), \E_USER_DEPRECATED);
Expand All @@ -68,6 +79,13 @@ public function __construct(FileManager $fileManager, DoctrineHelper $doctrineHe
} else {
$this->entityClassGenerator = $entityClassGenerator;
}

if (null === $phpCompatUtil) {
@trigger_error(sprintf('Passing a "%s" instance as 6th argument is mandatory since version 1.41.0', PhpCompatUtil::class), \E_USER_DEPRECATED);
$this->phpCompatUtil = new PhpCompatUtil($this->fileManager);
} else {
$this->phpCompatUtil = $phpCompatUtil;
}
}

public static function getCommandName(): string
Expand All @@ -80,7 +98,7 @@ public static function getCommandDescription(): string
return 'Creates or updates a Doctrine entity class, and optionally an API Platform resource';
}

public function configureCommand(Command $command, InputConfiguration $inputConf)
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument('name', InputArgument::OPTIONAL, sprintf('Class name of the entity to create or update (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
Expand All @@ -91,10 +109,10 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeEntity.txt'))
;

$inputConf->setArgumentAsNonInteractive('name');
$inputConfig->setArgumentAsNonInteractive('name');
}

public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
if ($input->getArgument('name')) {
return;
Expand Down Expand Up @@ -143,7 +161,7 @@ class_exists(Broadcast::class) &&
}
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$overwrite = $input->getOption('overwrite');

Expand Down Expand Up @@ -308,7 +326,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
]);
}

public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null)
public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null): void
{
if (null !== $input && $input->getOption('api-resource')) {
$dependencies->addClassDependency(
Expand Down Expand Up @@ -424,7 +442,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
return $data;
}

private function printAvailableTypes(ConsoleStyle $io)
private function printAvailableTypes(ConsoleStyle $io): void
{
$allTypes = $this->getTypesMap();

Expand Down Expand Up @@ -810,9 +828,7 @@ private function createClassManipulator(string $path, ConsoleStyle $io, bool $ov

private function getPathOfClass(string $class): string
{
$classDetails = new ClassDetails($class);

return $classDetails->getPath();
return (new ClassDetails($class))->getPath();
}

private function isClassInVendor(string $class): bool
Expand All @@ -822,7 +838,7 @@ private function isClassInVendor(string $class): bool
return $this->fileManager->isPathInVendor($path);
}

private function regenerateEntities(string $classOrNamespace, bool $overwrite, Generator $generator)
private function regenerateEntities(string $classOrNamespace, bool $overwrite, Generator $generator): void
{
$regenerator = new EntityRegenerator($this->doctrineHelper, $this->fileManager, $generator, $this->entityClassGenerator, $overwrite);
$regenerator->regenerateEntities($classOrNamespace);
Expand All @@ -841,6 +857,7 @@ private function getPropertyNames(string $class): array
}, $reflClass->getProperties());
}

/** @legacy Drop when Annotations are no longer supported */
private function doesEntityUseAnnotationMapping(string $className): bool
{
if (!class_exists($className)) {
Expand All @@ -857,9 +874,10 @@ private function doesEntityUseAnnotationMapping(string $className): bool
return $this->doctrineHelper->isClassAnnotated($className);
}

/** @legacy Drop when Annotations are no longer supported */
private function doesEntityUseAttributeMapping(string $className): bool
{
if (\PHP_MAJOR_VERSION < 8) {
if (!$this->phpCompatUtil->canUseAttributes()) {
return false;
}

Expand Down
23 changes: 16 additions & 7 deletions src/Maker/MakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
use Symfony\Bundle\MakerBundle\Security\UserClassBuilder;
use Symfony\Bundle\MakerBundle\Security\UserClassConfiguration;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
use Symfony\Bundle\MakerBundle\Util\YamlManipulationFailedException;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Yaml\Yaml;

/**
Expand All @@ -45,13 +49,9 @@
final class MakeUser extends AbstractMaker
{
private $fileManager;

private $userClassBuilder;

private $configUpdater;

private $entityClassGenerator;

private $doctrineHelper;

public function __construct(FileManager $fileManager, UserClassBuilder $userClassBuilder, SecurityConfigUpdater $configUpdater, EntityClassGenerator $entityClassGenerator, DoctrineHelper $doctrineHelper)
Expand Down Expand Up @@ -141,7 +141,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$classPath = $this->entityClassGenerator->generateEntityClass(
$userClassNameDetails,
false, // api resource
$userClassConfiguration->hasPassword() && interface_exists(PasswordUpgraderInterface::class) // security user
$userClassConfiguration->hasPassword() // security user
);
} else {
$classPath = $generator->generateClass($userClassNameDetails->getFullName(), 'Class.tpl.php');
Expand Down Expand Up @@ -169,13 +169,22 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
// C) Generate a custom user provider, if necessary
if (!$userClassConfiguration->isEntity()) {
$userClassConfiguration->setUserProviderClass($generator->getRootNamespace().'\\Security\\UserProvider');

$useStatements = new UseStatementGenerator([
UnsupportedUserException::class,
UserNotFoundException::class,
PasswordAuthenticatedUserInterface::class,
PasswordUpgraderInterface::class,
UserInterface::class,
UserProviderInterface::class,
]);

$customProviderPath = $generator->generateClass(
$userClassConfiguration->getUserProviderClass(),
'security/UserProvider.tpl.php',
[
'uses_user_identifier' => class_exists(UserNotFoundException::class),
'use_statements' => $useStatements,
'user_short_name' => $userClassNameDetails->getShortName(),
'use_legacy_password_upgrader_type' => !interface_exists(PasswordAuthenticatedUserInterface::class),
]
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/MakerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\MakeCommandRegistrationPass;
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\RemoveMissingParametersPass;
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\SetDoctrineAnnotatedPrefixesPass;
use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\SetDoctrineManagerRegistryClassPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -32,7 +31,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new DoctrineAttributesCheckPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11);
$container->addCompilerPass(new MakeCommandRegistrationPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
$container->addCompilerPass(new RemoveMissingParametersPass());
$container->addCompilerPass(new SetDoctrineManagerRegistryClassPass());
$container->addCompilerPass(new SetDoctrineAnnotatedPrefixesPass());
}
}
3 changes: 2 additions & 1 deletion src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
<service id="maker.maker.make_entity" class="Symfony\Bundle\MakerBundle\Maker\MakeEntity">
<argument type="service" id="maker.file_manager" />
<argument type="service" id="maker.doctrine_helper" />
<argument>%kernel.project_dir%</argument>
<argument>null</argument>
<argument type="service" id="maker.generator" />
<argument type="service" id="maker.entity_class_generator" />
<argument type="service" id="maker.php_compat_util" />
<tag name="maker.command" />
</service>

Expand Down
Loading

0 comments on commit e0c5f2b

Please sign in to comment.