Location: PHPKode > scripts > PAX > pax/core/classes/Resource.class.php
<?php


/**
 * Page DocBlock definition
 * @package org.zadara.marius.pax
 */

/**
 * General resource class definition.
 * 
 * @author Marius Zadara <hide@address.com>
 * @category Classes
 * @copyright (C) 2008-2009 Marius Zadara
 * @license Free for non-comercial use
 * @package org.zadara.marius.pax
 * @abstract 
 * @see PAXObject
 * @see IResource
 * @version 6.0
 * @since 5.0
 */
abstract class Resource extends PAXObject implements IResource 
{
	/**
	 * The path of the resource.
	 *
	 * @var string
	 * @access protected
	 */
	protected  $path;
		
	
	/**
	 * Class constructor.
	 * 
	 * @access public
	 */
	public function __construct()
	{
		// init the parent first
		parent::__construct();
		
		// init the path of the resource
		$this->path = null;		
	}
	
	/**
	 * Method to set the path of the resource.
	 * Throws exception in case of error.
	 *
	 * @access public
	 * @final 
	 * @param string <b>$path</b> The path of the resource
	 * @return void
	 */
	public final function setPath($path)
	{
		// check the path
		if (!is_string($path))
			throw new PAXException(Messages::$MSG_001, 1);
		
		// remove any extra-spaces from the path
		// in order to see if is really empty
		$path = trim($path);

		// check the path again
		if ($path == "")
			throw new PAXException(Messages::$MSG_002, 2);

		// set the path of the resource
		$this->path = $path;
	}	
	
	/**
	 * Method to check if the resource exists.
	 * It assumes that its path has been already set.
	 * Throws exception in case of error
	 * 
	 * @access public
	 * @final 
	 * @return boolean
	 */
	public final function exists()
	{
		// check to see if the resource path has been set
		// if not, throw exception
		if (is_null($this->path))
			throw new PAXException(Messages::$MSG_003, 3);					
		
		// check existence 
		return @file_exists($this->path);						
	}
	
	
	/**
	 * Method to check if the resource is readable.
	 * Throws exception in case of error.
	 *
	 * @access public
	 * @final 
	 * @return boolean
	 */
	public final function isReadable()
	{		
		try 
		{
			// in order to check if is readable,
			// the resource must already exists
			$exists = $this->exists();
			
			if (!$exists)
				throw new PAXException(Messages::$MSG_004, 4);
			
			// since this status is cached, 
			// clear it before checking 
			clearstatcache();
			
			// check if is readable
			return @is_readable($this->path);
		}
		catch (PAXException $pe)
		{
			// in case of specific error,
			// send the exception further
			throw $pe;
		}
		catch (Exception $e)
		{
			// in case of general error,
			// send it further
			throw $e;
		}
	}
	
	
	/**
	 * Method to check if the resource is writable.
	 * It throws exception in case of error
	 *
	 * @access public
	 * @final 
	 * @return boolean
	 */
	public final function isWritable()
	{
		try 
		{
			// in order to check if is writable,
			// the resource must already exists
			$exists = $this->exists();
			
			if (!$exists)
				throw new PAXException(Messages::$MSG_004, 4);
			
			// clear the status cache
			// before checking
			clearstatcache();
			
			return @is_writable($this->path);
		}
		catch (PAXException $pe)
		{
			// in case of error, 
			// send the exception further
			throw $pe;
		}
		catch (Exception $e)
		{
			// in case of general error,
			// send it further 
			throw $e;
		}
	}
	
	
	/**
	 * Class destructor.
	 * 
	 * @access private
	 */
	function __destruct()
	{
	}	
}



?>
Return current item: PAX