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\Mailer\MailerInterface
;
22 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
23 use Symfony\Component\Routing\RouterInterface
;
24 use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface
;
25 use Symfony\Contracts\Translation\TranslatorInterface
;
26 use Symfony\Contracts\Service\ServiceSubscriberInterface
;
28 use Rapsys\PackBundle\Util\SluggerUtil
;
30 use Rapsys\UserBundle\RapsysUserBundle
;
33 * Provides common features needed in controllers.
37 abstract class AbstractController
extends BaseAbstractController
implements ServiceSubscriberInterface
{
41 ///ContainerInterface instance
50 ///UserPasswordHasherInterface
59 ///EntityManagerInterface
68 ///Translator instance
69 protected $translator;
75 * Abstract constructor
77 * @param ContainerInterface $container The container instance
78 * @param ManagerRegistry $doctrine The doctrine instance
79 * @param UserPasswordHasherInterface $hasher The password hasher instance
80 * @param LoggerInterface $logger The logger instance
81 * @param MailerInterface $mailer The mailer instance
82 * @param EntityManagerInterface $manager The manager instance
83 * @param RouterInterface $router The router instance
84 * @param SluggerUtil $slugger The slugger instance
85 * @param RequestStack $stack The stack instance
86 * @param TranslatorInterface $translator The translator instance
88 public function __construct(ContainerInterface
$container, ManagerRegistry
$doctrine, UserPasswordHasherInterface
$hasher, LoggerInterface
$logger, MailerInterface
$mailer, EntityManagerInterface
$manager, RouterInterface
$router, SluggerUtil
$slugger, RequestStack
$stack, TranslatorInterface
$translator) {
90 $this->config
= $container->getParameter(RapsysUserBundle
::getAlias());
93 $this->container
= $container;
96 $this->doctrine
= $doctrine;
99 $this->hasher
= $hasher;
102 $this->logger
= $logger;
105 $this->mailer
= $mailer;
108 $this->manager
= $manager;
111 $this->router
= $router;
114 $this->slugger
= $slugger;
117 $this->translator
= $translator;
119 //Get current request
120 $request = $stack->getCurrentRequest();
123 $this->locale
= $request->getLocale();
126 $this->config
['context']['locale'] = str_replace('_', '-', $this->locale
);
128 //Set translate array
131 //Look for keys to translate
132 if (!empty($this->config
['translate'])) {
133 //Iterate on keys to translate
134 foreach($this->config
['translate'] as $translate) {
138 foreach(array_reverse(explode('.', $translate)) as $curkey) {
139 $tmp = array_combine([$curkey], [$tmp]);
142 $translates = array_replace_recursive($translates, $tmp);
146 //Inject every requested route in view and mail context
147 foreach($this->config
as $tag => $current) {
148 //Look for entry with title subkey
149 if (!empty($current['title'])) {
150 //Translate title value
151 $this->config
[$tag]['title'] = $this->translator
->trans($current['title']);
154 //Look for entry with route subkey
155 if (!empty($current['route'])) {
156 //Generate url for both view and mail
157 foreach(['view', 'mail'] as $view) {
158 //Check that context key is usable
159 if (isset($current[$view]['context']) && is_array($current[$view]['context'])) {
160 //Merge with global context
161 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
['context'], $this->config
[$tag][$view]['context']);
163 //Process every routes
164 foreach($current['route'] as $route => $key) {
166 if ($route == 'confirm') {
167 //Skip route as it requires some parameters
172 $value = $this->router
->generate(
173 $this->config
['route'][$route]['name'],
174 $this->config
['route'][$route]['context'],
175 //Generate absolute url for mails
176 $view=='mail'?UrlGeneratorInterface
::ABSOLUTE_URL
:UrlGeneratorInterface
::ABSOLUTE_PATH
180 if (strpos($key, '.') !== false) {
185 foreach(array_reverse(explode('.', $key)) as $curkey) {
186 $tmp = array_combine([$curkey], [$tmp]);
190 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp);
194 $this->config
[$tag][$view]['context'][$key] = $value;
198 //Look for successful intersections
199 if (!empty(array_intersect_key($translates, $this->config
[$tag][$view]['context']))) {
200 //Iterate on keys to translate
201 foreach($this->config
['translate'] as $translate) {
203 $keys = explode('.', $translate);
206 $tmp = $this->config
[$tag][$view]['context'];
209 foreach($keys as $curkey) {
211 if (!isset($tmp[$curkey])) {
217 $tmp = $tmp[$curkey];
220 //Translate tmp value
221 $tmp = $this->translator
->trans($tmp);
224 foreach(array_reverse($keys) as $curkey) {
226 $tmp = array_combine([$curkey], [$tmp]);
230 $this->config
[$tag][$view]['context'] = array_replace_recursive($this->config
[$tag][$view]['context'], $tmp);
235 if ($view == 'view') {
237 $pathInfo = $this->router
->getContext()->getPathInfo();
239 //Iterate on locales excluding current one
240 foreach($this->config
['locales'] as $locale) {
244 //Iterate on other locales
245 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
246 $titles[$other] = $this->translator
->trans($this->config
['languages'][$locale], [], null, $other);
249 //Retrieve route matching path
250 $route = $this->router
->match($pathInfo);
253 $name = $route['_route'];
256 unset($route['_route']);
258 //With current locale
259 if ($locale == $this->locale
) {
260 //Set locale locales context
261 $this->config
[$tag][$view]['context']['canonical'] = $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
);
263 //Set locale locales context
264 $this->config
[$tag][$view]['context']['alternates'][$locale] = [
265 'absolute' => $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
),
266 'relative' => $this->router
->generate($name, ['_locale' => $locale]+
$route),
267 'title' => implode('/', $titles),
268 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
273 if (empty($this->config
[$tag][$view]['context']['alternates'][$slocale = substr($locale, 0, 2)])) {
275 $this->config
[$tag][$view]['context']['alternates'][$slocale] = [
276 'absolute' => $this->router
->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
),
277 'relative' => $this->router
->generate($name, ['_locale' => $locale]+
$route),
278 'title' => implode('/', $titles),
279 'translated' => $this->translator
->trans($this->config
['languages'][$locale], [], null, $locale)
293 * @see vendor/symfony/framework-bundle/Controller/AbstractController.php
295 public static function getSubscribedServices(): array {
296 //Return subscribed services
298 'service_container' => ContainerInterface
::class,
299 'doctrine' => ManagerRegistry
::class,
300 'doctrine.orm.default_entity_manager' => EntityManagerInterface
::class,
301 'logger' => LoggerInterface
::class,
302 'mailer.mailer' => MailerInterface
::class,
303 'rapsys_pack.slugger_util' => SluggerUtil
::class,
304 'request_stack' => RequestStack
::class,
305 'router' => RouterInterface
::class,
306 'security.user_password_hasher' => UserPasswordHasherInterface
::class,
307 'translator' => TranslatorInterface
::class