From cbbfa3556880cc2c7b0b379cae8b6ba7d2fab0d5 Mon Sep 17 00:00:00 2001
From: root <fadhilimamk@gmail.com>
Date: Mon, 2 Oct 2017 17:26:15 +0700
Subject: [PATCH 1/3] Initialize simple MVC handler

---
 .htaccess                         |  4 +++
 public/index.php                  |  9 +++++
 public/main.css                   |  0
 public/scripts.js                 |  0
 src/app.php                       | 59 +++++++++++++++++++++++++++++++
 src/controller/MainController.php | 10 ++++++
 6 files changed, 82 insertions(+)
 create mode 100644 .htaccess
 create mode 100644 public/index.php
 create mode 100644 public/main.css
 create mode 100644 public/scripts.js
 create mode 100644 src/app.php
 create mode 100644 src/controller/MainController.php

diff --git a/.htaccess b/.htaccess
new file mode 100644
index 0000000..f97815c
--- /dev/null
+++ b/.htaccess
@@ -0,0 +1,4 @@
+Options -MultiViews
+RewriteEngine On
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule ^ public/index.php [QSA,L]
\ No newline at end of file
diff --git a/public/index.php b/public/index.php
new file mode 100644
index 0000000..b14eceb
--- /dev/null
+++ b/public/index.php
@@ -0,0 +1,9 @@
+<?php
+
+    error_reporting(E_ALL);
+    ini_set('display_errors', 1);
+
+    require __DIR__.'/../src/app.php';
+
+    $App = DagoJek::Instance();
+    $App->Start();
\ No newline at end of file
diff --git a/public/main.css b/public/main.css
new file mode 100644
index 0000000..e69de29
diff --git a/public/scripts.js b/public/scripts.js
new file mode 100644
index 0000000..e69de29
diff --git a/src/app.php b/src/app.php
new file mode 100644
index 0000000..62e26a9
--- /dev/null
+++ b/src/app.php
@@ -0,0 +1,59 @@
+<?php
+
+class DagoJek {
+
+    private function __construct() {
+        // empty constructor
+    }
+
+    public static function Instance() {
+        static $instance = null;
+        if ($instance === null) {
+            $instance = new DagoJek();
+        }
+
+        $instance->includeAllController();
+
+        return $instance;
+    }
+
+    private function includeAllController() {
+        foreach (scandir(dirname(__FILE__)."/controller") as $filename) {
+            $path = dirname(__FILE__)."/controller" . '/' . $filename;
+            if (is_file($path)) {
+                require_once $path;
+            }
+        }
+    }
+
+    private function getCurrentUri() {
+        $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
+        $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
+        if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
+        $uri = '/' . trim($uri, '/');
+        return $uri;
+    }
+
+    public function Start() {
+        $base_url = $this->getCurrentUri();
+        switch ($base_url) {
+            case "/" :
+                echo "Login page";
+                break;
+            case "/login" :
+                MainController::LoginHandler();
+                break;
+            case "/register" :
+                echo "Register page";
+                break;
+            case "/profil" :
+                echo "Profil page";
+                break;
+            default :
+                echo "404 not found";
+        }
+    }
+    
+}
+
+?>
\ No newline at end of file
diff --git a/src/controller/MainController.php b/src/controller/MainController.php
new file mode 100644
index 0000000..30991ff
--- /dev/null
+++ b/src/controller/MainController.php
@@ -0,0 +1,10 @@
+<?php
+
+class MainController {
+
+    public static function LoginHandler() {
+        echo "This is login handler";
+    }
+
+
+}
\ No newline at end of file
-- 
GitLab


From 15ce9b94bb7da34ccfe22d4fa450bf77f770736f Mon Sep 17 00:00:00 2001
From: root <fadhilimamk@gmail.com>
Date: Tue, 3 Oct 2017 00:30:03 +0700
Subject: [PATCH 2/3] Implement connection between routing and controller

---
 public/index.php                  |  1 +
 src/app.php                       | 38 +++++++++++++++++--------------
 src/controller/MainController.php |  3 +++
 src/route.php                     | 15 ++++++++++++
 4 files changed, 40 insertions(+), 17 deletions(-)
 create mode 100644 src/route.php

diff --git a/public/index.php b/public/index.php
index b14eceb..3496c60 100644
--- a/public/index.php
+++ b/public/index.php
@@ -6,4 +6,5 @@
     require __DIR__.'/../src/app.php';
 
     $App = DagoJek::Instance();
+    $App->prepareRouting();
     $App->Start();
\ No newline at end of file
diff --git a/src/app.php b/src/app.php
index 62e26a9..0bdcc30 100644
--- a/src/app.php
+++ b/src/app.php
@@ -1,7 +1,13 @@
 <?php
 
+
+/**
+ * DagoJek is a singleton class that representing DagoJek application
+ */
 class DagoJek {
 
+    private $routingTable = array();
+
     private function __construct() {
         // empty constructor
     }
@@ -26,6 +32,10 @@ class DagoJek {
         }
     }
 
+    public function prepareRouting() {
+        require __DIR__.'/route.php';
+    }
+
     private function getCurrentUri() {
         $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
         $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
@@ -34,26 +44,20 @@ class DagoJek {
         return $uri;
     }
 
+    public function addRoute($route, $function) {
+        $data = array(
+            $route => $function,
+        );
+        $this->routingTable += $data;
+    }
+
     public function Start() {
         $base_url = $this->getCurrentUri();
-        switch ($base_url) {
-            case "/" :
-                echo "Login page";
-                break;
-            case "/login" :
-                MainController::LoginHandler();
-                break;
-            case "/register" :
-                echo "Register page";
-                break;
-            case "/profil" :
-                echo "Profil page";
-                break;
-            default :
-                echo "404 not found";
+        if (array_key_exists ($base_url, $this->routingTable)) {
+            $this->routingTable[$base_url]();
+        } else {
+            die ("404 Page not Found");
         }
     }
     
 }
-
-?>
\ No newline at end of file
diff --git a/src/controller/MainController.php b/src/controller/MainController.php
index 30991ff..c0fa816 100644
--- a/src/controller/MainController.php
+++ b/src/controller/MainController.php
@@ -6,5 +6,8 @@ class MainController {
         echo "This is login handler";
     }
 
+    public static function DefaultHandler() {
+        echo "This is default handler";
+    }
 
 }
\ No newline at end of file
diff --git a/src/route.php b/src/route.php
new file mode 100644
index 0000000..0b033a3
--- /dev/null
+++ b/src/route.php
@@ -0,0 +1,15 @@
+<?php
+
+// ---------------------------- LIST OF ALL BASIC ROUTES ----------------------------
+
+$AppInstance = Dagojek::Instance();
+
+$AppInstance->addRoute("/",                     'MainController::LoginHandler');
+$AppInstance->addRoute("/login",                'MainController::LoginHandler');
+$AppInstance->addRoute("/register",             'MainController::DefaultHandler');
+$AppInstance->addRoute("/main/profil",          'MainController::DefaultHandler');
+$AppInstance->addRoute("/main/profil/edit",     'MainController::DefaultHandler');
+$AppInstance->addRoute("/main/history",         'MainController::DefaultHandler');
+$AppInstance->addRoute("/main/order/",          'MainController::DefaultHandler');
+$AppInstance->addRoute("/main/order/select",    'MainController::DefaultHandler');
+$AppInstance->addRoute("/main/order/finish",    'MainController::DefaultHandler');
-- 
GitLab


From 410e4a27a2f75b1010d7262569cee629f25f7f69 Mon Sep 17 00:00:00 2001
From: root <fadhilimamk@gmail.com>
Date: Tue, 3 Oct 2017 00:40:33 +0700
Subject: [PATCH 3/3] Hide error when not in development environment

---
 public/index.php | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/public/index.php b/public/index.php
index 3496c60..24fcb56 100644
--- a/public/index.php
+++ b/public/index.php
@@ -1,7 +1,11 @@
 <?php
 
-    error_reporting(E_ALL);
-    ini_set('display_errors', 1);
+    $env = getenv('DAGOJEK_ENV');
+    if (!$env || $env == "development") {
+        $env = "development";
+        error_reporting(E_ALL);
+        ini_set('display_errors', 1);
+    }
 
     require __DIR__.'/../src/app.php';
 
-- 
GitLab