-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
141 lines (119 loc) · 2.25 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Resty;
/**
* API Class
*
* Conductor of the Resty. It is the link between the different 'parts' of the framework.
*
* @package Resty
* @subpackage API
* @since 1.0
* @author Ludndev < ludndev [at] gmail [dot] com >
*/
class API
{
/**
* API Constructor
*
* @access public
* @return object
*/
public function __construct()
{
/* force displaying Json & setting app engine */
Providers\Header::Custom([
'Content-Type' => 'application/json; charset=UTF-8',
'X-Engine' => 'Resty/1.0'
]);
/* resolve and set base directory as APP_ROOT */
define( '__ROOT' , __DIR__ );
return $this;
}
/**
* API initilization
*
* @access public
* @return object
*/
public function Init():object
{
return $this
->EnvInit()
->DatabaseInit()
;
}
/**
* Load .env data from app root
*
* @access public
* @return object
*/
protected function EnvInit():object
{
$dotenv = \Dotenv\Dotenv::create( __ROOT );
$dotenv->load();
return $this;
}
/**
* Provide PDO instance
*
* @access protected
* @return object
*/
protected function DatabaseInit():object
{
$GLOBALS['DB'] = new Providers\DBController(
$_ENV['DB_DRIVER'],
$_ENV['DB_HOST'],
$_ENV['DB_NAME'],
$_ENV['DB_USER'],
$_ENV['DB_PASS'],
[
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_EMPTY_STRING
]
);
return $this;
}
/**
* Return jsonify success response base on method and uri
*
* @access public
* @param string $method
* @param string $uri
* @return string
*/
public function ResponseHandler(string $method, string $uri):string
{
return
Providers\Response::Success(
Providers\Router::Match( $method, $uri )
);
}
/**
* Output error code, instead of generic string
*
* @access public
* @param object $exception
* @return string
*/
public function ErrorHandler(object $exception):string
{
return
Providers\Response::Failed(
Providers\MOError::OnCode( $exception )
);
}
/**
* Push to screen api response
*
* @access public
* @param string $response
* @return void
*/
public function Serve(string $response):void
{
echo $response;
}
}