1 | <?php |
---|
2 | /** |
---|
3 | * |
---|
4 | * Copyright (C) 2012 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify it under |
---|
7 | * the terms of the GNU Affero General Public License version 3 as published by |
---|
8 | * the Free Software Foundation with the addition of the following permission |
---|
9 | * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED |
---|
10 | * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE |
---|
11 | * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
15 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
16 | * details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Affero General Public License |
---|
19 | * along with this program; if not, see www.gnu.org/licenses or write to |
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
21 | * MA 02110-1301 USA. |
---|
22 | * |
---|
23 | * This code is based on the OpenXchange Connector and on the Prognus pSync |
---|
24 | * Connector both developed by the community and licensed under the GPL |
---|
25 | * version 2 or above as published by the Free Software Foundation. |
---|
26 | * |
---|
27 | * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves, |
---|
28 | * 6731, PTI, Edifício do Saber, 3º floor, room 306, Foz do Iguaçu - PR - Brasil or at |
---|
29 | * e-mail address prognus@prognus.com.br. |
---|
30 | * |
---|
31 | * Classe de controle que faz manipulações de fluxo de informações para toda |
---|
32 | * a API a partir de vários métodos. |
---|
33 | * |
---|
34 | * @package Prototype |
---|
35 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
36 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
37 | * @version 2.4 |
---|
38 | * @sponsor Caixa Econômica Federal |
---|
39 | * @since Arquivo disponibilizado na versão 2.4 |
---|
40 | */ |
---|
41 | |
---|
42 | if( !defined( 'ROOTPATH' ) ) |
---|
43 | define( 'ROOTPATH', dirname(__FILE__).'/..' ); |
---|
44 | |
---|
45 | require_once(ROOTPATH.'/api/config.php'); |
---|
46 | |
---|
47 | /** |
---|
48 | TODO list: |
---|
49 | |
---|
50 | * definir de forma centralizada os caminhos e as constantes necessárias; |
---|
51 | * criar um User Agent detect e um OS server detect para customizações espeçÃficas de cada browser / servidor; |
---|
52 | * criar um registrador para fallback handlers; |
---|
53 | * criar um dependency manager na configuração dos serviços, para poder gerenciar os imports corretamente |
---|
54 | * criar um login e a recuperação da sessão; |
---|
55 | |
---|
56 | */ |
---|
57 | |
---|
58 | /** |
---|
59 | * |
---|
60 | * @package Prototype |
---|
61 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
62 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
63 | * @version 2.4 |
---|
64 | * @sponsor Caixa Econômica Federal |
---|
65 | * @since Classe disponibilizada na versão 2.4 |
---|
66 | */ |
---|
67 | class Controller { |
---|
68 | |
---|
69 | static $cache; |
---|
70 | static $services = array(); |
---|
71 | static $interceptors = array(); |
---|
72 | static $config = array(); |
---|
73 | static $includes = array(); |
---|
74 | static $tx = array(); |
---|
75 | static $isConcept = array(); |
---|
76 | static $hasOne = array(); |
---|
77 | static $fallbackHandlers = array(); |
---|
78 | static $txID = 0; |
---|
79 | static $wallet; |
---|
80 | |
---|
81 | public function __destruct() |
---|
82 | { |
---|
83 | // if( $this->service ) |
---|
84 | // $this->service->close(); |
---|
85 | // else |
---|
86 | self::closeAll(); |
---|
87 | } |
---|
88 | |
---|
89 | public static function closeAll() |
---|
90 | { |
---|
91 | if( self::$services ) |
---|
92 | foreach( self::$services as $serviceName => $service ) |
---|
93 | if( self::$config[ $serviceName ]['type'] === 'service' ) |
---|
94 | $service->close(); |
---|
95 | } |
---|
96 | |
---|
97 | public static function clearAll() |
---|
98 | { |
---|
99 | return self::$cache->clearAll(); |
---|
100 | } |
---|
101 | |
---|
102 | public static function clear( $id ) |
---|
103 | { |
---|
104 | return self::$cache->clear( $id ); |
---|
105 | } |
---|
106 | |
---|
107 | public static function check( $id ) |
---|
108 | { |
---|
109 | return self::$cache->get( $id ); |
---|
110 | } |
---|
111 | |
---|
112 | public static function store( $id, $data, $expires, $compressed ) |
---|
113 | { |
---|
114 | return self::$cache->put( $id, $data, $expires, $compressed ); |
---|
115 | } |
---|
116 | |
---|
117 | public static function find( $URI, $params = false, $criteria = false ) |
---|
118 | { |
---|
119 | if( isset($URI['id']) && $URI['id'] ) |
---|
120 | return self::read( $URI, $params, $criteria ); |
---|
121 | |
---|
122 | return self::call( 'find', $URI, $params, $criteria ); |
---|
123 | } |
---|
124 | |
---|
125 | public static function read( $URI, $params = false, $criteria = false ) |
---|
126 | { |
---|
127 | if( !isset($URI['id']) || !$URI['id'] ) |
---|
128 | return self::find( $URI, $params, $criteria ); |
---|
129 | |
---|
130 | return self::call( 'read', $URI, $params, $criteria ); |
---|
131 | } |
---|
132 | |
---|
133 | public static function deleteAll( $URI, $params = false, $criteria = false ) |
---|
134 | { |
---|
135 | if( isset($URI['id']) && $URI['id'] ) |
---|
136 | return self::delete( $URI, $params, $criteria ); |
---|
137 | |
---|
138 | return self::call( 'deleteAll', $URI, $params, $criteria ); |
---|
139 | } |
---|
140 | |
---|
141 | public static function delete( $URI, $params = false, $criteria = false ) |
---|
142 | { |
---|
143 | if( !isset($URI['id']) || !$URI['id'] ) |
---|
144 | return self::deleteAll( $URI, $params, $criteria ); |
---|
145 | |
---|
146 | return self::call( 'delete', $URI, $params, $criteria ); |
---|
147 | } |
---|
148 | |
---|
149 | public static function replace( $URI, $params, $criteria = false ) |
---|
150 | { |
---|
151 | if( isset($URI['id']) && $URI['id'] ) |
---|
152 | return self::update( $URI, $params, $criteria ); |
---|
153 | |
---|
154 | return self::call( 'replace', $URI, $params, $criteria ); |
---|
155 | } |
---|
156 | |
---|
157 | public static function update( $URI, $params, $criteria = false ) |
---|
158 | { |
---|
159 | if( !isset($URI['id']) || !$URI['id'] ) |
---|
160 | return self::replace( $URI, $params, $criteria ); |
---|
161 | |
---|
162 | return self::call( 'update', $URI, $params, $criteria ); |
---|
163 | } |
---|
164 | |
---|
165 | public static function create( $URI, $params, $criteria = false ) |
---|
166 | { |
---|
167 | return self::call( 'create', $URI, $params, $criteria ); |
---|
168 | } |
---|
169 | |
---|
170 | public static function begin( $URI, $params = false, $criteria = false ) |
---|
171 | { |
---|
172 | return self::call( 'begin', $URI, $params, $criteria ); |
---|
173 | } |
---|
174 | |
---|
175 | public static function commit( $URI, $criteria = false ) |
---|
176 | { |
---|
177 | return self::call( 'commit', $URI, false, $criteria ); |
---|
178 | } |
---|
179 | |
---|
180 | public static function rollback( $URI, $criteria = false ) |
---|
181 | { |
---|
182 | if( isset( $URI['service'] ) ) |
---|
183 | unset( self::$tx[ $URI['service'] ] ); |
---|
184 | |
---|
185 | self::$txID--; |
---|
186 | |
---|
187 | return self::call( 'rollback', $URI, false, $criteria ); |
---|
188 | } |
---|
189 | |
---|
190 | public static function format( $URI, $params, $criteria = false ) |
---|
191 | { |
---|
192 | return self::call( 'format', $URI, $params, $criteria ); |
---|
193 | } |
---|
194 | |
---|
195 | public static function parse( $URI, $data, $criteria = false ) |
---|
196 | { |
---|
197 | return self::call( 'parse', $URI, $data, $criteria ); |
---|
198 | } |
---|
199 | |
---|
200 | public static function URI( $className, $id = false, $service = false ) |
---|
201 | { |
---|
202 | return array( 'concept' => $className, |
---|
203 | 'service' => $service ? $service : false, |
---|
204 | 'id' => $id ? $id : '' ); |
---|
205 | } |
---|
206 | |
---|
207 | //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados |
---|
208 | public static function links( $concept = false, $linkage = false ) |
---|
209 | { |
---|
210 | |
---|
211 | |
---|
212 | if( !isset(self::$config[ $concept ]) ) |
---|
213 | self::$config[ $concept ] = self::loadConfig( $concept ); |
---|
214 | |
---|
215 | $links = array(); |
---|
216 | self::$isConcept[ $concept ] = array(); |
---|
217 | self::$hasOne[ $concept ] = array(); |
---|
218 | |
---|
219 | if( isset(self::$config[ $concept ][ 'model.hasOne' ]) ) |
---|
220 | foreach( self::$config[ $concept ][ 'model.hasOne' ] as $linkName => $linkTarget ) |
---|
221 | { |
---|
222 | list( $target, $link ) = explode( '.', $linkTarget ); |
---|
223 | |
---|
224 | if( $linkage === $linkName ) |
---|
225 | $return = $link; |
---|
226 | |
---|
227 | $links[$linkName] = $target; |
---|
228 | self::$hasOne[ $concept ][ $linkName ] = true; |
---|
229 | } |
---|
230 | if( isset(self::$config[ $concept ][ 'model.depends' ]) ) |
---|
231 | foreach( self::$config[ $concept ][ 'model.depends' ] as $linkName => $linkTarget ) |
---|
232 | { |
---|
233 | list( $target, $link ) = explode( '.', $linkTarget ); |
---|
234 | |
---|
235 | if( $linkage === $linkName ) |
---|
236 | $return = $link; |
---|
237 | |
---|
238 | $links[$linkName] = $target; |
---|
239 | self::$hasOne[ $concept ][ $linkName ] = true; |
---|
240 | self::$isConcept[ $concept ][ $linkName ] = true; |
---|
241 | } |
---|
242 | if( isset(self::$config[ $concept ][ 'model.hasMany' ]) ) |
---|
243 | foreach( self::$config[ $concept ][ 'model.hasMany' ] as $linkName => $linkTarget ) |
---|
244 | { |
---|
245 | list( $target, $link ) = explode( '.', $linkTarget ); |
---|
246 | |
---|
247 | if( $linkage === $linkName ) |
---|
248 | $return = $link; |
---|
249 | |
---|
250 | $links[$linkName] = $target; |
---|
251 | } |
---|
252 | |
---|
253 | return( isset($return) ? $return : $links ); |
---|
254 | } |
---|
255 | |
---|
256 | public static function isConcept( $concept, $linkName ) |
---|
257 | { |
---|
258 | if( !isset( self::$isConcept[ $concept ] ) ) |
---|
259 | self::links( $concept ); |
---|
260 | |
---|
261 | return( isset(self::$isConcept[ $concept ][ $linkName ]) ); |
---|
262 | } |
---|
263 | |
---|
264 | public static function hasOne( $concept, $linkName ) |
---|
265 | { |
---|
266 | if( !isset( self::$hasOne[ $concept ] ) ) |
---|
267 | self::links( $concept ); |
---|
268 | |
---|
269 | return( isset(self::$hasOne[ $concept ][ $linkName ]) ); |
---|
270 | } |
---|
271 | |
---|
272 | public static function getConcept( $concept, $moduleName = false ) |
---|
273 | { |
---|
274 | if( isset( self::$config[ $concept ] ) ) |
---|
275 | return( self::$config[ $concept ] ); |
---|
276 | |
---|
277 | return( self::$config[ $concept ] = self::loadConfig( $concept, $moduleName ) ); |
---|
278 | } |
---|
279 | |
---|
280 | public static function loadCache( $cacheType = 'Memory' ) |
---|
281 | { |
---|
282 | include_once( "cache/MemoryCache.php" ); |
---|
283 | return new MemoryCache(); |
---|
284 | } |
---|
285 | |
---|
286 | //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados |
---|
287 | public static function loadConfig( $className, $isService = false) |
---|
288 | { |
---|
289 | $fileName = $className.'.'.($isService ? 'srv' : 'ini'); |
---|
290 | |
---|
291 | $config = self::$cache->get( $fileName ); |
---|
292 | |
---|
293 | if( !$config ) |
---|
294 | { |
---|
295 | $config = parse_ini_file( ROOTPATH.'/config/'.$fileName, true ); |
---|
296 | |
---|
297 | self::$cache->put( $fileName, $config ); |
---|
298 | } |
---|
299 | |
---|
300 | return( $config ); |
---|
301 | } |
---|
302 | |
---|
303 | public static function import( $path, $ext = ".php" ) |
---|
304 | { |
---|
305 | if( !isset(self::$includes[$path]) ) |
---|
306 | { |
---|
307 | require_once( ROOTPATH.'/'.$path.$ext ); |
---|
308 | self::$includes[$path] = false; |
---|
309 | } |
---|
310 | |
---|
311 | return( self::$includes[$path] ); |
---|
312 | } |
---|
313 | |
---|
314 | public static function load( $path, $class = false ) |
---|
315 | { |
---|
316 | if( $return = self::import( $path, "" ) ) |
---|
317 | return( $return ); |
---|
318 | |
---|
319 | if( !$class ){ |
---|
320 | preg_match( '/^\/?.*\/([^\/]+).php$/', $path, $class ); |
---|
321 | $class = $class[1]; |
---|
322 | } |
---|
323 | |
---|
324 | $object = self::$cache->get( $class ); |
---|
325 | |
---|
326 | if( !$object ) |
---|
327 | { |
---|
328 | $object = new $class(); |
---|
329 | self::$cache->put( $class, $object ); |
---|
330 | } |
---|
331 | |
---|
332 | self::$includes[$path] = $object; |
---|
333 | |
---|
334 | return( $object ); |
---|
335 | } |
---|
336 | |
---|
337 | public static function wallet( $serviceName ) |
---|
338 | { |
---|
339 | if( !isset( self::$wallet ) ) |
---|
340 | { |
---|
341 | //// Hack //// TODO: passar o init da sessão no login do expresso |
---|
342 | Config::init(); |
---|
343 | |
---|
344 | if(isset($_SESSION['wallet'])) |
---|
345 | self::$wallet = $_SESSION['wallet']; |
---|
346 | ///////////// |
---|
347 | } |
---|
348 | |
---|
349 | return isset( self::$wallet[ $serviceName ] )? self::$wallet[ $serviceName ] : false; |
---|
350 | } |
---|
351 | |
---|
352 | public static function connect( $service, $config ) |
---|
353 | { |
---|
354 | $result = $service->open( $config ); |
---|
355 | |
---|
356 | if( is_string( $result ) ) |
---|
357 | throw new Exception( $result ); |
---|
358 | |
---|
359 | return( true ); |
---|
360 | } |
---|
361 | |
---|
362 | public static function configure( $config, $newConfig ) |
---|
363 | { |
---|
364 | foreach( $newConfig as $key => $value ) |
---|
365 | $config[$key] = $value; |
---|
366 | |
---|
367 | return( $config ); |
---|
368 | } |
---|
369 | |
---|
370 | public static function dispatch( $dispatcher, $data, $optionsMap = false ) |
---|
371 | { |
---|
372 | // if( $mappedTo ) |
---|
373 | // $data = array( $mappedTo => $data ); |
---|
374 | // |
---|
375 | // foreach( $data as $method => $params ) |
---|
376 | // { |
---|
377 | // // foreach( $data[ $method ] as $name => $value ) |
---|
378 | // } |
---|
379 | // |
---|
380 | // self::import( "$dispatcher.php" ); |
---|
381 | } |
---|
382 | |
---|
383 | //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados |
---|
384 | public static function service( $serviceName, $concept = false ) |
---|
385 | { |
---|
386 | if( isset( self::$services[ $serviceName ] ) ) |
---|
387 | return self::$services[ $serviceName ]; |
---|
388 | |
---|
389 | if( !isset(self::$config[ $serviceName ]) ) |
---|
390 | self::$config[ $serviceName ] = self::loadConfig( $serviceName, true ); |
---|
391 | |
---|
392 | if( !isset(self::$config[ $serviceName ]) ) |
---|
393 | return( false ); |
---|
394 | |
---|
395 | if( !isset(self::$config[ $serviceName ]['type']) ) |
---|
396 | self::$config[ $serviceName ]['type'] = 'service'; |
---|
397 | |
---|
398 | self::import( 'api/'.self::$config[ $serviceName ]['type'] ); //TODO: Item 4 |
---|
399 | |
---|
400 | $service = self::load( self::$config[ $serviceName ]['path'], |
---|
401 | self::$config[ $serviceName ]['class'] ); |
---|
402 | |
---|
403 | $srvConfig = array(); |
---|
404 | |
---|
405 | if( isset(self::$config[ $serviceName ][ 'config' ]) ) |
---|
406 | $srvConfig = self::configure( $srvConfig, self::$config[ $serviceName ][ 'config' ] ); |
---|
407 | if( $wallet = self::wallet( $serviceName ) ) |
---|
408 | $srvConfig = self::configure( $srvConfig, $wallet ); |
---|
409 | if( $concept && isset(self::$config[ $concept ]['service.config']) ) |
---|
410 | $srvConfig = self::configure( $srvConfig, self::$config[ $concept ]['service.config'] ); |
---|
411 | |
---|
412 | if( empty( $srvConfig ) ) |
---|
413 | $srvConfig = false; |
---|
414 | |
---|
415 | if( $service && self::$config[ $serviceName ]['type'] === 'service' ) |
---|
416 | self::connect( $service, $srvConfig ); |
---|
417 | |
---|
418 | return( self::$services[ $serviceName ] = $service ); |
---|
419 | } |
---|
420 | |
---|
421 | //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados |
---|
422 | public static function interceptor( $method, $concept = false, $serviceName = false, $isService = false ) |
---|
423 | { |
---|
424 | if( $concept && !isset(self::$config[ $concept ]) ) |
---|
425 | self::$config[ $concept ] = self::loadConfig( $concept ); |
---|
426 | |
---|
427 | if( !$concept ) $concept = 'global'; |
---|
428 | if( !$isService || !$serviceName ) $serviceName = 'global'; |
---|
429 | |
---|
430 | if( !isset( self::$interceptors[ $concept ] ) ) |
---|
431 | self::$interceptors[ $concept ] = array(); |
---|
432 | |
---|
433 | if( !isset( self::$interceptors[ $concept ][ $serviceName ] ) ) |
---|
434 | self::$interceptors[ $concept ][ $serviceName ] = array(); |
---|
435 | |
---|
436 | if( !isset( self::$interceptors[ $concept ][ $serviceName ][ $method ] ) ) |
---|
437 | { |
---|
438 | $events = array( 'before', 'after' ); |
---|
439 | $interceptors = array(); |
---|
440 | |
---|
441 | $prefix = ( $isService )? "$serviceName." : ""; |
---|
442 | |
---|
443 | foreach( $events as $i => $event ) |
---|
444 | { |
---|
445 | $interceptors[$event] = array(); |
---|
446 | |
---|
447 | if( !isset(self::$config[$concept]["$prefix$event.$method"]) ) |
---|
448 | continue; |
---|
449 | |
---|
450 | foreach( self::$config[$concept]["$prefix$event.$method"] as $intercept => $interceptor ) |
---|
451 | $interceptors[$event][$intercept] = self::load( $interceptor ); |
---|
452 | } |
---|
453 | |
---|
454 | self::$interceptors[ $concept ][ $serviceName ][ $method ] = $interceptors; |
---|
455 | } |
---|
456 | |
---|
457 | return( self::$interceptors[ $concept ][ $serviceName ][ $method ] ); |
---|
458 | } |
---|
459 | |
---|
460 | public static function interceptorCommit( $eventType, $commitList, $isService = false ) |
---|
461 | { |
---|
462 | $result = array( $eventType => array() ); |
---|
463 | |
---|
464 | if( is_array( $commitList ) ) |
---|
465 | foreach( $commitList as $i => $tx ) |
---|
466 | { |
---|
467 | $interceptors = self::interceptor( 'commit', $tx['concept'], $tx['service'], $isService ); |
---|
468 | |
---|
469 | $result[$eventType] = array_merge( $result[$eventType], $interceptors[$eventType] ); |
---|
470 | } |
---|
471 | |
---|
472 | return( $result ); |
---|
473 | } |
---|
474 | |
---|
475 | public static function fire( $eventType, $method, &$params, $original, $isService = false ) |
---|
476 | { |
---|
477 | if( $method === 'commit' ) |
---|
478 | $interceptors = self::interceptorCommit( $eventType, $params['criteria'], $isService ); |
---|
479 | |
---|
480 | else |
---|
481 | $interceptors = self::interceptor( $method, |
---|
482 | isset($original['URI']['concept']) ? $original['URI']['concept'] : false, |
---|
483 | isset($params['URI']['service']) ? $params['URI']['service'] : false, $isService ); |
---|
484 | |
---|
485 | if( $interceptors && isset($interceptors[ $eventType ]) ) |
---|
486 | foreach( $interceptors[ $eventType ] as $intercept => $interceptor ) |
---|
487 | { |
---|
488 | $return = $interceptor->$intercept( $params['URI'], $params['properties'], $params['criteria'], $original /*, $params['service'] */); |
---|
489 | |
---|
490 | if( $return === false ) |
---|
491 | return( false ); |
---|
492 | |
---|
493 | if( isset($return) ) |
---|
494 | $params['properties'] = $return; |
---|
495 | } |
---|
496 | |
---|
497 | return( $params ); |
---|
498 | } |
---|
499 | |
---|
500 | /* |
---|
501 | * ex: array |
---|
502 | * ( |
---|
503 | * [0] array( 'OR', array( array( '=', 'campo', 'valor' ), |
---|
504 | array( 'OR', array( array( '=', 'campo', 'valor' ) ) ) ) |
---|
505 | * [1] array( '=', 'campo' , 'valor' ) |
---|
506 | * [2] array( 'OR' , array( array( '=' , campo', 'valor' ) ) ) |
---|
507 | * [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) ) |
---|
508 | * ) |
---|
509 | * OR |
---|
510 | * array( '=' , 'campo' , 'valor' ) |
---|
511 | */ |
---|
512 | |
---|
513 | //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados |
---|
514 | public static function serviceName( $URI, $original = false ) |
---|
515 | { |
---|
516 | $concept = ""; |
---|
517 | |
---|
518 | if( $original && isset($original['concept']) && $original['concept'] ) |
---|
519 | $concept = $original['concept']; |
---|
520 | elseif( isset($URI['concept']) && $URI['concept'] ) |
---|
521 | $concept = $URI['concept']; |
---|
522 | |
---|
523 | if( ( !isset($URI['service']) || !$URI['service'] ) && $concept ) |
---|
524 | { |
---|
525 | if( !isset(self::$config[ $concept ]) ) |
---|
526 | self::$config[ $concept ] = self::loadConfig( $concept ); |
---|
527 | |
---|
528 | $URI['service'] = self::$config[ $concept ][ 'service' ]; |
---|
529 | } |
---|
530 | |
---|
531 | if( !isset($URI['service']) ) |
---|
532 | throw new Exception( "CONFIGURATION ERROR: service name from concept '$concept' not found" ); |
---|
533 | |
---|
534 | return( $URI ); |
---|
535 | } |
---|
536 | |
---|
537 | public static function finalizeCommit( $TX, $params, $original, $method ) |
---|
538 | { |
---|
539 | $TX['rollback'] = !!!$params['properties']; |
---|
540 | |
---|
541 | if( $params['properties'] && is_array($params['properties']) && isset($params['properties']['id']) ) |
---|
542 | $TX['id'] = $params['properties']['id']; |
---|
543 | |
---|
544 | self::$tx[ $params['URI']['service'] ][] = array_merge( $TX, $original['URI'], array( 'service' => $params['URI']['service'], 'method' => $method ) ); |
---|
545 | |
---|
546 | if( isset($commit) && $commit ) |
---|
547 | { |
---|
548 | if( !self::call( 'commit', $params['URI'], false, self::$tx[ $params['URI']['service'] ] ) ) |
---|
549 | self::call( 'rollback', $params['URI'] , false, self::$tx[ $params['URI']['service'] ] ); |
---|
550 | |
---|
551 | unset( self::$tx[ $params['URI']['service'] ] ); |
---|
552 | } |
---|
553 | |
---|
554 | return( empty($params['properties']) ? false : $params['properties'] ); |
---|
555 | } |
---|
556 | |
---|
557 | //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmos nas options passadas |
---|
558 | public static function call( $method, $URI, $properties = false, $options = false, $service = false ) |
---|
559 | { |
---|
560 | try |
---|
561 | { |
---|
562 | if( !isset($URI['concept']) ) $URI['concept'] = false; |
---|
563 | |
---|
564 | $original = $params = array( 'properties' => $properties, |
---|
565 | 'criteria' => $options, |
---|
566 | 'URI' => $URI, |
---|
567 | 'service' => $service ); |
---|
568 | |
---|
569 | if( isset($params['URI']['concept']) && !self::fire( 'before', $method, $params, $original ) ) |
---|
570 | return( !isset($TX) ? empty($params['properties']) ? false : $params['properties'] : self::finalizeCommit( $TX, $params, $original, $method ) ); |
---|
571 | |
---|
572 | |
---|
573 | |
---|
574 | if( $params && !$params['service'] ) |
---|
575 | { |
---|
576 | $params['URI'] = self::serviceName( $params['URI'], $original['URI'] ); |
---|
577 | |
---|
578 | $params['service'] = self::service( $params['URI']['service'], $params['URI']['concept'] ); |
---|
579 | } |
---|
580 | |
---|
581 | if( isset($params['URI']['service']) ) |
---|
582 | { |
---|
583 | if( $method === 'create' || $method === 'update' || $method === 'delete' ) |
---|
584 | { |
---|
585 | if( $commit = !isset(self::$tx[ $params['URI']['service'] ]) ) |
---|
586 | { |
---|
587 | self::call( 'begin', $params['URI'] ); |
---|
588 | } |
---|
589 | |
---|
590 | $TX = array(); |
---|
591 | } |
---|
592 | |
---|
593 | if( !self::fire( 'before', $method, $params, $original, true ) ) |
---|
594 | return( !isset($TX) ? empty($params['properties']) ? false : $params['properties'] : self::finalizeCommit( $TX, $params, $original, $method ) ); |
---|
595 | } |
---|
596 | |
---|
597 | if( $params['service'] ) |
---|
598 | switch( $method ) |
---|
599 | { |
---|
600 | case 'find': $return = $params['service']->find( $params['URI'], $params['properties'], $params['criteria'] ); break; |
---|
601 | |
---|
602 | case 'read': $return = $params['service']->read( $params['URI'], $params['properties'] , $params['criteria'] ); break; |
---|
603 | |
---|
604 | case 'create': $return = $params['service']->create( $params['URI'], $params['properties']/*, $criteria*/ ); break; |
---|
605 | |
---|
606 | case 'delete': $return = $params['service']->delete( $params['URI'], $params['properties'], $params['criteria'] ); break; |
---|
607 | |
---|
608 | case 'deleteAll': $return = $params['service']->deleteAll( $params['URI'], $params['properties'], $params['criteria'] ); break; |
---|
609 | |
---|
610 | case 'update': $return = $params['service']->update( $params['URI'], $params['properties'], $params['criteria'] ); break; |
---|
611 | |
---|
612 | case 'replace': $return = $params['service']->replace( $params['URI'], $params['properties'], $params['criteria'] ); break; |
---|
613 | |
---|
614 | case 'begin': $return = $params['service']->begin( $params['URI'] ); break; |
---|
615 | |
---|
616 | case 'commit': $return = $params['service']->commit( $params['URI'], $params['criteria'] ); break; |
---|
617 | |
---|
618 | case 'rollback': $return = $params['service']->rollback( $params['URI'], $params['criteria'] ); break; |
---|
619 | |
---|
620 | case 'parse': $return = $params['service']->parse( $params['properties'], $params['criteria'] ); break; |
---|
621 | |
---|
622 | case 'analize': $return = $params['service']->analize( $params['properties'], $params['criteria'] ); break; |
---|
623 | |
---|
624 | case 'format': $return = $params['service']->format( $params['properties'], $params['criteria'] ); break; |
---|
625 | |
---|
626 | default : $return = $params['service']->$method( $params['properties'], $params['criteria'] ); |
---|
627 | } |
---|
628 | |
---|
629 | if( isset($return) && $return !== false ) |
---|
630 | $params['properties'] = $return; |
---|
631 | |
---|
632 | if( isset($params['URI']['service']) ) |
---|
633 | if( !self::fire( 'after', $method, $params, $original, true ) ) |
---|
634 | return( !isset($TX) ? empty($params['properties']) ? false : $params['properties'] : self::finalizeCommit( $TX, $params, $original, $method ) ); |
---|
635 | |
---|
636 | if( isset($URI['concept']) ) |
---|
637 | self::fire( 'after', $method, $params, $original ); |
---|
638 | |
---|
639 | if( empty($params['properties']) ) |
---|
640 | $params['properties'] = false; |
---|
641 | |
---|
642 | if( isset( $TX ) ) |
---|
643 | self::finalizeCommit( $TX, $params, $original, $method ); |
---|
644 | } |
---|
645 | catch( Exception $e ) |
---|
646 | { |
---|
647 | if( !self::fallback( $e, $URI ) ) |
---|
648 | self::closeAll(); |
---|
649 | |
---|
650 | return( false ); |
---|
651 | } |
---|
652 | |
---|
653 | return( $params['properties'] ); |
---|
654 | } |
---|
655 | |
---|
656 | public static function fallback( $exception, $URI ) |
---|
657 | { |
---|
658 | $code = $exception->getCode(); |
---|
659 | |
---|
660 | if( isset( self::$fallbackHandlers[ $code ] ) ) |
---|
661 | { |
---|
662 | $fn = self::$fallbackHandlers[ $code ]; |
---|
663 | return $fn( $exception, $URI ); |
---|
664 | } |
---|
665 | |
---|
666 | error_log( $exception->getMessage() ); |
---|
667 | return( true ); |
---|
668 | } |
---|
669 | |
---|
670 | public static function addFallbackHandler( $code, $function ) |
---|
671 | { |
---|
672 | self::$fallbackHandlers[ $code ] = $function; |
---|
673 | } |
---|
674 | /* |
---|
675 | *NULL evita erros caso não seja passado nenhuma variavel por referência |
---|
676 | */ |
---|
677 | public static function put( $URI, $data, &$txIds = NULL ) |
---|
678 | { |
---|
679 | try |
---|
680 | { |
---|
681 | $URI = self::serviceName( $URI ); |
---|
682 | |
---|
683 | if( $commit = !$txIds ) |
---|
684 | $txIds = array(); |
---|
685 | |
---|
686 | if( !isset( self::$tx[ $URI['service'] ] ) ) |
---|
687 | { |
---|
688 | self::call( 'begin', $URI ); |
---|
689 | self::$tx[ $txIds[] = $URI['service'] ] = array(); |
---|
690 | } |
---|
691 | |
---|
692 | $method = $data ? isset( $data['id'] ) ? |
---|
693 | 'update' : 'create' : 'delete'; |
---|
694 | |
---|
695 | $links = self::links( $URI['concept'] ); |
---|
696 | |
---|
697 | $order = self::$txID++; |
---|
698 | |
---|
699 | $postpone = array(); |
---|
700 | $linkNames = array(); |
---|
701 | |
---|
702 | if( $data ) |
---|
703 | { |
---|
704 | $URI['id'] = isset( $data['id'] ) ? $data['id'] : false; |
---|
705 | |
---|
706 | foreach( $links as $linkName => $linkTarget ) |
---|
707 | { |
---|
708 | if( isset( $data[$linkName] ) && is_array( $data[$linkName] ) ) |
---|
709 | { |
---|
710 | if( self::isConcept( $URI['concept'], $linkName ) ) |
---|
711 | $data[$linkName] = self::put( array( 'concept' => $linkTarget ), $data[$linkName], $txIds ); |
---|
712 | else |
---|
713 | { |
---|
714 | $postpone[ $linkTarget ] = $data[$linkName]; |
---|
715 | $linkNames[ $linkTarget ] = $linkName; |
---|
716 | } |
---|
717 | } |
---|
718 | } |
---|
719 | } |
---|
720 | else |
---|
721 | $URI['id'] = isset( $data['id'] ) ? $data['id'] : $URI['id']; |
---|
722 | |
---|
723 | $result = Controller::call( $method, $URI, $data, false, false, true ); |
---|
724 | |
---|
725 | if( is_array( $result ) && isset( $result['id'] ) ) |
---|
726 | $URI['id'] = $result['id']; |
---|
727 | |
---|
728 | self::$tx[ $URI['service'] ][ count(self::$tx[ $URI['service'] ]) - 1 ]['order'] = $order; |
---|
729 | self::$tx[ $URI['service'] ][ count(self::$tx[ $URI['service'] ]) - 1 ]['id'] = $URI['id']; |
---|
730 | |
---|
731 | foreach( $postpone as $linkTarget => $dt ) |
---|
732 | { |
---|
733 | if( Controller::hasOne( $URI['concept'], $linkNames[ $linkTarget ] ) ) |
---|
734 | $dt = array( $dt ); |
---|
735 | |
---|
736 | foreach( $dt as $ii => $value ) |
---|
737 | { |
---|
738 | if( !is_array( $value ) ) |
---|
739 | $value = array( 'id' => $value ); |
---|
740 | |
---|
741 | $value[ self::links( $URI['concept'], $linkNames[ $linkTarget ] ) ] = $URI['id']; |
---|
742 | |
---|
743 | self::put( array( 'concept' => $linkTarget ), $value, $txIds ); |
---|
744 | } |
---|
745 | } |
---|
746 | if( $commit ) |
---|
747 | { |
---|
748 | $result = array(); |
---|
749 | |
---|
750 | for( $i = count( $txIds ) - 1; $i >= 0; $i-- ) |
---|
751 | { |
---|
752 | $currentTx = self::$tx[ $txIds[$i] ]; |
---|
753 | unset( self::$tx[ $txIds[$i] ] ); |
---|
754 | |
---|
755 | if( !self::commit( array( 'service' => $txIds[$i] ), $currentTx ) ) |
---|
756 | { |
---|
757 | self::rollback( array( 'service' => $txIds[$i] ), $currentTx ); |
---|
758 | |
---|
759 | foreach( $currentTx as $i => $st ) |
---|
760 | $currentTx[$i][ 'rollback' ] = true; |
---|
761 | } |
---|
762 | |
---|
763 | $result = array_merge( $result, $currentTx ); |
---|
764 | } |
---|
765 | |
---|
766 | self::$txID = 0; |
---|
767 | |
---|
768 | return( $result ); |
---|
769 | } |
---|
770 | |
---|
771 | } |
---|
772 | catch( Exception $e ) |
---|
773 | { |
---|
774 | if( !self::fallback( $e, $URI ) ) |
---|
775 | self::closeAll(); |
---|
776 | |
---|
777 | return( false ); |
---|
778 | } |
---|
779 | |
---|
780 | return( $URI['id'] ); |
---|
781 | } |
---|
782 | |
---|
783 | public static function get() |
---|
784 | { |
---|
785 | |
---|
786 | } |
---|
787 | } |
---|
788 | |
---|
789 | Controller::$cache = Controller::loadCache(); |
---|
790 | // ?> |
---|