ultimix
page_js.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
27 
38  var $JSFiles = array();
39 
50  var $Cache = false;
51  var $CachedMultyFS = false;
52  var $PageComposer = false;
53  var $Security = false;
54  var $String = false;
55  var $Tags = false;
56  var $Trace = false;
57 
68  function __construct()
69  {
70  try
71  {
72  $this->Cache = get_package( 'cache' , 'last' , __FILE__ );
73  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
74  $this->Security = get_package( 'security' , 'last' , __FILE__ );
75  $this->String = get_package( 'string' , 'last' , __FILE__ );
76  $this->Tags = get_package( 'string::tags' , 'last' , __FILE__ );
77  $this->Trace = get_package( 'trace' , 'last' , __FILE__ );
78  }
79  catch( Exception $e )
80  {
81  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
82  }
83  }
84 
111  private function scripts_join1( $Files , $Handle )
112  {
113  try
114  {
115  $RetFiles = array();
116 
117  foreach( $Files as $k => $v )
118  {
119  if( $v[ 'join' ] )
120  {
121  if( strpos( $v[ 'path' ] , '{http_host}/' ) === 0 )
122  {
123  $v[ 'path' ] = str_replace( '{http_host}/' , './' , $v[ 'path' ] );
124  }
125  $v[ 'path' ] = $this->Tags->compile_ultimix_tags( $v[ 'path' ] );
126  $Content = $this->CachedMultyFS->file_get_contents( $v[ 'path' ] );
127  fwrite( $Handle , "$Content\r\n" );
128  }
129  else
130  {
131  $RetFiles [] = array( 'path' => $v[ 'path' ] );
132  }
133  }
134 
135  return( $RetFiles );
136  }
137  catch( Exception $e )
138  {
139  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
140  }
141  }
142 
165  private function scripts_join2( $Files )
166  {
167  try
168  {
169  $RetFiles = array();
170 
171  foreach( $Files as $k => $v )
172  {
173  if( !$v[ 'join' ] )
174  {
175  $RetFiles [] = array( 'path' => $v[ 'path' ] );
176  }
177  }
178 
179  return( $RetFiles );
180  }
181  catch( Exception $e )
182  {
183  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
184  }
185  }
186 
209  private function join_compressed_scripts( $Files )
210  {
211  try
212  {
213  $FilesHash = md5( implode_ex( '' , $Files , 'path' ) );
214  $UnionFilePath = dirname( __FILE__ )."/tmp/$FilesHash.js";
215 
216  if( $this->CachedMultyFS->file_exists( $UnionFilePath ) === false ||
217  $this->Cache->get_data( $UnionFilePath ) === false )
218  {
219  $Handle = fopen( dirname( __FILE__ )."/tmp/$FilesHash.js" , "wb" );
220 
221  $RetFiles = $this->scripts_join1( $Files , $Handle );
222 
223  fclose( $Handle );
224  }
225  else
226  {
227  $RetFiles = $this->scripts_join2( $Files );
228  }
229 
230  return( $RetFiles );
231  }
232  catch( Exception $e )
233  {
234  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
235  }
236  }
237 
260  function join_scripts( $Files )
261  {
262  try
263  {
264  if( is_array( $Files ) && count( $Files ) > 0 )
265  {
266  $RetFiles = $this->join_compressed_scripts( $Files );
267 
268  $FilesHash = md5( implode_ex( '' , $Files , 'path' ) );
269 
270  $UnionFilePath = dirname( __FILE__ )."/tmp/$FilesHash.js";
271 
272  $RetFiles [] = array(
273  'path' => str_replace(
274  './' , '{http_host}/' , _get_package_relative_path( __FILE__ )."/tmp/$FilesHash.js"
275  )
276  );
277 
278  return( $RetFiles );
279  }
280  else
281  {
282  return( $Files );
283  }
284  }
285  catch( Exception $e )
286  {
287  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
288  }
289  }
290 
309  private function get_scripts_content()
310  {
311  try
312  {
313  $PageName = $this->Security->get_gp( 'page_name' , 'command' );
314 
315  if( $this->Cache->data_exists( "$PageName scripts" ) )
316  {
317  $Content = $this->Cache->get_data( "$PageName scripts" );
318  }
319  else
320  {
321  $this->JSFiles = $this->join_scripts( $this->JSFiles );
322  $Start = $this->CachedMultyFS->get_template( __FILE__ , 'script_link_start.tpl' );
323  $End = $this->CachedMultyFS->get_template( __FILE__ , 'script_link_end.tpl' );
324  $Content = $Start.implode_ex( $End.$Start , $this->JSFiles , 'path' ).$End;
325 
326  $this->Cache->add_data( "$PageName scripts" , $Content );
327  }
328 
329  return( $Content );
330  }
331  catch( Exception $e )
332  {
333  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
334  }
335  }
336 
359  function compile_scripts( &$Settings )
360  {
361  try
362  {
363  $Content = '';
364 
365  if( is_array( $this->JSFiles ) && count( $this->JSFiles ) )
366  {
367  $Content = $this->get_scripts_content();
368  }
369 
370  $this->JSFiles = array();
371 
372  return( $Content );
373  }
374  catch( Exception $e )
375  {
376  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
377  }
378  }
379 
402  function add_javascript( $Path , $Join = true )
403  {
404  try
405  {
406  if( $this->PageComposer === false )
407  {
408  $this->PageComposer = get_package( 'page::page_composer' , 'last' , __FILE__ );
409  }
410  $Path = $this->PageComposer->Template->compile_string( $Path );
411  foreach( $this->JSFiles as $k => $v )
412  {
413  if( $v[ 'path' ] == $Path )
414  {
415  return;
416  }
417  }
418  $this->JSFiles [] = array( 'path' => $Path , 'join' => $Join );
419  }
420  catch( Exception $e )
421  {
422  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
423  }
424  }
425  }
426 
427 ?>