<?php
include_once("Packagable.class.php");
/**
* Class Package
* <p>Class provides methods to import packages of classes without the need to use
* __autoload(). This is achieved via its public method import($path) which allows
* a Java-Like type of import.</p>
*
* <p>It allows to use the wild card character * to import all php
* classes in a directory.</p>
* <p>Example of usage in home directory:</p>
* <pre>include_once("classes/Package.class.php");
* $package = new Package();
* $package->import('classes.*');
* $package->import('classes.sql.*');
* Or can be used to import single classes
* $package->import('classes.sql.AClass.php')
* </pre>
* @author Carlo Tasca
* @version 1.0
* @package packagex
*
*/
class Package implements Packagable
{
/**
* Stores array of imports to be carried out by the object
*
* @var array
*/
private $imports;
/**
* Stores array of class names
*
* @var array
*/
private $classesIds;
/**
* Constuctor for Package objects
* Initialises private members imports and classesIds
*
*/
public function __construct()
{
$this->imports = array();
$this->classesIds = array();
}
/**
* import(String) method
* <p>Feeds the imports array for this class.</p>
* <p>Array of imports is to be processed in loadClasses($path) method</p>
* <p>$import must be of type dir.subdir.className (without extension .class.php)</p>
* <p>wild card * can be used to import all classes in a directory, provided they were
* <p>always named using the extention ".class.php"</p>
* <p>If you have a different class naming convention set up for your classes you simply need to edit this method and the loadClasses($path) method</p>
*
* @param String $import
*/
public function import($import)
{
// breakdown $import
$importToken = strtok($import, ".");
$classDir = "";
while ($importToken !== false)
{
// do something with the token
$classDir .= $importToken . '/';
// get next token
$importToken = strtok(".");
}
// remove last / from $classDir
$classDir = substr($classDir, 0, (strlen($classDir) - 1));
// check if wild card '*' has been used
if (strpos($classDir, "*") > 0)
{
array_push($this->imports, $classDir);
}
else
{
$classDir = $classDir . '.class.php';
array_push($this->imports, $classDir);
}
}
/**
* loadClasses($path)
* <p>To be called after all calls to import() have been made</p>
* <p>$path should be in the form (../ or ../../ etc) and represents the location where all classes for your projects are located</p>
* <p>For example if you have files calling this method in public_html,
* and classes to load in public_html/classes there is no need to change the default value
* for $path argument.</p>
* <p>If you are loading classes from a directory above the location of the file calling this
* method $path should be set to ../ or ../../ and so on.</p>
* <p>The best way to define $path is to do it via a constant variable in the Packagable interface file, before the definition of the interface itself is made.
* This to avoid having the / characters as a argument to this function.</p>
*
* @param string $path
*/
public function loadClasses($path = "")
{
// loop array of imports $v = values
foreach ($this->imports as $v)
{
$v = $path.$v;
if (strpos($v, "*") > 0)
{
// WILD CARD to be removed
$v = substr($v, 0, (strlen($v) - 1));
// make sure directory exists
if (file_exists($v))
{
$directory = opendir($v);
while (($file = readdir($directory)) !== false)
{
if (strpos($file, ".class.php") > 0)
{
array_push($this->classesIds, $v.$file);
}
}
// cl dir
closedir($directory);
}
else
{
$exception = "<strong style=\"color:red;\">Directory path <code>" . $v . "</code> error</strong>.<br/ >Check the argument to member import()";
trigger_error($exception, E_USER_ERROR);
}
}
else
{
if (file_exists($v))
{
array_push($this->classesIds, $v);
}
else
{
$exception = "<strong style=\"color:red;\">Class path <code>" . $v . "</code> error</strong>.<br/ >Check the argument to member import()";
trigger_error($exception, E_USER_ERROR);
}
}
}
$this->doLoadClasses();
}
// private helper member
private function stripDotClassDotPhp($s)
{
$s = strtok($s, ".");
return $s;
}
// private helper member
private function doLoadClasses()
{
foreach ($this->classesIds as $v)
{
include_once($v);
}
}
}
?>