Changeset 8012


Ignore:
Timestamp:
Dec 6, 2010, 9:52:44 PM (14 years ago)
Author:
rvelices
Message:

combined script now really merges files
added option $conftemplate_combine_files by default true to enable/disable file combining

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/themes/default/template/element_set_global.tpl

    r7995 r8012  
    44{footer_script}{literal}
    55        pwg_initialization_datepicker("#date_creation_day", "#date_creation_month", "#date_creation_year", "#date_creation_linked_date", "#date_creation_action_set");
    6 </script>
    76{/literal}{/footer_script}
    87
  • trunk/include/config_default.inc.php

    r7501 r8012  
    480480// production environment.
    481481$conf['template_force_compile'] = false;
     482
     483// activate merging of javascript / css files
     484$conf['template_combine_files'] = true;
    482485
    483486// this permit to show the php errors reporting (see INI 'error_reporting'
  • trunk/include/template.class.php

    r8008 r8012  
    397397          $scripts = $this->scriptLoader->get_head_scripts();
    398398          $content = array();
    399           foreach ($scripts as $id => $script)
     399          foreach ($scripts as $script)
    400400          {
    401401              $content[]=
     
    412412    {
    413413      ksort($this->css_by_priority);
    414       $combiner = new FileCombiner('css');
    415       foreach ($this->css_by_priority as $files)
    416       {
    417         foreach ($files as $file_ver)
    418         {
    419           $combiner->add( $file_ver[0], $file_ver[1] );
    420         }
    421       }
    422       if ( $combiner->combine( $out_file, $out_version) )
    423       {
    424         $href = get_root_url() . $out_file;
    425         if ($out_version !== false)
    426           $href .= '?v' . ($out_version ? $out_version : PHPWG_VERSION);
     414
     415                        global $conf;
     416                        $css = array();
     417                        if ($conf['template_combine_files'])
     418                        {
     419                                $combiner = new FileCombiner('css');
     420                                foreach ($this->css_by_priority as $files)
     421                                {
     422                                        foreach ($files as $file_ver)
     423                                                $combiner->add( $file_ver[0], $file_ver[1] );
     424                                }
     425                                if ( $combiner->combine( $out_file, $out_version) )
     426                                        $css[] = array($out_file, $out_version);
     427                        }
     428                        else
     429                        {
     430                                foreach ($this->css_by_priority as $files)
     431                                        $css = array_merge($css, $files);
     432                        }
     433                       
     434                        $content = array();
     435      foreach( $css as $file_ver )
     436      {
     437        $href = get_root_url() . $file_ver[0];
     438        if ($file_ver[1] !== false)
     439          $href .= '?v' . ($file_ver[1] ? $file_ver[1] : PHPWG_VERSION);
    427440        // trigger the event for eventual use of a cdn
    428         $href = trigger_event('combined_css', $href, $out_file, $out_version);
    429         $this->output = str_replace(self::COMBINED_CSS_TAG,
    430           '<link rel="stylesheet" type="text/css" href="'.$href.'">',
     441        $href = trigger_event('combined_css', $href, $file_ver[0], $file_ver[1]);
     442                                $content[] = '<link rel="stylesheet" type="text/css" href="'.$href.'">';
     443      }
     444      $this->output = str_replace(self::COMBINED_CSS_TAG,
     445          implode( "\n", $content ),
    431446          $this->output );
    432       }
    433447    }
    434448
     
    569583    else
    570584    {
    571       if (!$this->scriptLoader->did_head())
    572         fatal_error('get_combined_scripts didn\'t call header');
    573585      $scripts = $this->scriptLoader->get_footer_scripts();
    574       foreach ($scripts[0] as $id => $script)
     586      foreach ($scripts[0] as $script)
    575587      {
    576588        $content[]=
    577589          '<script type="text/javascript" src="'
    578           . Template::make_script_src($script)
     590          . self::make_script_src($script)
    579591          .'"></script>';
    580592      }
     
    596608          $content[]=
    597609            's=document.createElement(\'script\'); s.type = \'text/javascript\'; s.async = true; s.src = \''
    598             . Template::make_script_src($script)
     610            . self::make_script_src($script)
    599611            .'\';';
    600612          $content[]= 'after = after.parentNode.insertBefore(s, after);';
     
    611623  {
    612624    $ret = '';
    613     if ( url_is_remote($script->path) )
     625    if ( $script->is_remote() )
    614626      $ret = $script->path;
    615627    else
     
    621633      }
    622634    }
     635    // trigger the event for eventual use of a cdn
     636    $ret = trigger_event('combined_script', $ret, $script);
    623637    return $ret;
    624638  }
     
    639653    $version = isset($params['version']) ? $params['version'] : 0;
    640654    $this->css_by_priority[$order][] = array( $params['path'], $version);
    641     //var_export( $this->css_by_priority ); echo "<br>";
    642655  }
    643656
     
    830843  public $extra = array();
    831844
    832   function Script($id, $load_mode, $precedents, $path, $version)
     845  function Script($load_mode, $id, $path, $version, $precedents)
    833846  {
    834847    $this->id = $id;
    835848    $this->load_mode = $load_mode;
     849    $this->id = $id;
     850    $this->set_path($path);
     851    $this->version = $version;
    836852    $this->precedents = $precedents;
    837     $this->path = $path;
    838     $this->version = $version;
    839853  }
    840854
     
    844858      $this->path = $path;
    845859  }
    846  
     860
    847861  function is_remote()
    848862  {
     
    902916    if (! isset( $this->registered_scripts[$id] ) )
    903917    {
    904       $script = new Script($id, $load_mode, $require, $path, $version);
     918      $script = new Script($load_mode, $id, $path, $version, $require);
    905919      self::fill_well_known($id, $script);
    906920      $this->registered_scripts[$id] = $script;
     
    946960    }
    947961    $this->did_head = true;
    948     return $this->head_done_scripts;
     962    return self::do_combine($this->head_done_scripts, 0);
    949963  }
    950964
    951965  function get_footer_scripts()
    952966  {
    953     if (!$this->did_head)
     967    /*if (!$this->did_head)
    954968    {
    955969      trigger_error("Attempt to write footer scripts without header scripts", E_USER_ERROR );
    956     }
    957    
     970    }*/
    958971    $todo = array();
    959972    foreach( $this->registered_scripts as $id => $script)
     
    971984    uasort($todo, array('ScriptLoader', 'cmp_by_mode_and_order'));
    972985
    973    
    974986    $result = array( array(), array() );
    975987    foreach( $todo as $id => $script)
     
    977989      $result[$script->load_mode-1][$id] = $script;
    978990    }
    979     return $result;
    980   }
    981 
     991    return array( self::do_combine($result[0],1), self::do_combine($result[1],2) );
     992  }
     993
     994        private static function do_combine($scripts, $load_mode)
     995        {
     996                global $conf;
     997                if (count($scripts)<2 or !$conf['template_combine_files'])
     998                        return $scripts;
     999                $combiner = new FileCombiner('js');
     1000                foreach ($scripts as $script)
     1001                {
     1002                        if ($script->is_remote()) fatal_error("NOT IMPLEMENTED");// TODO - we cannot combine remote scripts
     1003                        $combiner->add( $script->path, $script->version );
     1004                }
     1005                if ( $combiner->combine( $out_file, $out_version) )
     1006                {
     1007                        return array( 'combi' => new Script($load_mode, 'combi', $out_file, $out_version, array() ) );
     1008                }
     1009                return null;
     1010        }
     1011       
     1012        // checks that if B depends on A, then B->load_mode >= A->load_mode in order to respect execution order
    9821013  private static function check_load_dep($scripts)
    9831014  {
     1015                global $conf;
    9841016    do
    9851017    {
     
    9991031            $changed = true;
    10001032          }
    1001           if ($load==2 && $scripts[$precedent]->load_mode==2 && $script->is_remote() )
     1033          if ($load==2 && $scripts[$precedent]->load_mode==2 && ($script->is_remote() or !$conf['template_combine_files']) )
    10021034          {// we are async -> a predecessor cannot be async unlesss it can be merged; otherwise script execution order is not guaranteed
    10031035            $scripts[$precedent]->load_mode = 1;
     
    10091041    while ($changed);
    10101042  }
    1011  
    1012  
     1043
     1044
    10131045  private static function fill_well_known($id, $script)
    10141046  {
     
    10491081    $ret = $s1->load_mode - $s2->load_mode;
    10501082    if ($ret) return $ret;
    1051    
     1083
    10521084    $ret = $s1->extra['order'] - $s2->extra['order'];
    10531085    if ($ret) return $ret;
    1054    
     1086
    10551087    if ($s1->extra['order']==0 and ($s1->is_remote() xor $s2->is_remote()) )
    10561088    {
     
    10741106    $this->type = $type;
    10751107  }
    1076  
     1108
    10771109  static function clear_combined_files()
    10781110  {
     
    11001132  function combine(&$out_file, &$out_version)
    11011133  {
    1102     //var_export($this);
    11031134    if (count($this->files) == 0)
    11041135    {
     
    11401171      $output .= "/* BEGIN $input_file */\n";
    11411172      if ($this->type == "css")
    1142       {
    1143         $output .= $this->process_css($input_file);
    1144       }
     1173        $output .= self::process_css($input_file);
    11451174      else
    1146         $output .= file_get_contents(PHPWG_ROOT_PATH . $input_file);
     1175                                $output .= self::process_js($input_file);
    11471176      $output .= "\n";
    11481177    }
     
    11551184  }
    11561185
    1157   private function process_css($file)
     1186        private static function process_js($file)
     1187        {
     1188                $js = file_get_contents(PHPWG_ROOT_PATH . $file);
     1189                if (strpos($file, '.min')===false and strpos($file, '.packed')===false )
     1190                {
     1191                        //TODO minify javascript with some php lib from www...
     1192                }
     1193                return $js;
     1194        }
     1195       
     1196  private static function process_css($file)
    11581197  {
    11591198    static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#";
     
    11811220      {
    11821221        $search[] = $match[0];
    1183         $replace[] = $this->process_css(dirname($file) . "/$match[1]");
     1222        $replace[] = self::process_css(dirname($file) . "/$match[1]");
    11841223      }
    11851224      $css = str_replace($search, $replace, $css);
  • trunk/plugins/admin_multi_view/controller.php

    r6358 r8012  
    1212  pwg_unset_session_var( 'multiview_debug_l10n' );
    1313  pwg_unset_session_var( 'multiview_debug_template' );
     14        pwg_unset_session_var( 'multiview_template_combine_files' );
    1415  pwg_unset_session_var( 'multiview_no_history' );
    1516?>
     
    8586  $refresh_main = true;
    8687}
     88
     89if ( isset($_GET['template_combine_files']) )
     90{
     91  if ( $_GET['template_combine_files']==0 )
     92    pwg_set_session_var( 'multiview_template_combine_files', 0 );
     93  else
     94    pwg_unset_session_var( 'multiview_template_combine_files' );
     95  $refresh_main = true;
     96}
     97
    8798
    8899if ( isset($_GET['no_history']) )
     
    195206
    196207// +-----------------------------------------------------------------------+
     208// | template combine files                                                |
     209$template_combine_files_html='';
     210if ($conf['template_combine_files'])
     211{
     212  if ( pwg_get_session_var( 'multiview_template_combine_files', 1 ) )
     213    $template_combine_files_html.='<a href="'.$my_url.'?template_combine_files=0">Don\'t combine js&amp;css</a>';
     214  else
     215    $template_combine_files_html.='<a href="'.$my_url.'?template_combine_files=1">Combine js&amp;css</a>';
     216}
     217
     218// +-----------------------------------------------------------------------+
    197219// | no history                                                            |
    198220$no_history_html='';
     
    225247<tr><td>Lang</td><td><?php echo $lang_html; ?></td></tr>
    226248</table>
    227 <?php echo implode( "<br/>\n", array($show_queries_html, $debug_l10n_html, $debug_template_html, $no_history_html) ); ?>
     249<?php echo implode( "<br/>\n", array($show_queries_html, $debug_l10n_html, $debug_template_html, $template_combine_files_html, $no_history_html) ); ?>
    228250
    229251<script type="text/javascript">
  • trunk/plugins/admin_multi_view/is_admin.inc.php

    r5128 r8012  
    2626  if (pwg_get_session_var( 'multiview_debug_template', 0 ))
    2727    $conf['debug_template'] = true;
    28   if (pwg_get_session_var( 'multiview_no_history', 0 ))
     28  if (!pwg_get_session_var( 'multiview_template_combine_files', 1 ))
     29    $conf['template_combine_files'] = false;
     30                if (pwg_get_session_var( 'multiview_no_history', 0 ))
    2931  {
    3032    add_event_handler( 'pwg_log_allowed', create_function( '', 'return false;' ) );
Note: See TracChangeset for help on using the changeset viewer.