123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Symfony\Component\Templating\Helper;
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * AssetsHelper is the base class for all helper classes that manages assets.
- *
- * Usage:
- *
- * <code>
- * <img src="<?php echo $view['assets']->getUrl('foo.png') ?>" />
- * </code>
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class AssetsHelper extends Helper
- {
- protected $version;
- protected $baseURLs;
- protected $basePath;
- /**
- * Constructor.
- *
- * @param string $basePath The base path
- * @param string|array $baseURLs The domain URL or an array of domain URLs
- * @param string $version The version
- */
- public function __construct($basePath = null, $baseURLs = array(), $version = null)
- {
- $this->setBasePath($basePath);
- $this->setBaseURLs($baseURLs);
- $this->version = $version;
- }
- /**
- * Gets the version to add to public URL.
- *
- * @return string The current version
- */
- public function getVersion()
- {
- return $this->version;
- }
- /**
- * Sets the version that is added to each public URL.
- *
- * @param string $id The version
- */
- public function setVersion($version)
- {
- $this->version = $version;
- }
- /**
- * Gets the base path.
- *
- * @return string The base path
- */
- public function getBasePath()
- {
- return $this->basePath;
- }
- /**
- * Sets the base path.
- *
- * @param string $basePath The base path
- */
- public function setBasePath($basePath)
- {
- if (strlen($basePath) && '/' != $basePath[0]) {
- $basePath = '/'.$basePath;
- }
- $this->basePath = rtrim($basePath, '/').'/';
- }
- /**
- * Gets the base URL.
- *
- * If multiple base URLs have been defined a random one will be picked for each asset.
- * In other words: for one asset path the same base URL will always be picked among the available base URLs.
- *
- * @param string $path The path
- *
- * @return string The base URL
- */
- public function getBaseURL($path)
- {
- $count = count($this->baseURLs);
- if (0 === $count) {
- return '';
- }
- if (1 === $count) {
- return $this->baseURLs[0];
- }
- return $this->baseURLs[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
- }
- /**
- * Gets the base URLs.
- *
- * @return array The base URLs
- */
- public function getBaseURLs()
- {
- return $this->baseURLs;
- }
- /**
- * Sets the base URLs.
- *
- * If you pass an array, the getBaseURL() will return a
- * randomly pick one to use for each asset.
- *
- * @param string|array $baseURLs The base URLs
- */
- public function setBaseURLs($baseURLs)
- {
- if (!is_array($baseURLs)) {
- $baseURLs = array($baseURLs);
- }
- $this->baseURLs = array();
- foreach ($baseURLs as $URL) {
- $this->baseURLs[] = rtrim($URL, '/');
- }
- }
- /**
- * Returns the public path.
- *
- * @param string $path A public path
- *
- * @return string A public path which takes into account the base path and URL path
- */
- public function getUrl($path)
- {
- if (false !== strpos($path, '://')) {
- return $path;
- }
- $base = $this->getBaseURL($path);
- if (0 !== strpos($path, '/')) {
- $path = $base ? '/'.$path : $this->basePath.$path;
- }
- return $base.$path.($this->version ? '?'.$this->version : '');
- }
- /**
- * Returns the canonical name of this helper.
- *
- * @return string The canonical name
- */
- public function getName()
- {
- return 'assets';
- }
- }
|