[TYPO3-ect] Registry-Pattern for lib/div?
Joerg Schoppet
joerg at schoppet.de
Tue Aug 14 10:41:04 CEST 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Elmar Hinz wrote:
> Hi,
>
> one idea that just came into my mind is a stack. As the program processes
> the tree stacks would be the classical form to manage the objects. The
> object on top of the stack is the current object.
>
> That would require 3 different steps.
>
> I am thinking of a static class tx_lib for static functions that are
> directly related to lib in contrast to tx_div.
>
> tx_lib::controller();
> tx_lib::pushController();
> tx_lib::popController();
>
> tx_lib::parameters();
> ....
>
> tx_lib::configurations();
> ....
>
Hi,
yes, a stack would be the best idea I think, as long as the controller-,
paramter- and configuration-objects are self-responsible for pushing and
poping themself to the stack (error-less).
The static class is exactly the way I've implemented it, here is my
class with a general registry-pattern (no stack) and in php5 only:
final class tx_enm_Main {
/**
* Holds all registered objects
*
* @var array
*/
static protected $registryArray = array();
/**
* contructur of the class
*
* Because this function is "private", it is not possible to
* instantiate this class
*/
private function __construct() {
} // private function __construct()
/**
* registry method
*
* Through this method, you can register objects, which should be
* globally available exactly one time.
* Usage:
* <code>
* <?php
* tx_jrenmbase_Main::register('test', new Hello());
* ?>
* </code>
*
* @uses tx_enm_Main::$registryArray fills the array with objects
* @param string $name name, under which the registered object can be
reached
* @param object $object object to register
* @return InvalidArgumentException|RuntimeException|boolean either an
exception or true
*/
static public function register($name, $object) {
if (!is_string($name)) {
throw new InvalidArgumentException('First argument $name must be a
string!');
} // if (!is_string($name))
if (!is_object($object)) {
throw new InvalidArgumentException('Only objects may be stored in the
registry!');
} // if (!is_object($object))
if (array_key_exists($name, self::$registryArray)) {
throw new RuntimeException('An object with the name ' . $name . ' is
already registered!');
} // if (array_key_exists($name, self::$registry))
foreach (self::$registryArray as $keyName => $registeredObject) {
if ($object === $registeredObject) {
throw new RuntimeException('Duplicate object handle exists in the
registry as ' . $keyName . '!');
break;
} // if ($object === $registeredObject)
} // foreach (self::$registryArray as $keyName => $registeredObject)
self::$registryArray[$name] = $object;
return TRUE;
} // static public function register($name, $object)
/**
* retrieve method
*
* Through this method, you can retrieve registered objects or a list
of all registered classes
*
* @uses tx_enm_Main::$registryArray retrieves a registered object
* @param string $name name of the object, which should be retrieved or
NULL, if a list should be retrieved
* @return object|array|InvalidArgumentException|RuntimeException
either an exception, the requested object or an array with all objects
*/
static public function retrieve($name = NULL) {
if (NULL === $name) {
$returnArray = array();
foreach (self::$registryArray as $keyName => $registeredObject) {
$returnArray[$keyName] = get_class($registeredObject);
} // foreach (self::$registryArray as $keyName => $registeredObject)
return (array)$returnArray;
} // if (NULL === $name)
if (!is_string($name)) {
throw new InvalidArgumentException('Argument $name must be a string,
or null to list registry!');
} // if (!is_string($name))
if (!array_key_exists($name, self::$registryArray)) {
throw new RuntimeException('No object named ' . $name . ' is
registered!');
} // if (!array_key_exists($name, self::$registryArray))
return self::$registryArray[$name];
} // static public function retrieve($name = NULL)
} // final class tx_enm_Main
Joerg
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGwWqgq5Me6rRDqPURAi0VAJ44org86d42Q/4QLQFSrkAwJ2Tx4QCgn5qN
qti5xQxEf1m2d+AWJDGAzeo=
=iUNe
-----END PGP SIGNATURE-----
More information about the TYPO3-team-extension-coordination
mailing list