PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Components/Templating/Helper/StylesheetsHelper.php

https://github.com/come/symfony
PHP | 112 lines | 43 code | 12 blank | 57 comment | 0 complexity | 5483f5763956c09de6fd01118a512914 MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. namespace Symfony\Components\Templating\Helper;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  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. * StylesheetsHelper is a helper that manages stylesheets.
  13. *
  14. * Usage:
  15. *
  16. * <code>
  17. * $this->stylesheets->add('foo.css', array('media' => 'print'));
  18. * echo $this->stylesheets;
  19. * </code>
  20. *
  21. * @package Symfony
  22. * @subpackage Components_Templating
  23. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24. */
  25. class StylesheetsHelper extends Helper
  26. {
  27. protected $stylesheets = array();
  28. protected $assetHelper;
  29. /**
  30. * Constructor.
  31. *
  32. * @param AssetsHelper $assetHelper A AssetsHelper instance
  33. */
  34. public function __construct(AssetsHelper $assetHelper)
  35. {
  36. $this->assetHelper = $assetHelper;
  37. }
  38. /**
  39. * Adds a stylesheets file.
  40. *
  41. * @param string $stylesheet A stylesheet file path
  42. * @param array $attributes An array of attributes
  43. */
  44. public function add($stylesheet, $attributes = array())
  45. {
  46. $this->stylesheets[$this->assetHelper->getUrl($stylesheet)] = $attributes;
  47. }
  48. /**
  49. * Returns all stylesheet files.
  50. *
  51. * @return array An array of stylesheet files to include
  52. */
  53. public function get()
  54. {
  55. return $this->stylesheets;
  56. }
  57. /**
  58. * Returns HTML representation of the links to stylesheets.
  59. *
  60. * @return string The HTML representation of the stylesheets
  61. */
  62. public function render()
  63. {
  64. $html = '';
  65. foreach ($this->stylesheets as $path => $attributes) {
  66. $atts = '';
  67. foreach ($attributes as $key => $value) {
  68. $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset));
  69. }
  70. $html .= sprintf('<link href="%s" rel="stylesheet" type="text/css"%s />', $path, $atts)."\n";
  71. }
  72. return $html;
  73. }
  74. /**
  75. * Outputs HTML representation of the links to stylesheets.
  76. *
  77. */
  78. public function output()
  79. {
  80. echo $this->render();
  81. }
  82. /**
  83. * Returns a string representation of this helper as HTML.
  84. *
  85. * @return string The HTML representation of the stylesheets
  86. */
  87. public function __toString()
  88. {
  89. return $this->render();
  90. }
  91. /**
  92. * Returns the canonical name of this helper.
  93. *
  94. * @return string The canonical name
  95. */
  96. public function getName()
  97. {
  98. return 'stylesheets';
  99. }
  100. }