From 4d0adeae16bf6733e637089b8121e25752e0d604 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 19 Jan 2015 20:26:55 +0100 Subject: [PATCH] [Asset] added a NullContext class --- .../Resources/config/assets.xml | 8 +--- .../Component/Asset/Context/NullContext.php | 38 +++++++++++++++++++ src/Symfony/Component/Asset/Package.php | 11 ++---- src/Symfony/Component/Asset/PathPackage.php | 11 ++---- .../Component/Asset/Tests/PathPackageTest.php | 4 +- .../Component/Asset/Tests/UrlPackageTest.php | 4 +- src/Symfony/Component/Asset/UrlPackage.php | 7 ++-- 7 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 src/Symfony/Component/Asset/Context/NullContext.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml index 15dbabd5fe..4e7a1b8f6b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml @@ -18,17 +18,13 @@ - - - + - - - + diff --git a/src/Symfony/Component/Asset/Context/NullContext.php b/src/Symfony/Component/Asset/Context/NullContext.php new file mode 100644 index 0000000000..47d29d7d2e --- /dev/null +++ b/src/Symfony/Component/Asset/Context/NullContext.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Asset\Context; + +use Symfony\Component\HttpFoundation\RequestStack; + +/** + * A context that does nothing. + * + * @author Fabien Potencier + */ +class NullContext implements ContextInterface +{ + /** + * {@inheritdoc} + */ + public function getBasePath() + { + return ''; + } + + /** + * {@inheritdoc} + */ + public function isSecure() + { + return false; + } +} diff --git a/src/Symfony/Component/Asset/Package.php b/src/Symfony/Component/Asset/Package.php index 50f813d86d..43bdcf21db 100644 --- a/src/Symfony/Component/Asset/Package.php +++ b/src/Symfony/Component/Asset/Package.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Asset; use Symfony\Component\Asset\Context\ContextInterface; +use Symfony\Component\Asset\Context\NullContext; use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; /** @@ -25,9 +26,10 @@ class Package implements PackageInterface private $versionStrategy; private $context; - public function __construct(VersionStrategyInterface $versionStrategy) + public function __construct(VersionStrategyInterface $versionStrategy, ContextInterface $context = null) { $this->versionStrategy = $versionStrategy; + $this->context = $context ?: new NullContext(); } /** @@ -50,13 +52,8 @@ class Package implements PackageInterface return $this->versionStrategy->applyVersion($path); } - public function setContext(ContextInterface $context) - { - $this->context = $context; - } - /** - * @return ContextInterface|null + * @return ContextInterface */ protected function getContext() { diff --git a/src/Symfony/Component/Asset/PathPackage.php b/src/Symfony/Component/Asset/PathPackage.php index 9f9c4ea17a..906879f8b0 100644 --- a/src/Symfony/Component/Asset/PathPackage.php +++ b/src/Symfony/Component/Asset/PathPackage.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Asset; +use Symfony\Component\Asset\Context\ContextInterface; use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; /** @@ -31,9 +32,9 @@ class PathPackage extends Package * @param string $basePath The base path to be prepended to relative paths * @param VersionStrategyInterface $versionStrategy The version strategy */ - public function __construct($basePath, VersionStrategyInterface $versionStrategy) + public function __construct($basePath, VersionStrategyInterface $versionStrategy, ContextInterface $context = null) { - parent::__construct($versionStrategy); + parent::__construct($versionStrategy, $context); if (!$basePath) { $this->basePath = '/'; @@ -65,10 +66,6 @@ class PathPackage extends Package */ public function getBasePath() { - if (null !== $context = $this->getContext()) { - return $context->getBasePath().$this->basePath; - } - - return $this->basePath; + return $this->getContext()->getBasePath().$this->basePath; } } diff --git a/src/Symfony/Component/Asset/Tests/PathPackageTest.php b/src/Symfony/Component/Asset/Tests/PathPackageTest.php index 3378ee1dcd..ff5b0a052f 100644 --- a/src/Symfony/Component/Asset/Tests/PathPackageTest.php +++ b/src/Symfony/Component/Asset/Tests/PathPackageTest.php @@ -52,8 +52,8 @@ class PathPackageTest extends \PHPUnit_Framework_TestCase */ public function testGetUrlWithContext($basePathRequest, $basePath, $format, $path, $expected) { - $package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format)); - $package->setContext($this->getContext($basePathRequest)); + $package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format), $this->getContext($basePathRequest)); + $this->assertEquals($expected, $package->getUrl($path)); } diff --git a/src/Symfony/Component/Asset/Tests/UrlPackageTest.php b/src/Symfony/Component/Asset/Tests/UrlPackageTest.php index 7066f84686..515bd926ea 100644 --- a/src/Symfony/Component/Asset/Tests/UrlPackageTest.php +++ b/src/Symfony/Component/Asset/Tests/UrlPackageTest.php @@ -57,8 +57,8 @@ class UrlPackageTest extends \PHPUnit_Framework_TestCase */ public function testGetUrlWithContext($secure, $baseUrls, $format, $path, $expected) { - $package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format)); - $package->setContext($this->getContext($secure)); + $package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format), $this->getContext($secure)); + $this->assertEquals($expected, $package->getUrl($path)); } diff --git a/src/Symfony/Component/Asset/UrlPackage.php b/src/Symfony/Component/Asset/UrlPackage.php index d675cf0037..0e9f82d0fa 100644 --- a/src/Symfony/Component/Asset/UrlPackage.php +++ b/src/Symfony/Component/Asset/UrlPackage.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Asset; +use Symfony\Component\Asset\Context\ContextInterface; use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; use Symfony\Component\Asset\Exception\InvalidArgumentException; use Symfony\Component\Asset\Exception\LogicException; @@ -42,9 +43,9 @@ class UrlPackage extends Package * @param string|array $baseUrls Base asset URLs * @param VersionStrategyInterface $versionStrategy The version strategy */ - public function __construct($baseUrls = array(), VersionStrategyInterface $versionStrategy) + public function __construct($baseUrls = array(), VersionStrategyInterface $versionStrategy, ContextInterface $context = null) { - parent::__construct($versionStrategy); + parent::__construct($versionStrategy, $context); if (!is_array($baseUrls)) { $baseUrls = (array) $baseUrls; @@ -74,7 +75,7 @@ class UrlPackage extends Package return $path; } - if (null !== $this->sslPackage && ($context = $this->getContext()) && $context->isSecure()) { + if (null !== $this->sslPackage && $this->getContext()->isSecure()) { return $this->sslPackage->getUrl($path); }