/src/functions.php

https://github.com/t7systems/assetic · PHP · 174 lines · 86 code · 21 blank · 67 comment · 11 complexity · 3664ae8c5f468e7b983515c5c00a0953 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Assetic\Factory\AssetFactory;
  11. use Assetic\Util\TraversableString;
  12. /**
  13. * Initializes the global Assetic object.
  14. *
  15. * @param AssetFactory $factory The asset factory
  16. */
  17. function assetic_init(AssetFactory $factory)
  18. {
  19. global $_assetic;
  20. $_assetic = new stdClass();
  21. $_assetic->factory = $factory;
  22. }
  23. /**
  24. * Returns an array of javascript URLs or
  25. * @param bool $debug If debug is true, this function will return a set of html script elements with all js files inside as src.
  26. * @param array $inputs Input strings
  27. * @param array $filters Filter names
  28. * @param array $options An array of options
  29. * @return array | string
  30. * @author David
  31. */
  32. function auto_assetic_javascripts($debug = false, $inputs = array(), $filters = array(), array $options = array())
  33. {
  34. if (!$debug)
  35. {
  36. return "<script src=\"".assetic_javascripts($inputs, $filters, $options)."\"></script>\n";
  37. }
  38. $s = '';
  39. if (is_array($inputs))
  40. {
  41. foreach ($inputs as $input)
  42. {
  43. $s .= "<script src=\"".$input."\"></script>\n";
  44. }
  45. }
  46. return $s;
  47. }
  48. /**
  49. * Returns an array of javascript URLs.
  50. *
  51. * @param array|string $inputs Input strings
  52. * @param array|string $filters Filter names
  53. * @param array $options An array of options
  54. *
  55. * @return array An array of javascript URLs
  56. */
  57. function assetic_javascripts($inputs = array(), $filters = array(), array $options = array())
  58. {
  59. if (!isset($options['output'])) {
  60. $options['output'] = 'js/*.js';
  61. }
  62. return _assetic_urls($inputs, $filters, $options);
  63. }
  64. /**
  65. * Returns an array of stylesheet URLs or
  66. * @param bool $debug If debug is true, this function will return a set of html link elements with all javascripts inside.
  67. * @param array $inputs Input strings
  68. * @param array $filters Filter names
  69. * @param array $options An array of options
  70. * @return array | string
  71. * @author David
  72. */
  73. function auto_assetic_stylesheets($debug = false, $inputs = array(), $filters = array(), array $options = array())
  74. {
  75. if (!$debug)
  76. {
  77. return "<link rel=\"stylesheet\" href=\"".assetic_javascripts($inputs, $filters, $options)."\" />\n";
  78. }
  79. $s = '';
  80. if (is_array($inputs))
  81. {
  82. foreach ($inputs as $input)
  83. {
  84. $s .= "<link rel=\"stylesheet\" href=\"".$input."\" />\n";
  85. }
  86. }
  87. return $s;
  88. }
  89. /**
  90. * Returns an array of stylesheet URLs.
  91. *
  92. * @param array|string $inputs Input strings
  93. * @param array|string $filters Filter names
  94. * @param array $options An array of options
  95. *
  96. * @return array An array of stylesheet URLs
  97. */
  98. function assetic_stylesheets($inputs = array(), $filters = array(), array $options = array())
  99. {
  100. if (!isset($options['output'])) {
  101. $options['output'] = 'css/*.css';
  102. }
  103. return _assetic_urls($inputs, $filters, $options);
  104. }
  105. /**
  106. * Returns an image URL.
  107. *
  108. * @param string $input An input
  109. * @param array|string $filters Filter names
  110. * @param array $options An array of options
  111. *
  112. * @return string An image URL
  113. */
  114. function assetic_image($input, $filters = array(), array $options = array())
  115. {
  116. if (!isset($options['output'])) {
  117. $options['output'] = 'images/*';
  118. }
  119. $urls = _assetic_urls($input, $filters, $options);
  120. return current($urls);
  121. }
  122. /**
  123. * Returns an array of asset urls.
  124. *
  125. * @param array|string $inputs Input strings
  126. * @param array|string $filters Filter names
  127. * @param array $options An array of options
  128. *
  129. * @return array An array of URLs
  130. */
  131. function _assetic_urls($inputs = array(), $filters = array(), array $options = array())
  132. {
  133. global $_assetic;
  134. if (!is_array($inputs)) {
  135. $inputs = array_filter(array_map('trim', explode(',', $inputs)));
  136. }
  137. if (!is_array($filters)) {
  138. $filters = array_filter(array_map('trim', explode(',', $filters)));
  139. }
  140. $coll = $_assetic->factory->createAsset($inputs, $filters, $options);
  141. $debug = isset($options['debug']) ? $options['debug'] : $_assetic->factory->isDebug();
  142. $combine = isset($options['combine']) ? $options['combine'] : !$debug;
  143. $one = $coll->getTargetPath();
  144. if ($combine) {
  145. $many = array();
  146. foreach ($coll as $leaf) {
  147. $many[] = $leaf->getTargetPath();
  148. }
  149. } else {
  150. $many = array($one);
  151. }
  152. return new TraversableString($one, $many);
  153. }