Completed
Branch master (dfac16)
by Robbert
05:19
created
src/prototype.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -303,7 +303,7 @@
 block discarded – undo
303 303
 
304 304
 	/**
305 305
 	 * Removes an observer callback for the given prototype.
306
-	 * @param \arc\prototype\Prototyp $prototype
306
+	 * @param prototype\Prototype $prototype
307 307
 	 * @param \Closure $callback the observer callback to be removed
308 308
 	 */
309 309
 	public static function unobserve($prototype, $callback) 
Please login to merge, or discard this patch.
Indentation   +355 added lines, -355 removed lines patch added patch discarded remove patch
@@ -17,308 +17,308 @@  discard block
 block discarded – undo
17 17
 final class Prototype 
18 18
 {
19 19
 
20
-	/**
21
-	 * @var \SplObjectStorage contains a list of frozen objects and the observer
22
-	 */
23
-	private static $frozen = null;
24
-
25
-	/**
26
-	 * @var \SplObjectStorage contains a list of objects made unextensible and the observer
27
-	 */
28
-	private static $notExtensible = null;
29
-
30
-	/**
31
-	 * @var \SplObjectStorage contains a list of all 'child' instances for each prototype
32
-	 */
33
-	private static $instances = null;
34
-
35
-	/**
36
-	 * @var \SplObjectStorage contains a list of all observers for each prototype
37
-	 */
38
-	private static $observers = null;
39
-
40
-	/**
41
-	 * Returns a new \arc\prototype\Prototype object with the given properties. The 
42
-	 * properties array may contain closures, these will be available as methods on 
43
-	 * the new Prototype object.
44
-	 * @param array $properties List of properties and methods
45
-	 * @return \arc\prototype\Prototype
46
-	 */
47
-	public static function create($properties) 
48
-	{
49
-		return new prototype\Prototype($properties);
50
-	}
51
-
52
-	/**
53
-	 * Returns a new \arc\prototype\Prototype object with the given object as its
54
-	 * prototype and the given properties and methods set.
55
-	 * @param \arc\prototype\Prototype $prototype The prototype for this object
56
-	 * @param array $properties List of properties and methods
57
-	 * @return \arc\prototype\Prototype
58
-	 */
59
-	public static function extend($prototype, $properties) 
60
-	{
61
-		if ( self::isExtensible($prototype) ) {
62
-			if (!isset(self::$instances)) {
63
-				self::$instances = new \SplObjectStorage();
64
-			};
65
-			if (!isset(self::$instances[$prototype])) {
66
-				self::$instances[$prototype] = [];
67
-			}
68
-			$properties['prototype'] = $prototype;
69
-			$instance = new prototype\Prototype($properties);
70
-			$list = self::$instances[$prototype];
71
-			array_push($list,$instance);
72
-			self::$instances[$prototype] = $list;
73
-			return $instance;
74
-		} else {
75
-			return null;
76
-		}
77
-	}
78
-
79
-	/**
80
-	 * Helper method to remove cache information when a prototype is no longer needed.
81
-	 * @param \arc\prototype\Prototype $obj The object to be removed
82
-	 */
83
-	public static function _destroy($obj) 
84
-	{
85
-		self::unfreeze($obj);
86
-		if ( isset(self::$notExtensible[$obj]) ) {
87
-			unset(self::$notExtensible[$obj]);
88
-		}
89
-		if ( isset(self::$observers[$obj]) ) {
90
-			unset(self::$observers[$obj]);
91
-		}
92
-		if ( isset($obj->prototype) ) {
93
-			unset(self::$instances[$obj->prototype]);
94
-		}
95
-	}
96
-
97
-	/**
98
-	 * Returns a new \arc\prototype\Prototype with the given prototype set. In addition 
99
-	 * all properties on the extra objects passed to this method will be copied to the 
100
-	 * new Prototype object. For any property that is set on multiple objects, the value 
101
-	 * of the property in the later object overwrites values from other objects.
102
-	 * @param \arc\prototype\Prototype $prototype the prototype for the new object
103
-	 * @param \arc\prototype\Prototype ...$object the objects whose properties will be assigned
104
-	 */
105
-	public static function assign($prototype) 
106
-	{
107
-		$objects = func_get_args();
108
-		array_shift($objects);
109
-		$properties = [];
110
-		foreach ($objects as $obj) {
111
-			$properties = $obj->properties + $properties;
112
-		}
113
-		return self::extend($prototype, $properties);
114
-	}
115
-
116
-	/**
117
-	 * This makes changes to the given Prototype object impossible, untill you call 
118
-	 * \arc\prototype::unfreeze($prototype). The object becomes immutable. Any 
119
-	 * attempt to change the object will silently fail.
120
-	 * @param \arc\prototype\Prototype $prototype the object to freeze
121
-	 */
122
-	public static function freeze($prototype) 
123
-	{
124
-		if (!isset(self::$frozen)) {
125
-			self::$frozen = new \SplObjectStorage();
126
-		}
127
-		self::$frozen[$prototype] = function($name, $value) {
128
-			return false;
129
-		};
130
-		return self::observe($prototype, self::$frozen[$prototype]);	
131
-	}
132
-
133
-	/**
134
-	 * Makes the given object mutable again.
135
-	 * @param \arc\prototype\Prototype $prototype
136
-	 */
137
-	public static function unfreeze($prototype) 
138
-	{
139
-		if ( isset(self::$frozen) && isset(self::$frozen[$prototype]) ) {
140
-			self::unobserve($prototype, self::$frozen[$prototype]);
141
-			unset(self::$frozen[$prototype]);
142
-		}
143
-	}
144
-
145
-	/**
146
-	 * Returns a list of keys of all the properties in the given prototype
147
-	 * @param \arc\prototype\Prototype $prototype
148
-	 * @return array
149
-	 */
150
-	public static function keys($prototype) 
151
-	{
152
-		$entries = static::entries($prototype);
153
-		return array_keys($entries);
154
-	}
155
-
156
-	/**
157
-	 * Returns an array with key:value pairs for all properties in the given prototype
158
-	 * @param \arc\prototype\Prototype $prototype
159
-	 * @return array
160
-	 */
161
-	public static function entries($prototype) 
162
-	{
163
-		return $prototype->properties;
164
-	}
165
-
166
-	/**
167
-	 * Returns a list of all the property values in the given prototype
168
-	 * @param \arc\prototype\Prototype $prototype
169
-	 * @return array
170
-	 */
171
-	public static function values($prototype) 
172
-	{
173
-		$entries = static::entries($prototype);
174
-		return array_values($entries);
175
-	}
176
-
177
-	/**
178
-	 * Returns true if the the property name is available in this prototype
179
-	 * @param \arc\prototype\Prototype $prototype
180
-	 * @param string $property
181
-	 * @return bool
182
-	 */
183
-	public static function hasProperty($prototype, $property) 
184
-	{
185
-		$entries = static::entries($prototype);
186
-		return array_key_exists($property, $entries);
187
-	}
188
-
189
-	/**
190
-	 * Returns a list of all the property names defined in this prototype instance
191
-	 * without traversing its prototypes.
192
-	 * @param \arc\prototype\Prototype $prototype
193
-	 * @return array
194
-	 */
195
-	public static function ownKeys($prototype) 
196
-	{
197
-		$entries = static::ownEntries($prototype);
198
-		return array_keys($entries);
199
-	}
200
-
201
-	/**
202
-	 * Returns an array with key:value pairs for all properties in this prototype
203
-	 * instance wihtout traversing its prototypes.
204
-	 * @param \arc\prototype\Prototype $prototype
205
-	 * @return array
206
-	 */
207
-	public static function ownEntries($prototype) 
208
-	{
209
-		// this needs access to the private _ownProperties variable
210
-		// this is one way to do that.
211
-		$f = \Closure::bind(function() use ($prototype) {
212
-			return $prototype->_ownProperties;
213
-		}, $prototype, $prototype);
214
-		return $f();
215
-	}
216
-
217
-	/**
218
-	 * Returns a list of all the property values in the given prototype
219
-	 * instance wihtout traversing its prototypes.
220
-	 * @param \arc\prototype\Prototype $prototype
221
-	 * @return array
222
-	 */
223
-	public static function ownValues($prototype) 
224
-	{
225
-		$entries = static::ownEntries($prototype);
226
-		return array_values($entries);
227
-	}
228
-
229
-	/**
230
-	 * Returns true if the the property name is available in this prototype
231
-	 * instance wihtout traversing its prototypes.
232
-	 * @param \arc\prototype\Prototype $prototype
233
-	 * @param string $property
234
-	 * @return bool
235
-	 */
236
-	public static function hasOwnProperty($prototype, $property) 
237
-	{
238
-		$entries = static::ownEntries($prototype);
239
-		return array_key_exists($property, $entries);
240
-	}
241
-
242
-	/**
243
-	 * Returns true if the given prototype is made immutable by freeze()
244
-	 * @param \arc\prototype\Prototype $prototype
245
-	 * @return bool
246
-	 */
247
-	public static function isFrozen($prototype) 
248
-	{
249
-		return isset(self::$frozen[$prototype]);
250
-	}
251
-
252
-	/**
253
-	 * Returns true if the given prototype is made not Extensible
254
-	 * @param \arc\prototype\Prototype $prototype
255
-	 * @return bool
256
-	 */
257
-	public static function isExtensible($prototype) 
258
-	{
259
-		return !isset(self::$notExtensible[$prototype]);
260
-	}
261
-
262
-	/**
263
-	 * This calls the $callback function each time a property of $prototype is 
264
-	 * changed or unset. The callback is called with the prototype object, the 
265
-	 * name of the property and the new value (null if unset).
266
-	 * If the closure returns false exactly (no other 'falsy' values will work), 
267
-	 * the change will be cancelled
268
-	 * @param \arc\prototype\Prototype $prototype
269
-	 * @param \Closure $callback
270
-	 */
271
-	public static function observe($prototype, $callback) 
272
-	{
273
-		if ( !isset(self::$observers) ) {
274
-			self::$observers = new \SplObjectStorage();
275
-		}
276
-		if ( !isset(self::$observers[$prototype]) ) {
277
-			self::$observers[$prototype] = new \SplObjectStorage();
278
-		}
279
-		self::$observers[$prototype][$callback] = true;
280
-	}
281
-
282
-	/**
283
-	 * Returns a list of observers for the given prototype.
284
-	 * @param \arc\prototype\Prototype $prototype
285
-	 * @return array
286
-	 */
287
-	public static function getObservers($prototype) 
288
-	{
289
-		return (isset(self::$observers[$prototype]) ? self::$observers[$prototype] : [] );
290
-	}
291
-
292
-	/**
293
-	 * Makes an object no longer extensible.
294
-	 * @param \arc\prototype\Prototype $prototype
295
-	 */
296
-	public static function preventExtensions($prototype) 
297
-	{
298
-		if ( !isset(self::$notExtensible) ) {
299
-			self::$notExtensible = new \SplObjectStorage();
300
-		}
301
-		self::$notExtensible[$prototype] = true;
302
-	}
303
-
304
-	/**
305
-	 * Removes an observer callback for the given prototype.
306
-	 * @param \arc\prototype\Prototyp $prototype
307
-	 * @param \Closure $callback the observer callback to be removed
308
-	 */
309
-	public static function unobserve($prototype, $callback) 
310
-	{
311
-		if ( isset(self::$observers) && isset(self::$observers[$prototype]) ) {
312
-			unset(self::$observers[$prototype][$callback]);
313
-		}
314
-	}
315
-
316
-	/**
317
-	 * Returns true if the object as the given prototype somewhere in its
318
-	 * prototype chain, including itself.
319
-	 */
320
-	public static function hasPrototype($obj, $prototype) 
321
-	{
20
+    /**
21
+     * @var \SplObjectStorage contains a list of frozen objects and the observer
22
+     */
23
+    private static $frozen = null;
24
+
25
+    /**
26
+     * @var \SplObjectStorage contains a list of objects made unextensible and the observer
27
+     */
28
+    private static $notExtensible = null;
29
+
30
+    /**
31
+     * @var \SplObjectStorage contains a list of all 'child' instances for each prototype
32
+     */
33
+    private static $instances = null;
34
+
35
+    /**
36
+     * @var \SplObjectStorage contains a list of all observers for each prototype
37
+     */
38
+    private static $observers = null;
39
+
40
+    /**
41
+     * Returns a new \arc\prototype\Prototype object with the given properties. The 
42
+     * properties array may contain closures, these will be available as methods on 
43
+     * the new Prototype object.
44
+     * @param array $properties List of properties and methods
45
+     * @return \arc\prototype\Prototype
46
+     */
47
+    public static function create($properties) 
48
+    {
49
+        return new prototype\Prototype($properties);
50
+    }
51
+
52
+    /**
53
+     * Returns a new \arc\prototype\Prototype object with the given object as its
54
+     * prototype and the given properties and methods set.
55
+     * @param \arc\prototype\Prototype $prototype The prototype for this object
56
+     * @param array $properties List of properties and methods
57
+     * @return \arc\prototype\Prototype
58
+     */
59
+    public static function extend($prototype, $properties) 
60
+    {
61
+        if ( self::isExtensible($prototype) ) {
62
+            if (!isset(self::$instances)) {
63
+                self::$instances = new \SplObjectStorage();
64
+            };
65
+            if (!isset(self::$instances[$prototype])) {
66
+                self::$instances[$prototype] = [];
67
+            }
68
+            $properties['prototype'] = $prototype;
69
+            $instance = new prototype\Prototype($properties);
70
+            $list = self::$instances[$prototype];
71
+            array_push($list,$instance);
72
+            self::$instances[$prototype] = $list;
73
+            return $instance;
74
+        } else {
75
+            return null;
76
+        }
77
+    }
78
+
79
+    /**
80
+     * Helper method to remove cache information when a prototype is no longer needed.
81
+     * @param \arc\prototype\Prototype $obj The object to be removed
82
+     */
83
+    public static function _destroy($obj) 
84
+    {
85
+        self::unfreeze($obj);
86
+        if ( isset(self::$notExtensible[$obj]) ) {
87
+            unset(self::$notExtensible[$obj]);
88
+        }
89
+        if ( isset(self::$observers[$obj]) ) {
90
+            unset(self::$observers[$obj]);
91
+        }
92
+        if ( isset($obj->prototype) ) {
93
+            unset(self::$instances[$obj->prototype]);
94
+        }
95
+    }
96
+
97
+    /**
98
+     * Returns a new \arc\prototype\Prototype with the given prototype set. In addition 
99
+     * all properties on the extra objects passed to this method will be copied to the 
100
+     * new Prototype object. For any property that is set on multiple objects, the value 
101
+     * of the property in the later object overwrites values from other objects.
102
+     * @param \arc\prototype\Prototype $prototype the prototype for the new object
103
+     * @param \arc\prototype\Prototype ...$object the objects whose properties will be assigned
104
+     */
105
+    public static function assign($prototype) 
106
+    {
107
+        $objects = func_get_args();
108
+        array_shift($objects);
109
+        $properties = [];
110
+        foreach ($objects as $obj) {
111
+            $properties = $obj->properties + $properties;
112
+        }
113
+        return self::extend($prototype, $properties);
114
+    }
115
+
116
+    /**
117
+     * This makes changes to the given Prototype object impossible, untill you call 
118
+     * \arc\prototype::unfreeze($prototype). The object becomes immutable. Any 
119
+     * attempt to change the object will silently fail.
120
+     * @param \arc\prototype\Prototype $prototype the object to freeze
121
+     */
122
+    public static function freeze($prototype) 
123
+    {
124
+        if (!isset(self::$frozen)) {
125
+            self::$frozen = new \SplObjectStorage();
126
+        }
127
+        self::$frozen[$prototype] = function($name, $value) {
128
+            return false;
129
+        };
130
+        return self::observe($prototype, self::$frozen[$prototype]);	
131
+    }
132
+
133
+    /**
134
+     * Makes the given object mutable again.
135
+     * @param \arc\prototype\Prototype $prototype
136
+     */
137
+    public static function unfreeze($prototype) 
138
+    {
139
+        if ( isset(self::$frozen) && isset(self::$frozen[$prototype]) ) {
140
+            self::unobserve($prototype, self::$frozen[$prototype]);
141
+            unset(self::$frozen[$prototype]);
142
+        }
143
+    }
144
+
145
+    /**
146
+     * Returns a list of keys of all the properties in the given prototype
147
+     * @param \arc\prototype\Prototype $prototype
148
+     * @return array
149
+     */
150
+    public static function keys($prototype) 
151
+    {
152
+        $entries = static::entries($prototype);
153
+        return array_keys($entries);
154
+    }
155
+
156
+    /**
157
+     * Returns an array with key:value pairs for all properties in the given prototype
158
+     * @param \arc\prototype\Prototype $prototype
159
+     * @return array
160
+     */
161
+    public static function entries($prototype) 
162
+    {
163
+        return $prototype->properties;
164
+    }
165
+
166
+    /**
167
+     * Returns a list of all the property values in the given prototype
168
+     * @param \arc\prototype\Prototype $prototype
169
+     * @return array
170
+     */
171
+    public static function values($prototype) 
172
+    {
173
+        $entries = static::entries($prototype);
174
+        return array_values($entries);
175
+    }
176
+
177
+    /**
178
+     * Returns true if the the property name is available in this prototype
179
+     * @param \arc\prototype\Prototype $prototype
180
+     * @param string $property
181
+     * @return bool
182
+     */
183
+    public static function hasProperty($prototype, $property) 
184
+    {
185
+        $entries = static::entries($prototype);
186
+        return array_key_exists($property, $entries);
187
+    }
188
+
189
+    /**
190
+     * Returns a list of all the property names defined in this prototype instance
191
+     * without traversing its prototypes.
192
+     * @param \arc\prototype\Prototype $prototype
193
+     * @return array
194
+     */
195
+    public static function ownKeys($prototype) 
196
+    {
197
+        $entries = static::ownEntries($prototype);
198
+        return array_keys($entries);
199
+    }
200
+
201
+    /**
202
+     * Returns an array with key:value pairs for all properties in this prototype
203
+     * instance wihtout traversing its prototypes.
204
+     * @param \arc\prototype\Prototype $prototype
205
+     * @return array
206
+     */
207
+    public static function ownEntries($prototype) 
208
+    {
209
+        // this needs access to the private _ownProperties variable
210
+        // this is one way to do that.
211
+        $f = \Closure::bind(function() use ($prototype) {
212
+            return $prototype->_ownProperties;
213
+        }, $prototype, $prototype);
214
+        return $f();
215
+    }
216
+
217
+    /**
218
+     * Returns a list of all the property values in the given prototype
219
+     * instance wihtout traversing its prototypes.
220
+     * @param \arc\prototype\Prototype $prototype
221
+     * @return array
222
+     */
223
+    public static function ownValues($prototype) 
224
+    {
225
+        $entries = static::ownEntries($prototype);
226
+        return array_values($entries);
227
+    }
228
+
229
+    /**
230
+     * Returns true if the the property name is available in this prototype
231
+     * instance wihtout traversing its prototypes.
232
+     * @param \arc\prototype\Prototype $prototype
233
+     * @param string $property
234
+     * @return bool
235
+     */
236
+    public static function hasOwnProperty($prototype, $property) 
237
+    {
238
+        $entries = static::ownEntries($prototype);
239
+        return array_key_exists($property, $entries);
240
+    }
241
+
242
+    /**
243
+     * Returns true if the given prototype is made immutable by freeze()
244
+     * @param \arc\prototype\Prototype $prototype
245
+     * @return bool
246
+     */
247
+    public static function isFrozen($prototype) 
248
+    {
249
+        return isset(self::$frozen[$prototype]);
250
+    }
251
+
252
+    /**
253
+     * Returns true if the given prototype is made not Extensible
254
+     * @param \arc\prototype\Prototype $prototype
255
+     * @return bool
256
+     */
257
+    public static function isExtensible($prototype) 
258
+    {
259
+        return !isset(self::$notExtensible[$prototype]);
260
+    }
261
+
262
+    /**
263
+     * This calls the $callback function each time a property of $prototype is 
264
+     * changed or unset. The callback is called with the prototype object, the 
265
+     * name of the property and the new value (null if unset).
266
+     * If the closure returns false exactly (no other 'falsy' values will work), 
267
+     * the change will be cancelled
268
+     * @param \arc\prototype\Prototype $prototype
269
+     * @param \Closure $callback
270
+     */
271
+    public static function observe($prototype, $callback) 
272
+    {
273
+        if ( !isset(self::$observers) ) {
274
+            self::$observers = new \SplObjectStorage();
275
+        }
276
+        if ( !isset(self::$observers[$prototype]) ) {
277
+            self::$observers[$prototype] = new \SplObjectStorage();
278
+        }
279
+        self::$observers[$prototype][$callback] = true;
280
+    }
281
+
282
+    /**
283
+     * Returns a list of observers for the given prototype.
284
+     * @param \arc\prototype\Prototype $prototype
285
+     * @return array
286
+     */
287
+    public static function getObservers($prototype) 
288
+    {
289
+        return (isset(self::$observers[$prototype]) ? self::$observers[$prototype] : [] );
290
+    }
291
+
292
+    /**
293
+     * Makes an object no longer extensible.
294
+     * @param \arc\prototype\Prototype $prototype
295
+     */
296
+    public static function preventExtensions($prototype) 
297
+    {
298
+        if ( !isset(self::$notExtensible) ) {
299
+            self::$notExtensible = new \SplObjectStorage();
300
+        }
301
+        self::$notExtensible[$prototype] = true;
302
+    }
303
+
304
+    /**
305
+     * Removes an observer callback for the given prototype.
306
+     * @param \arc\prototype\Prototyp $prototype
307
+     * @param \Closure $callback the observer callback to be removed
308
+     */
309
+    public static function unobserve($prototype, $callback) 
310
+    {
311
+        if ( isset(self::$observers) && isset(self::$observers[$prototype]) ) {
312
+            unset(self::$observers[$prototype][$callback]);
313
+        }
314
+    }
315
+
316
+    /**
317
+     * Returns true if the object as the given prototype somewhere in its
318
+     * prototype chain, including itself.
319
+     */
320
+    public static function hasPrototype($obj, $prototype) 
321
+    {
322 322
         if (!$obj->prototype) {
323 323
             return false;
324 324
         }
@@ -327,59 +327,59 @@  discard block
 block discarded – undo
327 327
         }
328 328
 
329 329
         return static::hasPrototype($obj->prototype, $prototype );
330
-	}
331
-
332
-	/**
333
-	 * Returns a list of prototype objects that have this prototype object
334
-	 * in their prototype chain.
335
-	 * @param \arc\prototype\Prototype $prototype
336
-	 * @return array
337
-	 */
338
-	public static function getDescendants($prototype) 
339
-	{
340
-		$instances = self::getInstances($prototype);
341
-		$descendants = $instances;
342
-		foreach ($instances as $instance) {
343
-			$descendants += self::getDescendants($instance);
344
-		}
345
-		return $descendants;
346
-	}
347
-
348
-	/**
349
-	 * Returns a list of prototype objects that have this prototype object
350
-	 * as their direct prototype.
351
-	 * @param \arc\prototype\Prototype $prototype
352
-	 * @return array
353
-	 */
354
-	public static function getInstances($prototype) 
355
-	{
356
-		return (isset(self::$instances[$prototype]) ? self::$instances[$prototype] : [] );
357
-	}
358
-
359
-	/**
360
-	 * Returns the full prototype chain for the given object.
361
-	 * @param \arc\prototype\Prototype $obj
362
-	 * @return array
363
-	 */
364
-	public static function getPrototypes($obj) 
365
-	{
366
-		$prototypes = [];
367
-		while ( $prototype = $obj->prototype ) {
368
-			$prototypes[] = $prototype;
369
-			$obj = $prototype;
370
-		}
371
-		return $prototypes;
372
-	}
373
-
374
-	/**
375
-	 * Returns a new function that calls the given function just once and then simply
376
-	 * returns its result on each subsequent call.
377
-	 * @param callable function to call just once and then remember the result
378
-	 */
379
-	public static function memoize($f) 
380
-	{
381
-		return memoize($f);
382
-	}
330
+    }
331
+
332
+    /**
333
+     * Returns a list of prototype objects that have this prototype object
334
+     * in their prototype chain.
335
+     * @param \arc\prototype\Prototype $prototype
336
+     * @return array
337
+     */
338
+    public static function getDescendants($prototype) 
339
+    {
340
+        $instances = self::getInstances($prototype);
341
+        $descendants = $instances;
342
+        foreach ($instances as $instance) {
343
+            $descendants += self::getDescendants($instance);
344
+        }
345
+        return $descendants;
346
+    }
347
+
348
+    /**
349
+     * Returns a list of prototype objects that have this prototype object
350
+     * as their direct prototype.
351
+     * @param \arc\prototype\Prototype $prototype
352
+     * @return array
353
+     */
354
+    public static function getInstances($prototype) 
355
+    {
356
+        return (isset(self::$instances[$prototype]) ? self::$instances[$prototype] : [] );
357
+    }
358
+
359
+    /**
360
+     * Returns the full prototype chain for the given object.
361
+     * @param \arc\prototype\Prototype $obj
362
+     * @return array
363
+     */
364
+    public static function getPrototypes($obj) 
365
+    {
366
+        $prototypes = [];
367
+        while ( $prototype = $obj->prototype ) {
368
+            $prototypes[] = $prototype;
369
+            $obj = $prototype;
370
+        }
371
+        return $prototypes;
372
+    }
373
+
374
+    /**
375
+     * Returns a new function that calls the given function just once and then simply
376
+     * returns its result on each subsequent call.
377
+     * @param callable function to call just once and then remember the result
378
+     */
379
+    public static function memoize($f) 
380
+    {
381
+        return memoize($f);
382
+    }
383 383
 }
384 384
 
385 385
 /**
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
 	 */
59 59
 	public static function extend($prototype, $properties) 
60 60
 	{
61
-		if ( self::isExtensible($prototype) ) {
61
+		if (self::isExtensible($prototype)) {
62 62
 			if (!isset(self::$instances)) {
63 63
 				self::$instances = new \SplObjectStorage();
64 64
 			};
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 			$properties['prototype'] = $prototype;
69 69
 			$instance = new prototype\Prototype($properties);
70 70
 			$list = self::$instances[$prototype];
71
-			array_push($list,$instance);
71
+			array_push($list, $instance);
72 72
 			self::$instances[$prototype] = $list;
73 73
 			return $instance;
74 74
 		} else {
@@ -83,13 +83,13 @@  discard block
 block discarded – undo
83 83
 	public static function _destroy($obj) 
84 84
 	{
85 85
 		self::unfreeze($obj);
86
-		if ( isset(self::$notExtensible[$obj]) ) {
86
+		if (isset(self::$notExtensible[$obj])) {
87 87
 			unset(self::$notExtensible[$obj]);
88 88
 		}
89
-		if ( isset(self::$observers[$obj]) ) {
89
+		if (isset(self::$observers[$obj])) {
90 90
 			unset(self::$observers[$obj]);
91 91
 		}
92
-		if ( isset($obj->prototype) ) {
92
+		if (isset($obj->prototype)) {
93 93
 			unset(self::$instances[$obj->prototype]);
94 94
 		}
95 95
 	}
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 	 */
137 137
 	public static function unfreeze($prototype) 
138 138
 	{
139
-		if ( isset(self::$frozen) && isset(self::$frozen[$prototype]) ) {
139
+		if (isset(self::$frozen) && isset(self::$frozen[$prototype])) {
140 140
 			self::unobserve($prototype, self::$frozen[$prototype]);
141 141
 			unset(self::$frozen[$prototype]);
142 142
 		}
@@ -270,10 +270,10 @@  discard block
 block discarded – undo
270 270
 	 */
271 271
 	public static function observe($prototype, $callback) 
272 272
 	{
273
-		if ( !isset(self::$observers) ) {
273
+		if (!isset(self::$observers)) {
274 274
 			self::$observers = new \SplObjectStorage();
275 275
 		}
276
-		if ( !isset(self::$observers[$prototype]) ) {
276
+		if (!isset(self::$observers[$prototype])) {
277 277
 			self::$observers[$prototype] = new \SplObjectStorage();
278 278
 		}
279 279
 		self::$observers[$prototype][$callback] = true;
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
 	 */
287 287
 	public static function getObservers($prototype) 
288 288
 	{
289
-		return (isset(self::$observers[$prototype]) ? self::$observers[$prototype] : [] );
289
+		return (isset(self::$observers[$prototype]) ? self::$observers[$prototype] : []);
290 290
 	}
291 291
 
292 292
 	/**
@@ -295,7 +295,7 @@  discard block
 block discarded – undo
295 295
 	 */
296 296
 	public static function preventExtensions($prototype) 
297 297
 	{
298
-		if ( !isset(self::$notExtensible) ) {
298
+		if (!isset(self::$notExtensible)) {
299 299
 			self::$notExtensible = new \SplObjectStorage();
300 300
 		}
301 301
 		self::$notExtensible[$prototype] = true;
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
 	 */
309 309
 	public static function unobserve($prototype, $callback) 
310 310
 	{
311
-		if ( isset(self::$observers) && isset(self::$observers[$prototype]) ) {
311
+		if (isset(self::$observers) && isset(self::$observers[$prototype])) {
312 312
 			unset(self::$observers[$prototype][$callback]);
313 313
 		}
314 314
 	}
@@ -326,7 +326,7 @@  discard block
 block discarded – undo
326 326
             return true;
327 327
         }
328 328
 
329
-        return static::hasPrototype($obj->prototype, $prototype );
329
+        return static::hasPrototype($obj->prototype, $prototype);
330 330
 	}
331 331
 
332 332
 	/**
@@ -353,7 +353,7 @@  discard block
 block discarded – undo
353 353
 	 */
354 354
 	public static function getInstances($prototype) 
355 355
 	{
356
-		return (isset(self::$instances[$prototype]) ? self::$instances[$prototype] : [] );
356
+		return (isset(self::$instances[$prototype]) ? self::$instances[$prototype] : []);
357 357
 	}
358 358
 
359 359
 	/**
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
 	public static function getPrototypes($obj) 
365 365
 	{
366 366
 		$prototypes = [];
367
-		while ( $prototype = $obj->prototype ) {
367
+		while ($prototype = $obj->prototype) {
368 368
 			$prototypes[] = $prototype;
369 369
 			$obj = $prototype;
370 370
 		}
@@ -388,10 +388,10 @@  discard block
 block discarded – undo
388 388
  */
389 389
 function memoize($f) 
390 390
 {
391
-    return function () use ($f) {
391
+    return function() use ($f) {
392 392
         static $result;
393 393
         if (null === $result) {
394
-            if ( $f instanceof \Closure && isset($this) ) {
394
+            if ($f instanceof \Closure && isset($this)) {
395 395
                 $f = \Closure::bind($f, $this);
396 396
             }
397 397
             $result = $f();
Please login to merge, or discard this patch.
src/prototype/Prototype.php 2 patches
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -27,8 +27,8 @@  discard block
 block discarded – undo
27 27
     private $_staticMethods = [];
28 28
 
29 29
     /**
30
-    * @var Object prototype Readonly reference to a prototype object. Can only be set in the constructor.
31
-    */
30
+     * @var Object prototype Readonly reference to a prototype object. Can only be set in the constructor.
31
+     */
32 32
     private $prototype = null;
33 33
 
34 34
     /**
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
38 38
     {
39 39
         foreach ($properties as $property => $value) {
40 40
             if ( !is_numeric( $property ) && $property!='properties' ) {
41
-                 if ( $property[0] == ':' ) {
41
+                    if ( $property[0] == ':' ) {
42 42
                     $property = substr($property, 1);
43 43
                     $this->_staticMethods[$property] = true;
44 44
                     $this->_ownProperties[$property] = $value;
@@ -222,7 +222,7 @@  discard block
 block discarded – undo
222 222
      */
223 223
     public function __destruct()
224 224
     {
225
-    	\arc\prototype::_destroy($this);
225
+        \arc\prototype::_destroy($this);
226 226
         return $this->_tryToCall( $this->__destruct );
227 227
     }
228 228
 
Please login to merge, or discard this patch.
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -37,15 +37,15 @@  discard block
 block discarded – undo
37 37
     public function __construct($properties = [])
38 38
     {
39 39
         foreach ($properties as $property => $value) {
40
-            if ( !is_numeric( $property ) && $property!='properties' ) {
41
-                 if ( $property[0] == ':' ) {
40
+            if (!is_numeric($property) && $property != 'properties') {
41
+                 if ($property[0] == ':') {
42 42
                     $property = substr($property, 1);
43 43
                     $this->_staticMethods[$property] = true;
44 44
                     $this->_ownProperties[$property] = $value;
45 45
                 } else if ($property == 'prototype') {
46 46
                     $this->prototype = $value;
47 47
                 } else {
48
-                    $this->_ownProperties[$property] = $this->_bind( $value );
48
+                    $this->_ownProperties[$property] = $this->_bind($value);
49 49
                 }
50 50
             }
51 51
         }
@@ -59,21 +59,21 @@  discard block
 block discarded – undo
59 59
      */
60 60
     public function __call($name, $args)
61 61
     {
62
-        if (array_key_exists( $name, $this->_ownProperties ) && is_callable( $this->_ownProperties[$name] )) {
63
-            if ( array_key_exists($name, $this->_staticMethods) ) {
62
+        if (array_key_exists($name, $this->_ownProperties) && is_callable($this->_ownProperties[$name])) {
63
+            if (array_key_exists($name, $this->_staticMethods)) {
64 64
                 array_unshift($args, $this);
65 65
             }
66
-            return call_user_func_array( $this->_ownProperties[$name], $args );
67
-        } elseif (is_object( $this->prototype)) {
68
-            $method = $this->_getPrototypeProperty( $name );
69
-            if (is_callable( $method )) {
70
-                if ( array_key_exists($name, $this->_staticMethods) ) {
66
+            return call_user_func_array($this->_ownProperties[$name], $args);
67
+        } elseif (is_object($this->prototype)) {
68
+            $method = $this->_getPrototypeProperty($name);
69
+            if (is_callable($method)) {
70
+                if (array_key_exists($name, $this->_staticMethods)) {
71 71
                     array_unshift($args, $this);
72 72
                 }
73
-                return call_user_func_array( $method, $args );
73
+                return call_user_func_array($method, $args);
74 74
             }
75 75
         }
76
-        throw new \arc\ExceptionMethodNotFound( $name.' is not a method on this Object', \arc\exceptions::OBJECT_NOT_FOUND );
76
+        throw new \arc\ExceptionMethodNotFound($name.' is not a method on this Object', \arc\exceptions::OBJECT_NOT_FOUND);
77 77
     }
78 78
 
79 79
     /**
@@ -90,10 +90,10 @@  discard block
 block discarded – undo
90 90
                 return $this->_getPublicProperties();
91 91
             break;
92 92
             default:
93
-                if ( array_key_exists($name, $this->_ownProperties) ) {
93
+                if (array_key_exists($name, $this->_ownProperties)) {
94 94
                     return $this->_ownProperties[$name];
95 95
                 }
96
-                return $this->_getPrototypeProperty( $name );
96
+                return $this->_getPrototypeProperty($name);
97 97
             break;
98 98
         }
99 99
     }
@@ -107,9 +107,9 @@  discard block
 block discarded – undo
107 107
         // get public properties only, so use closure to escape local scope.
108 108
         // the anonymous function / closure is needed to make sure that get_object_vars
109 109
         // only returns public properties.
110
-        return ( is_object( $this->prototype )
111
-            ? array_merge( $this->prototype->properties, $this->_getLocalProperties() )
112
-            : $this->_getLocalProperties() );
110
+        return (is_object($this->prototype)
111
+            ? array_merge($this->prototype->properties, $this->_getLocalProperties())
112
+            : $this->_getLocalProperties());
113 113
     }
114 114
 
115 115
     /**
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
         //$getLocalProperties = \Closure::bind(function ($o) {
122 122
         //    return get_object_vars($o);
123 123
         //}, new dummy(), new dummy());
124
-        return [ 'prototype' => $this->prototype ] + $this->_ownProperties;
124
+        return ['prototype' => $this->prototype] + $this->_ownProperties;
125 125
     }
126 126
 
127 127
     /**
@@ -131,23 +131,23 @@  discard block
 block discarded – undo
131 131
      */
132 132
     private function _getPrototypeProperty($name)
133 133
     {
134
-        if (is_object( $this->prototype )) {
134
+        if (is_object($this->prototype)) {
135 135
             // cache prototype access per property - allows fast but partial cache purging
136
-            if (!array_key_exists( $name, self::$properties )) {
137
-                self::$properties[ $name ] = new \SplObjectStorage();
136
+            if (!array_key_exists($name, self::$properties)) {
137
+                self::$properties[$name] = new \SplObjectStorage();
138 138
             }
139
-            if (!self::$properties[$name]->contains( $this->prototype )) {
139
+            if (!self::$properties[$name]->contains($this->prototype)) {
140 140
                 $property = $this->prototype->{$name};
141
-                if ( $property instanceof \Closure ) {
142
-                    if ( !array_key_exists($name, $this->prototype->_staticMethods)) {
143
-                        $property = $this->_bind( $property );
141
+                if ($property instanceof \Closure) {
142
+                    if (!array_key_exists($name, $this->prototype->_staticMethods)) {
143
+                        $property = $this->_bind($property);
144 144
                     } else {
145 145
                         $this->_staticMethods[$name] = true;
146 146
                     }
147 147
                 }
148
-                self::$properties[$name][ $this->prototype ] = $property;
148
+                self::$properties[$name][$this->prototype] = $property;
149 149
             }
150
-            return self::$properties[$name][ $this->prototype ];
150
+            return self::$properties[$name][$this->prototype];
151 151
         } else {
152 152
             return null;
153 153
         }
@@ -159,10 +159,10 @@  discard block
 block discarded – undo
159 159
      */
160 160
     public function __set($name, $value)
161 161
     {
162
-        if (!in_array( $name, [ 'prototype', 'properties' ] )) {
162
+        if (!in_array($name, ['prototype', 'properties'])) {
163 163
             $observers = \arc\prototype::getObservers($this);
164 164
             $continue = true;
165
-            foreach($observers as $observer) {
165
+            foreach ($observers as $observer) {
166 166
                 $result = $observer($this, $name, $value);
167 167
                 if ($result === false) {
168 168
                     $continue = false;
@@ -170,13 +170,13 @@  discard block
 block discarded – undo
170 170
             }
171 171
             if ($continue) {
172 172
                 if (!array_key_exists($name, $this->_staticMethods)) {
173
-                    $this->_ownProperties[$name] = $this->_bind( $value );
173
+                    $this->_ownProperties[$name] = $this->_bind($value);
174 174
                 } else {
175 175
                     $this->_ownProperties[$name] = $value;
176 176
                 }
177 177
                 // purge prototype cache for this property - this will clear too much but cache will be filled again
178 178
                 // clearing exactly the right entries from the cache will generally cost more performance than this
179
-                unset( self::$properties[ $name ] );
179
+                unset(self::$properties[$name]);
180 180
             }
181 181
         }
182 182
     }
@@ -187,19 +187,19 @@  discard block
 block discarded – undo
187 187
      */
188 188
     public function __isset($name)
189 189
     {
190
-        $val = $this->_getPrototypeProperty( $name );
190
+        $val = $this->_getPrototypeProperty($name);
191 191
 
192
-        return isset( $val );
192
+        return isset($val);
193 193
     }
194 194
 
195 195
     /**
196 196
      * @param $name
197 197
      */
198 198
     public function __unset($name) {
199
-        if (!in_array( $name, [ 'prototype', 'properties' ] )) {
199
+        if (!in_array($name, ['prototype', 'properties'])) {
200 200
             $observers = \arc\prototype::getObservers($this);
201 201
             $continue = true;
202
-            foreach($observers as $observer) {
202
+            foreach ($observers as $observer) {
203 203
                 $result = $observer($this, $name, null);
204 204
                 if ($result === false) {
205 205
                     $continue = false;
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
                 unset($this->_ownProperties[$name]);
213 213
                 // purge prototype cache for this property - this will clear too much but cache will be filled again
214 214
                 // clearing exactly the right entries from the cache will generally cost more performance than this
215
-                unset( self::$properties[ $name ] );
215
+                unset(self::$properties[$name]);
216 216
             }
217 217
         }
218 218
     }
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
     public function __destruct()
224 224
     {
225 225
     	\arc\prototype::_destroy($this);
226
-        return $this->_tryToCall( $this->__destruct );
226
+        return $this->_tryToCall($this->__destruct);
227 227
     }
228 228
 
229 229
     /**
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
      */
232 232
     public function __toString()
233 233
     {
234
-        return $this->_tryToCall( $this->__toString );
234
+        return $this->_tryToCall($this->__toString);
235 235
     }
236 236
 
237 237
     /**
@@ -240,10 +240,10 @@  discard block
 block discarded – undo
240 240
      */
241 241
     public function __invoke()
242 242
     {
243
-        if (is_callable( $this->__invoke )) {
244
-            return call_user_func_array( $this->__invoke, func_get_args() );
243
+        if (is_callable($this->__invoke)) {
244
+            return call_user_func_array($this->__invoke, func_get_args());
245 245
         } else {
246
-            throw new \arc\ExceptionMethodNotFound( 'No __invoke method found in this Object', \arc\exceptions::OBJECT_NOT_FOUND );
246
+            throw new \arc\ExceptionMethodNotFound('No __invoke method found in this Object', \arc\exceptions::OBJECT_NOT_FOUND);
247 247
         }
248 248
     }
249 249
 
@@ -253,10 +253,10 @@  discard block
 block discarded – undo
253 253
     public function __clone()
254 254
     {
255 255
         // make sure all methods are bound to $this - the new clone.
256
-        foreach (get_object_vars( $this ) as $property) {
257
-            $this->{$property} = $this->_bind( $property );
256
+        foreach (get_object_vars($this) as $property) {
257
+            $this->{$property} = $this->_bind($property);
258 258
         }
259
-        $this->_tryToCall( $this->__clone );
259
+        $this->_tryToCall($this->__clone);
260 260
     }
261 261
 
262 262
     /**
@@ -266,9 +266,9 @@  discard block
 block discarded – undo
266 266
      */
267 267
     private function _bind($property)
268 268
     {
269
-        if ($property instanceof \Closure ) {
269
+        if ($property instanceof \Closure) {
270 270
             // make sure any internal $this references point to this object and not the prototype or undefined
271
-            return \Closure::bind( $property, $this );
271
+            return \Closure::bind($property, $this);
272 272
         }
273 273
 
274 274
         return $property;
@@ -282,8 +282,8 @@  discard block
 block discarded – undo
282 282
      */
283 283
     private function _tryToCall($f, $args = [])
284 284
     {
285
-        if (is_callable( $f )) {
286
-            return call_user_func_array( $f, $args );
285
+        if (is_callable($f)) {
286
+            return call_user_func_array($f, $args);
287 287
         }
288 288
     }
289 289
 }
Please login to merge, or discard this patch.