[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/helper/ -> AssetHelper.php (source)

   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: AssetHelper.php 3313 2007-01-20 07:00:37Z fabien $
  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']) = split('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 (strpos($source, '/') !== 0)
 301    {
 302      $source = $sf_relative_url_root.'/'.$dir.'/'.$source;
 303    }
 304    if (strpos(basename($source), '.') === false)
 305    {
 306      $source .= '.'.$ext;
 307    }
 308    if ($sf_relative_url_root && strpos($source, $sf_relative_url_root) !== 0)
 309    {
 310      $source = $sf_relative_url_root.$source;
 311    }
 312  
 313    if ($absolute)
 314    {
 315      $source = 'http'.($request->isSecure() ? 's' : '').'://'.$request->getHost().$source;
 316    }
 317  
 318    return $source;
 319  }
 320  
 321  /**
 322   * Prints a set of <meta> tags according to the response attributes,
 323   * to be included in the <head> section of a HTML document.
 324   *
 325   * <b>Examples:</b>
 326   * <code>
 327   *  include_metas();
 328   *    => <meta name="title" content="symfony - open-source PHP5 web framework" />
 329   *       <meta name="robots" content="index, follow" />
 330   *       <meta name="description" content="symfony - open-source PHP5 web framework" />
 331   *       <meta name="keywords" content="symfony, project, framework, php, php5, open-source, mit, symphony" />
 332   *       <meta name="language" content="en" /><link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
 333   * </code>
 334   *
 335   * <b>Note:</b> Modify the sfResponse object or the view.yml to change, add or remove metas.
 336   *
 337   * @return string XHTML compliant <meta> tag(s)
 338   * @see    include_http_metas 
 339   */
 340  function include_metas()
 341  {
 342    foreach (sfContext::getInstance()->getResponse()->getMetas() as $name => $content)
 343    {
 344      echo tag('meta', array('name' => $name, 'content' => $content))."\n";
 345    }
 346  }
 347  
 348  /**
 349   * Returns a set of <meta http-equiv> tags according to the response attributes,
 350   * to be included in the <head> section of a HTML document.
 351   *
 352   * <b>Examples:</b>
 353   * <code>
 354   *  include_http_metas();
 355   *    => <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 356   * </code>
 357   *
 358   * <b>Note:</b> Modify the sfResponse object or the view.yml to change, add or remove metas.
 359   *
 360   * @return string XHTML compliant <meta> tag(s)
 361   * @see    include_metas 
 362   */
 363  function include_http_metas()
 364  {
 365    foreach (sfContext::getInstance()->getResponse()->getHttpMetas() as $httpequiv => $value)
 366    {
 367      echo tag('meta', array('http-equiv' => $httpequiv, 'content' => $value))."\n";
 368    }
 369  }
 370  
 371  /**
 372   * Returns the title of the current page according to the response attributes,
 373   * to be included in the <title> section of a HTML document.
 374   *
 375   * <b>Note:</b> Modify the sfResponse object or the view.yml to modify the title of a page.
 376   *
 377   * @return string page title
 378   */
 379  function include_title()
 380  {
 381    $title = sfContext::getInstance()->getResponse()->getTitle();
 382  
 383    echo content_tag('title', $title)."\n";
 384  }
 385  
 386  /**
 387   * Returns <script> tags for all javascripts configured in view.yml or added to the response object.
 388   *
 389   * You can use this helper to decide the location of javascripts in pages.
 390   * By default, if you don't call this helper, symfony will automatically include javascripts before </head>.
 391   * Calling this helper disables this behavior.
 392   *
 393   * @return string <script> tags
 394   */
 395  function get_javascripts()
 396  {
 397    $response = sfContext::getInstance()->getResponse();
 398    $response->setParameter('javascripts_included', true, 'symfony/view/asset');
 399  
 400    $already_seen = array();
 401    $html = '';
 402  
 403    foreach (array('first', '', 'last') as $position)
 404    {
 405      foreach ($response->getJavascripts($position) as $files)
 406      {
 407        if (!is_array($files))
 408        {
 409          $files = array($files);
 410        }
 411  
 412        foreach ($files as $file)
 413        {
 414          $file = javascript_path($file);
 415  
 416          if (isset($already_seen[$file])) continue;
 417  
 418          $already_seen[$file] = 1;
 419          $html .= javascript_include_tag($file);
 420        }
 421      }
 422    }
 423  
 424    return $html;
 425  }
 426  
 427  /**
 428   * Prints <script> tags for all javascripts configured in view.yml or added to the response object.
 429   *
 430   * @see get_javascripts()
 431   */
 432  function include_javascripts()
 433  {
 434    echo get_javascripts();
 435  }
 436  
 437  /**
 438   * Returns <link> tags for all stylesheets configured in view.yml or added to the response object.
 439   *
 440   * You can use this helper to decide the location of stylesheets in pages.
 441   * By default, if you don't call this helper, symfony will automatically include stylesheets before </head>.
 442   * Calling this helper disables this behavior.
 443   *
 444   * @return string <link> tags
 445   */
 446  function get_stylesheets()
 447  {
 448    $response = sfContext::getInstance()->getResponse();
 449    $response->setParameter('stylesheets_included', true, 'symfony/view/asset');
 450  
 451    $already_seen = array();
 452    $html = '';
 453  
 454    foreach (array('first', '', 'last') as $position)
 455    {
 456      foreach ($response->getStylesheets($position) as $files => $options)
 457      {
 458        if (!is_array($files))
 459        {
 460          $files = array($files);
 461        }
 462  
 463        foreach ($files as $file)
 464        {
 465          $file = stylesheet_path($file);
 466  
 467          if (isset($already_seen[$file])) continue;
 468  
 469          $already_seen[$file] = 1;
 470          $html .= stylesheet_tag($file, $options);
 471        }
 472      }
 473    }
 474  
 475    return $html;
 476  }
 477  
 478  /**
 479   * Prints <link> tags for all stylesheets configured in view.yml or added to the response object.
 480   *
 481   * @see get_stylesheets()
 482   */
 483  function include_stylesheets()
 484  {
 485    echo get_stylesheets();
 486  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7