b2evolution PHP Cross Reference Blogging Systems

Source: /inc/files/model/_filecache.class.php - 132 lines - 3983 bytes - Summary - Text - Print

Description: This file implements the FileCache class. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the FileCache class.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * }}
  21   *
  22   * @package evocore
  23   *
  24   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  25   * @author fplanque: Francois PLANQUE
  26   *
  27   * @version $Id: _filecache.class.php 6136 2014-03-08 07:59:48Z manuel $
  28   */
  29  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  30  
  31  load_class( '_core/model/dataobjects/_dataobjectcache.class.php', 'DataObjectCache' );
  32  
  33  /**
  34   * FileCache Class
  35   *
  36   * @package evocore
  37   */
  38  class FileCache extends DataObjectCache
  39  {
  40      /**
  41       * Cache for 'root_type:root_in_type_ID:relative_path' -> File object reference
  42       * @access private
  43       * @var array
  44       */
  45      var $cache_root_and_path = array();
  46  
  47      /**
  48       * Constructor
  49       */
  50  	function FileCache()
  51      {
  52          parent::DataObjectCache( 'File', false, 'T_files', 'file_', 'file_ID' );
  53      }
  54  
  55  
  56      /**
  57       * Instantiate a DataObject from a table row and then cache it.
  58       *
  59       * @param Object Database row
  60       * @return Object
  61       */
  62      function & instantiate( & $db_row )
  63      {
  64          // Get ID of the object we'ere preparing to instantiate...
  65          $obj_ID = $db_row->{$this->dbIDname};
  66  
  67           if( !empty($obj_ID) )
  68          {    // If the object ID is valid:
  69               if( !isset($this->cache[$obj_ID]) )
  70              {    // If not already cached:
  71                  // Instantiate a File object for this line:
  72                  $current_File = new File( $db_row->file_root_type, $db_row->file_root_ID, $db_row->file_path ); // COPY!
  73                  // Flow meta data into File object:
  74                  $current_File->load_meta( false, $db_row );
  75                  $this->add( $current_File );
  76              }
  77              else
  78              {    // Already cached:
  79                  $current_File = & $this->cache[$obj_ID];
  80                  // Flow meta data into File object:
  81                  $current_File->load_meta( false, $db_row );
  82              }
  83          }
  84  
  85          return $this->cache[$obj_ID];
  86      }
  87  
  88  
  89    /**
  90       * Creates an object of the {@link File} class, while providing caching
  91       * and making sure that only one reference to a file exists.
  92       *
  93       * @param string Root type: 'user', 'group' or 'collection'
  94       * @param integer ID of the user, the group or the collection the file belongs to...
  95       * @param string Subpath for this file/folder, relative the associated root, including trailing slash (if directory)
  96       * @param boolean check for meta data?
  97       * @return File a {@link File} object
  98       */
  99      function & get_by_root_and_path( $root_type, $root_in_type_ID, $rel_path, $load_meta = false )
 100      {
 101          global $Debuglog, $cache_File;
 102  
 103          if( is_windows() )
 104          {
 105              $rel_path = strtolower(str_replace( '\\', '/', $rel_path ));
 106          }
 107  
 108          // Generate cache key for this file:
 109          $cacheindex = $root_type.':'.$root_in_type_ID.':'.$rel_path;
 110  
 111          if( isset( $this->cache_root_and_path[$cacheindex] ) )
 112          {    // Already in cache
 113              $Debuglog->add( 'File retrieved from cache: '.$cacheindex, 'files' );
 114              $File = & $this->cache_root_and_path[$cacheindex];
 115              if( $load_meta )
 116              {    // Make sure meta is loaded:
 117                  $File->load_meta();
 118              }
 119          }
 120          else
 121          {    // Not in cache
 122              $Debuglog->add( 'File not in cache: '.$cacheindex, 'files' );
 123              $File = new File( $root_type, $root_in_type_ID, $rel_path, $load_meta ); // COPY !!
 124              $this->cache_root_and_path[$cacheindex] = & $File;
 125          }
 126          return $File;
 127      }
 128  
 129  
 130  }
 131  
 132  ?>

title

Description

title

Description

title

Description

title

title

Body