Exemplo n.º 1
0
function _a_get_assets_body($type, $assets)
{
    $gzip = sfConfig::get('app_a_minify_gzip', false);
    sfConfig::set('symfony.asset.' . $type . '_included', true);
    $html = '';
    // We need our own copy of the trivial case here because we rewrote the asset list
    // for stylesheets after LESS compilation, and there is no way to
    // reset the list in the response object
    if (!sfConfig::get('app_a_minify', false)) {
        // This branch is seen only for CSS, because javascript calls the original Symfony
        // functionality when minify is off
        foreach ($assets as $file => $options) {
            $html .= stylesheet_tag($file, $options);
        }
        return $html;
    }
    $sets = array();
    foreach ($assets as $file => $options) {
        if (preg_match('/^http(s)?:/', $file) || isset($options['data-minify']) && $options['data-minify'] === 0) {
            // Nonlocal URL or minify was explicitly shut off.
            // Don't get cute with it, otherwise things
            // like Addthis and ckeditor don't work
            if ($type === 'stylesheets') {
                $html .= stylesheet_tag($file, $options);
            } else {
                $html .= javascript_include_tag($file, $options);
            }
            continue;
        }
        /*
         *
         * Guts borrowed from stylesheet_tag and javascript_tag. We still do a tag if it's
         * a conditional stylesheet
         *
         */
        $absolute = false;
        if (isset($options['absolute']) && $options['absolute']) {
            unset($options['absolute']);
            $absolute = true;
        }
        $condition = null;
        if (isset($options['condition'])) {
            $condition = $options['condition'];
            unset($options['condition']);
        }
        if (!isset($options['raw_name'])) {
            if ($type === 'stylesheets') {
                $file = stylesheet_path($file, $absolute);
            } else {
                $file = javascript_path($file, $absolute);
            }
        } else {
            unset($options['raw_name']);
        }
        if (is_null($options)) {
            $options = array();
        }
        if ($type === 'stylesheets') {
            $options = array_merge(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'screen', 'href' => $file), $options);
        } else {
            $options = array_merge(array('type' => 'text/javascript', 'src' => $file), $options);
        }
        if (null !== $condition) {
            $tag = tag('link', $options);
            $tag = comment_as_conditional($condition, $tag);
            $html .= $tag . "\n";
        } else {
            unset($options['href'], $options['src']);
            $optionGroupKey = json_encode($options);
            $set[$optionGroupKey][] = $file;
        }
        // echo($file);
        // $html .= "<style>\n";
        // $html .= file_get_contents(sfConfig::get('sf_web_dir') . '/' . $file);
        // $html .= "</style>\n";
    }
    // CSS files with the same options grouped together to be loaded together
    foreach ($set as $optionsJson => $files) {
        $groupFilename = aAssets::getGroupFilename($files);
        $groupFilename .= $type === 'stylesheets' ? '.css' : '.js';
        if ($gzip) {
            $groupFilename .= 'gz';
        }
        $dir = aFiles::getUploadFolder(array('asset-cache'));
        if (!file_exists($dir . '/' . $groupFilename)) {
            $content = '';
            foreach ($files as $file) {
                $path = null;
                if (sfConfig::get('app_a_stylesheet_cache_http', false)) {
                    $url = sfContext::getRequest()->getUriPrefix() . $file;
                    $fileContent = file_get_contents($url);
                } else {
                    $path = sfConfig::get('sf_web_dir') . $file;
                    $fileContent = file_get_contents($path);
                }
                if ($type === 'stylesheets') {
                    $options = array();
                    if (!is_null($path)) {
                        // Rewrite relative URLs in CSS files.
                        // This trick is available only when we don't insist on
                        // pulling our CSS files via http rather than the filesystem
                        // dirname would resolve symbolic links, we don't want that
                        $fdir = preg_replace('/\\/[^\\/]*$/', '', $path);
                        $options['currentDir'] = $fdir;
                        $options['docRoot'] = sfConfig::get('sf_web_dir');
                    }
                    if (sfConfig::get('app_a_minify', false)) {
                        $fileContent = Minify_CSS::minify($fileContent, $options);
                    }
                } else {
                    // Trailing carriage return makes behavior more consistent with
                    // JavaScript's behavior when loading separate files. For instance,
                    // a missing trailing semicolon should be tolerated to the same
                    // degree it would be with separate files. The minifier is not
                    // a lint tool and should not surprise you with breakage
                    $fileContent = JSMin::minify($fileContent) . "\n";
                }
                $content .= $fileContent;
            }
            if ($gzip) {
                _gz_file_put_contents($dir . '/' . $groupFilename . '.tmp', $content);
            } else {
                file_put_contents($dir . '/' . $groupFilename . '.tmp', $content);
            }
            @rename($dir . '/' . $groupFilename . '.tmp', $dir . '/' . $groupFilename);
        }
        $options = json_decode($optionsJson, true);
        // Use stylesheet_path and javascript_path so we can respect relative_root_dir
        if ($type === 'stylesheets') {
            $options['href'] = stylesheet_path(sfConfig::get('app_a_assetCacheUrl', '/uploads/asset-cache') . '/' . $groupFilename);
            $html .= tag('link', $options);
        } else {
            $options['src'] = javascript_path(sfConfig::get('app_a_assetCacheUrl', '/uploads/asset-cache') . '/' . $groupFilename);
            $html .= content_tag('script', '', $options);
        }
    }
    return $html;
}
Exemplo n.º 2
0
/**
 * Returns a css <link> tag per source given as argument,
 * to be included in the <head> section of a HTML document.
 *
 * <b>Options:</b>
 * - rel - defaults to 'stylesheet'
 * - type - defaults to 'text/css'
 * - media - defaults to 'screen'
 *
 * <b>Examples:</b>
 * <code>
 *  echo stylesheet_tag('style');
 *    => <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
 *  echo stylesheet_tag('style', array('media' => 'all'));
 *    => <link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />
 *  echo stylesheet_tag('style', array('raw_name' => true));
 *    => <link href="style" media="all" rel="stylesheet" type="text/css" />
 *  echo stylesheet_tag('random.styles', '/css/stylish');
 *    => <link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" />
 *       <link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />
 * </code>
 *
 * @param string asset names
 * @param array  additional HTML compliant <link> tag parameters
 *
 * @return string XHTML compliant <link> tag(s)
 * @see    stylesheet_path
 */
function stylesheet_tag()
{
    $sources = func_get_args();
    $sourceOptions = func_num_args() > 1 && is_array($sources[func_num_args() - 1]) ? array_pop($sources) : array();
    $html = '';
    foreach ($sources as $source) {
        $absolute = false;
        if (isset($sourceOptions['absolute'])) {
            unset($sourceOptions['absolute']);
            $absolute = true;
        }
        $condition = null;
        if (isset($sourceOptions['condition'])) {
            $condition = $sourceOptions['condition'];
            unset($sourceOptions['condition']);
        }
        if (!isset($sourceOptions['raw_name'])) {
            $source = stylesheet_path($source, $absolute);
        } else {
            unset($sourceOptions['raw_name']);
        }
        $options = array_merge(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'screen', 'href' => $source), $sourceOptions);
        $tag = tag('link', $options);
        if (!is_null($condition)) {
            $tag = comment_as_conditional($condition, $tag);
        }
        $html .= $tag . "\n";
    }
    return $html;
}
Exemplo n.º 3
0
/**
 * Returns a css <link> tag per source given as argument,
 * to be included in the <head> section of a HTML document.
 *
 * <b>Options:</b>
 * - rel - defaults to 'stylesheet'
 * - type - defaults to 'text/css'
 * - media - defaults to 'screen'
 *
 * <b>Examples:</b>
 * <code>
 *  echo stylesheet_tag('style');
 *    => <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
 *  echo stylesheet_tag('style', array('media' => 'all'));
 *    => <link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />
 *  echo stylesheet_tag('style', array('raw_name' => true));
 *    => <link href="style" media="all" rel="stylesheet" type="text/css" />
 *  echo stylesheet_tag('random.styles', '/css/stylish');
 *    => <link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" />
 *       <link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />
 * </code>
 *
 * @param string asset names
 * @param array  additional HTML compliant <link> tag parameters
 *
 * @return string XHTML compliant <link> tag(s)
 * @see    stylesheet_path
 */
function stylesheet_tag($source, $options = array())
{
    if (!is_array($options)) {
        $options = array();
    }
    $options += array('absolute' => false);
    if (empty($options['raw_name'])) {
        $source = stylesheet_path($source, $options['absolute']);
    }
    $tag = tag('link', array_diff_key($options + array('href' => $source, 'media' => 'screen', 'rel' => 'stylesheet', 'type' => 'text/css'), array('absolute' => 'abosolute', 'condition' => 'condition', 'raw_name' => 'raw_name')));
    if (isset($options['condition'])) {
        $tag = comment_as_conditional($options['condition'], $tag);
    }
    return $tag . "\n";
}