8000 [Asset] added the component by fabpot · Pull Request #13234 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Asset] added the component #13234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 10, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Asset] added a NullContext class
  • Loading branch information
fabpot committed Feb 10, 2015
commit 4d0adeae16bf6733e637089b8121e25752e0d604
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@
<service id="assets.path_package" class="Symfony\Component\Asset\PathPackage" abstract="true">
<argument /> <!-- base path -->
<argument type="service" /> <!-- version strategy -->
<call method="setContext">
<argument type="service" id="assets.context" />
</call>
<argument type="service" id="assets.context" />
</service>

<service id="assets.url_package" class="Symfony\Component\Asset\UrlPackage" abstract="true">
<argument /> <!-- base URLs -->
<argument type="service" /> <!-- version strategy -->
<call method="setContext">
<argument type="service" id="assets.context" />
</call>
<argument type="service" id="assets.context" />
</service>

<service id="assets.static_version_strategy" class="Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy" abstract="true">
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Asset/Context/NullContext.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 4 additions & 7 deletions src/Symfony/Component/Asset/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

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

public function setContext(ContextInterface $context)
{
$this->context = $context;
}

/**
* @return ContextInterface|null
* @return ContextInterface
*/
protected function getContext()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should document it as @return ContextInterface|null. It is important for child classes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{
Expand Down
11 changes: 4 additions & 7 deletions src/Symfony/Component/Asset/PathPackage.php
10000
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Asset;

use Symfony\Component\Asset\Context\ContextInterface;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;

/**
Expand All @@ -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 = '/';
Expand Down Expand Up @@ -65,10 +66,6 @@ public function getUrl($path)
*/
public function getBasePath()
{
if (null !== $context = $this->getContext()) {
return $context->getBasePath().$this->basePath;
}

return $this->basePath;
return $this->getContext()->getBasePath().$this->basePath;
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Asset/Tests/PathPackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function getConfigs()
*/
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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Asset/Tests/UrlPackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function getConfigs()
*/
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));
}

Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Asset/UrlPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function getUrl($path)
return $path;
}

if (null !== $this->sslPackage && ($context = $this->getContext()) && $context->isSecure()) {
if (null !== $this->sslPackage && $this->getContext()->isSecure()) {
return $this->sslPackage->getUrl($path);
}

Expand Down
0