[Asset] added a NullContext class

This commit is contained in:
Fabien Potencier 2015-01-19 20:26:55 +01:00
parent d33c41d436
commit 4d0adeae16
7 changed files with 56 additions and 27 deletions

View File

@ -18,17 +18,13 @@
<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true"> <service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true">
<argument /> <!-- base path --> <argument /> <!-- base path -->
<argument type="service" /> <!-- version strategy --> <argument type="service" /> <!-- version strategy -->
<call method="setContext"> <argument type="service" id="assets.context" />
<argument type="service" id="assets.context" />
</call>
</service> </service>
<service id="assets.url_package" class="Symfony\Component\Asset\UrlPackage" abstract="true"> <service id="assets.url_package" class="Symfony\Component\Asset\UrlPackage" abstract="true">
<argument /> <!-- base URLs --> <argument /> <!-- base URLs -->
<argument type="service" /> <!-- version strategy --> <argument type="service" /> <!-- version strategy -->
<call method="setContext"> <argument type="service" id="assets.context" />
<argument type="service" id="assets.context" />
</call>
</service> </service>
<service id="assets.static_version_strategy" class="Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy" abstract="true"> <service id="assets.static_version_strategy" class="Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy" abstract="true">

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <fabien@symfony.com>
*/
class NullContext implements ContextInterface
{
/**
* {@inheritdoc}
*/
public function getBasePath()
{
return '';
}
/**
* {@inheritdoc}
*/
public function isSecure()
{
return false;
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Asset; namespace Symfony\Component\Asset;
use Symfony\Component\Asset\Context\ContextInterface; use Symfony\Component\Asset\Context\ContextInterface;
use Symfony\Component\Asset\Context\NullContext;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
/** /**
@ -25,9 +26,10 @@ class Package implements PackageInterface
private $versionStrategy; private $versionStrategy;
private $context; private $context;
public function __construct(VersionStrategyInterface $versionStrategy) public function __construct(VersionStrategyInterface $versionStrategy, ContextInterface $context = null)
{ {
$this->versionStrategy = $versionStrategy; $this->versionStrategy = $versionStrategy;
$this->context = $context ?: new NullContext();
} }
/** /**
@ -50,13 +52,8 @@ class Package implements PackageInterface
return $this->versionStrategy->applyVersion($path); return $this->versionStrategy->applyVersion($path);
} }
public function setContext(ContextInterface $context)
{
$this->context = $context;
}
/** /**
* @return ContextInterface|null * @return ContextInterface
*/ */
protected function getContext() protected function getContext()
{ {

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Asset; namespace Symfony\Component\Asset;
use Symfony\Component\Asset\Context\ContextInterface;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; 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 string $basePath The base path to be prepended to relative paths
* @param VersionStrategyInterface $versionStrategy The version strategy * @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) { if (!$basePath) {
$this->basePath = '/'; $this->basePath = '/';
@ -65,10 +66,6 @@ class PathPackage extends Package
*/ */
public function getBasePath() public function getBasePath()
{ {
if (null !== $context = $this->getContext()) { return $this->getContext()->getBasePath().$this->basePath;
return $context->getBasePath().$this->basePath;
}
return $this->basePath;
} }
} }

View File

@ -52,8 +52,8 @@ class PathPackageTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetUrlWithContext($basePathRequest, $basePath, $format, $path, $expected) public function testGetUrlWithContext($basePathRequest, $basePath, $format, $path, $expected)
{ {
$package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format)); $package = new PathPackage($basePath, new StaticVersionStrategy('v1', $format), $this->getContext($basePathRequest));
$package->setContext($this->getContext($basePathRequest));
$this->assertEquals($expected, $package->getUrl($path)); $this->assertEquals($expected, $package->getUrl($path));
} }

View File

@ -57,8 +57,8 @@ class UrlPackageTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetUrlWithContext($secure, $baseUrls, $format, $path, $expected) public function testGetUrlWithContext($secure, $baseUrls, $format, $path, $expected)
{ {
$package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format)); $package = new UrlPackage($baseUrls, new StaticVersionStrategy('v1', $format), $this->getContext($secure));
$package->setContext($this->getContext($secure));
$this->assertEquals($expected, $package->getUrl($path)); $this->assertEquals($expected, $package->getUrl($path));
} }

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Asset; namespace Symfony\Component\Asset;
use Symfony\Component\Asset\Context\ContextInterface;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface; use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
use Symfony\Component\Asset\Exception\InvalidArgumentException; use Symfony\Component\Asset\Exception\InvalidArgumentException;
use Symfony\Component\Asset\Exception\LogicException; use Symfony\Component\Asset\Exception\LogicException;
@ -42,9 +43,9 @@ class UrlPackage extends Package
* @param string|array $baseUrls Base asset URLs * @param string|array $baseUrls Base asset URLs
* @param VersionStrategyInterface $versionStrategy The version strategy * @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)) { if (!is_array($baseUrls)) {
$baseUrls = (array) $baseUrls; $baseUrls = (array) $baseUrls;
@ -74,7 +75,7 @@ class UrlPackage extends Package
return $path; return $path;
} }
if (null !== $this->sslPackage && ($context = $this->getContext()) && $context->isSecure()) { if (null !== $this->sslPackage && $this->getContext()->isSecure()) {
return $this->sslPackage->getUrl($path); return $this->sslPackage->getUrl($path);
} }