123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
-
-
-
- namespace Symfony\Bundle\AsseticBundle\Templating;
-
- use Assetic\Asset\AssetInterface;
- use Assetic\Factory\AssetFactory;
- use Assetic\Util\TraversableString;
- use Symfony\Component\Templating\Helper\Helper;
-
-
- abstract class AsseticHelper extends Helper
- {
- protected $factory;
-
-
-
- public function __construct(AssetFactory $factory)
- {
- $this->factory = $factory;
- }
-
-
-
- public function javascripts($inputs = array(), $filters = array(), array $options = array())
- {
- if (!isset($options['output'])) {
- $options['output'] = 'js/*.js';
- }
-
- return $this->getAssetUrls($inputs, $filters, $options);
- }
-
-
-
- public function stylesheets($inputs = array(), $filters = array(), array $options = array())
- {
- if (!isset($options['output'])) {
- $options['output'] = 'css/*.css';
- }
-
- return $this->getAssetUrls($inputs, $filters, $options);
- }
-
-
-
- public function image($inputs = array(), $filters = array(), array $options = array())
- {
- if (!isset($options['output'])) {
- $options['output'] = 'images/*';
- }
-
- $options['single'] = true;
-
- return $this->getAssetUrls($inputs, $filters, $options);
- }
-
-
-
- private function getAssetUrls($inputs = array(), $filters = array(), array $options = array())
- {
- $explode = function($value)
- {
- return array_map('trim', explode(',', $value));
- };
-
- if (!is_array($inputs)) {
- $inputs = $explode($inputs);
- }
-
- if (!is_array($filters)) {
- $filters = $explode($filters);
- }
-
- if (!isset($options['debug'])) {
- $options['debug'] = $this->factory->isDebug();
- }
-
- if (!isset($options['combine'])) {
- $options['combine'] = !$options['debug'];
- }
-
- if (isset($options['single']) && $options['single'] && 1 < count($inputs)) {
- $inputs = array_slice($inputs, -1);
- }
-
- if (!isset($options['name'])) {
- $options['name'] = $this->factory->generateAssetName($inputs, $filters, $options);
- }
-
- $asset = $this->factory->createAsset($inputs, $filters, $options);
-
- $one = $this->getAssetUrl($asset, $options);
- $many = array();
- if ($options['combine']) {
- $many[] = $one;
- } else {
- $i = 0;
- foreach ($asset as $leaf) {
- $many[] = $this->getAssetUrl($leaf, array_replace($options, array(
- 'name' => $options['name'].'_'.$i++,
- )));
- }
- }
-
- return new TraversableString($one, $many);
- }
-
-
-
- abstract protected function getAssetUrl(AssetInterface $asset, $options = array());
-
- public function getName()
- {
- return 'assetic';
- }
- }
|