functions.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 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.
  25. *
  26. * @param array|string $inputs Input strings
  27. * @param array|string $filters Filter names
  28. * @param array $options An array of options
  29. *
  30. * @return array An array of javascript URLs
  31. */
  32. function assetic_javascripts($inputs = array(), $filters = array(), array $options = array())
  33. {
  34. if (!isset($options['output'])) {
  35. $options['output'] = 'js/*.js';
  36. }
  37. return _assetic_urls($inputs, $filters, $options);
  38. }
  39. /**
  40. * Returns an array of stylesheet URLs.
  41. *
  42. * @param array|string $inputs Input strings
  43. * @param array|string $filters Filter names
  44. * @param array $options An array of options
  45. *
  46. * @return array An array of stylesheet URLs
  47. */
  48. function assetic_stylesheets($inputs = array(), $filters = array(), array $options = array())
  49. {
  50. if (!isset($options['output'])) {
  51. $options['output'] = 'css/*.css';
  52. }
  53. return _assetic_urls($inputs, $filters, $options);
  54. }
  55. /**
  56. * Returns an image URL.
  57. *
  58. * @param string $input An input
  59. * @param array|string $filters Filter names
  60. * @param array $options An array of options
  61. *
  62. * @return string An image URL
  63. */
  64. function assetic_image($input, $filters = array(), array $options = array())
  65. {
  66. if (!isset($options['output'])) {
  67. $options['output'] = 'images/*';
  68. }
  69. $urls = _assetic_urls($input, $filters, $options);
  70. return current($urls);
  71. }
  72. /**
  73. * Returns an array of asset urls.
  74. *
  75. * @param array|string $inputs Input strings
  76. * @param array|string $filters Filter names
  77. * @param array $options An array of options
  78. *
  79. * @return array An array of URLs
  80. */
  81. function _assetic_urls($inputs = array(), $filters = array(), array $options = array())
  82. {
  83. global $_assetic;
  84. if (!is_array($inputs)) {
  85. $inputs = array_filter(array_map('trim', explode(',', $inputs)));
  86. }
  87. if (!is_array($filters)) {
  88. $filters = array_filter(array_map('trim', explode(',', $filters)));
  89. }
  90. $coll = $_assetic->factory->createAsset($inputs, $filters, $options);
  91. $debug = isset($options['debug']) ? $options['debug'] : $_assetic->factory->isDebug();
  92. $combine = isset($options['combine']) ? $options['combine'] : !$debug;
  93. $one = $coll->getTargetPath();
  94. if ($combine) {
  95. $many = array();
  96. foreach ($coll as $leaf) {
  97. $many[] = $leaf->getTargetPath();
  98. }
  99. } else {
  100. $many = array($one);
  101. }
  102. return new TraversableString($one, $many);
  103. }