ultimix
page_css.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 $CSSFiles = 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 
107  private function compress_stylesheets( $Files )
108  {
109  try
110  {
111  $Compressed = array();
112 
113  foreach( $Files as $k => $v )
114  {
115  $v = $this->String->clear_ultimix_tags( $v );
116  if( $v[ 'join' ] )
117  {
118  $Compressed[ 'join' ] [] = $v[ 'path' ];
119  }
120  else
121  {
122  $Key = str_replace( '././' , './' , dirname( $v[ 'path' ] ) );
123  if( isset( $Compressed[ $Key ] ) == false )
124  {
125  $Compressed[ $Key ] = array();
126  }
127  $Compressed[ $Key ] [] = basename( $v[ 'path' ] );
128  }
129  }
130 
131  return( $Compressed );
132  }
133  catch( Exception $e )
134  {
135  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
136  }
137  }
138 
161  private function fill_file( $FileName , $Files )
162  {
163  try
164  {
165  $Handle = fopen( dirname( __FILE__ )."/tmp/$FileName.css" , "wb" );
166 
167  foreach( $Files as $k2 => $v2 )
168  {
169  $Content = $this->CachedMultyFS->file_get_contents( "$v2" );
170  fwrite( $Handle , $Content );
171  fwrite( $Handle , "\r\n" );
172  }
173 
174  fclose( $Handle );
175  }
176  catch( Exception $e )
177  {
178  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
179  }
180  }
181 
208  private function stylesheets_join1( $Files , $RetFiles )
209  {
210  try
211  {
212  $FilesHash = md5( implode_ex( '' , $Files , 'path' ) );
213  $UnionFilePath = dirname( __FILE__ )."/tmp/$FilesHash.css";
214 
215  if( $this->CachedMultyFS->file_exists( $UnionFilePath ) === false ||
216  $this->Cache->get_data( $UnionFilePath ) === false )
217  {
218  $this->fill_file( $FilesHash , $Files );
219 
220  $RetFiles [] = array(
221  'path' => _get_package_relative_path( __FILE__ )."/tmp/$FilesHash.css"
222  );
223  }
224  else
225  {
226  $RetFiles [] = array(
227  'path' => _get_package_relative_path( __FILE__ )."/tmp/$FilesHash.css"
228  );
229  }
230 
231  return( $RetFiles );
232  }
233  catch( Exception $e )
234  {
235  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
236  }
237  }
238 
269  private function stylesheets_join3( $Path , $Files , $RetFiles )
270  {
271  try
272  {
273  $FilesHash = md5( implode( '' , $Files ) );
274  $UnionFilePath = "$Path/$FilesHash.css";
275 
276  if( $this->CachedMultyFS->file_exists( $UnionFilePath ) === false ||
277  $this->Cache->get_data( $UnionFilePath ) === false )
278  {
279  $Handle = fopen( $UnionFilePath , "wb" );
280 
281  foreach( $Files as $k2 => $v2 )
282  {
283  $Content = $this->CachedMultyFS->file_get_contents( "$Path/$v2" );
284  fwrite( $Handle , $Content );
285  fwrite( $Handle , "\r\n" );
286  }
287 
288  fclose( $Handle );
289  }
290 
291  $RetFiles [] = array( 'path' => str_replace( './' , '{http_host}/' , $UnionFilePath ) );
292 
293  return( $RetFiles );
294  }
295  catch( Exception $e )
296  {
297  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
298  }
299  }
300 
323  private function join_compressed_stylesheets( $Compressed )
324  {
325  try
326  {
327  $RetFiles = array();
328 
329  foreach( $Compressed as $k1 => $v1 )
330  {
331  if( $k1 === 'join' )
332  {
333  $RetFiles = $this->stylesheets_join1( $v1 , $RetFiles );
334  }
335  elseif( count( $v1 ) == 1 )
336  {
337  $RetFiles [] = array( 'path' => str_replace( './' , '{http_host}/' , "$k1/".$v1[ 0 ] ) );
338  }
339  else
340  {
341  $RetFiles = $this->stylesheets_join3( $k1 ,$v1 , $RetFiles );
342  }
343  }
344 
345  return( $RetFiles );
346  }
347  catch( Exception $e )
348  {
349  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
350  }
351  }
352 
375  function join_stylesheets( $Files )
376  {
377  try
378  {
379  if( is_array( $Files ) && count( $Files ) > 0 )
380  {
381  $Compressed = $this->compress_stylesheets( $Files );
382 
383  $RetFiles = $this->join_compressed_stylesheets( $Compressed );
384 
385  return( $RetFiles );
386  }
387  else
388  {
389  return( $Files );
390  }
391  }
392  catch( Exception $e )
393  {
394  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
395  }
396  }
397 
416  private function get_stylesheets_content()
417  {
418  try
419  {
420  $this->CSSFiles = $this->join_stylesheets( $this->CSSFiles , 'css' );
421 
422  $Start = $this->CachedMultyFS->get_template( __FILE__ , 'stylesheet_link_start.tpl' );
423  $End = $this->CachedMultyFS->get_template( __FILE__ , 'stylesheet_link_end.tpl' );
424 
425  return( $Start.implode_ex( $End.$Start , $this->CSSFiles , 'path' ).$End );
426  }
427  catch( Exception $e )
428  {
429  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
430  }
431  }
432 
455  function compile_stylesheets( &$Settings )
456  {
457  try
458  {
459  $Content = '';
460 
461  if( is_array( $this->CSSFiles ) && count( $this->CSSFiles ) )
462  {
463  $Content = $this->get_stylesheets_content();
464  }
465 
466  $this->CSSFiles = array();
467 
468  return( $Content );
469  }
470  catch( Exception $e )
471  {
472  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
473  }
474  }
475 
498  function add_stylesheet( $Path , $Join = false )
499  {
500  try
501  {
502  if( $this->PageComposer === false )
503  {
504  $this->PageComposer = get_package( 'page::page_composer' , 'last' , __FILE__ );
505  }
506  $Path = $this->PageComposer->Template->compile_string( $Path );
507  $Path = str_replace( '/./' , '/' , $Path );
508  foreach( $this->CSSFiles as $k => $v )
509  {
510  if( $v[ 'path' ] == $Path )
511  {
512  return;
513  }
514  }
515  $this->CSSFiles [] = array( 'path' => $Path , 'join' => $Join );
516  }
517  catch( Exception $e )
518  {
519  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
520  }
521  }
522  }
523 
524 ?>