ultimix
cached_multy_fs.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 $MountedStorages = false;
39 
50  var $Cache = false;
51  var $CachedFS = false;
52  var $Text = false;
53 
68  function __construct()
69  {
70  try
71  {
72  $this->Cache = get_package( 'cache' , 'last' , __FILE__ );
73  }
74  catch( Exception $e )
75  {
76  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
77  }
78  }
79 
94  function reset()
95  {
96  try
97  {
98  $this->Cache->reset();
99  }
100  catch( Exception $e )
101  {
102  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
103  }
104  }
105 
124  private function get_mounted_storages_from_file( $FilePath )
125  {
126  try
127  {
128  $this->MountedStorages = @file_get_contents( $FilePath );
129  if( $this->MountedStorages !== false )
130  {
131  $this->Cache->add_data( $FilePath , $this->MountedStorages );
132  }
133  }
134  catch( Exception $e )
135  {
136  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
137  }
138  }
139 
158  function load_mounted_storages_from_file( $FilePath )
159  {
160  try
161  {
162  $Data = $this->Cache->get_data( $FilePath );
163  if( $Data !== false )
164  {
165  $this->MountedStorages = $Data;
166  }
167  else
168  {
169  $this->get_mounted_storages_from_file( $FilePath );
170  }
171 
172  $this->MountedStorages = str_replace( "\r" , "\n" , $this->MountedStorages );
173  $this->MountedStorages = str_replace( "\n\n" , "\n" , $this->MountedStorages );
174  $this->MountedStorages = explode( "\n" , $this->MountedStorages );
175  foreach( $this->MountedStorages as $i => $v )
176  {
177  $this->MountedStorages[ $i ] = dirname( __FILE__ ).'/../../'.$this->MountedStorages[ $i ];
178  }
179  }
180  catch( Exception $e )
181  {
182  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
183  }
184  }
185 
201  {
202  try
203  {
204  if( $this->MountedStorages === false )
205  {
206  if( file_exists( dirname( __FILE__ ).'/conf/'.DOMAIN.'.cf_data_storages' ) )
207  {
208  $StoragesConfig = dirname( __FILE__ ).'/conf/'.DOMAIN.'.cf_data_storages';
209  $this->load_mounted_storages_from_file( $StoragesConfig );
210  }
211  else
212  {
213  $this->load_mounted_storages_from_file( dirname( __FILE__ ).'/conf/cf_data_storages' );
214  }
215  }
216  }
217  catch( Exception $e )
218  {
219  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
220  }
221  }
222 
245  private function handle_file_was_not_found_error( $ThrowException )
246  {
247  try
248  {
249  if( $ThrowException )
250  {
251  throw( new Exception( "File $FilePath was not found" ) );
252  }
253  else
254  {
255  return( false );
256  }
257  }
258  catch( Exception $e )
259  {
260  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
261  }
262  }
263 
290  function get_file_path( $FilePath , $ThrowException = true )
291  {
292  try
293  {
295 
296  $FilePath = str_replace( '/./' , '/' , $FilePath );
297 
298  $FileName = basename( $FilePath );
299  foreach( $this->MountedStorages as $ms )
300  {
301  if( file_exists( $ms.'/'.$FileName ) )
302  {
303  return( $ms.'/'.$FileName );
304  }
305  }
306 
307  if( file_exists( $FilePath ) )
308  {
309  return( $FilePath );
310  }
311 
312  return( $this->handle_file_was_not_found_error( $ThrowException ) );
313  }
314  catch( Exception $e )
315  {
316  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
317  }
318  }
319 
342  function file_exists( $OriginalFilePath )
343  {
344  try
345  {
346  $Data = $this->Cache->get_data( $OriginalFilePath );
347 
348  if( $Data === false )
349  {
350  $FilePath = $this->get_file_path( $OriginalFilePath , false );
351 
352  if( $this->CachedFS === false )
353  {
354  $this->CachedFS = get_package( 'cached_fs' , 'last' , __FILE__ );
355  }
356 
357  return( $this->CachedFS->store_file_exists_info( $OriginalFilePath , $FilePath ) );
358  }
359  else
360  {
361  return( $Data == '_file_was_not_found' ? false : true );
362  }
363  }
364  catch( Exception $e )
365  {
366  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
367  }
368  }
369 
396  function transform_file_contents( $Data , $Mode )
397  {
398  try
399  {
400  if( $Mode == 'cleaned' )
401  {
402  $Data = str_replace( "\r" , "\n" , $Data );
403  $Data = str_replace( "\n\n" , "\n" , $Data );
404  if( $this->Text === false )
405  {
406  $this->Text = get_package( 'string::text' , 'last' , __FILE__ );
407  }
408  $Data = $this->Text->remove_bom( $Data );
409  }
410 
411  if( $Mode == 'exploded' )
412  {
413  $Data = str_replace( "\r" , "\n" , $Data );
414  $Data = str_replace( "\n\n" , "\n" , $Data );
415  $Data = explode( "\n" , $Data );
416  }
417 
418  return( $Data );
419  }
420  catch( Exception $e )
421  {
422  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
423  }
424  }
425 
452  function file_get_contents( $OriginalFilePath , $Mode = 'none' )
453  {
454  try
455  {
456  $Data = $this->Cache->get_data( $OriginalFilePath );
457  if( $Data === false )
458  {
459  $Data = @file_get_contents( $this->get_file_path( $OriginalFilePath ) );
460  if( $Data === false )
461  {
462  $Data = '';
463  }
464  $this->Cache->add_data( $OriginalFilePath , $Data );
465  }
466 
467  if( $Mode !== 'none' )
468  {
469  $Data = $this->transform_file_contents( $Data , $Mode );
470  }
471 
472  return( $Data );
473  }
474  catch( Exception $e )
475  {
476  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
477  }
478  }
479 
506  function compound_file_get_contents( $OriginalFilePath , $Mode = 'none' )
507  {
508  try
509  {
510  $Data = $this->Cache->get_data( $OriginalFilePath );
511  if( $Data === false )
512  {
513  $FileName = basename( $OriginalFilePath );
514  $Content = '';
515  if( file_exists( $OriginalFilePath ) )
516  {
517  $Content .= file_get_contents( $OriginalFilePath );
518  }
519  foreach( $this->MountedStorages as $ms )
520  {
521  if( file_exists( $ms.'/'.$FileName ) )
522  {
523  $Content .= file_get_contents( $ms.'/'.$FileName );
524  }
525  }
526  $this->Cache->add_data( $OriginalFilePath , $Content );
527  }
528  $Data = $Mode !== 'none' ? $this->transform_file_contents( $Data , $Mode ) : $Data;
529 
530  return( $Data );
531  }
532  catch( Exception $e )
533  {
534  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
535  }
536  }
537 
560  function file_put_contents( $OriginalFilePath , $Data )
561  {
562  try
563  {
564  if( $this->Cache->data_exists( $OriginalFilePath ) )
565  {
566  $this->Cache->set_data( $OriginalFilePath , $Data );
567  }
568  else
569  {
570  $this->Cache->add_data( $OriginalFilePath , $Data );
571  }
572 
573  $FilePath = $this->get_file_path( $OriginalFilePath , false );
574 
575  @file_put_contents( $FilePath === false ? $OriginalFilePath : $FilePath , $Data );
576  }
577  catch( Exception $e )
578  {
579  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
580  }
581  }
582 
609  function get_template( $PackagePath , $FileName )
610  {
611  try
612  {
613  return( $this->file_get_contents( dirname( $PackagePath )."/res/templates/$FileName" ) );
614  }
615  catch( Exception $e )
616  {
617  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
618  }
619  }
620 
647  function get_config( $PackagePath , $FileName )
648  {
649  try
650  {
651  return( $this->file_get_contents( dirname( $PackagePath )."/conf/$FileName" , 'cleaned' ) );
652  }
653  catch( Exception $e )
654  {
655  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
656  }
657  }
658 
689  function get_data( $PackagePath , $FileName , $Mode = 'none' )
690  {
691  try
692  {
693  return( $this->file_get_contents( dirname( $PackagePath )."/data/$FileName" , $Mode ) );
694  }
695  catch( Exception $e )
696  {
697  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
698  }
699  }
700 
731  function get_package_template( $PackageName , $PackageVersion , $FileName )
732  {
733  try
734  {
735  $PackagePath = _get_package_relative_path_ex( $PackageName , $PackageVersion );
736 
737  return( $this->file_get_contents( "$PackagePath/res/templates/$FileName" ) );
738  }
739  catch( Exception $e )
740  {
741  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
742  }
743  }
744 
775  function get_package_config( $PackageName , $PackageVersion , $FileName )
776  {
777  try
778  {
779  $PackagePath = _get_package_relative_path_ex( $PackageName , $PackageVersion );
780 
781  return( $this->file_get_contents( "$PackagePath/conf/$FileName" , 'cleaned' ) );
782  }
783  catch( Exception $e )
784  {
785  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
786  }
787  }
788 
823  function get_package_data( $PackageName , $PackageVersion , $FileName , $Mode = 'none' )
824  {
825  try
826  {
827  $PackagePath = _get_package_relative_path_ex( $PackageName , $PackageVersion );
828 
829  return( $this->file_get_contents( "$PackagePath/data/$FileName" , $Mode ) );
830  }
831  catch( Exception $e )
832  {
833  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
834  }
835  }
836 
856  {
857  try
858  {
859  return( $this->MountedStorages );
860  }
861  catch( Exception $e )
862  {
863  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
864  }
865  }
866 
881  function flush()
882  {
883  try
884  {
885  $this->Cache->flush();
886  }
887  catch( Exception $e )
888  {
889  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
890  }
891  }
892  }
893 
894 ?>