Skip to content

Commit 09e9cd0

Browse files
committed
PSR-4 added and refactoring
1 parent 6a9bf72 commit 09e9cd0

File tree

7 files changed

+57
-39
lines changed

7 files changed

+57
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/vendor
12
config.ini
23
.DS_Store
34
.idea

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
"email": "arojunior@gmail.com"
1010
}
1111
],
12+
"autoload": {
13+
"psr-4": {
14+
"SimpleORM\\": ""
15+
}
16+
},
1217
"require": {
13-
"php": ">=5.4.0"
18+
"php": ">=5.6.0"
1419
}
1520
}

composer.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

constants.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
const DS = DIRECTORY_SEPARATOR;
4+
const APP = __DIR__ . DS . 'app' . DS;
5+
const CORE = __DIR__ . DS . 'core' . DS;
6+
const PHP = '.php';
7+
8+
const APP_MODEL = APP . 'model' . DS;
9+
const APP_VIEW = APP . 'view' . DS;
10+
const APP_CONTROLLER = APP . 'controller' . DS;
11+
12+
const CORE_MODEL = CORE . 'model' . DS . 'Model.php';
13+
const CORE_CONTROLLER = CORE . 'controller' . DS . 'Controller.php';

core/controller/Controller.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace SimpleORM\core\controller;
44

5-
require CORE . 'helper' . DS . 'Helper.php';
65
use SimpleORM\core\helper\Helper;
76

87
class Controller
@@ -52,7 +51,6 @@ private function loadHelpers()
5251

5352
private function load($path, $class)
5453
{
55-
require APP . $path . DS . $class . '.php';
5654
$load_class = 'SimpleORM\app\\' . $path . '\\' . $class;
5755
$this->{$class} = new $load_class();
5856
}

core/model/Model.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace SimpleORM\core\model;
44

5-
require_once 'Database.php';
6-
75
use SimpleORM\core\model\Database;
86
use \PDO;
97

index.php

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
header('Access-Control-Allow-Origin: *');
44
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
55

6-
define('DS', DIRECTORY_SEPARATOR);
7-
define('APP', __DIR__ . DS . 'app' . DS);
8-
define('CORE', __DIR__ . DS . 'core' . DS);
6+
require 'constants.php';
7+
require __DIR__ . '/vendor/autoload.php';
8+
9+
require_once CORE_MODEL;
10+
require_once CORE_CONTROLLER;
911

10-
require_once CORE . 'model' . DS . 'Model.php';
11-
require_once CORE . 'controller' . DS . 'Controller.php';
1212
use SimpleORM\app\controller;
1313

1414
$uri = $_SERVER['REQUEST_URI'];
@@ -28,45 +28,28 @@
2828
exit;
2929
}
3030

31-
$src = explode('/', $uri);
32-
$model = ucfirst($src[1]);
31+
$src = explode('/', $uri);
32+
$model = ucfirst($src[1]);
3333
$controller = $model.'Controller';
34-
$method = (isset($src[2])) ? $src[2] : 'index';
34+
$method = (isset($src[2])) ? $src[2] : 'index';
3535

3636
if (isset($src[3]) && empty($the_request)) {
3737
$the_request = filter_var($src[3], FILTER_SANITIZE_STRING);
3838
}
3939

40-
/*
41-
* AppModel file
42-
*/
43-
$app_model = APP . 'model' . DS .'AppModel.php';
44-
45-
if (file_exists($app_model)) {
46-
require_once $app_model;
47-
}
48-
/*
49-
* require files of current Model/Controller
50-
*/
51-
$model_file = APP . 'model ' . DS . $model.'.php';
52-
53-
if (file_exists($model_file)) {
54-
require_once $model_file;
55-
}
5640
/*
5741
* call current class/method
5842
*/
59-
$controller_file = APP . 'controller' . DS . $controller.'.php';
43+
$controller_file = APP_CONTROLLER . $controller . PHP;
6044

61-
if (!file_exists($controller_file)) {
62-
throw new Exception('Controller '.$controller.' Not Found');
45+
try {
46+
require $controller_file;
47+
$load_class = 'SimpleORM\app\controller\\' . $controller;
48+
$class = new $load_class();
49+
$set = $class->$method($the_request);
50+
} catch(Exception $e) {
51+
echo 'No '.$controller.' found for this route', $e->getMessage(), "\n";
6352
}
64-
65-
require $controller_file;
66-
$load_class = 'SimpleORM\app\controller\\' . $controller;
67-
$class = new $load_class();
68-
$set = $class->$method($the_request);
69-
7053
/*
7154
* Declare all variables if passed in return
7255
*/
@@ -79,7 +62,7 @@
7962
/*
8063
* If method has a view file, include
8164
*/
82-
$view_file = __DIR__ . DS . 'app' . DS . 'view' . DS . $model . DS . $method .'.php';
65+
$view_file = APP_VIEW . $model . DS . $method . PHP;
8366

8467
if (file_exists($view_file)) {
8568
include $view_file;

0 commit comments

Comments
 (0)