10000 [Asset] Provide default context by ro0NL · Pull Request #21027 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Asset] Provide default context #21027

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Asset] Provide default request context
  • Loading branch information
ro0NL committed Sep 6, 2017
commit 7157dccfd53918341f94522c6ffadc132529b86e
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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 request context on e.g. CLI (similar to `router.request_context.*` parameters)

3.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<parameters>
<parameter key="asset.request_context.base_path"></parameter>
<parameter key="asset.request_context.secure">false</parameter>
</parameters>

<services>
<defaults public="false" />

Expand All @@ -19,6 +24,8 @@

<service id="assets.context" class="Symfony\Component\Asset\Context\RequestStackContext" public="true">
<argument type="service" id="request_stack" />
<argument>%asset.request_context.base_path%</argument>
<argument>%asset.request_context.secure%</argument>
</service>

<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true" public="true">
Expand Down
15 changes: 12 additions & 3 deletions src/Symfony/Component/Asset/Context/RequestStackContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -33,7 +42,7 @@ public function __construct(RequestStack $requestStack)
public function getBasePath()
{
if (!$request = $this->requestStack->getMasterRequest()) {
return '';
return $this->basePath;
}

return $request->getBasePath();
Expand All @@ -45,7 +54,7 @@ public function getBasePath()
public function isSecure()
{
if (!$request = $this->requestStack->getMasterRequest()) {
return false;
return $this->secure;
}

return $request->isSecure();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
0