Skip to content

Commit 7c0bf97

Browse files
committed
dev
1 parent 4041d1d commit 7c0bf97

File tree

4 files changed

+208
-188
lines changed

4 files changed

+208
-188
lines changed

MVC.php

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

Model.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
<?php
2-
3-
require_once __DIR__.'/../db/DB.php';
4-
5-
class Model {
6-
/**
7-
* @var DB
8-
*/
9-
protected $db;
10-
11-
function __construct($config) {
12-
$this->db = new DB();
13-
$this->db->config($config);
14-
}
15-
}
1+
<?php
2+
3+
require_once __DIR__.'/../db/DB.php';
4+
5+
class Model {
6+
/**
7+
* @var DB
8+
*/
9+
protected $db;
10+
11+
function __construct($config) {
12+
$this->db = new DB($config);
13+
}
14+
}

nbproject/project.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include.path=${php.global.include.path}
2+
php.version=PHP_54
3+
source.encoding=UTF-8
4+
src.dir=.
5+
tags.asp=false
6+
tags.short=false
7+
web.root=.

nbproject/project.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://www.netbeans.org/ns/project/1">
3+
<type>org.netbeans.modules.php.project</type>
4+
<configuration>
5+
<data xmlns="http://www.netbeans.org/ns/php-project/1">
6+
<name>lib_mvc</name>
7+
</data>
8+
</configuration>
9+
</project>

0 commit comments

Comments
 (0)