Source for file Load.class.php
Documentation is available at Load.class.php
* Gumbo Library Framework
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* This is the foundation to all Gumbo Classes. It is responsible for loading
* Library files into memory. By default, any name supplied with "Gumbo_" prepended
* to it will be classified as a Gumbo Library Class file. The class defines
* default rules for such cases. The class is also responsible for loading other
* file types into memory. This can be accomplished using a load method directly
* or by creating Load_Settings. The latter is easier.
* When the class is loaded, it will automatically define the Home directory. This
* is the physical location of this class file on the hard drive. The program
* should then define a Custom directory (if needed). When loading a Gumbo Library
* Class file, the custom directory, if set, will be checked first.
* The class implements the Singleton Interface, which means there is only one instance
* of this class. It can be accessed directly through the Singleton method or by
* Gumbo_Load::instance ()->method ();
* $load = Gumbo_Load::instance ();
* There is an overload method defined that works specifically with Gumbo Library
* Classes. These statements are placed above various class files to ensure that
* certain required Classes/Interfaces are loaded.
* $load->Gumbo_Valid_Address;
* The class implements the Gumbo_Interface_Settings interface. This allows it to
* save various settings. The Gumbo_Load_Setting extends the generic Gumbo_Setting
* class. The Gumbo_Load_Setting was specifically designed to be used by this
* class. It is recommended to define certain settings for certain file types
* prior to the start of the application. This will make loading specific file
* types easier by simply passing a key.
* // returns full path based on "templates" setting
* include ($load->load ("template_file", "templates", true));
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
gumbo_load ("Interface_Singleton");
gumbo_load ("Interface_Load");
gumbo_load ("Interface_Settings");
gumbo_load ("Load_Setting");
class Gumbo_Load implements Gumbo_Interface_Load, Gumbo_Interface_Singleton, Gumbo_Interface_Settings {
/** @var Gumbo_Interface_Load $_instance Singleton instance */
private static $_instance =
null;
/** @var string $_dir_home Gumbo directory location */
/** @var string $_dir_customer Gumbo custom directory */
/** @var Gumbo_Interface_Load_Setting[] $_settings Load settings */
* The Constructor will set the primary library home directory. This is
* the current location of the Gumbo_Load file.
* @return Gumbo_Interface_Load
if (self::$_instance ==
null) {
self::$_instance =
new Gumbo_load ();
* Quick load of a file using a setting key
* The method will require the file name and a key reference. The key
* will refer to a saved setting, which will be used to the load the
* file. If no key is defined, or the key doesn't exist, the default
* settings will be used. The method is a quick wrapper to the Load
* The return_path parameter, if set, will return the file name string. This
* is useful for performing direct includes of templates inside a particular
* @param string $name file name
* @param string $key settings reference
* @param bool $return_path returns the formatted path string
* @uses Gumbo_Load_Setting
* @throws Gumbo_Exception
public function load ($name, $key=
null, $path_only=
false) {
$e->setFunction (__METHOD__
);
// return the path if no setting exists
if (strstr ($name, "Gumbo_")) {
// returns only the file name
return $this->getFullPath ($name, $setting->getDirs (), $setting->getExtension (), $setting->getStretch (), $setting->getSeparator (), $setting->getPath (), $setting->getRemove ());
if ($setting->getClass ()) {
return $this->loadClass ($name, $setting->getDirs (), $setting->getExtension (), $setting->getStretch (), $setting->getSeparator (), $setting->getPath (), $setting->getRemove ());
return $this->loadFile ($name, $setting->getDirs (), $setting->getExtension (), $setting->getOnce (), $setting->getInclude (), $setting->getStretch (), $setting->getSeparator (), $setting->getPath (), $setting->getRemove ());
* Loads a Class file into memory
* The load class will load a class file into memory only once. This
* is so the same class name isn't loaded twice. The stretch setting
* will separate the class name into components. Each component would
* represent a sub-directory of the primary path. The separator
* indicates the character used to separate the directory class names.
* $load->loadClass ("This_Class_Name", null, null, true, "_");
* The search will look for the following file
* <path>/this/class/name.class.php where .class.php is the extension
* Any class name prepended with Gumbo_* will automatically create the
* appropriate settings. Simply load the class name.
* $load->loadClass ("Gumbo_Class");
* @param string $name class name
* @param string|array$dirs starting locations
* @param string $ext class file extension (default: class.php)
* @param bool $stretch stretch the class name as directories (default: false)
* @param string $separator class name separator (default: "_")
* @param string $path which path to search through (default: "include")
* @param string $remove removes string pattern from the class name when performing a file search
* @uses Gumbo_Load_Setting
* @throws Gumbo_Exception
public function loadClass ($name, $dirs=
null, $ext=
null, $stretch=
false, $separator=
null, $path=
"include", $remove=
null) {
// check for a Gumbo Class
if (substr ($name, 0, 6) ==
"Gumbo_") {
return gumbo_load ($name);
$setting->setDirs ($dirs);
$setting->setExtension ($ext);
$setting->setStretch ($stretch);
$setting->setSeparator ($separator);
$setting->setPath ($path);
$setting->setRemove ($remove);
if (is_null ($ext)) { $setting->setExtension ("class.php"); }
$this->loadFile ($name, $setting->getDirs (), $setting->getExtension (), true, false, $setting->getStretch (), $setting->getSeparator (), $setting->getPath (), $setting->getRemove ());
$e->setFunction (__METHOD__
);
* Loads a given class file into memory
* @param string $name class name
* @param string|array$dirs locations to search through
* @param string $ext file extension (default: "php")
* @param bool $once loads the file only once (default: false)
* @param bool $use_include use include instead of require (default: false)
* @param bool $stretch stretches the directory path based on the file name (default: false)
* @param string $separator character separating the directory names inside the file name (default: "_")
* @param string $path start path to the file (default: include) [include|home|absolute]
* @param string $remove removes a string pattern from the file name when performing a file search
* @throws Gumbo_Exception
public function loadFile ($name, $dirs=
null, $ext=
null, $once=
false, $use_include=
false, $stretch=
false, $separator=
null, $path=
"include", $remove=
null) {
$e->setFunction (__METHOD__
);
throw
new Gumbo_Exception ("Invalid Argument 'use_include:bool' => {$use_include}:" .
gettype ($use_include));
$e->setFunction (__METHOD__
);
$file =
$this->getFullPath ($name, $dirs, $ext, $stretch, $separator, $path, $remove);
// verify that the file wasn't previously included
return include_once ($file);
$e->setFunction (__METHOD__
);
* Adds a setting, overwriting the original setting if exists
* @postcondition remove all non alpha-numeric (except underscores) characters from $key
* @param Gumbo_Interface_Setting $setting
* @param string $key reference key
* @throws Gumbo_Exception
public function addSetting (Gumbo_Interface_Setting $setting, $key) {
$e->setFunction (__METHOD__
);
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* @postcondition clears all settings
* Sets the Custom Class File location
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Loads a class file into memory
* @param string $name class name
public function __get ($name) {
return gumbo_load ($name);
* Returns the home directory
* Returns the Custom directory
* Returns a formatted file path
* @param string $name file name
* @param string|array$dirs locations to search through (from path)
* @param string $ext file extension (if null, uses the string from the last dot *.ext)
* @param bool $stretch stretch the file name from the path directory
* @param string $separator stretch separator character (typically "." or "_")
* @param string $path path to start from (default: include) [include|home|absolute]
* @param string $remove removes string pattern from the file name before file search
* @uses Gumbo_Load_Setting
* @throws Gumbo_Exception
* @todo parse separator by lowercase/uppercase letters
public function getFullPath ($name, $dirs=
null, $ext=
null, $stretch=
false, $separator=
null, $path=
"include", $remove=
null) {
$setting->setDirs ($dirs);
$setting->setExtension ($ext);
$setting->setStretch ($stretch);
$setting->setSeparator ($separator);
$setting->setPath ($path);
$setting->setRemove ($remove);
// check for extension in file name
$last =
count ($tmp) -
1;
$setting->setExtension ($tmp [$last]);
// remove extension from file name, if it exists
if (substr ($name, strlen ($name) -
strlen ($setting->getExtension ())) ==
$setting->getExtension ()) {
// check if the file stretches the directory
$file =
str_replace ($setting->getRemove (), "", $name);
if ($setting->getStretch () &&
strstr ($file, $setting->getSeparator ())) {
$tmp =
explode ($setting->getSeparator (), $file);
foreach ($tmp as $key=>
$val) {
$dirs_stretch .=
gumbo_appendLocation ($val);
// format the directories listed
if (is_array ($setting->getDirs ()) &&
count ($setting->getDirs ()) >
0) {
foreach ($setting->getDirs () as $val) {
// checking if from Home path
if ($setting->getPath () ==
"home") {
$path =
gumbo_appendLocation ($this->getDirCustom (), $val, $dirs_stretch);
$path =
gumbo_appendLocation ($this->getDirHome (), $val, $dirs_stretch);
$path =
gumbo_appendLocation ($val, $dirs_stretch);
if ($setting->getPath () ==
"home") {
$path =
gumbo_appendLocation ($this->getDirCustom (), $dirs_stretch);
$path =
gumbo_appendLocation ($this->getDirHome (), $dirs_stretch);
$file =
gumbo_appendFile ($file, $setting->getExtension ());
// set the location of the first found file
foreach ($dirs as $dir) {
$e->setFunction (__METHOD__
);
* Returns the list of all Settings
* @return Gumbo_Interface_Setting[]
* Returns a single Setting object based on the key
* @return Gumbo_Interface_Setting
* @throws Gumbo_Exception
if (is_null ($key)) { return null; }
$e->setFunction (__METHOD__
);
* Returns if a Setting exists
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);