1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys UserBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\UserBundle\Controller
;
14 use Doctrine\ORM\EntityManagerInterface
;
15 use Doctrine\Persistence\ManagerRegistry
;
16 use Psr\Log\LoggerInterface
;
17 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController
as BaseAbstractController
;
18 use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait
;
19 use Symfony\Component\DependencyInjection\ContainerInterface
;
20 use Symfony\Component\HttpFoundation\RequestStack
;
21 use Symfony\Component\HttpFoundation\Response
;
22 use Symfony\Component\Mailer\MailerInterface
;
23 use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface
;
24 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
25 use Symfony\Component\Routing\RouterInterface
;
26 use Symfony\Contracts\Service\ServiceSubscriberInterface
;
27 use Symfony\Contracts\Translation\TranslatorInterface
;
30 use Rapsys\PackBundle\Util\SluggerUtil
;
32 use Rapsys\UserBundle\RapsysUserBundle
;
35 * Provides common features needed in controllers.
39 abstract class AbstractController
extends BaseAbstractController
implements ServiceSubscriberInterface
{
41 protected array $config;
44 protected array $context;
47 protected ManagerRegistry
$doctrine;
49 ///UserPasswordHasherInterface
50 protected UserPasswordHasherInterface
$hasher;
53 protected LoggerInterface
$logger;
56 protected MailerInterface
$mailer;
58 ///EntityManagerInterface
59 protected EntityManagerInterface
$manager;
62 protected RouterInterface
$router;
65 protected SluggerUtil
$slugger;
67 ///Translator instance
68 protected TranslatorInterface
$translator;
70 ///Twig\Environment instance
71 protected Environment
$twig;
74 protected string $locale;
77 * Abstract constructor
79 * @param ContainerInterface $container The container instance
80 * @param ManagerRegistry $doctrine The doctrine instance
81 * @param UserPasswordHasherInterface $hasher The password hasher instance
82 * @param LoggerInterface $logger The logger instance
83 * @param MailerInterface $mailer The mailer instance
84 * @param EntityManagerInterface $manager The manager instance
85 * @param RouterInterface $router The router instance
86 * @param SluggerUtil $slugger The slugger instance
87 * @param RequestStack $stack The stack instance
88 * @param TranslatorInterface $translator The translator instance
89 * @param Environment $twig The twig environment instance
91 public function __construct(ContainerInterface
$container, ManagerRegistry
$doctrine, UserPasswordHasherInterface
$hasher, LoggerInterface
$logger, MailerInterface
$mailer, EntityManagerInterface
$manager, RouterInterface
$router, SluggerUtil
$slugger, RequestStack
$stack, TranslatorInterface
$translator, Environment
$twig) {
93 $this->config
= $container->getParameter(RapsysUserBundle
::getAlias());
96 $this->container
= $container;
99 $this->doctrine
= $doctrine;
102 $this->hasher
= $hasher;
105 $this->logger
= $logger;
108 $this->mailer
= $mailer;
111 $this->manager
= $manager;
114 $this->router
= $router;
117 $this->slugger
= $slugger;
120 $this->translator
= $translator;
125 //Get current request
126 $request = $stack->getCurrentRequest();
129 $this->locale
= $request->getLocale();
131 //Set translate array
134 //Look for keys to translate
135 if (!empty($this->config
['translate'])) {
136 //Iterate on keys to translate
137 foreach($this->config
['translate'] as $translate) {
142 foreach(array_reverse(explode('.', $translate)) as $curkey) {
143 $tmp = array_combine([$curkey], [$tmp]);
147 $translates = array_replace_recursive($translates, $tmp);
151 //Inject every requested route in view and mail context
152 foreach($this->config
as $tag => $current) {
153 //Look for entry with route subkey
154 if (!empty($current['route'])) {
155 //Generate url for both view and mail
156 foreach(['view', 'mail'] as $view) {
157 //Check that context key is usable
158 if (isset($current[$view]['context']) && is_array($current[$view]['context'])) {
159 //Merge with global context
160 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
['context'], $this->config
[$tag][$view]['context']);
162 //Process every routes
163 foreach($current['route'] as $route => $key) {
165 if ($route == 'confirm') {
166 //Skip route as it requires some parameters
171 $value = $this->router
->generate(
172 $this->config
['route'][$route]['name'],
173 $this->config
['route'][$route]['context'],
174 //Generate absolute url for mails
175 $view=='mail'?UrlGeneratorInterface
::ABSOLUTE_URL
:UrlGeneratorInterface
::ABSOLUTE_PATH
179 if (strpos($key, '.') !== false) {
184 foreach(array_reverse(explode('.', $key)) as $curkey) {
185 $tmp = array_combine([$curkey], [$tmp]);
189 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp);
193 $this->config
[$tag][$view]['context'][$key] = $value;
197 //Look for successful intersections
198 if (!empty(array_intersect_key($translates, $this->config
[$tag][$view]['context']))) {
199 //Iterate on keys to translate
200 foreach($this->config
['translate'] as $translate) {
202 $keys = explode('.', $translate);
205 $tmp = $this->config
[$tag][$view]['context'];
208 foreach($keys as $curkey) {
210 if (!isset($tmp[$curkey])) {
216 $tmp = $tmp[$curkey];
219 //Translate tmp value
220 $tmp = $this->translator
->trans($tmp);
223 foreach(array_reverse($keys) as $curkey) {
225 $tmp = array_combine([$curkey], [$tmp]);
229 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp);
234 if ($view == 'view') {
236 $pathInfo = $this->router
->getContext()->getPathInfo();
238 //Iterate on locales excluding current one
239 foreach(($locales = array_keys($this->config
['languages'])) as $locale) {
243 //Iterate on other locales
244 foreach(array_diff($locales, [$locale]) as $other) {
245 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
248 //Retrieve route matching path
249 $route = $this->router
->match($pathInfo);
252 $name = $route['_route'];
255 unset($route['_route']);
257 //With current locale
258 if ($locale == $this->locale
) {
259 //Set locale locales context
260 $this->config
[$tag][$view]['context']['canonical'] = $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
);
262 //Set locale locales context
263 $this->config
[$tag][$view]['context']['head']['alternates'][$locale] = [
264 'absolute' => $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
),
265 'relative' => $this->router
->generate($name, ['_locale' => $locale]+
$route),
266 'title' => implode('/', $titles),
267 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
272 if (empty($this->config
[$tag][$view]['context']['head']['alternates'][$slocale = substr($locale, 0, 2)])) {
274 $this->config
[$tag][$view]['context']['head']['alternates'][$slocale] = [
275 'absolute' => $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
),
276 'relative' => $this->router
->generate($name, ['_locale' => $locale]+
$route),
277 'title' => implode('/', $titles),
278 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
294 protected function render(string $view, array $parameters = [], Response
$response = null): Response
{
295 //Create response when null
296 $response ??= new Response();
298 //With empty head locale
299 if (empty($parameters['head']['locale'])) {
301 $parameters['head']['locale'] = $this->locale
;
304 //With empty head title and section
305 if (empty($parameters['head']['title']) && !empty($parameters['section'])) {
307 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['section'], $parameters['head']['site']]);
308 //With empty head title
309 } elseif (empty($parameters['head']['title'])) {
311 $parameters['head']['title'] = implode(' - ', [$parameters['title'], $parameters['head']['site']]);
314 //Call twig render method
315 $content = $this->twig
->render($view, $parameters);
317 //Invalidate OK response on invalid form
318 if (200 === $response->getStatusCode()) {
319 foreach ($parameters as $v) {
320 if ($v instanceof FormInterface
&& $v->isSubmitted() && !$v->isValid()) {
321 $response->setStatusCode(422);
327 //Store content in response
328 $response->setContent($content);
337 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
339 public static function getSubscribedServices(): array {
340 //Return subscribed services
342 'service_container' => ContainerInterface
::class,
343 'doctrine' => ManagerRegistry
::class,
344 'doctrine.orm.default_entity_manager' => EntityManagerInterface
::class,
345 'logger' => LoggerInterface
::class,
346 'mailer.mailer' => MailerInterface
::class,
347 'rapsys_pack.slugger_util' => SluggerUtil
::class,
348 'request_stack' => RequestStack
::class,
349 'router' => RouterInterface
::class,
350 'security.user_password_hasher' => UserPasswordHasherInterface
::class,
351 'translator' => TranslatorInterface
::class