1: <?php
2: /*
3: * Simpletools Framework.
4: * Copyright (c) 2009, Marcin Rosinski. (https://www.getsimpletools.com/)
5: * All rights reserved.
6: *
7: * LICENCE
8: *
9: * Redistribution and use in source and binary forms, with or without modification,
10: * are permitted provided that the following conditions are met:
11: *
12: * - Redistributions of source code must retain the above copyright notice,
13: * this list of conditions and the following disclaimer.
14: *
15: * - Redistributions in binary form must reproduce the above copyright notice,
16: * this list of conditions and the following disclaimer in the documentation and/or other
17: * materials provided with the distribution.
18: *
19: * - Neither the name of the Simpletools nor the names of its contributors may be used to
20: * endorse or promote products derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: *
31: * @framework Simpletools
32: * @description MVC framework
33: * @copyright Copyright (c) 2009 Marcin Rosinski. (https://www.getsimpletools.com/)
34: * @license (BSD)
35: *
36: */
37:
38: namespace Simpletools\Mvc;
39:
40: /**
41: * Hook
42: */
43: class RoutingHook
44: {
45: protected static $_listeners = array();
46: protected static $_env = array();
47:
48: public static function setEnv($env)
49: {
50: self::$_env['routingNamespace'] = isset($env['routingNamespace']) ? $env['routingNamespace'] : null;
51: self::$_env['router'] = isset($env['router']) ? $env['router'] : null;
52: }
53:
54: public static function on($event,$handler)
55: {
56: self::$_listeners[$event][] = $handler;
57: }
58:
59: private static function _callReflection($callable, array $args = array())
60: {
61: $args['routingNamespace'] = self::$_env['routingNamespace'];
62: $args['router'] = self::$_env['router'];
63:
64: if(is_array($callable))
65: {
66: $reflection = new \ReflectionMethod($callable[0], $callable[1]);
67: }
68: elseif(is_string($callable))
69: {
70: $reflection = new \ReflectionFunction($callable);
71: }
72: elseif(is_a($callable, 'Closure') || is_callable($callable, '__invoke'))
73: {
74: $objReflector = new \ReflectionObject($callable);
75: $reflection = $objReflector->getMethod('__invoke');
76: }
77:
78: $pass = array();
79: foreach($reflection->getParameters() as $param)
80: {
81: $name = $param->getName();
82: if(isset($args[$name]))
83: {
84: $pass[] = $args[$name];
85: }
86: else
87: {
88: try
89: {
90: $pass[] = $param->getDefaultValue();
91: }
92: catch(\Exception $e)
93: {
94: $pass[] = null;
95: }
96: }
97: }
98:
99: return $reflection->invokeArgs($callable, $pass);
100: }
101:
102: public static function fire($event,$args=array())
103: {
104: if(isset(self::$_listeners[$event]))
105: {
106: foreach(self::$_listeners[$event] as $handler)
107: {
108: if(is_callable($handler))
109: {
110: $res = self::_callReflection($handler,$args);
111: if($res===false)
112: {
113: break;
114: }
115: }
116: }
117: }
118: }
119: }
120: ?>