From 9137d57ecd43f1d42e6e09b06ed090614e5f6c8c Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 24 Dec 2016 11:15:47 +0000 Subject: [PATCH] [Asset] Provide default context --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 2 ++ .../FrameworkBundle/Resources/config/assets.xml | 7 +++++++ src/Symfony/Component/Asset/CHANGELOG.md | 6 ++++++ .../Asset/Context/RequestStackContext.php | 15 ++++++++++++--- .../Tests/Context/RequestStackContextTest.php | 9 +++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index dafd4c46bb0f..8bcbe06ae666 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -53,6 +53,8 @@ CHANGELOG `EventDispatcherDebugCommand`, `RouterDebugCommand`, `RouterMatchCommand`, `TranslationDebugCommand`, `TranslationUpdateCommand`, `XliffLintCommand` and `YamlLintCommand` classes have been marked as final + * Added `asset.request_context.base_path` and `asset.request_context.secure` parameters + to provide a default request context in case the stack is empty (similar to `router.request_context.*` parameters) 3.3.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml index e714e2834c5f..cb0f11c926f8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml @@ -4,6 +4,11 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + false + + @@ -19,6 +24,8 @@ + %asset.request_context.base_path% + %asset.request_context.secure% diff --git a/src/Symfony/Component/Asset/CHANGELOG.md b/src/Symfony/Component/Asset/CHANGELOG.md index 72030a9d65b1..bb4c02f33318 100644 --- a/src/Symfony/Component/Asset/CHANGELOG.md +++ b/src/Symfony/Component/Asset/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +3.4.0 +----- + + * added optional arguments `$basePath` and `$secure` in `RequestStackContext::__construct()` + to provide a default request context in case the stack is empty + 3.3.0 ----- * Added `JsonManifestVersionStrategy` as a way to read final, diff --git a/src/Symfony/Component/Asset/Context/RequestStackContext.php b/src/Symfony/Component/Asset/Context/RequestStackContext.php index ba7113851d86..c18f83326483 100644 --- a/src/Symfony/Component/Asset/Context/RequestStackContext.php +++ b/src/Symfony/Component/Asset/Context/RequestStackContext.php @@ -21,10 +21,19 @@ class RequestStackContext implements ContextInterface { private $requestStack; + private $basePath; + private $secure; - public function __construct(RequestStack $requestStack) + /** + * @param RequestStack $requestStack + * @param string $basePath + * @param bool $secure + */ + public function __construct(RequestStack $requestStack, $basePath = '', $secure = false) { $this->requestStack = $requestStack; + $this->basePath = $basePath; + $this->secure = $secure; } /** @@ -33,7 +42,7 @@ public function __construct(RequestStack $requestStack) public function getBasePath() { if (!$request = $this->requestStack->getMasterRequest()) { - return ''; + return $this->basePath; } return $request->getBasePath(); @@ -45,7 +54,7 @@ public function getBasePath() public function isSecure() { if (!$request = $this->requestStack->getMasterRequest()) { - return false; + return $this->secure; } return $request->isSecure(); diff --git a/src/Symfony/Component/Asset/Tests/Context/RequestStackContextTest.php b/src/Symfony/Component/Asset/Tests/Context/RequestStackContextTest.php index 7269d0e6bc98..7f24534eba20 100644 --- a/src/Symfony/Component/Asset/Tests/Context/RequestStackContextTest.php +++ b/src/Symfony/Component/Asset/Tests/Context/RequestStackContextTest.php @@ -61,4 +61,13 @@ public function testIsSecureTrue() $this->assertTrue($requestStackContext->isSecure()); } + + public function testDefaultContext() + { + $requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock(); + $requestStackContext = new RequestStackContext($requestStack, 'default-path', true); + + $this->assertSame('default-path', $requestStackContext->getBasePath()); + $this->assertTrue($requestStackContext->isSecure()); + } }