Skip to content

Commit 62e67d4

Browse files
authored
Add files via upload
1 parent 23f6781 commit 62e67d4

File tree

11 files changed

+832
-0
lines changed

11 files changed

+832
-0
lines changed

README.md

Lines changed: 474 additions & 0 deletions
Large diffs are not rendered by default.

index.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
// PHP Core
3+
// Author: Hung Thanh Nguyen
4+
5+
// Web server configurations
6+
// Apache: modify .htaccess as following
7+
// RewriteEngine On
8+
// RewriteCond %{REQUEST_FILENAME} !-f
9+
// RewriteCond %{REQUEST_FILENAME} !-d
10+
// RewriteRule ^(.*)$ index.php?q=$1 [QSA,NC,L]
11+
// Nginx: modify nginx.conf as following:
12+
// location / {
13+
// index index.html index.htm index.php;
14+
// if (!-e $request_filename) {
15+
// rewrite ^(.*)$ /index.php?q=$1;
16+
// }
17+
// }
18+
19+
$App = null;
20+
21+
try {
22+
require_once "vendor/autoload.php";
23+
24+
session_start();
25+
26+
require_once("src/server/config.php");
27+
28+
$AppName = STARTUP;
29+
$App = new $AppName();
30+
$App->process();
31+
}
32+
catch (Exception $e) {
33+
$App->ErrorHandler->process($e);
34+
}
35+
?>

src/server/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
// define boostrap class
3+
define("STARTUP", "phpcore\\Startup", true);
4+
5+
// define default views folder
6+
define("DEFAULT_VIEWS_FOLDER", "src/server/views/", true);
7+
?>

src/server/core/ApiController.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace phpcore\core;
3+
4+
use Exception;
5+
6+
abstract class ApiController extends Controller {
7+
8+
public function process() {
9+
try {
10+
if (preg_match("/^GET$/", $this->Request->Method)) {
11+
$this->get();
12+
}
13+
else if (preg_match("/^POST$/", $this->Request->Method)) {
14+
$this->post();
15+
}
16+
else if (preg_match("/^PUT$/", $this->Request->Method)) {
17+
$this->put();
18+
}
19+
else if (preg_match("/^DELETE$/", $this->Request->Method)) {
20+
$this->delete();
21+
}
22+
else if (preg_match("/^PATCH$/", $this->Request->Method)) {
23+
$this->patch();
24+
}
25+
else if (preg_match("/^OPTIONS$/", $this->Request->Method)) {
26+
$this->options();
27+
}
28+
}
29+
catch (Exception $e) {
30+
throw $e;
31+
}
32+
}
33+
34+
public abstract function get();
35+
public abstract function post();
36+
public abstract function put();
37+
public abstract function delete();
38+
public abstract function patch();
39+
public abstract function options();
40+
}
41+
?>

src/server/core/App.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace phpcore\core;
3+
4+
use Exception;
5+
6+
abstract class App {
7+
public $Request;
8+
public $Route;
9+
public $ErrorHandler;
10+
11+
public function __construct() {
12+
try {
13+
$this->Request = new Request();
14+
$this->Route = new Route();
15+
$this->setErrorHandler(new DefaultErrorHandler());
16+
}
17+
catch (Exception $e) {
18+
throw $e;
19+
}
20+
}
21+
22+
public function setErrorHandler(IErrorHandler $errorHandler) {
23+
try {
24+
$this->ErrorHandler = $errorHandler;
25+
}
26+
catch (Exception $e) {
27+
throw $e;
28+
}
29+
}
30+
31+
public function useMvc() {
32+
try {
33+
$ControllerClass = $this->Route->getController($this->Request->Controller, $this->Request->IsApi);
34+
$Controller = new $ControllerClass($this->Request);
35+
$Controller->process();
36+
}
37+
catch (Exception $e) {
38+
throw $e;
39+
}
40+
}
41+
42+
public function useCors(string $origin) {
43+
try {
44+
if ($origin == "" || $origin == "*")
45+
$origin = $this->Request->Origin;
46+
header('Access-Control-Allow-Origin: ' . $origin);
47+
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS');
48+
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
49+
}
50+
catch (Exception $e) {
51+
throw $e;
52+
}
53+
}
54+
55+
abstract public function process();
56+
}
57+
?>

src/server/core/Controller.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
namespace phpcore\core;
3+
4+
use Exception;
5+
6+
abstract class Controller {
7+
protected $Request;
8+
protected $ViewFolder;
9+
10+
public function __construct(&$request) {
11+
try {
12+
$this->Request = $request;
13+
$this->ViewFolder = DEFAULT_VIEWS_FOLDER;
14+
$this->fixViewFolder();
15+
}
16+
catch (Exception $e) {
17+
throw $e;
18+
}
19+
}
20+
21+
public function fixViewFolder() {
22+
try {
23+
$this->ViewFolder = preg_replace("/\//", "\\", $this->ViewFolder);
24+
if (substr($this->ViewFolder, -1) != "\\")
25+
$this->ViewFolder = $this->ViewFolder . "\\";
26+
}
27+
catch (Exception $e) {
28+
throw $e;
29+
}
30+
}
31+
32+
public function view($view) {
33+
try {
34+
require_once($this->ViewFolder . $view . ".php");
35+
}
36+
catch (Exception $e) {
37+
throw $e;
38+
}
39+
}
40+
41+
public abstract function process();
42+
}
43+
?>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace phpcore\core;
3+
4+
use Exception;
5+
6+
class DefaultErrorHandler implements IErrorHandler {
7+
public function process($exception) {
8+
trigger_error($exception->getMessage());
9+
HttpCodes::internalServerError();
10+
}
11+
}
12+
?>

src/server/core/HttpCodes.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace phpcore\core;
3+
4+
use Exception;
5+
6+
class HttpCodes {
7+
public static function ok() {
8+
header("HTTP/1.1 200 OK");
9+
header("Content-Type: text/plain");
10+
}
11+
12+
public static function unauthorized() {
13+
header("HTTP/1.1 401 Unauthorized");
14+
header("Content-Type: text/plain");
15+
}
16+
17+
public static function forbidden() {
18+
header("HTTP/1.1 403 Forbidden");
19+
header("Content-Type: text/plain");
20+
}
21+
22+
public static function notFound() {
23+
header("HTTP/1.1 404 Not Found");
24+
header("Content-Type: text/plain");
25+
}
26+
27+
public static function methodNotAllowed() {
28+
header("HTTP/1.1 405 Method Not Allowed");
29+
header("Content-Type: text/plain");
30+
}
31+
32+
public static function internalServerError() {
33+
header("HTTP/1.1 500 Internal Server Error");
34+
header("Content-Type: text/plain");
35+
}
36+
}
37+
?>

src/server/core/IErrorHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace phpcore\core;
3+
4+
interface IErrorHandler {
5+
public function Process($exception);
6+
}
7+
?>

src/server/core/Request.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace phpcore\core;
3+
4+
use Exception;
5+
6+
class Request {
7+
public $IsApi = false;
8+
public $Origin;
9+
public $Method;
10+
public $Controller;
11+
public $Arguments = array();
12+
public $Headers = array();
13+
14+
public function __construct() {
15+
try {
16+
// Requests from the same server don't have a HTTP_ORIGIN header
17+
if (array_key_exists('HTTP_ORIGIN', $_SERVER))
18+
$this->Origin = $_SERVER['HTTP_ORIGIN'];
19+
else
20+
$this->Origin = $_SERVER['SERVER_NAME'];
21+
$this->Method = $_SERVER['REQUEST_METHOD'];
22+
if (isset($_REQUEST['q'])) {
23+
$this->Arguments = explode('/', ltrim(rtrim($_REQUEST['q'], '/'), '/'));
24+
}
25+
$this->Controller = array_shift($this->Arguments);
26+
if (preg_match('/^api$/i', $this->Controller)) {
27+
$this->IsApi = true;
28+
$this->Controller = array_shift($this->Arguments);
29+
}
30+
$this->Headers = $this->GetRequestHeaders();
31+
}
32+
catch (Exception $e) {
33+
throw $e;
34+
}
35+
}
36+
37+
public function getRequestHeaders() {
38+
try {
39+
if (!function_exists('apache_request_headers')) {
40+
foreach ($_SERVER as $key => $value) {
41+
if (preg_match('/^HTTP_/', $key)) {
42+
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
43+
$headers[$key] = $value;
44+
} else if ($key == "CONTENT_TYPE") {
45+
$headers["Content-Type"] = $value;
46+
} else if ($key == "CONTENT_LENGTH") {
47+
$headers["Content-Length"] = $value;
48+
}
49+
}
50+
}
51+
else {
52+
$headers = apache_request_headers();
53+
}
54+
return $headers;
55+
}
56+
catch (Exception $e) {
57+
throw $e;
58+
}
59+
}
60+
}
61+
?>

0 commit comments

Comments
 (0)