1 | <?php |
---|
2 | |
---|
3 | //Helper function and defines that must be moved to a common area |
---|
4 | ///////////////////////////////////////////////////// |
---|
5 | |
---|
6 | if(!function_exists('define_once') ) |
---|
7 | { |
---|
8 | function define_once( $constant, $value ) |
---|
9 | { |
---|
10 | if( !defined( $constant ) ) |
---|
11 | { |
---|
12 | define( $constant, $value ); |
---|
13 | } |
---|
14 | } |
---|
15 | } |
---|
16 | |
---|
17 | define_once( 'ROOT', $_SESSION['rootPath'].'/' ); |
---|
18 | |
---|
19 | define_once( 'SERVICES', ROOT.'/services/' ); |
---|
20 | |
---|
21 | define_once( 'LIBRARY', ROOT.'/library/' ); |
---|
22 | |
---|
23 | define_once( 'API', ROOT.'/API/' ); |
---|
24 | |
---|
25 | if( !class_exists('ServiceLocator') ){ |
---|
26 | |
---|
27 | ///////////////////////////////////////////////////// |
---|
28 | class ServiceLocator |
---|
29 | { |
---|
30 | static $locators = array(); |
---|
31 | |
---|
32 | static $cache = array(); |
---|
33 | |
---|
34 | //$service = null; |
---|
35 | |
---|
36 | //$serviceName = null; |
---|
37 | |
---|
38 | // static $empty_locator = new ServiceLocator( 'empty' ); |
---|
39 | |
---|
40 | static function load( $configuration ) |
---|
41 | { |
---|
42 | $configuration = SERVICES.$configuration; |
---|
43 | |
---|
44 | if( !file_exists ( $configuration ) ) |
---|
45 | return( false ); |
---|
46 | |
---|
47 | $configuration = parse_ini_file( $configuration ); |
---|
48 | |
---|
49 | foreach( $configuration as $serviceType => $serviceName ) |
---|
50 | { |
---|
51 | self::deploy( $serviceType, $serviceName ); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | static function deploy( $serviceType, $serviceName = null ) |
---|
56 | { |
---|
57 | require_once( SERVICES."class.".$serviceType.".php" ); |
---|
58 | |
---|
59 | if( $serviceName ) |
---|
60 | { |
---|
61 | self::register( $serviceType, new $serviceName() ); |
---|
62 | } |
---|
63 | |
---|
64 | return( self::$locators[ $serviceType ] ); |
---|
65 | } |
---|
66 | |
---|
67 | //make all the treatment of |
---|
68 | static function register( $service, $object ) |
---|
69 | { |
---|
70 | self::$locators[ $service ] = new ServiceLocator( $service, $object ); |
---|
71 | |
---|
72 | self::configure( $service ); |
---|
73 | |
---|
74 | return( self::$locators[ $service ] ); |
---|
75 | } |
---|
76 | |
---|
77 | static function unregister( $service ) |
---|
78 | { |
---|
79 | $old = self::$locators[ $service ]; |
---|
80 | |
---|
81 | unset( self::$locators[ $service ] ); |
---|
82 | |
---|
83 | return( $old ); |
---|
84 | } |
---|
85 | |
---|
86 | //implement |
---|
87 | static function configure( $service ) |
---|
88 | { |
---|
89 | return( null ); |
---|
90 | } |
---|
91 | |
---|
92 | static function locate( $service, $arguments = array(), $target = false ) |
---|
93 | { |
---|
94 | if( !$target ) |
---|
95 | { |
---|
96 | list( $target, $service ) = explode( ".", $service ); |
---|
97 | } |
---|
98 | |
---|
99 | if( !is_array( $arguments ) ) |
---|
100 | { |
---|
101 | $arguments = array( $arguments ); |
---|
102 | } |
---|
103 | |
---|
104 | $locator = self::$locators[ $target ]; |
---|
105 | |
---|
106 | if( !$locator ) |
---|
107 | { |
---|
108 | $locator = self::deploy( $target ); |
---|
109 | } |
---|
110 | if( !$locator ) |
---|
111 | { |
---|
112 | return( false ); |
---|
113 | } |
---|
114 | |
---|
115 | try |
---|
116 | { |
---|
117 | return $locator->proxy( $service, $arguments ); |
---|
118 | } |
---|
119 | catch( Exception $e ) |
---|
120 | { |
---|
121 | //Implement the exception stack to the fallback handlers treat correctly |
---|
122 | //by now - fly it |
---|
123 | } |
---|
124 | |
---|
125 | return( null ); |
---|
126 | } |
---|
127 | |
---|
128 | function __construct( $serviceName, $object ) |
---|
129 | { |
---|
130 | $this->service = $object; |
---|
131 | $this->serviceName = $serviceName; |
---|
132 | } |
---|
133 | |
---|
134 | function proxy( $method, $arguments ) |
---|
135 | { |
---|
136 | //handle here cases of miss of methods |
---|
137 | if( !method_exists( $this->service, $method ) ) return( false ); |
---|
138 | |
---|
139 | $many = count( $arguments ); |
---|
140 | |
---|
141 | $proxy = self::$cache[$many]; |
---|
142 | |
---|
143 | if( !$proxy ) |
---|
144 | { |
---|
145 | $params = array(); |
---|
146 | |
---|
147 | for( $i = 0; $i < $many; $params[] = '$params['.$i++.']' ); |
---|
148 | |
---|
149 | $proxy = create_function( '$method, $params, $obj', 'return $obj->$method('.implode( ', ', $params ).');' ); |
---|
150 | |
---|
151 | self::$cache[$many] = $proxy; |
---|
152 | } |
---|
153 | |
---|
154 | if( !isset( $arguments ) ) return( $proxy ); |
---|
155 | |
---|
156 | return $proxy( $method, $arguments, $this->service ); |
---|
157 | } |
---|
158 | |
---|
159 | function call() |
---|
160 | { |
---|
161 | $arguments = func_get_args(); |
---|
162 | |
---|
163 | $service = array_pop( $arguments ); |
---|
164 | |
---|
165 | //handle here dispatch with invalid services |
---|
166 | if( $this ) |
---|
167 | { |
---|
168 | $service = strrpos( $service, "." ) ? $service : $this->serviceName.".".$service; |
---|
169 | } |
---|
170 | |
---|
171 | return self::locate( $service, $arguments ); |
---|
172 | } |
---|
173 | |
---|
174 | function dispatch( $service, $arguments ) |
---|
175 | { |
---|
176 | //handle here dispatch with invalid services |
---|
177 | if( $this ) |
---|
178 | { |
---|
179 | $service = strrpos( $service, "." ) ? $service : $this->serviceName.".".$service; |
---|
180 | } |
---|
181 | |
---|
182 | return self::locate( $service, $arguments ); |
---|
183 | } |
---|
184 | |
---|
185 | function __call( $method, $arguments ) |
---|
186 | { |
---|
187 | return self::locate( $method, $arguments, $this->serviceName ); |
---|
188 | } |
---|
189 | |
---|
190 | function __get( $name ) |
---|
191 | { |
---|
192 | return $this->service->$name; |
---|
193 | } |
---|
194 | |
---|
195 | static function __callStatic( $method, $arguments ) |
---|
196 | { |
---|
197 | return self::locate( $method, $arguments ); |
---|
198 | } |
---|
199 | |
---|
200 | static function getService( $service ) |
---|
201 | { |
---|
202 | if( !self::$locators[ $service ] ) |
---|
203 | { |
---|
204 | self::deploy( $service ); |
---|
205 | } |
---|
206 | |
---|
207 | return( clone self::$locators[ $service ] ); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | //estudar uma forma mais elegante de carregar os servicos |
---|
214 | //ServiceLocator::load( "services.conf" ); |
---|
215 | |
---|
216 | ?> |
---|