Nooku Framework is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

nooku-framework

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
 * @version        $Id$
 * @category    Koowa
 * @package        Koowa_Pattern
 * @subpackage    Decorator
 * @copyright    Copyright (C) 2007 - 2010 Johan Janssens and Mathias Verraes. All rights reserved.
 * @license        GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link         http://www.koowa.org
 */

/**
 * Decorator class
 *
 * @author        Johan Janssens <johan@koowa.org>
 * @category    Koowa
 * @package     Koowa_Pattern
 * @subpackage  Decorator
 * @uses         KObject
 */
abstract class KPatternDecorator extends KObject
{
    /**
     * The decorated object
     *
     * @var object
     */
    protected $_object;

    /**
     * Constructor
     *
     * @param    object    The object to decorate
     * @return    void
     */
    public function __construct($object)
    {
        $this->_object = $object;
    }

    /**
     * Get the decorated object
     *
     * @return    object The decorated object
     */
    public function getObject()
    {
        return $this->_object;
    }

    /**
     * Set the decorated object
     *
     * @param     object
     * @return     KPatternDecorator
     */
    public function setObject($object)
    {
        $this->_object = $object;
        return $this;
    }

    /**
     * Get a list of all the available methods
     *
     * This function returns an array of all the methods, both native and mixed.
     * It will also return the methods exposed by the decorated object.
     *
     * @return array An array
     */
    public function getMethods()
    {
        $object = $this->getObject();

        if($object instanceof KObject) {
             $methods = $object->getMethods();
         } else {
             $methods = get_class_methods(get_class($object));
         }

        return  array_merge(parent::getMethods(), $methods);
    }

    /**
     * Checks if the decorated object or one of it's mixins inherits from a class.
     *
     * @param     string|object     The class to check
     * @return     boolean         Returns TRUE if the object inherits from the class
     */
    public function inherits($class)
    {
        $result = false;
        $object = $this->getObject();

        if($object instanceof KObject) {
              $result = $object->inherits($class);
        } else {
            $result = $object instanceof $class;
        }

        return $result;
    }

    /**
     * Overloaded set function
     *
     * @param  string    The variable name
     * @param  mixed     The variable value.
     * @return mixed
     */
    public function __set($key, $value)
    {
        $this->getObject()->$key = $value;
    }

    /**
     * Overloaded get function
     *
     * @param  string     The variable name.
     * @return mixed
     */
    public function __get($key)
    {
        return $this->getObject()->$key;
    }

    /**
     * Overloaded isset function
     *
     * Allows testing with empty() and isset() functions
     *
     * @param  string     The variable name
     * @return boolean
     */
    public function __isset($key)
    {
        return isset($this->getObject()->$key);
    }

    /**
     * Overloaded isset function
     *
     * Allows unset() on object properties to work
     *
     * @param string     The variable name.
     * @return void
     */
    public function __unset($key)
    {
        if (isset($this->getObject()->$key)) {
            unset($this->getObject()->$key);
        }
    }

       /**
     * Overloaded call function
     *
     * @param  string     The function name
     * @param  array      The function arguments
     * @throws BadMethodCallException     If method could not be found
     * @return mixed The result of the function
     */
    public function __call($method, array $arguments)
    {
        $object = $this->getObject();

        //Check if the method exists
        if($object instanceof KObject)
        {
            $methods = $object->getMethods();
            $exists  = in_array($method, $methods);
        }
        else $exists = method_exists($object, $method);

        //Call the method if it exists
        if($exists)
        {
             $result = null;

            // Call_user_func_array is ~3 times slower than direct method calls.
             switch(count($arguments))
             {
                 case 0 :
                     $result = $object->$method();
                     break;
                 case 1 :
                       $result = $object->$method($arguments[0]);
                        break;
                    case 2:
                        $result = $object->$method($arguments[0], $arguments[1]);
                        break;
                   case 3:
                       $result = $object->$method($arguments[0], $arguments[1], $arguments[2]);
                        break;
                    default:
                      // Resort to using call_user_func_array for many segments
                     $result = call_user_func_array(array($object, $method), $arguments);
              }

              //Allow for method chaining through the decorator
              $class = get_class($object);
             if ($result instanceof $class) {
                  return $this;
             }

             return $result;
        }

        return parent::__call($method, $arguments);
    }
}
Ajax-loader Loading, please wait...