Skip to content

Commit d35c79a

Browse files
committed
Refactoring
1 parent e1481a0 commit d35c79a

14 files changed

+99
-76
lines changed

app/Adapter/UserAdapter.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

1212
use app\BaseAdapter;
13-
use app\ModelInterface;
1413

15-
class UserAdapter extends BaseAdapter implements ModelInterface
14+
class UserAdapter extends BaseAdapter
1615
{
1716
private $id = 0;
1817

app/App.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,24 @@
99

1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

12-
use flight\Engine;
13-
1412
class App
1513
{
1614
protected $app = null;
15+
1716
protected $id = 0;
1817
protected $token = null;
1918
protected $routers = [];
2019
protected $startTime;
2120
protected $config = [];
2221

23-
public function __construct(Engine $app, $config, $path)
22+
public function __construct(AppEngine $app, $config)
2423
{
2524
$this->app = $app;
2625

2726
// $config['app']['path'] = $path;
2827
$this->config = $config;
2928

3029
$this->app->set('flight.config', $config);
31-
32-
$this->app->register('request', 'app\Net\AppRequest', [$path]);
33-
$this->app->register('response', 'app\Net\AppResponse');
34-
$this->app->register('helper', 'app\Helper');
35-
$this->app->register('plugin', 'app\Plugin');
36-
$this->app->register('logger', 'app\Logger');
37-
$this->app->register('mailer', 'app\Lib\Mailer');
38-
$this->app->register('jwt', 'app\Lib\JWTAuth');
39-
$this->app->register('db', 'app\Lib\Db');
40-
4130
$this->app->set('flight.views.path', $this->app->request()->path() . '/resources/views');
4231

4332
$this->app->plugin()->configure($this->app);
@@ -74,6 +63,8 @@ private function configureDatabase()
7463
private function ckeckToken()
7564
{
7665
$token = $this->app->request()->getToken();
66+
if ($token === null) return;
67+
7768
$header = $this->app->jwt()->getHeader($token);
7869

7970
$this->app->plugin()->trigger('before', [$this, &$token, &$header]); // App_ckeckToken_before

app/AppEngine.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @author Raiza Rhamdan (Leonardo DaVchezt) <[email protected]>
4+
* @copyright Copyright (c), 2021 Raiza Rhamdan
5+
* @license MIT public license
6+
*/
7+
8+
namespace app;
9+
10+
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
11+
12+
use flight\Engine;
13+
14+
/**
15+
* The Engine class contains the core functionality of the framework.
16+
* It is responsible for loading an HTTP request, running the assigned services,
17+
* and generating an HTTP response.
18+
*
19+
* Core methods
20+
* @method void start() Starts engine
21+
* @method void stop() Stops framework and outputs current response
22+
* @method void halt(int $code = 200, string $message = '') Stops processing and returns a given response.
23+
*
24+
*
25+
* Routing
26+
* @method void route(string $pattern, callable $callback, bool $pass_route = false) Routes a URL to a callback function.
27+
* @method \flight\net\Router router() Gets router
28+
*
29+
* Views
30+
* @method void render(string $file, array $data = null, string $key = null) Renders template
31+
* @method \flight\template\View view() Gets current view
32+
*
33+
* Request-response
34+
* @method \app\Net\AppRequest request() Gets current request
35+
* @method \app\Net\AppResponse response() Gets current response
36+
* @method void error(\Exception $e) Sends an HTTP 500 response for any errors.
37+
* @method void notFound() Sends an HTTP 404 response when a URL is not found.
38+
* @method void redirect(string $url, int $code = 303) Redirects the current request to another URL.
39+
* @method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) Sends a JSON response.
40+
* @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) Sends a JSONP response.
41+
*
42+
* HTTP caching
43+
* @method void etag($id, string $type = 'strong') Handles ETag HTTP caching.
44+
* @method void lastModified(int $time) Handles last modified HTTP caching.
45+
*/
46+
47+
class AppEngine extends Engine
48+
{
49+
protected $appPath;
50+
51+
public function __construct($path) {
52+
$this->appPath = $path;
53+
54+
parent::__construct();
55+
}
56+
57+
public function init() {
58+
parent::init();
59+
60+
$this->loader->register('request', 'app\Net\AppRequest', [$this->appPath]);
61+
$this->loader->register('response', 'app\Net\AppResponse');
62+
$this->loader->register('helper', 'app\Helper');
63+
$this->loader->register('plugin', 'app\Plugin');
64+
$this->loader->register('logger', 'app\Logger');
65+
$this->loader->register('mailer', 'app\Lib\Mailer');
66+
$this->loader->register('jwt', 'app\Lib\JWTAuth');
67+
$this->loader->register('db', 'app\Lib\Db');
68+
}
69+
}

app/BaseAdapter.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

12-
use flight\Engine;
13-
1412
class BaseAdapter
1513
{
1614
protected $app;
@@ -21,7 +19,7 @@ public function __construct($tableName)
2119
$this->table = $tableName;
2220
}
2321

24-
public function setup(Engine $app)
22+
public function setup(AppEngine $app)
2523
{
2624
$this->app = $app;
2725
}

app/BasePlugin.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99

1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

12-
use flight\Engine;
13-
1412
abstract class BasePlugin
1513
{
1614
protected $app;
1715

18-
public function __construct(Engine $app)
16+
public function __construct(AppEngine $app)
1917
{
2018
$this->app = $app;
2119
}

app/BaseRouter.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99

1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

12-
use flight\Engine;
13-
1412
abstract class BaseRouter
1513
{
1614
protected $app;
1715
protected $id;
1816

19-
public function __construct(Engine $app, $userId)
17+
public function __construct(AppEngine $app, $userId)
2018
{
2119
$this->app = $app;
2220
$this->id = $userId;

app/Lib/JWTAuth.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @author Raiza Rhamdan (Leonardo DaVchezt) <[email protected]>
45
* @copyright Copyright (c), 2021 Raiza Rhamdan
@@ -73,12 +74,16 @@ public static function getToken($id, $username, $period = '12 hours')
7374
public static function getHeader($token)
7475
{
7576
$result = [];
76-
$obj = self::verifyToken($token);
77+
// config secret
78+
$secret = self::$app->get('flight.config')['app']['secret'];
79+
80+
// decode token
81+
$obj = JWT::decode($token, $secret, ['HS256']);
7782

78-
if ($obj && isset($obj->header)) {
83+
if (self::verifyToken($token) && isset($obj->header)) {
7984
$result = (array) $obj->header;
8085
}
81-
86+
8287
return $result;
8388
}
8489

@@ -103,10 +108,12 @@ public static function verifyToken($token)
103108
$now = strtotime(self::$app->helper()->timeNow(false, false, 'Y-m-d H:i:s'));
104109
// expiration date
105110
$exp = strtotime($obj->payload->exp);
106-
111+
107112
// chech expiration
108113
if (($exp - $now) > 0) {
109-
return $obj;
114+
// return $obj;
115+
116+
return true;
110117
}
111118
}
112119
} catch (\Exception $ex) {

app/Logger.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@
99

1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

12-
use flight\Engine;
13-
use app\Helper;
14-
1512
class Logger
1613
{
1714
private static $app;
1815
private static $path;
1916

20-
public static function configure(Engine $app)
17+
public static function configure(AppEngine $app)
2118
{
2219
self::$app = $app;
2320
self::$path = self::$app->request()->path() . '/logs';
2421
}
2522

2623
public static function path($path)
2724
{
28-
$this->path = $path;
25+
self::$path = $path;
2926
}
3027

3128
public static function write($message, $file = 'log')

app/Model.php

+2-20
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99

1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

12-
use flight\Engine;
13-
use app\ModelInterface;
14-
1512
class Model
1613
{
1714
protected $app;
1815
protected $adapter;
1916

20-
public function __construct(Engine $app)
17+
public function __construct(AppEngine $app)
2118
{
2219
$this->app = $app;
2320
}
@@ -29,7 +26,7 @@ public function __call($method, $args)
2926
}
3027
}
3128

32-
public function setAdapter(ModelInterface $adapter)
29+
public function setAdapter(BaseAdapter $adapter)
3330
{
3431
$this->adapter = $adapter;
3532
$this->adapter->setup($this->app);
@@ -39,19 +36,4 @@ public function getAdapter()
3936
{
4037
return $this->adapter;
4138
}
42-
43-
public function getById($id = 0)
44-
{
45-
return $this->adapter->getById($id);
46-
}
47-
48-
public function getAll()
49-
{
50-
return $this->adapter->getAll();
51-
}
52-
53-
public function getList($start = 0, $limit = 30)
54-
{
55-
return $this->adapter->getList($start, $limit);
56-
}
5739
}

app/ModelInterface.php

-17
This file was deleted.

app/Plugin.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99

1010
defined("__DAVCHEZT") or die("{ \"response\" : \"error 403\"}");
1111

12-
use flight\Engine;
13-
1412
class Plugin
1513
{
1614
protected static $app;
1715
protected static $pluginList = [];
1816
protected static $plugins = [];
1917

20-
public static function configure(Engine $app)
18+
public static function configure(AppEngine $app)
2119
{
2220
self::$app = $app;
2321
}

config.example.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
'host' => 'mail.domain.com',
2424
'user' => '[email protected]',
2525
'pass' => 'password',
26-
'name' => 'Raiza Rhamdan'
26+
'name' => 'Raiza Rhamdan'
2727
]
2828
];

index.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
}
1515

1616
use app\App;
17+
use app\AppEngine;
1718
use flight\Engine;
1819

1920
date_default_timezone_set('Asia/Jakarta');
2021
define('__DAVCHEZT', true);
2122

2223
require 'vendor/autoload.php';
2324

24-
$app = new App(new Engine, require 'config.php', realpath(dirname(__FILE__)));
25+
$app = new App(new AppEngine(realpath(dirname(__FILE__))), require 'config.php');
2526
$app->start();

resources/views/index.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
1313
<link href="https://fonts.googleapis.com/css2?family=Mr+Dafoe&amp;family=Inter:wght@400;600;700&amp;display=swap" rel="stylesheet"/>
1414
<!-- Styles -->
15+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css" />
1516
<style>
1617
html,
1718
body {
@@ -95,6 +96,7 @@
9596
</div>
9697
</div>
9798
</div>
99+
<a class="github-fork-ribbon" target="_blank" href="https://github.com/davchezt/rest-api" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
98100
</body>
99101

100102
</html>

0 commit comments

Comments
 (0)