Skip to content

Commit 24224a7

Browse files
committed
added: Initial commit
0 parents  commit 24224a7

File tree

7 files changed

+1291
-0
lines changed

7 files changed

+1291
-0
lines changed

.htaccess

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Options +FollowSymLinks
2+
3+
RewriteEngine On
4+
RewriteCond %{REQUEST_FILENAME} !-d
5+
RewriteCond %{REQUEST_FILENAME} !-f
6+
RewriteCond %{REQUEST_FILENAME} !-l
7+
8+
# RewriteCond %{HTTPS} off
9+
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
10+
RewriteRule ^(.+)$ index.php [QSA,L,NC]
11+
# Options -Indexes

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# php API class
2+
3+
A PHP class to use for RESTfull API methods.
4+
5+
It uses the express (node.js) route concepts.
6+
7+
This class needs the Friendly URL class as dependency.
8+
9+
---
10+
11+
## How to use (documentation still on process)
12+

api.class.php

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/**
4+
* Class created by Matheus Marques https://github.com/Matheus2212
5+
* 2021-06-12 -> First version of the API class
6+
* 2021-06-18 -> Updated ALL methods for the class
7+
* 2021-06-19 -> Defined methods for routes and route groups for the class
8+
* 2021-06-20 -> removed all own methods and made the API class work together with the Friendly URL class
9+
* 2021-06-22 -> Fully integrated API class with Friendly URL class (now its a dependency)
10+
* */
11+
12+
class API
13+
{
14+
15+
private $version = 0;
16+
17+
private $url = null;
18+
19+
private $request = null;
20+
21+
private $URLclass = null;
22+
23+
private $routes = array();
24+
25+
private $data = array();
26+
27+
private $mode = "";
28+
29+
public function __construct($url, $version)
30+
{
31+
header("Access-Control-Allow-Origin: *");
32+
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
33+
header('Access-Control-Allow-Credentials: true');
34+
header('Access-Control-Max-Age: 86400');
35+
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT');
36+
37+
if ($url && is_object($url)) {
38+
$this->version = $version;
39+
$this->url = $url->getURL() . "v" . explode(".", $this->version)[0] . "/";
40+
$now = str_replace(preg_replace("/http(s)?\:\/\//", "", $url->getURL()), "", preg_replace("/http(s)?\:\/\//", "", $url->agora()));
41+
$this->parts = explode("/", $now);
42+
$this->URLclass = $url;
43+
$this->URLclass->partes = $this->parts;
44+
}
45+
}
46+
47+
public function __set($key, $value)
48+
{
49+
$this->$key = $value;
50+
}
51+
52+
public function version()
53+
{
54+
$this->join(array('version' => $this->version));
55+
return $this->version;
56+
}
57+
58+
public function join($array, $status = true)
59+
{
60+
$this->data = array_merge($this->data, $array);
61+
$this->data = array_merge($this->data, array("status" => $status));
62+
if (isset($this->data['error'])) {
63+
$this->data['status'] = false;
64+
}
65+
$this->data["time"] = time();
66+
return $this;
67+
}
68+
69+
public function define($route, $callback)
70+
{
71+
$parts = explode("/", $route);
72+
$parts = array_filter($parts, function ($item) {
73+
if (trim($item) === "") {
74+
return false;
75+
} else {
76+
return true;
77+
}
78+
});
79+
sort($parts);
80+
$route = $parts[0];
81+
$this->routes[$route] = array("callback" => $callback);
82+
if (isset($parts[1])) {
83+
$this->routes[$route]["url"] = $parts[1];
84+
}
85+
}
86+
87+
public function route()
88+
{
89+
$operation = $this->URLclass->get(0);
90+
if (isset($this->routes[$operation])) {
91+
$reflection = new ReflectionFunction($this->routes[$operation]["callback"]);
92+
$totalParams = $reflection->getNumberOfParameters();
93+
if ($totalParams > 1) {
94+
if ($this->request === null) {
95+
$this->join(array("error" => "002", "message" => "No request was sent"));
96+
} else {
97+
$this->routes[$operation]["callback"]($this, $this->request, true);
98+
}
99+
} else {
100+
$this->routes[$operation]["callback"]($this);
101+
}
102+
} else {
103+
$this->join(array("error" => "001", "message" => "This route doesn't exists"));
104+
}
105+
}
106+
107+
private function APIReturn()
108+
{
109+
switch ($this->mode) {
110+
case "json":
111+
function recursive_json($data)
112+
{
113+
if (is_array($data)) {
114+
return array_map("recursive_json", $data);
115+
} else {
116+
return utf8_encode($data);
117+
}
118+
}
119+
120+
return json_encode(recursive_json($this->data));
121+
break;
122+
}
123+
}
124+
125+
public function type($type)
126+
{
127+
switch ($type) {
128+
case "json":
129+
header("Content-type: application/json");
130+
$this->request = json_decode(file_get_contents('php://input'), true);
131+
$this->mode = "json";
132+
break;
133+
}
134+
}
135+
136+
public function response($echo = false)
137+
{
138+
$data = $this->APIReturn();
139+
if ($echo) {
140+
echo $data;
141+
}
142+
return $data;
143+
}
144+
}

config.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$api_name = "training-beer";
4+
$version = "1.0";
5+
6+
$url = "http://127.0.0.1/server-training-beer/";
7+
8+
$production = false;
9+
10+
$DB_NAME = "training-beer";
11+
$DB_HOST = "localhost";
12+
$DB_USER = "root";
13+
$DB_PASSWORD = "";
14+

0 commit comments

Comments
 (0)