|
1 |
| -<?php |
2 |
| - |
3 |
| -/** |
4 |
| - * MVC.php |
5 |
| - * @author Fábio Nogueira |
6 |
| - * @version 0.0.4 |
7 |
| - */ |
8 |
| - |
9 |
| -error_reporting(E_ALL & ~E_NOTICE); |
10 |
| -define('MVC_VERSION', '0.0.4'); |
11 |
| - |
12 |
| -//carrega classes do core |
13 |
| -require __DIR__.'/Session.php'; //{{Session.php}} |
14 |
| -require __DIR__.'/View.php'; //{{View.php}} |
15 |
| -require __DIR__.'/Controller.php'; //{{Controller.php}} |
16 |
| - |
17 |
| -class MVC{ |
18 |
| - private static $_config = array(); |
19 |
| - private static $_url; |
20 |
| - |
21 |
| - private static function info(){ |
22 |
| - Response::success(array( |
23 |
| - 'version' => MVC_VERSION, |
24 |
| - 'copyright' => '(c) 2014, DTEC. All rights reserved.', |
25 |
| - 'tostring' => 'MVC Version '.MVC_VERSION . "\nCopyright (c) 2014, DTEC. All rights reserved." |
26 |
| - )); |
27 |
| - } |
28 |
| - private static function routerError($err_message){ |
29 |
| - $templates = self::$_config['templates']; |
30 |
| - $name = isset($templates["error"]) ? $templates["error"] : $templates["default"].'_error'; |
31 |
| - |
32 |
| - if ( file_exists(ROOT.'template/'.$name.'.php') ){ |
33 |
| - require ROOT.'template/'.$name.'.php'; |
34 |
| - exit; |
35 |
| - }else{ |
36 |
| - exit ("router error! <b>$err_message</b>" ); |
37 |
| - } |
38 |
| - exit; |
39 |
| - } |
40 |
| - private static function splitUrl(){ |
41 |
| - self::$_url = array(); |
42 |
| - |
43 |
| - if (BASE_URL!=''){ |
44 |
| - $arr = explode(BASE_URL, $_SERVER['REDIRECT_URL']); |
45 |
| - $url = $arr[1]!='' ? trim($arr[1], '/') : trim($arr[0], '/'); |
46 |
| - }else{ |
47 |
| - $url = trim($_SERVER['REDIRECT_URL'], '/'); |
48 |
| - } |
49 |
| - $url = filter_var($url, FILTER_SANITIZE_URL); |
50 |
| - $arr = explode('/', $url); |
51 |
| - |
52 |
| - // Put URL parts into according properties |
53 |
| - // By the way, the syntax here is just a short form of if/else, called "Ternary Operators" |
54 |
| - // @see http://davidwalsh.name/php-shorthand-if-else-ternary-operators |
55 |
| - self::$_url['controller'] = (isset($arr[0]) ? $arr[0] : null); |
56 |
| - self::$_url['action'] = (isset($arr[1]) ? $arr[1] : null); |
57 |
| - self::$_url['parameter_1'] = (isset($arr[2]) ? $arr[2] : null); |
58 |
| - self::$_url['parameter_2'] = (isset($arr[3]) ? $arr[3] : null); |
59 |
| - self::$_url['parameter_3'] = (isset($arr[4]) ? $arr[4] : null); |
60 |
| - } |
61 |
| - private static function controllerInstance($controllerClassName, $modelClassName=NULL){ |
62 |
| - require ROOT . self::$_config['controllerPath'] . $controllerClassName.'.php'; |
63 |
| - |
64 |
| - $viewName = str_replace("Controller", "View", $controllerClassName); |
65 |
| - |
66 |
| - $instance = new $controllerClassName(); |
67 |
| - if (!self::$_config['ajax']){ |
68 |
| - $instance->view = new View(ROOT. 'view'. DIRECTORY_SEPARATOR . $viewName.'.php'); |
69 |
| - } |
70 |
| - |
71 |
| - if (!is_null($modelClassName)){ |
72 |
| - require ROOT.'model/'.$modelClassName.'.php'; |
73 |
| - $instance->model = new $modelClassName(self::$_config["model"]); |
74 |
| - } |
75 |
| - |
76 |
| - return $instance; |
77 |
| - } |
78 |
| - private static function runController($instance, $actionName){ |
79 |
| - $templates = self::$_config['templates']; |
80 |
| - $parameter_1 = self::$_url['parameter_1']; |
81 |
| - $parameter_2 = self::$_url['parameter_2']; |
82 |
| - $parameter_3 = self::$_url['parameter_3']; |
83 |
| - $view = $instance->view; |
84 |
| - $controller = self::$_url['controller']; |
85 |
| - |
86 |
| - if (isset($instance->model)){ |
87 |
| - $view->model = $instance->model; |
88 |
| - } |
89 |
| - |
90 |
| - //se não tem action, action será 'index' |
91 |
| - $actionName = ($actionName=='' ? 'index' : $actionName); |
92 |
| - |
93 |
| - //chama a action de acordo com a quantidade de parâmetros enviados |
94 |
| - if (method_exists($instance, $actionName)) { |
95 |
| - $output = $instance->{$actionName}($parameter_1, $parameter_2, $parameter_3); |
96 |
| - if (self::$_config['ajax']){ |
97 |
| - Response::success($output); |
98 |
| - } |
99 |
| - }else{ |
100 |
| - self::routerError('action not found'); |
101 |
| - } |
102 |
| - |
103 |
| - //se a action não retornou o conteúdo da view, busca direto da view |
104 |
| - if (is_null($output) && !self::$_config['ajax']){ |
105 |
| - $output = $view->output(); |
106 |
| - } |
107 |
| - |
108 |
| - //carrega o template |
109 |
| - if (!self::$_config['ajax']){ |
110 |
| - $name = isset($templates[$controller]) ? $templates[$controller] : $templates["default"]; |
111 |
| - require ROOT.'template/'.$name.'.php'; |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - public static function getParameter($index){ |
116 |
| - return self::$_url['parameter_'.$index]; |
117 |
| - } |
118 |
| - public static function getConfig(){ |
119 |
| - return self::$_config; |
120 |
| - } |
121 |
| - public static function config($config){ |
122 |
| - self::$_config = array_merge_recursive(self::$_config, $config); |
123 |
| - } |
124 |
| - public static function run(){ |
125 |
| - $cfg = self::$_config; |
126 |
| - |
127 |
| - //define algumas constantes úteis |
128 |
| - define('TEMPLATE', $cfg['template']); |
129 |
| - define('BASE_URL', $cfg['url']['base']); |
130 |
| - |
131 |
| - //separa a url em controller, action e parâmetros |
132 |
| - self::splitUrl(); |
133 |
| - |
134 |
| - if (self::$_url['controller']==='mvc-info'){ |
135 |
| - self::info(); |
136 |
| - } |
137 |
| - |
138 |
| - $controllerClassName = self::$_url['controller']=='' ? 'MainController' : ucfirst(self::$_url['controller'] . $cfg['controllerSufix']); |
139 |
| - $modelClassName = self::$_url['controller']=='' ? 'MainModel' : ucfirst(self::$_url['controller'] . 'Model'); |
140 |
| - $actionName = self::$_url['action']; |
141 |
| - |
142 |
| - //se o arquivo do controller não existe |
143 |
| - if (!file_exists(ROOT.$cfg['controllerPath'] . $controllerClassName . '.php')) { |
144 |
| - self::routerError('controller not found'); |
145 |
| - }else{ |
146 |
| - if (!file_exists(ROOT.'model/' . $modelClassName . '.php')){ |
147 |
| - $modelClassName = NULL; |
148 |
| - } |
149 |
| - |
150 |
| - if (!is_null($modelClassName)){ |
151 |
| - require __DIR__.'/Model.php'; |
152 |
| - } |
153 |
| - |
154 |
| - if ($cfg['ajax'] && function_exists("ajax_bootstrap") ){ |
155 |
| - ajax_bootstrap(); |
156 |
| - } |
157 |
| - |
158 |
| - $instance = self::controllerInstance($controllerClassName, $modelClassName); |
159 |
| - self::runController($instance, $actionName); |
160 |
| - } |
161 |
| - } |
162 |
| -} |
163 |
| - |
164 |
| -MVC::config(array( |
165 |
| - "controllerSufix" => "Controller", |
166 |
| - "controllerPath" => "controller/" |
167 |
| -)); |
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * MVC.php |
| 5 | + * @author Fábio Nogueira |
| 6 | + * @version 0.0.4 |
| 7 | + */ |
| 8 | + |
| 9 | +error_reporting(E_ALL & ~E_NOTICE); |
| 10 | +define('MVC_VERSION', '0.0.4'); |
| 11 | + |
| 12 | +//carrega classes do core |
| 13 | +require __DIR__.'/Session.php'; //{{Session.php}} |
| 14 | +require __DIR__.'/View.php'; //{{View.php}} |
| 15 | +require __DIR__.'/Controller.php'; //{{Controller.php}} |
| 16 | + |
| 17 | +class MVC{ |
| 18 | + private static $_config = array(); |
| 19 | + private static $_url; |
| 20 | + |
| 21 | + private static function info(){ |
| 22 | + Response::success(array( |
| 23 | + 'version' => MVC_VERSION, |
| 24 | + 'copyright' => '(c) 2014, DTEC. All rights reserved.', |
| 25 | + 'tostring' => 'MVC Version '.MVC_VERSION . "\nCopyright (c) 2014, DTEC. All rights reserved." |
| 26 | + )); |
| 27 | + } |
| 28 | + private static function routerError($err_message){ |
| 29 | + $templates = self::$_config['templates']; |
| 30 | + $name = isset($templates["error"]) ? $templates["error"] : $templates["default"].'_error'; |
| 31 | + |
| 32 | + if ( file_exists(ROOT.'template/'.$name.'.php') ){ |
| 33 | + require ROOT.'template/'.$name.'.php'; |
| 34 | + exit; |
| 35 | + }else{ |
| 36 | + exit ("router error! <b>$err_message</b>" ); |
| 37 | + } |
| 38 | + exit; |
| 39 | + } |
| 40 | + private static function splitUrl(){ |
| 41 | + self::$_url = array(); |
| 42 | + |
| 43 | + if (BASE_URL!=''){ |
| 44 | + $arr = explode(BASE_URL, $_SERVER['REDIRECT_URL']); |
| 45 | + $url = $arr[1]!='' ? trim($arr[1], '/') : trim($arr[0], '/'); |
| 46 | + }else{ |
| 47 | + $url = trim($_SERVER['REDIRECT_URL'], '/'); |
| 48 | + } |
| 49 | + $url = filter_var($url, FILTER_SANITIZE_URL); |
| 50 | + $arr = explode('/', $url); |
| 51 | + |
| 52 | + // Put URL parts into according properties |
| 53 | + // By the way, the syntax here is just a short form of if/else, called "Ternary Operators" |
| 54 | + // @see http://davidwalsh.name/php-shorthand-if-else-ternary-operators |
| 55 | + self::$_url['controller'] = (isset($arr[0]) ? $arr[0] : null); |
| 56 | + self::$_url['action'] = (isset($arr[1]) ? $arr[1] : null); |
| 57 | + self::$_url['parameter_1'] = (isset($arr[2]) ? $arr[2] : null); |
| 58 | + self::$_url['parameter_2'] = (isset($arr[3]) ? $arr[3] : null); |
| 59 | + self::$_url['parameter_3'] = (isset($arr[4]) ? $arr[4] : null); |
| 60 | + } |
| 61 | + private static function controllerInstance($controllerClassName, $modelClassName=NULL){ |
| 62 | + require ROOT . self::$_config['controllerPath'] . $controllerClassName.'.php'; |
| 63 | + |
| 64 | + $viewName = str_replace("Controller", "View", $controllerClassName); |
| 65 | + |
| 66 | + $instance = new $controllerClassName(); |
| 67 | + if (!self::$_config['ajax']){ |
| 68 | + $instance->view = new View(ROOT. 'view'. DIRECTORY_SEPARATOR . $viewName.'.php'); |
| 69 | + } |
| 70 | + |
| 71 | + if (!is_null($modelClassName)){ |
| 72 | + require ROOT.'model/'.$modelClassName.'.php'; |
| 73 | + $instance->model = new $modelClassName(self::$_config["model"]); |
| 74 | + } |
| 75 | + |
| 76 | + return $instance; |
| 77 | + } |
| 78 | + private static function runController($instance, $actionName){ |
| 79 | + $templates = self::$_config['templates']; |
| 80 | + $parameter_1 = self::$_url['parameter_1']; |
| 81 | + $parameter_2 = self::$_url['parameter_2']; |
| 82 | + $parameter_3 = self::$_url['parameter_3']; |
| 83 | + $view = $instance->view; |
| 84 | + $controller = self::$_url['controller']; |
| 85 | + |
| 86 | + if (isset($instance->model)){ |
| 87 | + $view->model = $instance->model; |
| 88 | + } |
| 89 | + |
| 90 | + //se não tem action, action será 'index' |
| 91 | + $actionName = ($actionName=='' ? 'index' : $actionName); |
| 92 | + |
| 93 | + //chama a action de acordo com a quantidade de parâmetros enviados |
| 94 | + if (method_exists($instance, $actionName)) { |
| 95 | + $output = $instance->{$actionName}($parameter_1, $parameter_2, $parameter_3); |
| 96 | + if (self::$_config['ajax']){ |
| 97 | + Response::success($output); |
| 98 | + } |
| 99 | + }else{ |
| 100 | + self::routerError('action not found'); |
| 101 | + } |
| 102 | + |
| 103 | + //se a action não retornou o conteúdo da view, busca direto da view |
| 104 | + if (is_null($output) && !self::$_config['ajax']){ |
| 105 | + $output = $view->output(); |
| 106 | + } |
| 107 | + |
| 108 | + //carrega o template |
| 109 | + if (!self::$_config['ajax']){ |
| 110 | + $name = isset($templates[$controller]) ? $templates[$controller] : $templates["default"]; |
| 111 | + require ROOT.'template/'.$name.'.php'; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public static function getParameter($index){ |
| 116 | + return self::$_url['parameter_'.$index]; |
| 117 | + } |
| 118 | + public static function getConfig(){ |
| 119 | + return self::$_config; |
| 120 | + } |
| 121 | + public static function config($config){ |
| 122 | + self::$_config = array_merge_recursive(self::$_config, $config); |
| 123 | + } |
| 124 | + public static function run(){ |
| 125 | + $cfg = self::$_config; |
| 126 | + |
| 127 | + //define algumas constantes úteis |
| 128 | + define('TEMPLATE', $cfg['template']); |
| 129 | + define('BASE_URL', $cfg['url']['base']); |
| 130 | + |
| 131 | + //separa a url em controller, action e parâmetros |
| 132 | + self::splitUrl(); |
| 133 | + |
| 134 | + if (self::$_url['controller']==='mvc-info'){ |
| 135 | + self::info(); |
| 136 | + } |
| 137 | + |
| 138 | + $controllerClassName = self::$_url['controller']=='' ? 'MainController' : ucfirst(self::$_url['controller'] . $cfg['controllerSufix']); |
| 139 | + $modelClassName = self::$_url['controller']=='' ? 'MainModel' : ucfirst(self::$_url['controller'] . 'Model'); |
| 140 | + $actionName = self::$_url['action']; |
| 141 | + |
| 142 | + //se o arquivo do controller não existe |
| 143 | + if (!file_exists(ROOT.$cfg['controllerPath'] . $controllerClassName . '.php')) { |
| 144 | + self::routerError('controller not found'); |
| 145 | + }else{ |
| 146 | + if (!file_exists(ROOT.'model/' . $modelClassName . '.php')){ |
| 147 | + $modelClassName = NULL; |
| 148 | + } |
| 149 | + |
| 150 | + if (!is_null($modelClassName)){ |
| 151 | + require_once __DIR__.'/Model.php'; |
| 152 | + } |
| 153 | + |
| 154 | + if ($cfg['ajax'] && function_exists("ajax_bootstrap") ){ |
| 155 | + ajax_bootstrap(); |
| 156 | + } |
| 157 | + |
| 158 | + $instance = self::controllerInstance($controllerClassName, $modelClassName); |
| 159 | + self::runController($instance, $actionName); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +MVC::config(array( |
| 165 | + "controllerSufix" => "Controller", |
| 166 | + "controllerPath" => "controller/" |
| 167 | +)); |
0 commit comments