className = $className; if( is_array( $id ) ) { $parents = $id; $id = null; } $this->parents = $parents; $this->target = $id; $this->includes = array(); include_once( "cache/MemoryCache.php" ); $this->cache = new MemoryCache(); $this->config = $this->cache->get( "$className.ini" ); if( !$this->config ) { $this->config = parse_ini_file( "config/$className.ini", true ); $this->cache->put( "$className.ini", $this->config[$className] ); } $this->service = $this->load( "service" ); if( $this->service ) $this->service->connect( $this->config["config"] ); } public function __destruct() { if( $this->service ) $this->service->close(); } public function load( $type ) { if( !$this->config[$type] ) return( false ); return $this->import( $this->config[$type]["path"], $this->config[$type]["class"] ); } public function import( $path, $class ) { if( !$class ){ preg_match( "/^\/?.*\/([^\/]+).php$/", $path, $class ); $class = $class[1]; } if( !$this->includes[$path] ) { include_once( $path ); $this->includes[ $path ] = true; } $object = $this->cache->get( $class ); if( $object ) return( $object ); $object = new $class(); $this->cache->put( $class, $object ); return( $object ); } public function clearAll() { return $this->cache->clearAll(); } public function clear( $id ) { return $this->cache->clear( $id ); } public function get( $id ) { return $this->cache->get( $id ); } public function put( $id, $data, $expires, $compressed ) { return $this->cache->put( $id, $data, $expires, $compressed ); } public function fireEvent( $event, $method, $params ) { if( !$this->config["$event.$method"] ) return( $params ); $original = $params; foreach( $this->config["$event.$method"] as $intercept => $interceptor ) { if( is_string( $interceptor ) ) $this->config["$event.$method"][$intercept] = $interceptor = $this->import( $interceptor ); $return = $interceptor->$intercept( $original, $params ); if( $return === false ) return( false ); if( $return ) $params = $return; } return( $params ); } public function find( $params, $criteria = false ) { if( ($params = $this->fireEvent( "before", "find", $params )) === false ) return( false ); if( $this->service ) { if( $this->target ) $result = $this->service->retrieve( $this->className, $this->target, $this->parents, $params, $criteria ); else $result = $this->service->find( $this->className, $this->parents, $params, $criteria ); if( $result ) $params = $result; } if( ($result = $this->fireEvent( "after", "find", $params )) === false ) return( false ); $result = json_encode( $result ); return( $result ); } public function update( $params, $criteria = false ) { if( !($params = $this->fireEvent( "before", "update", $params )) ) return( false ); if( $this->service ) { if( is_string( $idOrfilter ) ) $result = $this->service->update( $this->className, $this->target, $this->parents, $params, $criteria ); else $result = $this->service->replace( $this->className, $this->parents, $params, $criteria ); if( $result ) $params = $result; } if( !($result = $this->fireEvent( "after", "update", $params )) ) return( false ); $result = json_encode( $result ); return( $result ); } public function delete( $params, $criteria = false ) { if( ($params = $this->fireEvent( "before", "delete", $params )) === false ) return( false ); if( $this->service ) { if( is_string( $idOrfilter ) ) $result = $this->service->delete( $this->className, $this->target, $this->parents, $params, $criteria ); else $result = $this->service->deleteAll( $this->className, $this->parents, $params, $criteria ); if( $result ) $params = $result; } if( ($result = $this->fireEvent( "after", "delete", $params )) === false ) return( false ); $result = json_encode( $result ); return( $result ); } public function create( $params ) { if( ($params = $this->fireEvent( "before", "create", $params )) === false ) return( false ); if( $this->service ) { $result = $this->service->create( $this->className, $this->parents, $params ); if( $result ) $params = $result; } if( ($result = $this->fireEvent( "after", "create", $params )) === false ) return( false ); $result = json_encode( $result ); return( $result ); } } ?>