Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

models.php

Go to the documentation of this file.
00001 
00002 /** @file models.php
00003 * Definition of class models.
00004 */
00005 #include "xml_event_parser_user.php"
00006 #include "model_abstract.php"
00007 /**
00008 * Models definitions.
00009 * This class represents models database.
00010 * @todo inherit cache behaviour from another class.
00011 */
00012 class models : public  xml_event_parser_user
00013 {
00014 public:
00015   /** Path to models. */
00016   var base_path = '';
00017   var models = array();
00018   var current_model = '';
00019   var cache = 'not_cached';
00020   var cache_name = '#cache#_#models#';
00021 
00022   /** Return array with models names. */
00023   function get_models_list()
00024   {
00025     list = array();
00026     foreach( this->models as name => location )
00027     {
00028       list[] = name;
00029     }
00030     return list;
00031   }
00032   /** Return model`s location.
00033   * @param name - name of model to return location
00034   */
00035   function get_location( name )
00036   {
00037     return location_to_filename( this->models[ name ], this->base_path );
00038   }
00039   /** Return model`s relations.
00040   * @param name - name of model to return relations
00041   */
00042   function get_relations( name )
00043   {
00044     if( this->cache == 'not_cached' )
00045       if( !this->read_cache() )
00046         return this->err( 'Cannot read cache', 'get_relations', __LINE__, __FILE__ );
00047     if( !isset( this->cache[ name ] )
00048       || this->has_expired( name ) )
00049       if( !this->pre_cache( name ) )
00050         return this->err( 'Cannot cache model: '.name, 'get_relations', __LINE__, __FILE__ );
00051     return this->cache[ name ][ 'relations' ];
00052   }
00053   /** Return model`s info.
00054   * @param name - name of model to return info
00055   */
00056   function get_info( name )
00057   {
00058     if( this->cache == 'not_cached' )
00059       if( !this->read_cache() )
00060         return this->err( 'Cannot read cache', 'get_info', __LINE__, __FILE__ );
00061     if( !isset( this->cache[ name ] )
00062       || this->has_expired( name ) )
00063       if( !this->pre_cache( name ) )
00064         return this->err( 'Cannot cache model: '.name, 'get_info', __LINE__, __FILE__ );
00065     return this->cache[ name ][ 'info' ];
00066   }
00067   /** Read entire cache
00068   */
00069   function read_cache()
00070   {
00071     if( this->cache_name != '' )
00072     {
00073       if( is_file( this->cache_name ) )
00074       {
00075         fd = fopen( this->cache_name, 'rb' );
00076         if( !fd )
00077           return this->err( 'Cannot open cache file for read', 'read_cache' );
00078         cache_contents = fread( fd, filesize( this->cache_name ) );
00079         fclose( fd );
00080         this->cache = unserialize( cache_contents );
00081       }
00082       else
00083       {
00084         this->cache = array();
00085         fd = fopen( this->cache_name, 'wb' );
00086         if( !fd )
00087           return this->err( 'Cannot open cache file for writing', 'read_cache' );
00088         fwrite( fd, serialize( this->cache ) );
00089         fclose( fd );
00090       }
00091     }
00092     else
00093       return false;
00094     return true;
00095   }
00096   /** Check if record about model has expired
00097   */
00098   function has_expired( name )
00099   {
00100     return
00101       this->cache[ name ][ 'datetime' ] !=
00102       filemtime( this->get_location( name ) );
00103   }
00104   /** Cache model information
00105   */
00106   function pre_cache( name )
00107   {
00108     location = this->get_location( name );
00109     model = new model_abstract;
00110     model = model->produce( array( 'action' => 'querry' ) );
00111     model->import( location );
00112     this->cache[ name ][ 'relations' ] = model->get( MODEL_RELATIONS );
00113     this->cache[ name ][ 'info' ] = model->get( MODEL_INFO );
00114     this->cache[ name ][ 'datetime' ] = filemtime( location );
00115     // Update cache file
00116     fd = fopen( this->cache_name, 'wb' );
00117     if( !fd )
00118       return this->err( 'Cannot open cache file for writing', 'pre_cache' );
00119     fwrite( fd, serialize( this->cache ) );
00120     fclose( fd );
00121     return true;
00122   }
00123 
00124   function xep_open_models( attributes )
00125   {
00126     this->base_path = attributes[ 'base_path' ];
00127   }
00128 
00129   function xep_open_models_model( attributes )
00130   {
00131     this->models[ attributes[ 'name' ] ] = attributes[ 'location' ];
00132   }
00133 }
00134 X-Powered-By: PHP/4.0.8-dev
00135 Content-type: text/html
00136 
00137 

Generated on Tue Mar 11 08:35:13 2003 for SES by doxygen1.2.16