Skip to content

Commit

Permalink
feature #21027 [Asset] Provide default context (ro0NL)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.4 branch (closes #21027).

Discussion
----------

[Asset] Provide default context

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19396
| License       | MIT
| Doc PR        | should be noted somewhere, ill create an issue

Allows configuring the default asset context to make things works on CLI for example. Same approach as the routing component.

Introduces
```yaml
# parameters.yml
asset.request_context.base_path: '/base/path'
asset.request_context.secure: false
```

Commits
-------

9137d57 [Asset] Provide default context
  • Loading branch information
fabpot committed Sep 28, 2017
2 parents 648a895 + 9137d57 commit c7f664c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -54,6 +54,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
-----
Expand Down
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">
<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">
Expand Down
6 changes: 6 additions & 0 deletions 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,
Expand Down
15 changes: 12 additions & 3 deletions src/Symfony/Component/Asset/Context/RequestStackContext.php
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
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 comments on commit c7f664c

Please sign in to comment.