|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony framework. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* This source file is subject to the MIT license that is bundled |
|
|
9 |
* with this source code in the file LICENSE. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\AsseticBundle\Templating; |
|
|
13 |
|
|
|
14 |
use Assetic\Asset\AssetInterface; |
|
|
15 |
use Assetic\Factory\AssetFactory; |
|
|
16 |
use Assetic\Util\TraversableString; |
|
|
17 |
use Symfony\Component\Templating\Helper\Helper; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* The "assetic" templating helper. |
|
|
21 |
* |
|
|
22 |
* @author Kris Wallsmith <kris@symfony.com> |
|
|
23 |
*/ |
|
|
24 |
abstract class AsseticHelper extends Helper |
|
|
25 |
{ |
|
|
26 |
protected $factory; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* Constructor. |
|
|
30 |
* |
|
|
31 |
* @param AssetFactory $factory The asset factory |
|
|
32 |
*/ |
|
|
33 |
public function __construct(AssetFactory $factory) |
|
|
34 |
{ |
|
|
35 |
$this->factory = $factory; |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
/** |
|
|
39 |
* Returns an array of javascript urls. |
|
|
40 |
*/ |
|
|
41 |
public function javascripts($inputs = array(), $filters = array(), array $options = array()) |
|
|
42 |
{ |
|
|
43 |
if (!isset($options['output'])) { |
|
|
44 |
$options['output'] = 'js/*.js'; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
return $this->getAssetUrls($inputs, $filters, $options); |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
/** |
|
|
51 |
* Returns an array of stylesheet urls. |
|
|
52 |
*/ |
|
|
53 |
public function stylesheets($inputs = array(), $filters = array(), array $options = array()) |
|
|
54 |
{ |
|
|
55 |
if (!isset($options['output'])) { |
|
|
56 |
$options['output'] = 'css/*.css'; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
return $this->getAssetUrls($inputs, $filters, $options); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Returns an array of one image url. |
|
|
64 |
*/ |
|
|
65 |
public function image($inputs = array(), $filters = array(), array $options = array()) |
|
|
66 |
{ |
|
|
67 |
if (!isset($options['output'])) { |
|
|
68 |
$options['output'] = 'images/*'; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
$options['single'] = true; |
|
|
72 |
|
|
|
73 |
return $this->getAssetUrls($inputs, $filters, $options); |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
/** |
|
|
77 |
* Gets the URLs for the configured asset. |
|
|
78 |
* |
|
|
79 |
* Usage looks something like this: |
|
|
80 |
* |
|
|
81 |
* <?php foreach ($view['assetic']->assets('@jquery, js/src/core/*', '?yui_js') as $url): ?> |
|
|
82 |
* <script src="<?php echo $url ?>" type="text/javascript"></script> |
|
|
83 |
* <?php endforeach; ?> |
|
|
84 |
* |
|
|
85 |
* When in debug mode, the helper returns an array of one or more URLs. |
|
|
86 |
* When not in debug mode it returns an array of one URL. |
|
|
87 |
* |
|
|
88 |
* @param array|string $inputs An array or comma-separated list of input strings |
|
|
89 |
* @param array|string $filters An array or comma-separated list of filter names |
|
|
90 |
* @param array $options An array of options |
|
|
91 |
* |
|
|
92 |
* @return array An array of URLs for the asset |
|
|
93 |
*/ |
|
|
94 |
private function getAssetUrls($inputs = array(), $filters = array(), array $options = array()) |
|
|
95 |
{ |
|
|
96 |
$explode = function($value) |
|
|
97 |
{ |
|
|
98 |
return array_map('trim', explode(',', $value)); |
|
|
99 |
}; |
|
|
100 |
|
|
|
101 |
if (!is_array($inputs)) { |
|
|
102 |
$inputs = $explode($inputs); |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
if (!is_array($filters)) { |
|
|
106 |
$filters = $explode($filters); |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
if (!isset($options['debug'])) { |
|
|
110 |
$options['debug'] = $this->factory->isDebug(); |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
if (!isset($options['combine'])) { |
|
|
114 |
$options['combine'] = !$options['debug']; |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
if (isset($options['single']) && $options['single'] && 1 < count($inputs)) { |
|
|
118 |
$inputs = array_slice($inputs, -1); |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
if (!isset($options['name'])) { |
|
|
122 |
$options['name'] = $this->factory->generateAssetName($inputs, $filters, $options); |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
$asset = $this->factory->createAsset($inputs, $filters, $options); |
|
|
126 |
|
|
|
127 |
$one = $this->getAssetUrl($asset, $options); |
|
|
128 |
$many = array(); |
|
|
129 |
if ($options['combine']) { |
|
|
130 |
$many[] = $one; |
|
|
131 |
} else { |
|
|
132 |
$i = 0; |
|
|
133 |
foreach ($asset as $leaf) { |
|
|
134 |
$many[] = $this->getAssetUrl($leaf, array_replace($options, array( |
|
|
135 |
'name' => $options['name'].'_'.$i++, |
|
|
136 |
))); |
|
|
137 |
} |
|
|
138 |
} |
|
|
139 |
|
|
|
140 |
return new TraversableString($one, $many); |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
/** |
|
|
144 |
* Returns an URL for the supplied asset. |
|
|
145 |
* |
|
|
146 |
* @param AssetInterface $asset An asset |
|
|
147 |
* @param array $options An array of options |
|
|
148 |
* |
|
|
149 |
* @return string An echo-ready URL |
|
|
150 |
*/ |
|
|
151 |
abstract protected function getAssetUrl(AssetInterface $asset, $options = array()); |
|
|
152 |
|
|
|
153 |
public function getName() |
|
|
154 |
{ |
|
|
155 |
return 'assetic'; |
|
|
156 |
} |
|
|
157 |
} |