/src/functions.php
https://github.com/t7systems/assetic · PHP · 174 lines · 86 code · 21 blank · 67 comment · 11 complexity · 3664ae8c5f468e7b983515c5c00a0953 MD5 · raw file
- <?php
- /*
- * This file is part of the Assetic package, an OpenSky project.
- *
- * (c) 2010-2011 OpenSky Project Inc
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- use Assetic\Factory\AssetFactory;
- use Assetic\Util\TraversableString;
- /**
- * Initializes the global Assetic object.
- *
- * @param AssetFactory $factory The asset factory
- */
- function assetic_init(AssetFactory $factory)
- {
- global $_assetic;
- $_assetic = new stdClass();
- $_assetic->factory = $factory;
- }
- /**
- * Returns an array of javascript URLs or
- * @param bool $debug If debug is true, this function will return a set of html script elements with all js files inside as src.
- * @param array $inputs Input strings
- * @param array $filters Filter names
- * @param array $options An array of options
- * @return array | string
- * @author David
- */
- function auto_assetic_javascripts($debug = false, $inputs = array(), $filters = array(), array $options = array())
- {
- if (!$debug)
- {
- return "<script src=\"".assetic_javascripts($inputs, $filters, $options)."\"></script>\n";
- }
- $s = '';
- if (is_array($inputs))
- {
- foreach ($inputs as $input)
- {
- $s .= "<script src=\"".$input."\"></script>\n";
- }
- }
- return $s;
- }
- /**
- * Returns an array of javascript URLs.
- *
- * @param array|string $inputs Input strings
- * @param array|string $filters Filter names
- * @param array $options An array of options
- *
- * @return array An array of javascript URLs
- */
- function assetic_javascripts($inputs = array(), $filters = array(), array $options = array())
- {
- if (!isset($options['output'])) {
- $options['output'] = 'js/*.js';
- }
- return _assetic_urls($inputs, $filters, $options);
- }
- /**
- * Returns an array of stylesheet URLs or
- * @param bool $debug If debug is true, this function will return a set of html link elements with all javascripts inside.
- * @param array $inputs Input strings
- * @param array $filters Filter names
- * @param array $options An array of options
- * @return array | string
- * @author David
- */
- function auto_assetic_stylesheets($debug = false, $inputs = array(), $filters = array(), array $options = array())
- {
- if (!$debug)
- {
- return "<link rel=\"stylesheet\" href=\"".assetic_javascripts($inputs, $filters, $options)."\" />\n";
- }
- $s = '';
- if (is_array($inputs))
- {
- foreach ($inputs as $input)
- {
- $s .= "<link rel=\"stylesheet\" href=\"".$input."\" />\n";
- }
- }
- return $s;
- }
- /**
- * Returns an array of stylesheet URLs.
- *
- * @param array|string $inputs Input strings
- * @param array|string $filters Filter names
- * @param array $options An array of options
- *
- * @return array An array of stylesheet URLs
- */
- function assetic_stylesheets($inputs = array(), $filters = array(), array $options = array())
- {
- if (!isset($options['output'])) {
- $options['output'] = 'css/*.css';
- }
- return _assetic_urls($inputs, $filters, $options);
- }
- /**
- * Returns an image URL.
- *
- * @param string $input An input
- * @param array|string $filters Filter names
- * @param array $options An array of options
- *
- * @return string An image URL
- */
- function assetic_image($input, $filters = array(), array $options = array())
- {
- if (!isset($options['output'])) {
- $options['output'] = 'images/*';
- }
- $urls = _assetic_urls($input, $filters, $options);
- return current($urls);
- }
- /**
- * Returns an array of asset urls.
- *
- * @param array|string $inputs Input strings
- * @param array|string $filters Filter names
- * @param array $options An array of options
- *
- * @return array An array of URLs
- */
- function _assetic_urls($inputs = array(), $filters = array(), array $options = array())
- {
- global $_assetic;
- if (!is_array($inputs)) {
- $inputs = array_filter(array_map('trim', explode(',', $inputs)));
- }
- if (!is_array($filters)) {
- $filters = array_filter(array_map('trim', explode(',', $filters)));
- }
- $coll = $_assetic->factory->createAsset($inputs, $filters, $options);
- $debug = isset($options['debug']) ? $options['debug'] : $_assetic->factory->isDebug();
- $combine = isset($options['combine']) ? $options['combine'] : !$debug;
- $one = $coll->getTargetPath();
- if ($combine) {
- $many = array();
- foreach ($coll as $leaf) {
- $many[] = $leaf->getTargetPath();
- }
- } else {
- $many = array($one);
- }
- return new TraversableString($one, $many);
- }