Skip to content

Commit a27847e

Browse files
committedAug 15, 2018
change some comment and fix problem
1 parent 05f3b8a commit a27847e

File tree

9 files changed

+132
-94
lines changed

9 files changed

+132
-94
lines changed
 

‎System/Database/DB/PDO.php

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace Database\DB;
99

1010
/**
11-
* Class PDO
11+
* Global Class PDO
1212
*/
1313
final class PDO {
14+
15+
/**
16+
* @var
17+
*/
1418
private $pdo = null;
19+
20+
/**
21+
* @var
22+
*/
1523
private $statement = null;
1624

25+
/**
26+
* Construct, create opject of PDO class
27+
*/
1728
public function __construct($hostname, $username, $password, $database, $port) {
1829
try {
1930
$this->pdo = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true));
@@ -22,56 +33,30 @@ public function __construct($hostname, $username, $password, $database, $port) {
2233
exit();
2334
}
2435

36+
// set default setting database
2537
$this->pdo->exec("SET NAMES 'utf8'");
2638
$this->pdo->exec("SET CHARACTER SET utf8");
2739
$this->pdo->exec("SET CHARACTER_SET_CONNECTION=utf8");
2840
$this->pdo->exec("SET SQL_MODE = ''");
2941

3042
}
31-
32-
public function prepare($sql) {
33-
$this->statement = $this->pdo->prepare($sql);
34-
}
35-
36-
public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) {
37-
if ($length) {
38-
$this->statement->bindParam($parameter, $variable, $data_type, $length);
39-
} else {
40-
$this->statement->bindParam($parameter, $variable, $data_type);
41-
}
42-
}
43-
44-
public function execute() {
45-
try {
46-
if ($this->statement && $this->statement->execute()) {
47-
$data = array();
48-
49-
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
50-
$data[] = $row;
51-
}
52-
53-
$result = new \stdClass();
54-
$result->row = (isset($data[0])) ? $data[0] : array();
55-
$result->rows = $data;
56-
$result->num_rows = $this->statement->rowCount();
57-
}
58-
} catch(\PDOException $e) {
59-
trigger_error('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode());
60-
}
61-
}
62-
63-
public function query($sql, $params = array()) {
43+
44+
/**
45+
* exec query statement
46+
*/
47+
public function query($sql) {
6448
$this->statement = $this->pdo->prepare($sql);
6549
$result = false;
6650

6751
try {
68-
if ($this->statement && $this->statement->execute($params)) {
52+
if ($this->statement && $this->statement->execute()) {
6953
$data = array();
7054

7155
while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) {
7256
$data[] = $row;
7357
}
7458

59+
// create std class
7560
$result = new \stdClass();
7661
$result->row = (isset($data[0]) ? $data[0] : array());
7762
$result->rows = $data;
@@ -93,12 +78,18 @@ public function query($sql, $params = array()) {
9378
}
9479
}
9580

81+
/**
82+
* claen data
83+
*/
9684
public function escape($value) {
9785
$search = array("\\", "\0", "\n", "\r", "\x1a", "'", '"');
9886
$replace = array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"');
9987
return str_replace($search, $replace, $value);
10088
}
10189

90+
/**
91+
* return last id insert
92+
*/
10293
public function getLastId() {
10394
return $this->pdo->lastInsertId();
10495
}

‎System/Database/DatabaseAdapter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace Database;
99

10+
/**
11+
* Class DatabaseAdapter for handel database query
12+
*
13+
* @author Mohammad Rahmani <rto1680@gmail.com>
14+
*
15+
* @package Database
16+
*/
1017
class DatabaseAdapter {
18+
1119
/**
1220
* Database Connection
1321
*

‎System/Http/Request.php

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

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace Http;
@@ -56,11 +56,24 @@ public function get(String $key = '') {
5656
return $this->clean($_GET);
5757
}
5858

59+
/**
60+
* Get $_POST parameter
61+
*
62+
* @param String $key
63+
* @return string
64+
*/
65+
public function post(String $key = '') {
66+
if ($key != '')
67+
return isset($_POST[$key]) ? $this->clean($_POST[$key]) : null;
68+
69+
return $this->clean($_POST);
70+
}
71+
5972
/**
6073
* Get POST parameter
6174
*
6275
* @param String $key
63-
*
76+
* @return string
6477
*/
6578
public function input(String $key = '') {
6679
$postdata = file_get_contents("php://input");
@@ -72,19 +85,6 @@ public function input(String $key = '') {
7285

7386
return ($request);
7487
}
75-
76-
/**
77-
* Get $_POST parameter
78-
*
79-
* @param String $key
80-
*
81-
*/
82-
public function post(String $key = '') {
83-
if ($key != '')
84-
return isset($_POST[$key]) ? $this->clean($_POST[$key]) : null;
85-
86-
return $this->clean($_POST);
87-
}
8888

8989
/**
9090
* Get value for server super global var

‎System/Http/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace Http;

‎System/MVC/Controller.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace MVC;
99

10+
/**
11+
* Class Controller, a port of MVC
12+
*
13+
* @author Mohammad Rahmani <rto1680@gmail.com>
14+
*
15+
* @package MVC
16+
*/
1017
class Controller {
1118

1219
/**
@@ -19,6 +26,9 @@ class Controller {
1926
*/
2027
public $response;
2128

29+
/**
30+
* Construct
31+
*/
2232
public function __construct() {
2333
$this->request = $GLOBALS['request'];
2434
$this->response = $GLOBALS['response'];
@@ -34,10 +44,12 @@ public function __construct() {
3444
public function model($model) {
3545
$file = MODELS . ucfirst($model) . '.php';
3646

47+
// check exists file
3748
if (file_exists($file)) {
3849
require_once $file;
3950

4051
$model = 'Models' . str_replace('/', '', ucwords($model, '/'));
52+
// check class exists
4153
if (class_exists($model))
4254
return new $model;
4355
else
@@ -47,6 +59,7 @@ public function model($model) {
4759
}
4860
}
4961

62+
// send response faster
5063
public function send($status = 200, $msg) {
5164
$this->response->setHeader(sprintf('HTTP/1.1 ' . $status . ' %s' , $this->response->getStatusCodeText($status)));
5265
$this->response->setContent($msg);

‎System/MVC/Model.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace MVC;
99

10+
/**
11+
* Class Model, a port of MVC
12+
*
13+
* @author Mohammad Rahmani <rto1680@gmail.com>
14+
*
15+
* @package MVC
16+
*/
1017
class Model {
1118

1219
/**
@@ -26,5 +33,7 @@ public function __construct() {
2633
DATABASE['Name'],
2734
DATABASE['Port']
2835
);
36+
37+
$this->pagination = $GLOBALS['pagination'];
2938
}
3039
}

‎System/Router/Route.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace Router;
99

10+
/**
11+
* Class Route For Save Route
12+
*
13+
* @author Mohammad Rahmani <rto1680@gmail.com>
14+
*
15+
* @package Router
16+
*/
1017
final class Route {
1118

1219
/**
@@ -50,7 +57,7 @@ public function __construct(String $method, String $pattern, $callback) {
5057
* check valid method
5158
*/
5259
private function validateMethod(string $method) {
53-
if (in_array($method, $this->list_method))
60+
if (in_array(strtoupper($method), $this->list_method))
5461
return $method;
5562

5663
throw new Exception('Invalid Method Name');

‎System/Router/Router.php

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
/**
44
*
5-
* This file is part of simple-mvc-rest-api for PHP.
5+
* This file is part of mvc-rest-api for PHP.
66
*
77
*/
88
namespace Router;
99

10+
/**
11+
* Class Router For Handel Router
12+
*
13+
* @author Mohammad Rahmani <rto1680@gmail.com>
14+
*
15+
* @package Router
16+
*/
1017
class Router {
1118

1219
/**
@@ -53,7 +60,7 @@ class Router {
5360
* constaruct
5461
*/
5562
public function __construct(string $url, string $method) {
56-
$this->url = $url;
63+
$this->url = rtrim($url, '/');
5764
$this->method = $method;
5865

5966
// get response class of $GLOBALS var
@@ -74,6 +81,13 @@ public function post($pattern, $callback) {
7481
$this->addRoute('POST', $pattern, $callback);
7582
}
7683

84+
/**
85+
* set put request http method for route
86+
*/
87+
public function put($pattern, $callback) {
88+
$this->addRoute('PUT', $pattern, $callback);
89+
}
90+
7791
/**
7892
* set delete request http method for route
7993
*/
@@ -160,14 +174,6 @@ private function convertPatternToRegex($matches) {
160174
return '(?P<' . $key . '>[a-zA-Z0-9_\-\.\!\~\*\\\'\(\)\:\@\&\=\$\+,%]+)';
161175
}
162176

163-
protected function trimeRoute($route) {
164-
return ltrim($route, '/');
165-
}
166-
167-
protected function trimeUrl($route) {
168-
return rtrim($route, '/');
169-
}
170-
171177
/**
172178
* run application
173179
*/
@@ -179,55 +185,55 @@ public function run() {
179185
$this->getMatchRoutersByPattern($this->matchRouter);
180186

181187
if (!$this->matchRouter || empty($this->matchRouter)) {
182-
$this->response->sendStatus(404);
183-
$this->response->setContent(['error' => 'Sorry This Route Not Found !', 'status_code' => 404]);
184-
} else {
185-
186-
// cal to callback method
187-
if (is_callable($this->matchRouter[0]->getCallback())) {
188+
$this->sendNotFound();
189+
} else {
190+
// call to callback method
191+
if (is_callable($this->matchRouter[0]->getCallback()))
188192
call_user_func($this->matchRouter[0]->getCallback(), $this->params);
189-
} else {
193+
else
190194
$this->runController($this->matchRouter[0]->getCallback(), $this->params);
191-
}
192195
}
193196
}
194197

195198
/**
196199
* run as controller
197200
*/
198201
private function runController($controller, $params) {
199-
$part = explode('@', $controller);
200-
$file = CONTROLLERS . ucfirst($part[0]) . '.php';
202+
$parts = explode('@', $controller);
203+
$file = CONTROLLERS . ucfirst($parts[0]) . '.php';
201204

202205
if (file_exists($file)) {
203206
require_once($file);
204207

205208
// controller class
206-
$controller = 'Controllers' . ucfirst($part[0]);
209+
$controller = 'Controllers' . ucfirst($parts[0]);
207210

208-
if (class_exists($controller)) {
211+
if (class_exists($controller))
209212
$controller = new $controller();
210-
} else {
211-
$this->response->sendStatus(404);
212-
$this->response->setContent(['error' => 'this route not found !', 'status_code' => 404]);
213-
}
213+
else
214+
$this->sendNotFound();
214215

215216
// set function in controller
216-
if (isset($part[1])) {
217-
$method = $part[1];
218-
if (!method_exists($controller, $method)) {
219-
$this->response->sendStatus(404);
220-
$this->response->setContent(['error' => 'this route not found !', 'status_code' => 404]);
221-
}
217+
if (isset($parts[1])) {
218+
$method = $parts[1];
219+
220+
if (!method_exists($controller, $method))
221+
$this->sendNotFound();
222+
222223
} else {
223224
$method = 'index';
224225
}
225226

226227
// call to controller
227228
if (is_callable([$controller, $method]))
228229
return call_user_func([$controller, $method], $params);
229-
else
230-
$this->response->setContent(['error' => 'this route not found !', 'status_code' => 404]);
230+
else
231+
$this->sendNotFound();
231232
}
232233
}
234+
235+
private function sendNotFound() {
236+
$this->response->sendStatus(404);
237+
$this->response->setContent(['error' => 'Sorry This Route Not Found !', 'status_code' => 404]);
238+
}
233239
}

‎System/Startup.php

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

33
// autoload class
44
function autoload($class) {
5+
// set file class
56
$file = SYSTEM . str_replace('\\', '/', $class) . '.php';
67

7-
if (file_exists($file)) {
8+
if (file_exists($file))
89
require_once $file;
9-
} else {
10+
else
1011
throw new Exception(sprintf('Class { %s } Not Found!', $class));
11-
}
1212
}
1313

14+
// set autoload function
1415
spl_autoload_register('autoload');
16+
17+
// load helper
18+
require_once SYSTEM . 'Helper/public.php';

0 commit comments

Comments
 (0)
Please sign in to comment.