Skip to content

Set default options to routes created in the route-providers #5561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<argument type="service" id="sulu_core.webspace.request_analyzer"/>
<argument type="service" id="sulu_document_manager.path_builder"/>
<argument type="string">%kernel.environment%</argument>
<argument type="collection" />

<tag name="sulu.context" context="website"/>
</service>
Expand Down
4 changes: 4 additions & 0 deletions src/Sulu/Bundle/CustomUrlBundle/SuluCustomUrlBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\CustomUrlBundle;

use Sulu\Component\Route\RouteDefaultOptionsCompilerPass;
use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -24,6 +25,9 @@ public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(
new RouteDefaultOptionsCompilerPass('sulu_custom_urls.routing.provider', 3)
);
$container->addCompilerPass(
new RegisterRouteEnhancersPass('sulu_custom_urls.routing.router', 'sulu_custom_urls.route_enhancer')
);
Expand Down
1 change: 1 addition & 0 deletions src/Sulu/Bundle/RouteBundle/Resources/config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<argument type="service" id="sulu_route.routing.defaults_provider"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="sulu_route.routing.proxy_factory"/>
<argument type="collection" />

<tag name="sulu.context" context="website"/>
</service>
Expand Down
18 changes: 14 additions & 4 deletions src/Sulu/Bundle/RouteBundle/Routing/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class RouteProvider implements RouteProviderInterface
*/
private $proxyFactory;

/**
* @var array
*/
private $defaultOptions;

/**
* @var Route[]
*/
Expand All @@ -74,14 +79,15 @@ public function __construct(
RequestAnalyzerInterface $requestAnalyzer,
RouteDefaultsProviderInterface $routeDefaultsProvider,
RequestStack $requestStack,
LazyLoadingValueHolderFactory $proxyFactory = null
LazyLoadingValueHolderFactory $proxyFactory = null,
array $defaultOptions = []
) {
$this->routeRepository = $routeRepository;
$this->requestAnalyzer = $requestAnalyzer;
$this->routeDefaultsProvider = $routeDefaultsProvider;
$this->requestStack = $requestStack;

$this->proxyFactory = $proxyFactory ?: new LazyLoadingValueHolderFactory();
$this->defaultOptions = $defaultOptions;
}

public function getRouteCollectionForRequest(Request $request)
Expand Down Expand Up @@ -220,7 +226,9 @@ protected function createRoute(RouteInterface $route, Request $request, RequestA
. $attributes->getAttribute('resourceLocatorPrefix')
. $route->getTarget()->getPath()
. ($request->getQueryString() ? ('?' . $request->getQueryString()) : ''),
]
],
[],
$this->defaultOptions
);
}

Expand All @@ -238,7 +246,9 @@ function(&$wrappedObject, LazyLoadingInterface $proxy, $method, array $parameter
$route->getEntityClass(),
$route->getEntityId(),
$request->getLocale()
)
),
[],
$this->defaultOptions
);

return true;
Expand Down
4 changes: 4 additions & 0 deletions src/Sulu/Bundle/RouteBundle/SuluRouteBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sulu\Bundle\PersistenceBundle\PersistenceBundleTrait;
use Sulu\Bundle\RouteBundle\DependencyInjection\RouteGeneratorCompilerPass;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Route\RouteDefaultOptionsCompilerPass;
use Sulu\Component\Symfony\CompilerPass\TaggedServiceCollectorCompilerPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -31,6 +32,9 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new RouteGeneratorCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1024);
$container->addCompilerPass(
new RouteDefaultOptionsCompilerPass('sulu_route.routing.provider', 5)
);
$container->addCompilerPass(
new TaggedServiceCollectorCompilerPass('sulu_route.routing.defaults_provider', 'sulu_route.defaults_provider')
);
Expand Down
1 change: 1 addition & 0 deletions src/Sulu/Bundle/WebsiteBundle/Resources/config/website.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<argument type="service" id="sulu.content.resource_locator.strategy_pool"/>
<argument type="service" id="sulu.content.structure_manager"/>
<argument type="service" id="sulu_core.webspace.webspace_manager"/>
<argument type="collection" />

<tag name="sulu.context" context="website"/>
</service>
Expand Down
17 changes: 14 additions & 3 deletions src/Sulu/Bundle/WebsiteBundle/Routing/ContentRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,25 @@ class ContentRouteProvider implements RouteProviderInterface
*/
private $webspaceManager;

/**
* @var array
*/
private $defaultOptions;

public function __construct(
DocumentManagerInterface $documentManager,
DocumentInspector $documentInspector,
ResourceLocatorStrategyPoolInterface $resourceLocatorStrategyPool,
StructureManagerInterface $structureManager,
WebspaceManagerInterface $webspaceManager
WebspaceManagerInterface $webspaceManager,
array $defaultOptions = []
) {
$this->documentManager = $documentManager;
$this->documentInspector = $documentInspector;
$this->resourceLocatorStrategyPool = $resourceLocatorStrategyPool;
$this->structureManager = $structureManager;
$this->webspaceManager = $webspaceManager;
$this->defaultOptions = $defaultOptions;
}

public function getRouteCollectionForRequest(Request $request)
Expand Down Expand Up @@ -243,7 +250,9 @@ protected function getRedirectRoute(Request $request, $url)
[
'_controller' => 'sulu_website.redirect_controller:redirectAction',
'url' => $url,
]
],
[],
$this->defaultOptions
);
}

Expand All @@ -258,7 +267,9 @@ protected function getStructureRoute(Request $request, PageBridge $content)
'_controller' => $content->getController(),
'structure' => $content,
'partial' => 'true' === $request->get('partial', 'false'),
]
],
[],
$this->defaultOptions
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Sulu/Bundle/WebsiteBundle/SuluWebsiteBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\WebsiteBundle;

use Sulu\Bundle\WebsiteBundle\DependencyInjection\Compiler\DeregisterDefaultRouteListenerCompilerPass;
use Sulu\Component\Route\RouteDefaultOptionsCompilerPass;
use Sulu\Component\Symfony\CompilerPass\TaggedServiceCollectorCompilerPass;
use Sulu\Component\Util\SuluVersionPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -25,6 +26,9 @@ public function build(ContainerBuilder $container)

$container->addCompilerPass(new SuluVersionPass());
$container->addCompilerPass(new DeregisterDefaultRouteListenerCompilerPass());
$container->addCompilerPass(
new RouteDefaultOptionsCompilerPass('sulu_website.provider.content', 5)
);

$container->addCompilerPass(
new TaggedServiceCollectorCompilerPass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ imports:
- { resource: services.yml }

framework:
router: { resource: "%kernel.project_dir%/config/routing_website.yml" }
router:
resource: "%kernel.project_dir%/config/routing_website.yml"
utf8: true

twig:
paths: ['%kernel.project_dir%/templates']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\RouteBundle\Tests\Functional\Controller;

use Sulu\Bundle\TestBundle\Testing\WebsiteTestCase;
use Symfony\Component\HttpFoundation\Request;

class ContentRouteProviderTest extends WebsiteTestCase
{
public function testRouteIncludesUtf8Option()
{
static::initPhpcr();

$request = Request::create('/', 'GET', [], [], [], ['HTTP_HOST' => 'sulu.lo']);

// set _sulu attributes via RequestAnalyzer
$requestAnalyzer = static::getContainer()->get('sulu_core.webspace.request_analyzer');
$requestAnalyzer->analyze($request);
$requestAnalyzer->validate($request);

$routeProvider = static::getContainer()->get('sulu_website.provider.content');
$collection = $routeProvider->getRouteCollectionForRequest($request);
$routes = \array_values($collection->all());

$this->assertCount(1, $routes);
$this->assertTrue($routes[0]->getOption('utf8')); // see https://github.com/sulu/sulu/pull/5561
}
}
22 changes: 18 additions & 4 deletions src/Sulu/Component/CustomUrl/Routing/CustomUrlRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ class CustomUrlRouteProvider implements RouteProviderInterface
*/
private $environment;

public function __construct(RequestAnalyzerInterface $requestAnalyzer, PathBuilder $pathBuilder, $environment)
{
/**
* @var array
*/
private $defaultOptions;

public function __construct(
RequestAnalyzerInterface $requestAnalyzer,
PathBuilder $pathBuilder,
$environment,
array $defaultOptions = []
) {
$this->requestAnalyzer = $requestAnalyzer;
$this->pathBuilder = $pathBuilder;
$this->environment = $environment;
$this->defaultOptions = $defaultOptions;
}

public function getRouteCollectionForRequest(Request $request)
Expand Down Expand Up @@ -92,7 +102,9 @@ public function getRouteCollectionForRequest(Request $request)
'_custom_url' => $customUrlDocument,
'_webspace' => $this->requestAnalyzer->getWebspace(),
'_environment' => $this->environment,
]
],
[],
$this->defaultOptions
)
);

Expand Down Expand Up @@ -137,7 +149,9 @@ private function addHistoryRedirectToRouteCollection(
'_controller' => 'sulu_website.redirect_controller:redirectAction',
'_finalized' => true,
'url' => $url,
]
],
[],
$this->defaultOptions
)
);

Expand Down
64 changes: 64 additions & 0 deletions src/Sulu/Component/Route/RouteDefaultOptionsCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Component\Route;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RouteDefaultOptionsCompilerPass implements CompilerPassInterface
{
/**
* @var string
*/
private $targetService;

/**
* @var string
*/
private $targetDefaultOptionsArgument;

/**
* @param string $targetService
* @param int $targetDefaultOptionsArgument
*/
public function __construct($targetService, $targetDefaultOptionsArgument)
{
$this->targetService = $targetService;
$this->targetDefaultOptionsArgument = $targetDefaultOptionsArgument;
}

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('routing.loader')) {
return;
}

if (!$container->hasDefinition('sulu_custom_urls.routing.provider')) {
return;
}

// copy default route options which are set by the symfony FrameworkExtension based on the config:
// https://github.com/symfony/symfony/pull/31900
$routeDefaultOptions = $container->getDefinition('routing.loader')->getArgument(1);

// symfony 4.4 passes the default options on index 2 instead of index 1
$deprecatedRouteDefaultOptions = $container->getDefinition('routing.loader')->getArgument(2);
if (\is_array($deprecatedRouteDefaultOptions) && isset($deprecatedRouteDefaultOptions['utf8'])) {
$routeDefaultOptions = $deprecatedRouteDefaultOptions;
}

$container->getDefinition($this->targetService)->replaceArgument(
$this->targetDefaultOptionsArgument,
$routeDefaultOptions
);
}
}