From 052d1f652e5a2e6547bcedd8b2d982448c872971 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 13:06:36 +0100 Subject: [PATCH 01/16] Add user cache in framework cache pools Add cache and form factory --- Resources/config/packages/rapsys_user.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Resources/config/packages/rapsys_user.yaml b/Resources/config/packages/rapsys_user.yaml index 5e91556..4bfea90 100644 --- a/Resources/config/packages/rapsys_user.yaml +++ b/Resources/config/packages/rapsys_user.yaml @@ -1,3 +1,9 @@ +#Framework configuration +framework: + cache: + pools: + user.cache: + adapter: 'cache.adapter.system' #Service configuration services: #Register security context service @@ -7,7 +13,7 @@ services: arguments: [ [ '@security.access.role_hierarchy_voter' ] ] #Register default controller Rapsys\UserBundle\Controller\UserController: - arguments: [ '@security.authorization_checker', '@service_container', '@doctrine', '@security.user_password_hasher', '@logger', '@mailer.mailer', '@doctrine.orm.default_entity_manager', '@router', '@security.helper', '@rapsys_pack.slugger_util', '@request_stack', '@translator', '@twig' ] + arguments: [ '@user.cache', '@security.authorization_checker', '@service_container', '@doctrine', '@form.factory', '@security.user_password_hasher', '@logger', '@mailer.mailer', '@doctrine.orm.default_entity_manager', '@router', '@security.helper', '@rapsys_pack.slugger_util', '@request_stack', '@translator', '@twig' ] tags: [ 'controller.service_arguments' ] #Register default repository factory Rapsys\UserBundle\Factory\RepositoryFactory: -- 2.41.1 From dbcf9148dedb53d7f52e2b39051c61ee8d64e773 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 13:08:22 +0100 Subject: [PATCH 02/16] Add cache and form factory Php 8.x style contructor Update subscribed services --- Controller/AbstractController.php | 120 ++++-------------------------- 1 file changed, 13 insertions(+), 107 deletions(-) diff --git a/Controller/AbstractController.php b/Controller/AbstractController.php index d6f34a2..e0e1ff2 100644 --- a/Controller/AbstractController.php +++ b/Controller/AbstractController.php @@ -13,11 +13,14 @@ namespace Rapsys\UserBundle\Controller; use Doctrine\ORM\EntityManagerInterface; use Doctrine\Persistence\ManagerRegistry; + use Psr\Log\LoggerInterface; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController; use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; @@ -27,8 +30,10 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Service\ServiceSubscriberInterface; use Symfony\Contracts\Translation\TranslatorInterface; + use Twig\Environment; use Rapsys\PackBundle\Util\SluggerUtil; @@ -51,11 +56,6 @@ abstract class AbstractController extends BaseAbstractController implements Serv */ protected array $context; - /** - * Limit integer - */ - protected int $limit; - /** * Locale string */ @@ -66,72 +66,14 @@ abstract class AbstractController extends BaseAbstractController implements Serv */ protected int $page; - /** - * AuthorizationCheckerInterface instance - */ - protected AuthorizationCheckerInterface $checker; - - /** - * ManagerRegistry instance - */ - protected ManagerRegistry $doctrine; - - /** - * UserPasswordHasherInterface instance - */ - protected UserPasswordHasherInterface $hasher; - - /** - * LoggerInterface instance - */ - protected LoggerInterface $logger; - - /** - * MailerInterface instance - */ - protected MailerInterface $mailer; - - /** - * EntityManagerInterface instance - */ - protected EntityManagerInterface $manager; - - /** - * Request instance - */ - protected Request $request; - - /** - * Router instance - */ - protected RouterInterface $router; - - /** - * Security instance - */ - protected Security $security; - - /** - * Slugger util instance - */ - protected SluggerUtil $slugger; - - /** - * Translator instance - */ - protected TranslatorInterface $translator; - - /** - * Twig\Environment instance - */ - protected Environment $twig; - /** * Abstract constructor * + * @param CacheInterface $cache The cache instance * @param AuthorizationCheckerInterface $checker The checker instance * @param ContainerInterface $container The container instance * @param ManagerRegistry $doctrine The doctrine instance + * @param FormFactoryInterface $factory The factory instance * @param UserPasswordHasherInterface $hasher The password hasher instance * @param LoggerInterface $logger The logger instance * @param MailerInterface $mailer The mailer instance @@ -144,49 +86,10 @@ abstract class AbstractController extends BaseAbstractController implements Serv * @param Environment $twig The twig environment instance * @param integer $limit The page limit */ - public function __construct(AuthorizationCheckerInterface $checker, ContainerInterface $container, ManagerRegistry $doctrine, UserPasswordHasherInterface $hasher, LoggerInterface $logger, MailerInterface $mailer, EntityManagerInterface $manager, RouterInterface $router, Security $security, SluggerUtil $slugger, RequestStack $stack, TranslatorInterface $translator, Environment $twig, int $limit = 5) { + public function __construct(protected CacheInterface $cache, protected AuthorizationCheckerInterface $checker, protected ContainerInterface $container, protected ManagerRegistry $doctrine, protected FormFactoryInterface $factory, protected UserPasswordHasherInterface $hasher, protected LoggerInterface $logger, protected MailerInterface $mailer, protected EntityManagerInterface $manager, protected RouterInterface $router, protected Security $security, protected SluggerUtil $slugger, protected RequestStack $stack, protected TranslatorInterface $translator, protected Environment $twig, protected int $limit = 5) { //Retrieve config $this->config = $container->getParameter(RapsysUserBundle::getAlias()); - //Set checker - $this->checker = $checker; - - //Set container - $this->container = $container; - - //Set doctrine - $this->doctrine = $doctrine; - - //Set hasher - $this->hasher = $hasher; - - //Set logger - $this->logger = $logger; - - //Set limit - $this->limit = $limit; - - //Set mailer - $this->mailer = $mailer; - - //Set manager - $this->manager = $manager; - - //Set router - $this->router = $router; - - //Set security - $this->security = $security; - - //Set slugger - $this->slugger = $slugger; - - //Set translator - $this->translator = $translator; - - //Set twig - $this->twig = $twig; - //Get current request $this->request = $stack->getCurrentRequest(); @@ -414,16 +317,19 @@ abstract class AbstractController extends BaseAbstractController implements Serv return [ 'doctrine' => ManagerRegistry::class, 'doctrine.orm.default_entity_manager' => EntityManagerInterface::class, + 'form.factory' => FormFactoryInterface::class, 'logger' => LoggerInterface::class, 'mailer.mailer' => MailerInterface::class, 'rapsys_pack.slugger_util' => SluggerUtil::class, 'request_stack' => RequestStack::class, 'router' => RouterInterface::class, 'security.authorization_checker' => AuthorizationCheckerInterface::class, - 'security' => Security::class, + 'security.helper' => Security::class, 'security.user_password_hasher' => UserPasswordHasherInterface::class, 'service_container' => ContainerInterface::class, - 'translator' => TranslatorInterface::class + 'translator' => TranslatorInterface::class, + 'twig' => Environment::class, + 'user.cache' => CacheInterface::class ]; } } -- 2.41.1 From 098b66c569cab0032e63f1140dc2c687d4394da9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 14:11:22 +0100 Subject: [PATCH 03/16] Rename Rapsys\UserBundle\Factory\RepositoryFactory in Rapsys\UserBundle\Factory Add doctrine orm repository factory --- Resources/config/packages/rapsys_user.yaml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Resources/config/packages/rapsys_user.yaml b/Resources/config/packages/rapsys_user.yaml index 4bfea90..2fecff2 100644 --- a/Resources/config/packages/rapsys_user.yaml +++ b/Resources/config/packages/rapsys_user.yaml @@ -1,9 +1,17 @@ +#Doctrine configuration +doctrine: + #Orm configuration + orm: + #Replace repository factory + repository_factory: 'Rapsys\UserBundle\Factory' + #Framework configuration framework: cache: pools: user.cache: adapter: 'cache.adapter.system' + #Service configuration services: #Register security context service @@ -15,9 +23,8 @@ services: Rapsys\UserBundle\Controller\UserController: arguments: [ '@user.cache', '@security.authorization_checker', '@service_container', '@doctrine', '@form.factory', '@security.user_password_hasher', '@logger', '@mailer.mailer', '@doctrine.orm.default_entity_manager', '@router', '@security.helper', '@rapsys_pack.slugger_util', '@request_stack', '@translator', '@twig' ] tags: [ 'controller.service_arguments' ] - #Register default repository factory - Rapsys\UserBundle\Factory\RepositoryFactory: - arguments: [ '@request_stack', '@router', '@rapsys_pack.slugger_util', '@translator', '%rapsys_user.languages%', '%rapsys_user.locale%' ] + Rapsys\UserBundle\Factory: + arguments: [ '@request_stack', '@router', '@rapsys_pack.slugger_util', '@translator', '%kernel.default_locale%' ] #Register Authentication success handler security.authentication.success_handler: class: 'Rapsys\UserBundle\Handler\AuthenticationSuccessHandler' -- 2.41.1 From 7b065b7bc56a349a0e70f455ec90a91faea8acf9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 14:13:39 +0100 Subject: [PATCH 04/16] Rename Factory/RepositoryFactory in Factory Remove languages argument --- Factory/RepositoryFactory.php => Factory.php | 72 ++------------------ 1 file changed, 5 insertions(+), 67 deletions(-) rename Factory/RepositoryFactory.php => Factory.php (65%) diff --git a/Factory/RepositoryFactory.php b/Factory.php similarity index 65% rename from Factory/RepositoryFactory.php rename to Factory.php index 0de0e0a..3c5f3d6 100644 --- a/Factory/RepositoryFactory.php +++ b/Factory.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Rapsys\UserBundle\Factory; +namespace Rapsys\UserBundle; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Repository\RepositoryFactory as RepositoryFactoryInterface; @@ -23,56 +23,12 @@ use Rapsys\PackBundle\Util\SluggerUtil; /** * This factory is used to create default repository objects for entities at runtime. */ -final class RepositoryFactory implements RepositoryFactoryInterface { +final class Factory implements RepositoryFactoryInterface { /** * The list of EntityRepository instances - * - * @var array */ private array $repositoryList = []; - /** - * The list of languages - * - * @var array - */ - private array $languages = []; - - /** - * The current locale - * - * @var string - */ - private string $locale; - - /** - * The RequestStack instance - * - * @var RequestStack - */ - private RequestStack $request; - - /** - * The RouterInterface instance - * - * @var RouterInterface - */ - private RouterInterface $router; - - /** - * The SluggerUtil instance - * - * @var SluggerUtil - */ - private SluggerUtil $slugger; - - /** - * The TranslatorInterface instance - * - * @var TranslatorInterface - */ - private TranslatorInterface $translator; - /** * Initializes a new RepositoryFactory instance * @@ -80,27 +36,9 @@ final class RepositoryFactory implements RepositoryFactoryInterface { * @param RouterInterface $router The router instance * @param SluggerUtil $slugger The SluggerUtil instance * @param TranslatorInterface $translator The TranslatorInterface instance - * @param array $languages The languages list * @param string $locale The current locale */ - public function __construct(RequestStack $request, RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator, array $languages, string $locale) { - //Set request - $this->request = $request; - - //Set router - $this->router = $router; - - //Set slugger - $this->slugger = $slugger; - - //Set translator - $this->translator = $translator; - - //Set languages - $this->languages = $languages; - - //Set locale - $this->locale = $locale; + public function __construct(private RequestStack $request, private RouterInterface $router, private SluggerUtil $slugger, private TranslatorInterface $translator, private string $locale) { } /** @@ -138,7 +76,7 @@ final class RepositoryFactory implements RepositoryFactoryInterface { $this->locale = $this->request->getCurrentRequest()->getLocale() ?? $this->locale; //Return repository class instance - //XXX: router, slugger, translator, languages and locale arguments will be ignored by default - return new $repositoryClass($entityManager, $metadata, $this->router, $this->slugger, $this->translator, $this->languages, $this->locale); + //XXX: router, slugger, translator and locale arguments will be ignored by default + return new $repositoryClass($entityManager, $metadata, $this->router, $this->slugger, $this->translator, $this->locale); } } -- 2.41.1 From 7147766cba7c940cba95c8028b14d02846b055fa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 14:15:29 +0100 Subject: [PATCH 05/16] Php 8.x constructor style --- Listener/LogoutListener.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/Listener/LogoutListener.php b/Listener/LogoutListener.php index 304f123..888d670 100644 --- a/Listener/LogoutListener.php +++ b/Listener/LogoutListener.php @@ -34,26 +34,15 @@ class LogoutListener implements EventSubscriberInterface { */ protected $config; - /** - * Target url - */ - private $targetUrl; - /** * {@inheritdoc} * * @xxx Second argument will be replaced by security.firewalls.main.logout.target * @see vendor/symfony/security-bundle/Resources/config/security_listeners.php +79 */ - public function __construct(ContainerInterface $container, string $targetUrl, RouterInterface $router) { + public function __construct(protected ContainerInterface $container, protected string $targetUrl, protected RouterInterface $router) { //Set config $this->config = $container->getParameter(RapsysUserBundle::getAlias()); - - //Set target url - $this->targetUrl = $targetUrl; - - //Set router - $this->router = $router; } /** -- 2.41.1 From 49ec5adccd0de8452c6ef97a07e7c47772da25df Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 14:16:02 +0100 Subject: [PATCH 06/16] Php 8.x constructor style --- Handler/AuthenticationFailureHandler.php | 57 ++---------------------- 1 file changed, 4 insertions(+), 53 deletions(-) diff --git a/Handler/AuthenticationFailureHandler.php b/Handler/AuthenticationFailureHandler.php index 4cbd0c6..c88e138 100644 --- a/Handler/AuthenticationFailureHandler.php +++ b/Handler/AuthenticationFailureHandler.php @@ -47,7 +47,6 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler { * Config array */ protected array $config; - protected array $options; protected array $defaultOptions = [ 'failure_path' => null, 'failure_forward' => false, @@ -55,36 +54,6 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler { 'failure_path_parameter' => '_failure_path', ]; - /** - * Doctrine instance - */ - protected ManagerRegistry $doctrine; - - /** - * MailerInterface - */ - protected MailerInterface $mailer; - - /** - * Router instance - */ - protected RouterInterface $router; - - /** - * Slugger instance - */ - protected SluggerUtil $slugger; - - /** - * RequestStack instance - */ - protected RequestStack $stack; - - /** - * Translator instance - */ - protected TranslatorInterface $translator; - /** * @xxx Second argument will be replaced by security.firewalls.main.logout.target * @see vendor/symfony/security-bundle/DependencyInjection/SecurityExtension.php +360 @@ -103,30 +72,12 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler { * * {@inheritdoc} */ - public function __construct(HttpKernelInterface $httpKernel, HttpUtils $httpUtils, array $options, LoggerInterface $logger, ContainerInterface $container, ManagerRegistry $doctrine, MailerInterface $mailer, RouterInterface $router, SluggerUtil $slugger, RequestStack $stack, TranslatorInterface $translator) { - //Set config - $this->config = $container->getParameter(self::getAlias()); - - //Set doctrine - $this->doctrine = $doctrine; - - //Set mailer - $this->mailer = $mailer; - - //Set router - $this->router = $router; - - //Set slugger - $this->slugger = $slugger; - - //Set stack - $this->stack = $stack; - - //Set translator - $this->translator = $translator; - + public function __construct(protected HttpKernelInterface $httpKernel, protected HttpUtils $httpUtils, protected array $options, protected LoggerInterface $logger, protected ContainerInterface $container, protected ManagerRegistry $doctrine, protected MailerInterface $mailer, protected RouterInterface $router, protected SluggerUtil $slugger, protected RequestStack $stack, protected TranslatorInterface $translator) { //Call parent constructor parent::__construct($httpKernel, $httpUtils, $options, $logger); + + //Set config + $this->config = $container->getParameter(self::getAlias()); } /** -- 2.41.1 From a1bf95f93f8b4954704f82283e09608b8201396c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 14:16:22 +0100 Subject: [PATCH 07/16] Php 8.x constructor style --- Handler/AuthenticationSuccessHandler.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/Handler/AuthenticationSuccessHandler.php b/Handler/AuthenticationSuccessHandler.php index 579c110..11e8325 100644 --- a/Handler/AuthenticationSuccessHandler.php +++ b/Handler/AuthenticationSuccessHandler.php @@ -45,23 +45,10 @@ class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler { 'use_referer' => false, ]; - /** - * Options - */ - protected array $options; - - /** - * Router instance - */ - protected RouterInterface $router; - /** * {@inheritdoc} */ - public function __construct(RouterInterface $router, array $options = []) { - //Set router - $this->router = $router; - + public function __construct(protected RouterInterface $router, protected array $options = []) { //Set options $this->setOptions($options); } -- 2.41.1 From e57fd4e1f21e9a7b44e7d9db078e261ddc0450cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 27 Feb 2024 14:18:57 +0100 Subject: [PATCH 08/16] Rename Repository/EntityRepository in Repository --- .../EntityRepository.php => Repository.php | 61 ++----------------- Repository/UserRepository.php | 4 +- 2 files changed, 8 insertions(+), 57 deletions(-) rename Repository/EntityRepository.php => Repository.php (71%) diff --git a/Repository/EntityRepository.php b/Repository.php similarity index 71% rename from Repository/EntityRepository.php rename to Repository.php index db1921c..b21b0ca 100644 --- a/Repository/EntityRepository.php +++ b/Repository.php @@ -9,10 +9,10 @@ * file that was distributed with this source code. */ -namespace Rapsys\UserBundle\Repository; +namespace Rapsys\UserBundle; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository as BaseEntityRepository; +use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Mapping\ClassMetadata; use Symfony\Component\Routing\RouterInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -20,25 +20,11 @@ use Symfony\Contracts\Translation\TranslatorInterface; use Rapsys\PackBundle\Util\SluggerUtil; /** - * EntityRepository + * Repository * * {@inheritdoc} */ -class EntityRepository extends BaseEntityRepository { - /** - * The RouterInterface instance - * - * @var RouterInterface - */ - protected RouterInterface $router; - - /** - * The SluggerUtil instance - * - * @var SluggerUtil - */ - protected SluggerUtil $slugger; - +class Repository extends EntityRepository { /** * The table keys array * @@ -53,27 +39,6 @@ class EntityRepository extends BaseEntityRepository { */ protected array $tableValues; - /** - * The TranslatorInterface instance - * - * @var TranslatorInterface - */ - protected TranslatorInterface $translator; - - /** - * The list of languages - * - * @var string[] - */ - protected array $languages = []; - - /** - * The current locale - * - * @var string - */ - protected string $locale; - /** * Initializes a new LocationRepository instance * @@ -82,28 +47,12 @@ class EntityRepository extends BaseEntityRepository { * @param RouterInterface $router The router instance * @param SluggerUtil $slugger The SluggerUtil instance * @param TranslatorInterface $translator The TranslatorInterface instance - * @param array $languages The languages list * @param string $locale The current locale */ - public function __construct(EntityManagerInterface $manager, ClassMetadata $class, RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator, array $languages, string $locale) { + public function __construct(protected EntityManagerInterface $manager, protected ClassMetadata $class, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected string $locale) { //Call parent constructor parent::__construct($manager, $class); - //Set languages - $this->languages = $languages; - - //Set locale - $this->locale = $locale; - - //Set router - $this->router = $router; - - //Set slugger - $this->slugger = $slugger; - - //Set translator - $this->translator = $translator; - //Get quote strategy $qs = $manager->getConfiguration()->getQuoteStrategy(); $dp = $manager->getConnection()->getDatabasePlatform(); diff --git a/Repository/UserRepository.php b/Repository/UserRepository.php index da5952f..db4d84e 100644 --- a/Repository/UserRepository.php +++ b/Repository/UserRepository.php @@ -16,10 +16,12 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\PasswordUpgraderInterface; +use Rapsys\UserBundle\Repository; + /** * UserRepository */ -class UserRepository extends EntityRepository implements PasswordUpgraderInterface { +class UserRepository extends Repository implements PasswordUpgraderInterface { /** * Find user count as int * -- 2.41.1 From 7270555c0ddde10d9cfdccaf00e77259ed7d25a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 28 Feb 2024 11:41:32 +0100 Subject: [PATCH 09/16] Comment doctrine orm repository factory to avoid missing extra arguments in other bundles --- Resources/config/packages/rapsys_user.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Resources/config/packages/rapsys_user.yaml b/Resources/config/packages/rapsys_user.yaml index 2fecff2..d77a2b9 100644 --- a/Resources/config/packages/rapsys_user.yaml +++ b/Resources/config/packages/rapsys_user.yaml @@ -1,9 +1,9 @@ -#Doctrine configuration -doctrine: - #Orm configuration - orm: - #Replace repository factory - repository_factory: 'Rapsys\UserBundle\Factory' +##Doctrine configuration +#doctrine: +# #Orm configuration +# orm: +# #Replace repository factory +# repository_factory: 'Rapsys\UserBundle\Factory' #Framework configuration framework: -- 2.41.1 From 89c69fc76c57fd4a17ba68b0e8889c1844fe5701 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 28 Feb 2024 11:46:51 +0100 Subject: [PATCH 10/16] Follow container interface path change --- Controller/AbstractController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/AbstractController.php b/Controller/AbstractController.php index e0e1ff2..eecc2d0 100644 --- a/Controller/AbstractController.php +++ b/Controller/AbstractController.php @@ -14,12 +14,12 @@ namespace Rapsys\UserBundle\Controller; use Doctrine\ORM\EntityManagerInterface; use Doctrine\Persistence\ManagerRegistry; +use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController; use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait; use Symfony\Bundle\SecurityBundle\Security; -use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; -- 2.41.1 From 1675b4362da8161d1e9f8ab67da2027ed0f51389 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 28 Feb 2024 11:47:05 +0100 Subject: [PATCH 11/16] Remove unused languages Allow default and class extra keys Cleanup --- DependencyInjection/Configuration.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index fa3306c..ffe1d14 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -34,8 +34,8 @@ class Configuration implements ConfigurationInterface { //The bundle default values $defaults = [ 'class' => [ - 'group' => 'Rapsys\\UserBundle\\Entity\\Group', 'civility' => 'Rapsys\\UserBundle\\Entity\\Civility', + 'group' => 'Rapsys\\UserBundle\\Entity\\Group', 'user' => 'Rapsys\\UserBundle\\Entity\\User' ], 'default' => [ @@ -70,9 +70,6 @@ class Configuration implements ConfigurationInterface { ] ], 'translate' => [], - 'languages' => [ - 'en_gb' => 'English' - ], 'contact' => [ 'address' => 'contact@example.com', 'name' => 'John Doe' @@ -152,12 +149,17 @@ class Configuration implements ConfigurationInterface { ->addDefaultsIfNotSet() ->children() ->arrayNode('class') - ->treatNullLike([]) - ->defaultValue($defaults['class']) - ->scalarPrototype()->end() + ->addDefaultsIfNotSet() + ->ignoreExtraKeys() + ->children() + ->scalarNode('civility')->cannotBeEmpty()->defaultValue($defaults['class']['civility'])->end() + ->scalarNode('group')->cannotBeEmpty()->defaultValue($defaults['class']['group'])->end() + ->scalarNode('user')->cannotBeEmpty()->defaultValue($defaults['class']['user'])->end() + ->end() ->end() ->arrayNode('default') ->addDefaultsIfNotSet() + ->ignoreExtraKeys() ->children() ->scalarNode('admin')->cannotBeEmpty()->defaultValue($defaults['default']['admin'])->end() ->scalarNode('civility')->cannotBeEmpty()->defaultValue($defaults['default']['civility'])->end() @@ -244,12 +246,6 @@ class Configuration implements ConfigurationInterface { ->defaultValue($defaults['translate']) ->scalarPrototype()->end() ->end() - #TODO: see if we can't prevent key normalisation with ->normalizeKeys(false) - ->arrayNode('languages') - ->treatNullLike([]) - ->defaultValue($defaults['languages']) - ->scalarPrototype()->end() - ->end() ->arrayNode('contact') ->addDefaultsIfNotSet() ->children() -- 2.41.1 From ec911bef2270131d6c18b864db293339cfad7519 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 28 Feb 2024 11:48:25 +0100 Subject: [PATCH 12/16] Fix constructor prototype --- Handler/AuthenticationFailureHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Handler/AuthenticationFailureHandler.php b/Handler/AuthenticationFailureHandler.php index c88e138..c5d0ec5 100644 --- a/Handler/AuthenticationFailureHandler.php +++ b/Handler/AuthenticationFailureHandler.php @@ -72,7 +72,7 @@ class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler { * * {@inheritdoc} */ - public function __construct(protected HttpKernelInterface $httpKernel, protected HttpUtils $httpUtils, protected array $options, protected LoggerInterface $logger, protected ContainerInterface $container, protected ManagerRegistry $doctrine, protected MailerInterface $mailer, protected RouterInterface $router, protected SluggerUtil $slugger, protected RequestStack $stack, protected TranslatorInterface $translator) { + public function __construct(protected HttpKernelInterface $httpKernel, protected HttpUtils $httpUtils, protected array $options, protected ?LoggerInterface $logger, protected ContainerInterface $container, protected ManagerRegistry $doctrine, protected MailerInterface $mailer, protected RouterInterface $router, protected SluggerUtil $slugger, protected RequestStack $stack, protected TranslatorInterface $translator) { //Call parent constructor parent::__construct($httpKernel, $httpUtils, $options, $logger); -- 2.41.1 From 60b5bbf626f4aca106b66be539a9b8c4cf71892e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 29 Feb 2024 14:22:00 +0100 Subject: [PATCH 13/16] Php 8.x constructor style Strict types Cleanup --- Entity/User.php | 44 +------------------------------------------- 1 file changed, 1 insertion(+), 43 deletions(-) diff --git a/Entity/User.php b/Entity/User.php index a565187..68b0dcf 100644 --- a/Entity/User.php +++ b/Entity/User.php @@ -29,36 +29,6 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { */ protected ?int $id; - /** - * @var string - */ - protected string $mail; - - /** - * @var string - */ - protected string $password; - - /** - * @var ?string - */ - protected ?string $forename; - - /** - * @var ?string - */ - protected ?string $surname; - - /** - * @var bool - */ - protected bool $active; - - /** - * @var bool - */ - protected bool $enable; - /** * @var \DateTime */ @@ -69,11 +39,6 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { */ protected \DateTime $updated; - /** - * @var Civility - */ - protected ?Civility $civility; - /** * @var Doctrine\Common\Collections\Collection */ @@ -90,15 +55,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { * @param bool $active The user active * @param bool $enable The user enable */ - public function __construct(string $mail, string $password, ?Civility $civility = null, ?string $forename = null, ?string $surname = null, bool $active = false, bool $enable = true) { + public function __construct(protected string $mail, protected string $password, protected ?Civility $civility = null, protected ?string $forename = null, protected ?string $surname = null, protected bool $active = false, protected bool $enable = true) { //Set defaults - $this->mail = $mail; - $this->password = $password; - $this->civility = $civility; - $this->forename = $forename; - $this->surname = $surname; - $this->active = $active; - $this->enable = $enable; $this->created = new \DateTime('now'); $this->updated = new \DateTime('now'); -- 2.41.1 From edaded281e162f8aa78b6b329b3c9cec8fe176c0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 29 Feb 2024 14:23:50 +0100 Subject: [PATCH 14/16] Readd languages and locale in default key Prevent ignore extra keys removing extra keys --- DependencyInjection/Configuration.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ffe1d14..208bf98 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -41,6 +41,10 @@ class Configuration implements ConfigurationInterface { 'default' => [ 'admin' => 'ROLE_ADMIN', 'civility' => 'Mister', + 'languages' => [ + 'en_gb' => 'English' + ], + 'locales' => [ 'en_gb' ], 'group' => [ 'User' ] ], 'route' => [ @@ -150,7 +154,8 @@ class Configuration implements ConfigurationInterface { ->children() ->arrayNode('class') ->addDefaultsIfNotSet() - ->ignoreExtraKeys() + #XXX: ignoreExtraKeys(bool $remove = true) + ->ignoreExtraKeys(false) ->children() ->scalarNode('civility')->cannotBeEmpty()->defaultValue($defaults['class']['civility'])->end() ->scalarNode('group')->cannotBeEmpty()->defaultValue($defaults['class']['group'])->end() @@ -159,10 +164,23 @@ class Configuration implements ConfigurationInterface { ->end() ->arrayNode('default') ->addDefaultsIfNotSet() - ->ignoreExtraKeys() + #XXX: ignoreExtraKeys(bool $remove = true) + ->ignoreExtraKeys(false) ->children() ->scalarNode('admin')->cannotBeEmpty()->defaultValue($defaults['default']['admin'])->end() ->scalarNode('civility')->cannotBeEmpty()->defaultValue($defaults['default']['civility'])->end() + #TODO: see if we can't prevent key normalisation with ->normalizeKeys(false) + ->arrayNode('languages') + ->treatNullLike([]) + ->defaultValue($defaults['default']['languages']) + ->scalarPrototype()->end() + ->end() + #TODO: see if we can't prevent key normalisation with ->normalizeKeys(false) + ->arrayNode('locales') + ->treatNullLike([]) + ->defaultValue($defaults['default']['locales']) + ->scalarPrototype()->end() + ->end() ->arrayNode('group') ->treatNullLike([]) ->defaultValue($defaults['default']['group']) -- 2.41.1 From 0dacd7d23d5b201804a277284b15c6f5e47f99c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 29 Feb 2024 14:25:28 +0100 Subject: [PATCH 15/16] Use factory to create forms --- Controller/UserController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Controller/UserController.php b/Controller/UserController.php index cce4deb..8f3e4f4 100644 --- a/Controller/UserController.php +++ b/Controller/UserController.php @@ -146,7 +146,7 @@ class UserController extends AbstractController { } //Create the EditType form and give the proper parameters - $edit = $this->createForm($this->config['edit']['view']['edit'], $user, [ + $edit = $this->factory->create($this->config['edit']['view']['edit'], $user, [ //Set action to edit route name and context 'action' => $this->generateUrl($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']), //Set civility class @@ -160,7 +160,7 @@ class UserController extends AbstractController { //With admin role if ($this->checker->isGranted($this->config['default']['admin'])) { //Create the EditType form and give the proper parameters - $reset = $this->createForm($this->config['edit']['view']['reset'], $user, [ + $reset = $this->factory->create($this->config['edit']['view']['reset'], $user, [ //Set action to edit route name and context 'action' => $this->generateUrl($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']), //Set method @@ -254,7 +254,7 @@ class UserController extends AbstractController { */ public function login(Request $request, AuthenticationUtils $authenticationUtils, ?string $hash, ?string $mail): Response { //Create the LoginType form and give the proper parameters - $login = $this->createForm($this->config['login']['view']['form'], null, [ + $login = $this->factory->create($this->config['login']['view']['form'], null, [ //Set action to login route name and context 'action' => $this->generateUrl($this->config['route']['login']['name'], $this->config['route']['login']['context']), //Set method @@ -297,7 +297,7 @@ class UserController extends AbstractController { $login->get('mail')->addError(new FormError($error)); //Create the RecoverType form and give the proper parameters - $recover = $this->createForm($this->config['recover']['view']['form'], null, [ + $recover = $this->factory->create($this->config['recover']['view']['form'], null, [ //Set action to recover route name and context 'action' => $this->generateUrl($this->config['route']['recover']['name'], $this->config['route']['recover']['context']), //Without password @@ -382,7 +382,7 @@ class UserController extends AbstractController { } //Create the LoginType form and give the proper parameters - $form = $this->createForm($this->config['recover']['view']['form'], $user, [ + $form = $this->factory->create($this->config['recover']['view']['form'], $user, [ //Set action to recover route name and context 'action' => $this->generateUrl($this->config['route']['recover']['name'], $context+$this->config['route']['recover']['context']), //With user disable mail @@ -578,7 +578,7 @@ class UserController extends AbstractController { $user = $reflection->newInstance('', ''); //Create the RegisterType form and give the proper parameters - $form = $this->createForm($this->config['register']['view']['form'], $user, [ + $form = $this->factory->create($this->config['register']['view']['form'], $user, [ //Set action to register route name and context 'action' => $this->generateUrl($this->config['route']['register']['name'], $this->config['route']['register']['context']), //Set civility class -- 2.41.1 From f4334bf103ec3b821270b61e270f9222a5c3d495 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 29 Feb 2024 14:26:47 +0100 Subject: [PATCH 16/16] Fix languages and locale set in config default key New context layout without head --- Controller/AbstractController.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Controller/AbstractController.php b/Controller/AbstractController.php index eecc2d0..a859e5a 100644 --- a/Controller/AbstractController.php +++ b/Controller/AbstractController.php @@ -212,13 +212,13 @@ abstract class AbstractController extends BaseAbstractController implements Serv $pathInfo = $this->router->getContext()->getPathInfo(); //Iterate on locales excluding current one - foreach(($locales = array_keys($this->config['languages'])) as $locale) { + foreach(($locales = array_keys($this->config['default']['languages'])) as $locale) { //Set titles $titles = []; //Iterate on other locales foreach(array_diff($locales, [$locale]) as $other) { - $titles[$other] = $this->translator->trans($this->config['languages'][$locale], [], null, $other); + $titles[$other] = $this->translator->trans($this->config['default']['languages'][$locale], [], null, $other); } //Retrieve route matching path @@ -233,25 +233,25 @@ abstract class AbstractController extends BaseAbstractController implements Serv //With current locale if ($locale == $this->locale) { //Set locale locales context - $this->config[$tag][$view]['context']['head']['canonical'] = $this->router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL); + $this->config[$tag][$view]['context']['canonical'] = $this->router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL); } else { //Set locale locales context - $this->config[$tag][$view]['context']['head']['alternates'][$locale] = [ + $this->config[$tag][$view]['context']['alternates'][$locale] = [ 'absolute' => $this->router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL), 'relative' => $this->router->generate($name, ['_locale' => $locale]+$route), 'title' => implode('/', $titles), - 'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale) + 'translated' => $this->translator->trans($this->config['default']['languages'][$locale], [], null, $locale) ]; } //Add shorter locale - if (empty($this->config[$tag][$view]['context']['head']['alternates'][$slocale = substr($locale, 0, 2)])) { + if (empty($this->config[$tag][$view]['context']['alternates'][$slocale = substr($locale, 0, 2)])) { //Add shorter locale - $this->config[$tag][$view]['context']['head']['alternates'][$slocale] = [ + $this->config[$tag][$view]['context']['alternates'][$slocale] = [ 'absolute' => $this->router->generate($name, ['_locale' => $locale]+$route, UrlGeneratorInterface::ABSOLUTE_URL), 'relative' => $this->router->generate($name, ['_locale' => $locale]+$route), 'title' => implode('/', $titles), - 'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale) + 'translated' => $this->translator->trans($this->config['default']['languages'][$locale], [], null, $locale) ]; } } @@ -272,20 +272,21 @@ abstract class AbstractController extends BaseAbstractController implements Serv $response ??= new Response(); //With empty head locale - if (empty($parameters['head']['locale'])) { + if (empty($parameters['locale'])) { //Set head locale - $parameters['head']['locale'] = $this->locale; + $parameters['locale'] = $this->locale; } + /*TODO: XXX: to drop, we have title => [ 'page' => XXX, section => XXX, site => XXX ] //With empty head title and section if (empty($parameters['head']['title']) && !empty($parameters['section'])) { //Set head title - $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $parameters['head']['site']]); + $parameters['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $parameters['head']['site']]); //With empty head title } elseif (empty($parameters['head']['title'])) { //Set head title $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['head']['site']]); - } + }*/ //Call twig render method $content = $this->twig->render($view, $parameters); -- 2.41.1