コード例 #1
0
/**
 * Returns <script> tags for all javascripts configured in view.yml or added to the response object.
 *
 * You can use this helper to decide the location of javascripts in pages.
 * By default, if you don't call this helper, symfony will automatically include javascripts before </head>.
 * Calling this helper disables this behavior.
 *
 * @return string <script> tags
 */
function get_combined_javascripts()
{
    if (!sfConfig::get('app_sfCombinePlugin_enabled', false)) {
        return get_javascripts();
    }
    sfConfig::set('symfony.asset.javascripts_included', true);
    $html = '';
    $jsFiles = array();
    $regularJsFiles = array();
    $response = sfContext::getInstance()->getResponse();
    $config = sfConfig::get('app_sfCombinePlugin_js', array());
    $doNotCombine = isset($config['combine_skip']) ? $config['combine_skip'] : array();
    foreach ($response->getJavascripts() as $files => $options) {
        if (!is_array($files)) {
            $files = array($files);
        }
        // check for js files that should not be combined
        foreach ($files as $key => $value) {
            if (skip_asset($value, $doNotCombine)) {
                array_push($regularJsFiles, $value);
                unset($files[$key]);
            }
        }
        $jsFiles = array_merge($jsFiles, $files);
    }
    if (!empty($jsFiles)) {
        $html .= str_replace(array('.js', '.pjs'), '', javascript_include_tag(url_for('sfCombine/js?key=' . _get_key($jsFiles))));
    }
    foreach ($regularJsFiles as $file) {
        $file = javascript_path($file);
        $html .= javascript_include_tag($file);
    }
    return $html;
}
コード例 #2
0
 /**
  * Processes the assets corresponding to a hash
  * 
  * @param string $key Key to a list of asset files in the `sf_combine` table
  *
  * @return string Processed Javascript code
  */
 public function process($key)
 {
     $files = $this->getContents($key);
     $config = sfConfig::get('app_sfCombinePlugin_js', array());
     if (isset($config['minify']) && $config['minify']) {
         // minification
         $skipMinify = isset($config['minify_skip']) ? $config['minify_skip'] : array();
         foreach ($files as $filePath => $content) {
             if (!skip_asset($filePath, $skipMinify)) {
                 $files[$filePath] = $this->minify($content);
             }
         }
     }
     // packing
     if (isset($config['pack']) && $config['pack']) {
         $skipPack = isset($config['pack_skip']) ? $config['pack_skip'] : array();
         if (!$skipPack) {
             // simple: pack everything together
             $finalContent = $this->pack($this->merge($files));
         } else {
             // less simple: pack groups of files, avoiding the ones that should not be packed
             $finalContent = '';
             $toProcess = '';
             foreach ($files as $filePath => $content) {
                 if (skip_asset($filePath, $skipPack)) {
                     $finalContent .= $this->pack($toProcess);
                     $finalContent .= $content;
                     $toProcess = '';
                 } else {
                     $toProcess .= $content;
                 }
             }
             if ($toProcess) {
                 $packer = new JavaScriptPacker($toProcess, 'Normal', true, false);
                 $finalContent .= $packer->pack();
             }
         }
     } else {
         // no packing at all, simply merge
         $includeComment = isset($config['minify']) ? !$config['minify'] : true;
         $finalContent = $this->merge($files, $includeComment);
     }
     return $finalContent;
 }