Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69fff2e

Browse files
committed
[Asset] moved the Asset classes from the Templating Component to a new one
1 parent 833f389 commit 69fff2e

16 files changed

+503
-192
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
2.7.0
5+
-----
6+
7+
* added the component

src/Symfony/Component/Asset/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2004-2015 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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;
13+
14+
/**
15+
* The basic package will add a version to asset URLs.
16+
*
17+
* @author Kris Wallsmith <[email protected]>
18+
*/
19+
class Package implements PackageInterface
20+
{
21+
private $version;
22+
private $format;
23+
24+
/**
25+
* Constructor.
26+
*
27+
* @param string $version The package version
28+
* @param string $format The format used to apply the version
29+
*/
30+
public function __construct($version = null, $format = '')
31+
{
32+
$this->version = $version;
33+
$this->format = $format ?: '%s?%s';
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getVersion()
40+
{
41+
return $this->version;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getUrl($path, $version = null)
48+
{
49+
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
50+
return $path;
51+
}
52+
53+
return $this->applyVersion($path, $version);
54+
}
55+
56+
/**
57+
* Applies version to the supplied path.
58+
*
59+
* @param string $path A path
60+
* @param string|bool|null $version A specific version
61+
*
62+
* @return string The versionized path
63+
*/
64+
protected function applyVersion($path, $version = null)
65+
{
66+
$version = null !== $version ? $version : $this->version;
67+
if (null === $version || false === $version) {
68+
return $path;
69+
}
70+
71+
$versionized = sprintf($this->format, ltrim($path, '/'), $version);
72+
73+
if ($path && '/' == $path[0]) {
74+
$versionized = '/'.$versionized;
75+
}
76+
77+
return $versionized;
78+
}
79+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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;
13+
14+
/**
15+
* Asset package interface.
16+
*
17+
* @author Kris Wallsmith <[email protected]>
18+
*/
19+
interface PackageInterface
20+
{
21+
/**
22+
* Returns the asset package version.
23+
*
24+
* @return string The version string
25+
*/
26+
public function getVersion();
27+
28+
/**
29+
* Returns an absolute or root-relative public path.
30+
*
31+
* @param string $path A path
32+
* @param string|bool|null $version A specific version for the path
33+
*
34+
* @return string The public path
35+
*/
36+
public function getUrl($path, $version = null);
37+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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;
13+
14+
/**
15+
* Helps manage asset URLs.
16+
*
17+
* @author Fabien Potencier <[email protected]>
18+
* @author Kris Wallsmith <[email protected]>
19+
*/
20+
class Packages
21+
{
22+
private $defaultPackage;
23+
private $packages = array();
24+
25+
/**
26+
* @param PackageInterface $defaultPackage The default package
27+
* @param PackageInterface[] $packages Additional packages indexed by name
28+
*/
29+
public function __construct(PackageInterface $defaultPackage, array $packages = array())
30+
{
31+
$this->defaultPackage = $defaultPackage;
32+
33+
foreach ($packages as $name => $package) {
34+
$this->addPackage($name, $package);
35+
}
36+
}
37+
38+
/**
39+
* Sets the default package.
40+
*
41+
* @param PackageInterface $defaultPackage The default package
42+
*/
43+
public function setDefaultPackage(PackageInterface $defaultPackage)
44+
{
45+
$this->defaultPackage = $defaultPackage;
46+
}
47+
48+
/**
49+
* Adds a package.
50+
*
51+
* @param string $name The package name
52+
* @param PackageInterface $package The package
53+
*/
54+
public function addPackage($name, PackageInterface $package)
55+
{
56+
$this->packages[$name] = $package;
57+
}
58+
59+
/**
60+
* Returns an asset package.
61+
*
62+
* @param string $name The name of the package or null for the default package
63+
*
64+
* @return PackageInterface An asset package
65+
*
66+
* @throws \InvalidArgumentException If there is no package by that name
67+
*/
68+
public function getPackage($name = null)
69+
{
70+
if (null === $name) {
71+
return $this->defaultPackage;
72+
}
73+
74+
if (!isset($this->packages[$name])) {
75+
throw new \InvalidArgumentException(sprintf('There is no "%s" asset package.', $name));
76+
}
77+
78+
return $this->packages[$name];
79+
}
80+
81+
/**
82+
* Gets the version to add to public URL.
83+
*
84+
* @param string $packageName A package name
85+
*
86+
* @return string The current version
87+
*/
88+
public function getVersion($packageName = null)
89+
{
90+
return $this->getPackage($packageName)->getVersion();
91+
}
92+
93+
/**
94+
* Returns the public path.
95+
*
96+
* Absolute paths (i.e. http://...) are returned unmodified.
97+
*
98+
* @param string $path A public path
99+
* @param string $packageName The name of the asset package to use
100+
* @param string|bool|null $version A specific version
101+
*
102+
* @return string A public path which takes into account the base path and URL path
103+
*/
104+
public function getUrl($path, $packageName = null, $version = null)
105+
{
106+
return $this->getPackage($packageName)->getUrl($path, $version);
107+
}
108+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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;
13+
14+
/**
15+
* The path packages adds a version and a base path to asset URLs.
16+
*
17+
* @author Kris Wallsmith <[email protected]>
18+
*/
19+
class PathPackage extends Package
20+
{
21+
private $basePath;
22+
23+
/**
24+
* Constructor.
25+
*
26+
* @param string $basePath The base path to be prepended to relative paths
27+
* @param string $version The package version
28+
* @param string $format The format used to apply the version
29+
*/
30+
public function __construct($basePath = null, $version = null, $format = null)
31+
{
32+
parent::__construct($version, $format);
33+
34+
if (!$basePath) {
35+
$this->basePath = '/';
36+
} else {
37+
if ('/' != $basePath[0]) {
38+
$basePath = '/'.$basePath;
39+
}
40+
41+
$this->basePath = rtrim($basePath, '/').'/';
42+
}
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function getUrl($path, $version = null)
49+
{
50+
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
51+
return $path;
52+
}
53+
54+
$url = $this->applyVersion($path, $version);
55+
56+
// apply the base path
57+
if ('/' !== substr($url, 0, 1)) {
58+
$url = $this->basePath.$url;
59+
}
60+
61+
return $url;
62+
}
63+
64+
/**
65+
* Returns the base path.
66+
*
67+
* @return string The base path
68+
*/
69+
public function getBasePath()
70+
{
71+
return $this->basePath;
72+
}
73+
}

src/Symfony/Component/Asset/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Asset Component
2+
===============
3+
4+
Asset provides classes to manage asset URLs.
5+
6+
Resources
7+
---------
8+
9+
You can run the unit tests with the following command:
10+
11+
$ cd path/to/Symfony/Component/Asset/
12+
$ composer.phar install
13+
$ phpunit

0 commit comments

Comments
 (0)