Skip to content

Commit

Permalink
Router now use real route name instead of "section_id_#".
Browse files Browse the repository at this point in the history
Fix ApplicationCore wrong usage of requestStack service.
Creation of a RouterResolver service to resolve section mapped route.
  • Loading branch information
pascallapointe committed Oct 13, 2019
1 parent 7f27a11 commit b7f2025
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 35 deletions.
82 changes: 71 additions & 11 deletions src/Extensions/RoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Extensions;

use App\Services\RouteResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bridge\Twig\Extension\RoutingExtension as BaseRoutingExtension;
use App\Services\RouterAutoParametersHandler;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Twig\TwigFunction;

/**
* Class RoutingExtension
Expand All @@ -28,48 +31,105 @@ class RoutingExtension extends BaseRoutingExtension
*/
private $router;

/** @var RouteResolver */
private $routeResolver;

/**
* @inheritdoc
*/
public function __construct(UrlGeneratorInterface $generator, RouterAutoParametersHandler $autoParametersHandler,
RouterInterface $router)
RouterInterface $router, RouteResolver $routeResolver)
{
$this->generator = $generator;
$this->autoParametersHandler = $autoParametersHandler;
$this->router = $router;
$this->routeResolver = $routeResolver;

parent::__construct($generator);
}

/**
* Returns a list of functions to add to the existing list.
*
* @return array An array of functions
*/
public function getFunctions()
{
return [
new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
new TwigFunction('sectionPath', [$this, 'getSectionPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
new TwigFunction('sectionUrl', [$this, 'getSectionUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']])
];
}

/**
* Overridden to handle automatics parameters.
*
* @inheritdoc
*/
public function getPath($name, $parameters = array(), $relative = false)
public function getPath($name, $parameters = [], $relative = false)
{
$route = $this->router->getRouteCollection()->get($name);

if ($route && preg_match('#\{sectionId\}#', $route->getPath())) {
$parameters = $this->autoParametersHandler->inject($parameters);
}

return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
return $this->generator->generate($name, $this->injectAutoParameters($route, $parameters),
$relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
}

/**
* Overridden to handle automatics parameters.
*
* @inheritdoc
*/
public function getUrl($name, $parameters = array(), $schemeRelative = false)
public function getUrl($name, $parameters = [], $schemeRelative = false)
{
$route = $this->router->getRouteCollection()->get($name);

if ($route && preg_match('#\{sectionId\}#', $route->getPath())) {
$parameters = $this->autoParametersHandler->inject($parameters);
return $this->generator->generate($name, $this->injectAutoParameters($route, $parameters),
$schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}

/**
* Inject the current section id if not defined in the $parameters array
*
* @param Route $route
* @param array $parameters
* @return array
*/
private function injectAutoParameters(?Route $route, array $parameters): array
{
if ($route && preg_match('#\{sectionId\}#', $route->getPath()) && !isset($parameters['sectionId'])) {
return $this->autoParametersHandler->inject($parameters);
}

return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
return $parameters;
}

/**
* @param int $sectionId
* @param string|null $name
* @param array $parameters
* @param bool $relative
* @return string
* @throws \Exception
*/
public function getSectionPath(int $sectionId, string $name = null, array $parameters = [], bool $relative = false)
{
return $this->generator->generate($this->routeResolver->resolveSectionRoute($sectionId, $name, $parameters), $parameters,
$relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
}

/**
* @param int $sectionId
* @param string $name
* @param array $parameters
* @param bool $schemeRelative
* @return string
* @throws \Exception
*/
public function getSectionUrl(int $sectionId, string $name, array $parameters = [], bool $schemeRelative = false)
{
return $this->generator->generate($this->routeResolver->resolveSectionRoute($sectionId, $name, $parameters), $parameters,
$schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}
}
10 changes: 5 additions & 5 deletions src/Library/ApplicationCoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Library;

use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\Section;
Expand Down Expand Up @@ -34,15 +34,15 @@ public function isSectionNavInitialized() : bool;
public function initSectionNav(): void;

/**
* @return Request
* @return RequestStack
*/
public function getRequest(): Request;
public function getRequestStack(): RequestStack;

/**
* @param Request $request
* @param RequestStack $requestStack
* @return ApplicationCoreInterface
*/
public function setRequest(Request $request): ApplicationCoreInterface;
public function setRequestStack(RequestStack $requestStack): ApplicationCoreInterface;

/**
* @return RegistryInterface
Expand Down
63 changes: 44 additions & 19 deletions src/Services/ApplicationCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*/
class ApplicationCore implements ApplicationCoreInterface
{
private const HOMEPAGE_SECTION_ID = 1;

/**
* @var Request
* @var RequestStack
*/
private $request;
private $requestStack;

/**
* @var RegistryInterface
Expand Down Expand Up @@ -86,6 +88,11 @@ class ApplicationCore implements ApplicationCoreInterface
*/
protected $translator;

/**
* @var routeResolver
*/
protected $routeResolver;

/**
* @var bool
*/
Expand All @@ -105,16 +112,17 @@ class ApplicationCore implements ApplicationCoreInterface
* @param PageTitle $pageTitle
*/
public function __construct(RequestStack $requestStack, RegistryInterface $doctrine, SessionInterface $session,
Breadcrumbs $breadcrumbs, PageTitle $pageTitle)
Breadcrumbs $breadcrumbs, PageTitle $pageTitle, RouteResolver $routeResolver)
{
$this->request = $requestStack->getCurrentRequest();
$this->requestStack = $requestStack;
$this->doctrine = $doctrine;
$this->session = $session;
$this->breadcrumbs = $breadcrumbs;
$this->pageTitle = $pageTitle;
$this->locale = null;
$this->currentElement = null;
$this->elements = [];
$this->routeResolver = $routeResolver;

$this->sectionNavInitialized = false;
}
Expand All @@ -125,7 +133,7 @@ public function __construct(RequestStack $requestStack, RegistryInterface $doctr
public function isReady(): bool
{
return isset(
$this->request,
$this->requestStack,
$this->doctrine,
$this->session,
$this->breadcrumbs,
Expand All @@ -143,6 +151,7 @@ public function isSectionNavInitialized() : bool

/**
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws \Exception
*/
public function initSectionNav(): void
{
Expand All @@ -157,21 +166,28 @@ public function initSectionNav(): void

$criteria = ['id' => $sectionId];

if ('cms' !== $this->getRequest()->get('_tutoriuxContext', 'site')) {
if ('cms' !== $this->getRequestStack()->getCurrentRequest()->get('_tutoriuxContext', 'site')) {
// Avoid displaying an inactive section while browsing the public site
$criteria['active'] = true;
} else {
$this->setEditLocaleEnabled(true);
}

$this->setSection($sectionRepository->findOneBy($criteria));
}

// If a section has been found
// If a section has been found, bootstrap navigation elements
if ($section = $this->getSection()) {
foreach ($section->getParents() as $parent) {
if ($parent instanceof NavigationElementInterface) {
$parent->setRoute($this->getRouteResolver()->resolveSectionRoute($parent->getId()));
}
$this->addNavigationElement($parent);
}

$section->setRoute($this->getRouteResolver()->resolveSectionRoute($section->getId()));
$this->addNavigationElement($section);

} else {
throw new NotFoundHttpException(
sprintf('The section [id: %s] does not exist or is not active in the database', $sectionId)
Expand All @@ -183,20 +199,20 @@ public function initSectionNav(): void
}

/**
* @return Request
* @return RequestStack
*/
public function getRequest(): Request
public function getRequestStack(): RequestStack
{
return $this->request;
return $this->requestStack;
}

/**
* @param Request $request
* @param RequestStack $requestStack
* @return ApplicationCoreInterface
*/
public function setRequest(Request $request): ApplicationCoreInterface
public function setRequestStack(RequestStack $requestStack): ApplicationCoreInterface
{
$this->request = $request;
$this->requestStack = $requestStack;

return $this;
}
Expand Down Expand Up @@ -246,10 +262,11 @@ public function setSession(SessionInterface $session): ApplicationCoreInterface
*/
public function getSectionId(): int
{
$sectionId = 0;
$sectionId = self::HOMEPAGE_SECTION_ID;

if ($this->request) {
$tutoriuxRequest = $this->request->attributes->get('_tutoriuxRequest', ['sectionId' => 0]);
if ($this->getRequestStack()->getCurrentRequest()) {
$tutoriuxRequest = $this->getRequestStack()->getCurrentRequest()
->attributes->get('_tutoriuxRequest', ['sectionId' => self::HOMEPAGE_SECTION_ID]);
$sectionId = $tutoriuxRequest['sectionId'];
}

Expand Down Expand Up @@ -344,7 +361,7 @@ public function setLocale(string $locale): ApplicationCoreInterface
public function getLocale(): string
{
if (!$this->locale) {
$this->locale = $this->getRequest()->getLocale();
$this->locale = $this->getRequestStack()->getCurrentRequest()->getLocale();
}

return $this->locale;
Expand All @@ -359,8 +376,8 @@ public function getLocale(): string
*/
public function getEditLocale(): string
{
if ($this->request->get('edit-locale')) {
$this->getSession()->set('edit-locale', $this->request->get('edit-locale'));
if ($this->getRequestStack()->getCurrentRequest()->get('edit-locale')) {
$this->getSession()->set('edit-locale', $this->getRequestStack()->getCurrentRequest()->get('edit-locale'));
}

if (!$this->getSession()->get('edit-locale')) {
Expand Down Expand Up @@ -495,4 +512,12 @@ public function setTranslator(callable $translator): ApplicationCoreInterface

return $this;
}

/**
* @return RouteResolver
*/
public function getRouteResolver(): RouteResolver
{
return $this->routeResolver;
}
}
Loading

0 comments on commit b7f2025

Please sign in to comment.