lazy-registry-class

Gesponsorde koppelingen

PHP script bestanden

  1. lazy-registry-class

« Lees de omschrijving en reacties

Gebruiksvoorbeeld (niet getest):

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
//Lazy load
$args = array('pgsql:host=localhost port=5432 dbname=testdb',
              'testuser',
              'testpass',
              array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
Registry::setLazy('dbconn1', 'PDO', $args);
//Ophalen van object
$db = Registry::get('dbconn1');

//Lazy load met static method voor bijvoorbeeld singleton
$args = array('localhost',
              5432,
              'testuser',
              'testpass',
              'testdb');
Registry::setLazy('dbconn2', 'dbClass', $args, 'singleton');//Bij initialisatie van dbClass wordt de static method 'singleton' dus aangeroepen (en er wordt een return-value verwacht)
//Ophalen gaat weer hetzelfde

$db = Registry::get('dbconn2');

//Zonder lazy load
$db = new PDO('pgsql:host=localhost port=5432 dbname=testdb',
              'testuser',
              'testpass',
              array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
Registry::set('dbconn3', $db);
//En ophalen gaat weer hetzelfde
$db2 = Registry::get('dbconn3');
?>

De Registry class
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * Lazy Registry class
 *
 * A class you can use to register objects, so you can always find the object.
 * It is possible to register a object lazy, so it is created when needed.
 *
 */

class Registry {
    /**
     * Internal variable holding a instance of Registry for the singleton created by Registry::getInstance().
     *
     * @var Registry
     */

    private static $instance;
    /**
     * Internal variable holding a array of registered objects.
     *
     * @var array
     */

    protected $objects = array();
    /**
     * The constructor is only created to prevent the Registry class from direct creation of objects.
     * This is done by giving it private and final modifiers.
     *
     */

    private final function __construct() {
    }

    /**
     * Internal method that creates an instance of Registry when it does not exist. Then it returns the instance.
     *
     * @return Registry
     */

    private static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * With this method you can register a new object in the Registry.
     *
     * @param string $name
     * @param object $object
     */

    public static function set($name, $object) {
        if (!is_string($name)) {
            throw new RegistryException('Parameter $name is not a string.', RegistryException::INVALID_PARAMETER);
        }

        if (!is_object($object)) {
            throw new RegistryException('Parameter $object is not a object.', RegistryException::INVALID_PARAMETER);
        }

        $instance = self::getInstance();
        $instance->objects[$name] = $object;
    }

    /**
     * With this method you can register a object lazy in the registry.
     * This means that it will be created wen it is needed.
     *
     * @param string $name
     * @param string $className
     * @param array $args
     * @param string $staticMethod
     */

    public static function setLazy($name, $className, array $args = array(), $staticMethod = null) {
        if (!is_string($name)) {
            throw new RegistryException('Parameter $name is not a string.', RegistryException::INVALID_PARAMETER);
        }

        if (!class_exists($className)) {
            throw new RegistryException('Parameter $className does not contain a defined classname.', RegistryException::INVALID_PARAMETER);
        }

        if (!is_null($staticMethod)) {
            if (!is_string($staticMethod)) {
                throw new RegistryException('Parameter $staticMethod is not a static method name.', RegistryException::INVALID_PARAMETER);
            }

            $reflectionClass = new ReflectionClass($className);
            if (!$reflectionClass->hasMethod($staticMethod)) {
                throw new RegistryException('Parameter $staticMethod is not a static method name.', RegistryException::INVALID_PARAMETER);
            }

            $reflectionMethod = $reflectionClass->getMethod($staticMethod);
            if (!$reflectionMethod->isStatic() || !$reflectionMethod->isPublic()) {
                throw new RegistryException('Parameter $staticMethod is not a static method name.', RegistryException::INVALID_PARAMETER);
            }
        }

        $instance = self::getInstance();
        $instance->objects[$name] = array($className, $args, $staticMethod);
    }

    /**
     * With this method you can get a registered object. Objects who are registered lazy will be created.
     *
     * @param string $name
     * @return object
     */

    public static function get($name) {
        if (!is_string($name)) {
            throw new RegistryException('Parameter $name is not a string.', RegistryException::INVALID_PARAMETER);
        }

        $instance = self::getInstance();
        if (array_key_exists($name, $instance->objects)) {
            if (is_array($instance->objects[$name])) {
                $reflectionClass = new ReflectionClass($instance->objects[$name][0]);
                if (is_null($instance->objects[$name][2])) {
                    $instance->objects[$name] = $reflectionClass->newInstanceArgs($instance->objects[$name][1]);
                }
else {
                    $instance->objects[$name] = $reflectionClass->getMethod($instance->objects[$name][2])->invokeArgs(null, $instance->objects[$name][1]);
                }

                return $instance->objects[$name];
            }

            return $instance->objects[$name];
        }

        return false;
    }
}

/**
 * Exception class for the Registry class
 *
 */

class RegistryException extends Exception {
    /**
     * When this constant is given as code in a throw, there is an unknown error.
     *
     */

    const UNKNOWN = 0;
    /**
     * When this constant is given as code in a throw, there is given a wrong parameter to a method of Registry.
     *
     */

    const INVALID_PARAMETER = 2;
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.