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