Skip to content

Commit f6c07ea

Browse files
committed
namespace fixes
1 parent a3a7d21 commit f6c07ea

File tree

7 files changed

+69
-30
lines changed

7 files changed

+69
-30
lines changed
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
<?php
22

3+
namespace SimpleORM\app\controller;
4+
5+
use SimpleORM\core\controller\Controller;
6+
37
class UsuariosController extends Controller
48
{
5-
public function login($dados)
9+
10+
public function index()
611
{
7-
if (!empty($dados)) {
8-
$dados['nome'] = utf8_decode($dados['nome']);
9-
$this->Usuarios->save($dados);
10-
} else {
11-
throw new Exception('Acesso indevido');
12-
}
12+
return ['texto' => 'teste'];
1313
}
1414

1515
public function listar()
1616
{
1717
$result = $this->Usuarios->find();
1818

19-
$helper = new Helper();
20-
return ['listagem' => $helper->json($result)];
19+
return ['listagem' => $this->Helper->json($result)];
2120
}
2221
}

app/model/AppModel.php

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

33
namespace SimpleORM\app\model;
44

5+
require_once CORE . 'model' . DS . 'Model.php';
6+
use SimpleORM\core\model\Model;
7+
58
class AppModel extends Model
69
{
710
public function where($dados = null)

app/model/Usuarios.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<?php
22

3-
class Usuarios extends Model
3+
namespace SimpleORM\app\model;
4+
5+
require_once APP . 'model' . DS .'AppModel.php';
6+
use SimpleORM\app\model\AppModel;
7+
8+
class Usuarios extends AppModel
49
{
5-
protected $table = 'chat_usuarios';
10+
protected $table = 'usuarios';
611
//protected $pk = 'id';
712
}

app/view/Usuarios/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo $texto;

core/controller/Controller.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,54 @@
22

33
namespace SimpleORM\core\controller;
44

5+
require CORE . 'helper' . DS . 'Helper.php';
6+
use SimpleORM\core\helper\Helper;
7+
58
class Controller
69
{
10+
private $class;
711
/*
812
* create instance of model with same controller name
913
*/
1014
public function __construct()
1115
{
12-
$class = str_replace('Controller', '', get_class($this));
13-
$this->{$class} = new $class();
16+
$namespace = 'SimpleORM\app\controller\\';
17+
$class = str_replace($namespace, '', get_class($this));
18+
$this->class = str_replace('Controller', '', $class);
19+
20+
$this->Helper = new Helper();
1421

1522
self::loadModels();
23+
self::loadHelpers();
1624
}
1725

1826
/*
1927
* create instances of models with different names when set $this->use = []
2028
*/
2129
private function loadModels()
2230
{
23-
if (isset($this->use)) {
31+
$this->use = !isset($this->use) ? [$this->class] : $this->use;
32+
33+
if ($this->use) {
2434
foreach ($this->use as $model) {
25-
require './app/model/'.$model.'.php';
26-
$this->{$model} = new $model();
35+
self::load('model', $model);
2736
}
2837
}
2938
}
39+
40+
private function loadHelpers()
41+
{
42+
if (isset($this->helpers)) {
43+
foreach ($this->helpers as $helper) {
44+
self::load('helper', $helper);
45+
}
46+
}
47+
}
48+
49+
private function load($path, $class)
50+
{
51+
require APP . $path . DS . $class . '.php';
52+
$load_class = 'SimpleORM\app\\' . $path . '\\' . $class;
53+
$this->{$class} = new $load_class();
54+
}
3055
}

core/model/Model.php

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

33
namespace SimpleORM\core\model;
44

5+
require_once 'Database.php';
6+
57
use SimpleORM\core\model\Database;
8+
use \PDO;
69

710
class Model extends Database
811
{

index.php

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

66
define('DS', DIRECTORY_SEPARATOR);
7+
define('APP', __DIR__ . DS . 'app' . DS);
8+
define('CORE', __DIR__ . DS . 'core' . DS);
79

8-
use SimpleORM\core\helper\Helper;
9-
use SimpleORM\core\model\Model;
10-
use SimpleORM\core\controller\Controller;
11-
use SimpleORM\core\model\AppModel;
10+
require_once CORE . 'controller' . DS . 'Controller.php';
11+
use SimpleORM\app\controller;
1212

1313
$uri = $_SERVER['REQUEST_URI'];
1414

@@ -28,7 +28,7 @@
2828
$src = explode('/', $uri);
2929
$model = ucfirst($src[1]);
3030
$controller = $model.'Controller';
31-
$method = (isset($src[2])) ? $src[2] : 'index';
31+
$method = (isset($src[2])) ? $src[2] : 'index';
3232

3333
if (isset($src[3]) && empty($the_request)) {
3434
$the_request = filter_var($src[3], FILTER_SANITIZE_STRING);
@@ -37,26 +37,29 @@
3737
/*
3838
* require files of current Model/Controller
3939
*/
40-
$model_file = __DIR__ . DS .'app' . DS. ' model ' . DS . $model.'.php';
40+
$model_file = __DIR__ . DS . 'app' . DS. ' model ' . DS . $model.'.php';
4141

4242
if (file_exists($model_file)) {
4343
require_once $model_file;
4444
}
4545

46-
$controller_file = __DIR__ . DS . 'app' . DS . 'controller' . DS . $controller.'.php';
46+
/*
47+
* call current class/method
48+
*/
49+
$controller_file = APP . 'controller' . DS . $controller.'.php';
4750

4851
if (file_exists($controller_file)) {
49-
require_once $controller_file;
52+
53+
require $controller_file;
54+
55+
$load_class = 'SimpleORM\app\controller\\' . $controller;
56+
$class = new $load_class();
57+
$set = $class->$method($the_request);
58+
5059
} else {
5160
throw new Exception('Controller '.$controller.' Not Found');
5261
}
5362

54-
/*
55-
* call current class/method
56-
*/
57-
$class = new $controller();
58-
$set = $class->$method($the_request);
59-
6063
/*
6164
* Declare all variables if passed in return
6265
*/

0 commit comments

Comments
 (0)