8000 [Asset] added a NullContext class · symfony/symfony@4d0adea · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d0adea

Browse files
committed
[Asset] added a NullContext class
1 parent d33c41d commit 4d0adea

File tree

7 files changed

+56
-27
lines changed

7 files changed

+56
-27
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818
<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true">
1919
<argument /> <!-- base path -->
2020
<argument type="service" /> <!-- version strategy -->
21-
<call method="setContext">
22-
<argument type="service" id="assets.context" />
23-
</call>
21+
<argument type="service" id="assets.context" />
2422
</service>
2523

2624
<service id="assets.url_package" class="Symfony\Component\Asset\UrlPackage" abstract="true">
2725
<argument /> <!-- base URLs -->
2826
<argument type="service" /> <!-- version strategy -->
29-
<call method="setContext">
30-
<argument type="service" id="assets.context" />
31-
</call>
27+
<argument type="service" id="assets.context" />
3228
</service>
3329

3430
<service id="assets.static_version_strategy" class="Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy" abstract="true">
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Asset\Context;
13+
14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
16+
/**
17+
* A context that does nothing.
18+
*
19+
* @author Fabien Potencier <fabien@symfony.com>
20+
*/
21+
class NullContext implements ContextInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function getBasePath()
27+
{
28+
return '';
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function isSecure()
35+
{
36+
return false;
37+
}
38+
}

src/Symfony/Component/Asset/Package.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Asset;
1313

1414
use Symfony\Component\Asset\Context\ContextInterface;
15+
use Symfony\Component\Asset\Context\NullContext;
1516
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
1617

1718
/**
@@ -25,9 +26,10 @@ class Package implements PackageInterface
2526
private $versionStrategy;
2627
private $context;
2728

28-
public function __construct(VersionStrategyInterface $versionStrategy)
29+
public function __construct(VersionStrategyInterface $versionStrategy, ContextInterface $context = null)
2930
{
3031
$this->versionStrategy = $versionStrategy;
32+
$this->context = $context ?: new NullContext();
3133
}
3234

3335
/**
@@ -50,13 +52,8 @@ public function getUrl($path)
5052
return $this->versionStrategy->applyVersion($path);
5153
}
5254

53-
public function setContext(ContextInterface $context)
54-
{
55-
$this->context = $context;
56-
}
57-
5855
/**
59-
* @return ContextInterface|null
56+
* @return ContextInterface
6057
*/
6158
protected function getContext()
6259
{

src/Symfony/Component/Asset/PathPackage.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Asset;
1313

14+
use Symfony\Component\Asset\Context\ContextInterface;
1415
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
1516

1617
/**
@@ -31,9 +32,9 @@ class PathPackage extends Package
3132
* @param string $basePath The base path to be prepended to relative paths
3233
* @param VersionStrategyInterface $versionStrategy The version strategy
3334
*/
34-
public function __construct($basePath, VersionStrategyInterface $versionStrategy)
35+
public function __construct($basePath, VersionStrategyInterface $versionStrategy, ContextInterface $context = null)
3536
{
36-
parent::__construct($versionStrategy);
37+
parent::__construct($versionStrategy, $context);
3738

3839
if (!$basePath) {
3940
$this->basePath = '/';
@@ -65,10 +66,6 @@ public function getUrl($path)
6566
*/
6667
public function getBasePath()
6768
{
68-
if (null !== $context = $this->getContext()) {
69-
return $context->getBasePath().$this->basePath;
70-
}
71-
72-
return $this->basePath;
69+
return $this->getContext()->getBasePath().$this->basePath;
7370
}
7471
}

src/Symfony/Component/Asset/Tests/PathPackageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function getConfigs()
5252
*/
5353
public function testGetUrlWithContext($basePathRequest, $basePath, $format, $path, $expected)
5454
{
55-
$package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format));
56-
$package->setContext($this->getContext($basePathRequest));
55+
$package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format), $this->getContext($basePathRequest));
56+
5757
$this->assertEquals($expected, $package->getUrl($path));
5858
}
5959

src/Symfony/Component/Asset/Tests/UrlPackageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function getConfigs()
5757
*/
5858
public function testGetUrlWithContext($secure, $baseUrls, $format, $path, $expected)
5959
{
60-
$package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format));
61-
$package->setContext($this->getContext($secure));
60+
$package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format), $this->getContext($secure));
61+
6262
$this->assertEquals($expected, $package->getUrl($path));
6363
}
6464

src/Symfony/Component/Asset/UrlPackage.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Asset;
1313

14+
use Symfony\Component\Asset\Context\ContextInterface;
1415
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
1516
use Symfony\Component\Asset\Exception\InvalidArgumentException;
1617
use Symfony\Component\Asset\Exception\LogicException;
@@ -42,9 +43,9 @@ class UrlPackage extends Package
4243
* @param string|array $baseUrls Base asset URLs
4344
* @param VersionStrategyInterface $versionStrategy The version strategy
4445
*/
45-
public function __construct($baseUrls = array(), VersionStrategyInterface $versionStrategy)
46+
public function __construct($baseUrls = array(), VersionStrategyInterface $versionStrategy, ContextInterface $context = null)
4647
{
47-
parent::__construct($versionStrategy);
48+
parent::__construct($versionStrategy, $context);
4849

4950
if (!is_array($baseUrls)) {
5051
$baseUrls = (array) $baseUrls;
@@ -74,7 +75,7 @@ public function getUrl($path)
7475
return $path;
7576
}
7677

77-
if (null !== $this->sslPackage && ($context = $this->getContext()) && $context->isSecure()) {
78+
if (null !== $this->sslPackage && $this->getContext()->isSecure()) {
7879
return $this->sslPackage->getUrl($path);
7980
}
8081

0 commit comments

Comments
 (0)
0