-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationController.php
105 lines (101 loc) · 3.65 KB
/
ApplicationController.php
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
<?php
namespace BlueSeed;
/**
*
* This Controller launch the target controller and request the request action
* @author ivonascimento <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @package system
*/
use BlueSeed\Observer\Observer;
use \Application;
use \System;
class ApplicationController extends Controller {
/**
*
* The constructor of Controller set type of ObserverCollection
* @param \BlueSeed\Request $R
*/
public function __construct(Request $R){
parent::__construct($R);
// $this->observerCollection = new ObserverCollection();
}
/**
*
* Test the Request and Dispatch it
* @access public
* @return void
*/
public function dispatch(){
if (!is_null(APP_ROUTE_FILE) and is_file(APP_ROUTE_FILE)) {
$routes = parse_ini_file(APP_ROUTE_FILE);
if ($routes !== false) {
$router = new Router($routes, $this->getRequest());
$router->resolve(LANGUAGE);
}
}
if ( $this->hasController($this->getRequest()->getQuery(0))){
try{
$controller = $this->getRequest()->getController();//getQuery(0);
$controllername = "\\Application\\Controller\\".$controller;//>controller;
$controller = new $controllername($this->getRequest());
$controllermethod = $controller->getRequest()->getAction();//getQuery(1);
if( method_exists($controller, $controllermethod)) {
foreach ($this->observerCollection as $obs) {
$controller->attachObserver($obs);
}
$controller->notifyObservers();
$controller->$controllermethod();
}
else {
if (defined("LOGFILE")) {
$log = fopen(LOGFILE,"a");
var_dump("not found:{$controllername}-> {$this->action}\n");
fwrite($log,"not found:{$controllername}-> {$this->action}\n");
//fwrite($log, (string) $E."\n");
fclose($log);
}
$this->notfound($this->controller, $this->action);
// $controller->Index();
}
}catch(\Exception $E){
if (defined("LOGFILE")) {
$log = fopen(LOGFILE,"a");
fwrite($log, (string) $E."\n");
fclose($log);
}
$this->notfound($this->controller, $this->action);
}
}else{
$this->notFound($this->controller, $this->action);
}
}
/**
*
* verify if controller exist
* @param string $controllername
* @return bool
* @access private
*/
private function hasController($controllername){
return file_exists( APP_PATH."Controller/{$controllername}.php" );
}
/**
*
* Default behavior if controller and method does not exist
* @param string $controller
* @param string $action
* @return void
*/
private function notfound($controller, $action){
$controller = new \Application\Controller\IndexController($this->getRequest());
foreach ($this->observerCollection as $obs) {
$controller->attachObserver($obs);
}
$controller->notifyObservers();
$controller->notfound();
// \BlueSeed\View::set('controller', $controller);
// \BlueSeed\View::set('action', $action);
// \BlueSeed\View::render('system_notfound');
}
}