Development

/branches/1.0/lib/helper/AssetHelper.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/helper/AssetHelper.php

Revision 23543, 14.0 kB (checked in by fabien, 5 years ago)

[1.0] fixed deprecated use of split for PHP 5.3 (closes #7509)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004 David Heinemeier Hansson
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 /**
13  * AssetHelper.
14  *
15  * @package    symfony
16  * @subpackage helper
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     David Heinemeier Hansson
19  * @version    SVN: $Id$
20  */
21
22 /**
23  * Returns a <link> tag that browsers and news readers
24  * can use to auto-detect a RSS or ATOM feed for the current page,
25  * to be included in the <head> section of a HTML document.
26  *
27  * <b>Options:</b>
28  * - rel - defaults to 'alternate'
29  * - type - defaults to 'application/rss+xml'
30  * - title - defaults to the feed type in upper case
31  *
32  * <b>Examples:</b>
33  * <code>
34  *  echo auto_discovery_link_tag('rss', 'module/feed');
35  *    => <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.curenthost.com/module/feed" />
36  *  echo auto_discovery_link_tag('rss', 'module/feed', array('title' => 'My RSS'));
37  *    => <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.curenthost.com/module/feed" />
38  * </code>
39  *
40  * @param  string feed type ('rss', 'atom')
41  * @param  string 'module/action' or '@rule' of the feed
42  * @param  array additional HTML compliant <link> tag parameters
43  * @return string XHTML compliant <link> tag
44  */
45 function auto_discovery_link_tag($type = 'rss', $url_options = array(), $tag_options = array())
46 {
47   return tag('link', array(
48     'rel'   => isset($tag_options['rel']) ? $tag_options['rel'] : 'alternate',
49     'type'  => isset($tag_options['type']) ? $tag_options['type'] : 'application/'.$type.'+xml',
50     'title' => isset($tag_options['title']) ? $tag_options['title'] : ucfirst($type),
51     'href'  => url_for($url_options, true)
52   ));
53 }
54
55 /**
56  * Returns the path to a JavaScript asset.
57  *
58  * <b>Example:</b>
59  * <code>
60  *  echo javascript_path('myscript');
61  *    => /js/myscript.js
62  * </code>
63  *
64  * <b>Note:</b> The asset name can be supplied as a...
65  * - full path, like "/my_js/myscript.css"
66  * - file name, like "myscript.js", that gets expanded to "/js/myscript.js"
67  * - file name without extension, like "myscript", that gets expanded to "/js/myscript.js"
68  *
69  * @param  string asset name
70  * @param  bool return absolute path ?
71  * @return string file path to the JavaScript file
72  * @see    javascript_include_tag
73  */
74 function javascript_path($source, $absolute = false)
75 {
76   return _compute_public_path($source, 'js', 'js', $absolute);
77 }
78
79 /**
80  * Returns a <script> include tag per source given as argument.
81  *
82  * <b>Examples:</b>
83  * <code>
84  *  echo javascript_include_tag('xmlhr');
85  *    => <script language="JavaScript" type="text/javascript" src="/js/xmlhr.js"></script>
86  *  echo javascript_include_tag('common.javascript', '/elsewhere/cools');
87  *    => <script language="JavaScript" type="text/javascript" src="/js/common.javascript"></script>
88  *       <script language="JavaScript" type="text/javascript" src="/elsewhere/cools.js"></script>
89  * </code>
90  *
91  * @param  string asset names
92  * @return string XHTML compliant <script> tag(s)
93  * @see    javascript_path
94  */
95 function javascript_include_tag()
96 {
97   $html = '';
98   foreach (func_get_args() as $source)
99   {
100     $source = javascript_path($source);
101     $html .= content_tag('script', '', array('type' => 'text/javascript', 'src' => $source))."\n";
102   }
103
104   return $html;
105 }
106
107 /**
108  * Returns the path to a stylesheet asset.
109  *
110  * <b>Example:</b>
111  * <code>
112  *  echo stylesheet_path('style');
113  *    => /css/style.css
114  * </code>
115  *
116  * <b>Note:</b> The asset name can be supplied as a...
117  * - full path, like "/my_css/style.css"
118  * - file name, like "style.css", that gets expanded to "/css/style.css"
119  * - file name without extension, like "style", that gets expanded to "/css/style.css"
120  *
121  * @param  string asset name
122  * @param  bool return absolute path ?
123  * @return string file path to the stylesheet file
124  * @see    stylesheet_tag 
125  */
126 function stylesheet_path($source, $absolute = false)
127 {
128   return _compute_public_path($source, 'css', 'css', $absolute);
129 }
130
131 /**
132  * Returns a css <link> tag per source given as argument,
133  * to be included in the <head> section of a HTML document.
134  *
135  * <b>Options:</b>
136  * - rel - defaults to 'stylesheet'
137  * - type - defaults to 'text/css'
138  * - media - defaults to 'screen'
139  *
140  * <b>Examples:</b>
141  * <code>
142  *  echo stylesheet_tag('style');
143  *    => <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
144  *  echo stylesheet_tag('style', array('media' => 'all'));
145  *    => <link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />
146  *  echo stylesheet_tag('random.styles', '/css/stylish');
147  *    => <link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" />
148  *       <link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />
149  * </code>
150  *
151  * @param  string asset names
152  * @param  array additional HTML compliant <link> tag parameters
153  * @return string XHTML compliant <link> tag(s)
154  * @see    stylesheet_path
155  */
156 function stylesheet_tag()
157 {
158   $sources = func_get_args();
159   $sourceOptions = (func_num_args() > 1 && is_array($sources[func_num_args() - 1])) ? array_pop($sources) : array();
160
161   $html = '';
162   foreach ($sources as $source)
163   {
164     $source  = stylesheet_path($source);
165     $options = array_merge(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'screen', 'href' => $source), $sourceOptions);
166     $html   .= tag('link', $options)."\n";
167   }
168
169   return $html;
170 }
171
172 /**
173  * Adds a stylesheet to the response object.
174  *
175  * @see sfResponse->addStylesheet()
176  */
177 function use_stylesheet($css, $position = '', $options = array())
178 {
179   sfContext::getInstance()->getResponse()->addStylesheet($css, $position, $options);
180 }
181
182 /**
183  * Adds a javascript to the response object.
184  *
185  * @see sfResponse->addJavascript()
186  */
187 function use_javascript($js, $position = '')
188 {
189   sfContext::getInstance()->getResponse()->addJavascript($js, $position);
190 }
191
192 /**
193  * Decorates the current template with a given layout.
194  *
195  * @param mixed The layout name or path or false to disable the layout
196  */
197 function decorate_with($layout)
198 {
199   $view = sfContext::getInstance()->getActionStack()->getLastEntry()->getViewInstance();
200   if (false === $layout)
201   {
202     $view->setDecorator(false);
203   }
204   else
205   {
206     $view->setDecoratorTemplate($layout);
207   }
208 }
209
210 /**
211  * Returns the path to an image asset.
212  *
213  * <b>Example:</b>
214  * <code>
215  *  echo image_path('foobar');
216  *    => /images/foobar.png
217  * </code>
218  *
219  * <b>Note:</b> The asset name can be supplied as a...
220  * - full path, like "/my_images/image.gif"
221  * - file name, like "rss.gif", that gets expanded to "/images/rss.gif"
222  * - file name without extension, like "logo", that gets expanded to "/images/logo.png"
223  *
224  * @param  string asset name
225  * @param  bool return absolute path ?
226  * @return string file path to the image file
227  * @see    image_tag 
228  */
229 function image_path($source, $absolute = false)
230 {
231   return _compute_public_path($source, 'images', 'png', $absolute);
232 }
233
234 /**
235  * Returns an <img> image tag for the asset given as argument.
236  *
237  * <b>Options:</b>
238  * - 'absolute' - to output absolute file paths, useful for embedded images in emails
239  * - 'alt'  - defaults to the file name part of the asset (capitalized and without the extension)
240  * - 'size' - Supplied as "XxY", so "30x45" becomes width="30" and height="45"
241  *
242  * <b>Examples:</b>
243  * <code>
244  *  echo image_tag('foobar');
245  *    => <img src="images/foobar.png" alt="Foobar" />
246  *  echo image_tag('/my_images/image.gif', array('alt' => 'Alternative text', 'size' => '100x200'));
247  *    => <img src="/my_images/image.gif" alt="Alternative text" width="100" height="200" />
248  * </code>
249  *
250  * @param  string image asset name
251  * @param  array additional HTML compliant <img> tag parameters
252  * @return string XHTML compliant <img> tag
253  * @see    image_path
254  */
255 function image_tag($source, $options = array())
256 {
257   if (!$source)
258   {
259     return '';
260   }
261
262   $options = _parse_attributes($options);
263
264   $absolute = false;
265   if (isset($options['absolute']))
266   {
267     unset($options['absolute']);
268     $absolute = true;
269   }
270
271   $options['src'] = image_path($source, $absolute);
272
273   if (!isset($options['alt']))
274   {
275     $path_pos = strrpos($source, '/');
276     $dot_pos = strrpos($source, '.');
277     $begin = $path_pos ? $path_pos + 1 : 0;
278     $nb_str = ($dot_pos ? $dot_pos : strlen($source)) - $begin;
279     $options['alt'] = ucfirst(substr($source, $begin, $nb_str));
280   }
281
282   if (isset($options['size']))
283   {
284     list($options['width'], $options['height']) = explode('x', $options['size'], 2);
285     unset($options['size']);
286   }
287
288   return tag('img', $options);
289 }
290
291 function _compute_public_path($source, $dir, $ext, $absolute = false)
292 {
293   if (strpos($source, '://'))
294   {
295     return $source;
296   }
297
298   $request = sfContext::getInstance()->getRequest();
299   $sf_relative_url_root = $request->getRelativeUrlRoot();
300   if (0 !== strpos($source, '/'))
301   {
302     $source = $sf_relative_url_root.'/'.$dir.'/'.$source;
303   }
304
305   $query_string = '';
306   if (false !== $pos = strpos($source, '?'))
307   {
308     $query_string = substr($source, $pos);
309     $source = substr($source, 0, $pos);
310   }
311
312   if (false === strpos(basename($source), '.'))
313   {
314     $source .= '.'.$ext;
315   }
316
317   if ($sf_relative_url_root && 0 !== strpos($source, $sf_relative_url_root))
318   {
319     $source = $sf_relative_url_root.$source;
320   }
321
322   if ($absolute)
323   {
324     $source = 'http'.($request->isSecure() ? 's' : '').'://'.$request->getHost().$source;
325   }
326
327   return $source.$query_string;
328 }
329
330 /**
331  * Prints a set of <meta> tags according to the response attributes,
332  * to be included in the <head> section of a HTML document.
333  *
334  * <b>Examples:</b>
335  * <code>
336  *  include_metas();
337  *    => <meta name="title" content="symfony - open-source PHP5 web framework" />
338  *       <meta name="robots" content="index, follow" />
339  *       <meta name="description" content="symfony - open-source PHP5 web framework" />
340  *       <meta name="keywords" content="symfony, project, framework, php, php5, open-source, mit, symphony" />
341  *       <meta name="language" content="en" /><link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
342  * </code>
343  *
344  * <b>Note:</b> Modify the sfResponse object or the view.yml to change, add or remove metas.
345  *
346  * @return string XHTML compliant <meta> tag(s)
347  * @see    include_http_metas
348  */
349 function include_metas()
350 {
351   foreach (sfContext::getInstance()->getResponse()->getMetas() as $name => $content)
352   {
353     echo tag('meta', array('name' => $name, 'content' => $content))."\n";
354   }
355 }
356
357 /**
358  * Returns a set of <meta http-equiv> tags according to the response attributes,
359  * to be included in the <head> section of a HTML document.
360  *
361  * <b>Examples:</b>
362  * <code>
363  *  include_http_metas();
364  *    => <meta http-equiv="content-type" content="text/html; charset=utf-8" />
365  * </code>
366  *
367  * <b>Note:</b> Modify the sfResponse object or the view.yml to change, add or remove HTTP metas.
368  *
369  * @return string XHTML compliant <meta> tag(s)
370  * @see    include_metas
371  */
372 function include_http_metas()
373 {
374   foreach (sfContext::getInstance()->getResponse()->getHttpMetas() as $httpequiv => $value)
375   {
376     echo tag('meta', array('http-equiv' => $httpequiv, 'content' => $value))."\n";
377   }
378 }
379
380 /**
381  * Returns the title of the current page according to the response attributes,
382  * to be included in the <title> section of a HTML document.
383  *
384  * <b>Note:</b> Modify the sfResponse object or the view.yml to modify the title of a page.
385  *
386  * @return string page title
387  */
388 function include_title()
389 {
390   $title = sfContext::getInstance()->getResponse()->getTitle();
391
392   echo content_tag('title', $title)."\n";
393 }
394
395 /**
396  * Returns <script> tags for all javascripts configured in view.yml or added to the response object.
397  *
398  * You can use this helper to decide the location of javascripts in pages.
399  * By default, if you don't call this helper, symfony will automatically include javascripts before </head>.
400  * Calling this helper disables this behavior.
401  *
402  * @return string <script> tags
403  */
404 function get_javascripts()
405 {
406   $response = sfContext::getInstance()->getResponse();
407   $response->setParameter('javascripts_included', true, 'symfony/view/asset');
408
409   $already_seen = array();
410   $html = '';
411
412   foreach (array('first', '', 'last') as $position)
413   {
414     foreach ($response->getJavascripts($position) as $files)
415     {
416       if (!is_array($files))
417       {
418         $files = array($files);
419       }
420
421       foreach ($files as $file)
422       {
423         $file = javascript_path($file);
424
425         if (isset($already_seen[$file])) continue;
426
427         $already_seen[$file] = 1;
428         $html .= javascript_include_tag($file);
429       }
430     }
431   }
432
433   return $html;
434 }
435
436 /**
437  * Prints <script> tags for all javascripts configured in view.yml or added to the response object.
438  *
439  * @see get_javascripts()
440  */
441 function include_javascripts()
442 {
443   echo get_javascripts();
444 }
445
446 /**
447  * Returns <link> tags for all stylesheets configured in view.yml or added to the response object.
448  *
449  * You can use this helper to decide the location of stylesheets in pages.
450  * By default, if you don't call this helper, symfony will automatically include stylesheets before </head>.
451  * Calling this helper disables this behavior.
452  *
453  * @return string <link> tags
454  */
455 function get_stylesheets()
456 {
457   $response = sfContext::getInstance()->getResponse();
458   $response->setParameter('stylesheets_included', true, 'symfony/view/asset');
459
460   $already_seen = array();
461   $html = '';
462
463   foreach (array('first', '', 'last') as $position)
464   {
465     foreach ($response->getStylesheets($position) as $files => $options)
466     {
467       if (!is_array($files))
468       {
469         $files = array($files);
470       }
471
472       foreach ($files as $file)
473       {
474         $file = stylesheet_path($file);
475
476         if (isset($already_seen[$file])) continue;
477
478         $already_seen[$file] = 1;
479         $html .= stylesheet_tag($file, $options);
480       }
481     }
482   }
483
484   return $html;
485 }
486
487 /**
488  * Prints <link> tags for all stylesheets configured in view.yml or added to the response object.
489  *
490  * @see get_stylesheets()
491  */
492 function include_stylesheets()
493 {
494   echo get_stylesheets();
495 }
496
Note: See TracBrowser for help on using the browser.