Skip to content
This repository has been archived by the owner on May 8, 2021. It is now read-only.

Commit

Permalink
Opl_Loader, Opl_Registry and Opl_Translation_Interface moved to…
Browse files Browse the repository at this point in the history
… separate files as they are completely independent from Opl_Class
  • Loading branch information
eXtreme committed Jul 6, 2010
1 parent b0484bb commit f847a8f
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 321 deletions.
321 changes: 0 additions & 321 deletions lib/Opl/Base.php
Expand Up @@ -16,327 +16,6 @@
die('Open Power Libs requires PHP 5.3.0 or newer. Your version: '.PHP_VERSION);
}

/**
* The translation interface for OPL libraries.
*
* @author Tomasz Jędrzejewski
* @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
* @license http://www.invenzzia.org/license/new-bsd New BSD License
*/
interface Opl_Translation_Interface
{
/**
* This method is supposed to return the specified localized message.
*
* @param string $group The message group
* @param string $id The message identifier
* @return string
*/
public function _($group, $id);

/**
* Assigns the external data to the message body. The operation of
* concatenating the message and the data is left for the programmer.
* The method should save it in the internal buffer and use in the
* next first call of _() method.
*
* @param string $group The message group
* @param string $id The message identifier
* @param ... Custom arguments for the specified text.
*/
public function assign($group, $id);
} // end Opl_Translation_Interface;

/**
* The generic class autoloader is a slightly enhanced version of the
* AggregateAutoloader <http://github.com/zyxist/AggregateAutoloader>
* originally distributed under the terms of MIT license.
*
* @author Tomasz Jędrzejewski
* @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
* @license http://www.invenzzia.org/license/new-bsd New BSD License
*/
class Opl_Loader
{
/**
* The main directory used by autoloader
* @static
* @var string
*/
static private $_directory = '';

/**
* The list of available libraries.
* @var array
*/
private $_libraries = array();

/**
* The library extensions.
* @var array
*/
private $_extensions = array();

/**
* The namespace separator
* @var string
*/
private $_namespaceSeparator = '\\';

/**
* Constructs the autoloader.
*
* @param string $namespaceSeparator The namespace separator used in this autoloader.
* @param string $defaultPath The default library path.
*/
public function __construct($namespaceSeparator = '\\', $defaultPath = './')
{
$this->_namespaceSeparator = $namespaceSeparator;

if($defaultPath[strlen($defaultPath) - 1] != '/')
{
$defaultPath .= '/';
}
$this->_defaultPath = $defaultPath;
} // end __construct();

/**
* Registers a new library to match.
*
* @param string $library The library name to add.
* @param string $path The path to the library.
* @param string $extension The library file extension.
*/
public function addLibrary($library, $path, $extension = '.php')
{
if(isset($this->_libraries[(string)$library]))
{
throw new RuntimeException('Library '.$library.' is already added.');
}
if($path !== null)
{
if($path[strlen($path) - 1] != '/')
{
$path .= '/';
}
$this->_libraries[(string)$library] = $path;
}
else
{
$this->_libraries[(string)$library] = $this->_defaultPath.$library.'/';
}
$this->_extensions[(string)$library] = $extension;
} // end addLibrary();

/**
* Checks if the specified library is available.
*
* @param string $library The library name to check.
*/
public function hasLibrary($library)
{
return isset($this->_libraries[(string)$library]);
} // end hasLibrary();

/**
* Removes a recognized library.
*
* @param string $library The library name to remove.
*/
public function removeLibrary($library)
{
if(!isset($this->_libraries[(string)$library]))
{
throw new RuntimeException('Library '.$library.' is not available.');
}
unset($this->_libraries[(string)$library]);
unset($this->_extensions[(string)$library]);
} // end removeLibrary();

/**
* Sets the namespace separator used by classes in the namespace of this class loader.
*
* @param string $sep The separator to use.
*/
public function setNamespaceSeparator($sep)
{
$this->_namespaceSeparator = $sep;
} // end setNamespaceSeparator();

/**
* Gets the namespace seperator used by classes in the namespace of this class loader.
*
* @return string
*/
public function getNamespaceSeparator()
{
return $this->_namespaceSeparator;
} // end getNamespaceSeparator();

/**
* Sets the default path used by the libraries. Note that it does not affect
* the already added libraries.
*
* @param string $defaultPath The new default path.
*/
public function setDefaultPath($defaultPath)
{
if($defaultPath[strlen($defaultPath) - 1] != '/')
{
$defaultPath .= '/';
}
$this->_defaultPath = $defaultPath;
} // end setDefaultPath();

/**
* Returns the default path used by the libraries.
*
* @return string The current default path.
*/
public function getDefaultPath()
{
return $this->_defaultPath;
} // end getDefaultPath();

/**
* Installs this class loader on the SPL autoload stack.
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
} // end register();

/**
* Uninstalls this class loader from the SPL autoloader stack.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
} // end unregister();

/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return void
*/
public function loadClass($className)
{
$className = ltrim($className, $this->_namespaceSeparator);
$match = strstr($className, $this->_namespaceSeparator, true);

if(false === $match || !isset($this->_libraries[$match]))
{
return false;
}
$rest = strrchr($className, $this->_namespaceSeparator);
$replacement =
str_replace($this->_namespaceSeparator, '/', substr($className, 0, strlen($className) - strlen($rest))).
str_replace(array('_', $this->_namespaceSeparator), '/', $rest);

require($this->_libraries[$match].$replacement.$this->_extensions[$match]);
return true;
} // end loadClass();
} // end Opl_Loader;

/**
* The Registry provides a safe registry of the main system objects and
* certain values. Contrary to singleton implementations, the registry
* can be erased. Intentionally, the programmer should use different buffers
* for scalar values and for objects.
*
* @author Tomasz Jędrzejewski
* @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
* @license http://www.invenzzia.org/license/new-bsd New BSD License
*/
class Opl_Registry
{
/**
* The list of stored objects.
* @static
* @var array
*/
static private $_objects = array();

/**
* The list of stored values.
* @static
* @var array
*/
static private $_values = array();

/**
* Registers a new object in the registry.
*
* @static
* @param string $name The object key
* @param object $object The registered object
*/
static public function set($name, $object)
{
self::$_objects[$name] = $object;
} // end set();

/**
* Returns the previously registered object. If the object does not
* exist, it throws an exception.
*
* @static
* @throws Opl_Registry_Exception
* @param string $name The registered object key.
* @return object The object stored under the specified key.
*/
static public function get($name)
{
if(!isset(self::$_objects[$name]))
{
throw new Opl_Registry_Exception('The specified registry object: '.$name.' does not exist');
}
return self::$_objects[$name];
} // end get();

/**
* Check whether there is an object registered under a specified key.
*
* @static
* @param string $name The object key
* @return boolean A boolean value indicating whether the object exists or not.
*/
static public function exists($name)
{
return !empty(self::$_objects[$name]);
} // end exists();

/**
* Sets the state variable in the registry.
*
* @static
* @param string $name The variable name
* @param mixed $value The variable value
*/
static public function setValue($name, $value)
{
self::$_values[$name] = $value;
} // end setValue();

/**
* Returns the state variable from the registry. If the
* variable does not exist, it returns NULL.
*
* @static
* @param string $name The variable name
* @return mixed
*/
static public function getValue($name)
{
if(!isset(self::$_values[$name]))
{
return NULL;
}
return self::$_values[$name];
} // end getValue();
} // end Opl_Registry;

/**
* The base class for the other OPL libraries. It provides the configuration
* and plugin handling.
Expand Down

0 comments on commit f847a8f

Please sign in to comment.