feature #21027 [Asset] Provide default context (ro0NL)
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
-------
9137d57ecd
[Asset] Provide default context
This commit is contained in:
commit
c7f664c5e0
@ -54,6 +54,8 @@ CHANGELOG
|
|||||||
`EventDispatcherDebugCommand`, `RouterDebugCommand`, `RouterMatchCommand`,
|
`EventDispatcherDebugCommand`, `RouterDebugCommand`, `RouterMatchCommand`,
|
||||||
`TranslationDebugCommand`, `TranslationUpdateCommand`, `XliffLintCommand`
|
`TranslationDebugCommand`, `TranslationUpdateCommand`, `XliffLintCommand`
|
||||||
and `YamlLintCommand` classes have been marked as final
|
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
|
3.3.0
|
||||||
-----
|
-----
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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>
|
<services>
|
||||||
<defaults public="false" />
|
<defaults public="false" />
|
||||||
|
|
||||||
@ -19,6 +24,8 @@
|
|||||||
|
|
||||||
<service id="assets.context" class="Symfony\Component\Asset\Context\RequestStackContext">
|
<service id="assets.context" class="Symfony\Component\Asset\Context\RequestStackContext">
|
||||||
<argument type="service" id="request_stack" />
|
<argument type="service" id="request_stack" />
|
||||||
|
<argument>%asset.request_context.base_path%</argument>
|
||||||
|
<argument>%asset.request_context.secure%</argument>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true">
|
<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true">
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
CHANGELOG
|
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
|
3.3.0
|
||||||
-----
|
-----
|
||||||
* Added `JsonManifestVersionStrategy` as a way to read final,
|
* Added `JsonManifestVersionStrategy` as a way to read final,
|
||||||
|
@ -21,10 +21,19 @@ use Symfony\Component\HttpFoundation\RequestStack;
|
|||||||
class RequestStackContext implements ContextInterface
|
class RequestStackContext implements ContextInterface
|
||||||
{
|
{
|
||||||
private $requestStack;
|
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->requestStack = $requestStack;
|
||||||
|
$this->basePath = $basePath;
|
||||||
|
$this->secure = $secure;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,7 +42,7 @@ class RequestStackContext implements ContextInterface
|
|||||||
public function getBasePath()
|
public function getBasePath()
|
||||||
{
|
{
|
||||||
if (!$request = $this->requestStack->getMasterRequest()) {
|
if (!$request = $this->requestStack->getMasterRequest()) {
|
||||||
return '';
|
return $this->basePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $request->getBasePath();
|
return $request->getBasePath();
|
||||||
@ -45,7 +54,7 @@ class RequestStackContext implements ContextInterface
|
|||||||
public function isSecure()
|
public function isSecure()
|
||||||
{
|
{
|
||||||
if (!$request = $this->requestStack->getMasterRequest()) {
|
if (!$request = $this->requestStack->getMasterRequest()) {
|
||||||
return false;
|
return $this->secure;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $request->isSecure();
|
return $request->isSecure();
|
||||||
|
@ -61,4 +61,13 @@ class RequestStackContextTest extends TestCase
|
|||||||
|
|
||||||
$this->assertTrue($requestStackContext->isSecure());
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user