Skip to content

Commit dec4090

Browse files
committed
add autoload controller
1 parent 73a67fe commit dec4090

File tree

7 files changed

+420
-90
lines changed

7 files changed

+420
-90
lines changed

src/App.php

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,114 @@
11
<?php
22
namespace Appkita\SPARK;
3+
require_once __DIR__.DIRECTORY_SEPARATOR.'config.php';
4+
require_once __DIR__.DIRECTORY_SEPARATOR.'helps.php';
5+
require_once __DIR__.DIRECTORY_SEPARATOR.'Request.php';
6+
37
use Exception;
8+
use \Appkita\SPARK\Helps;
9+
use \Appkita\SPARK\Request;
10+
use \Appkita\SPARK\Config;
411

512
Class App {
6-
protected $path = '';
7-
protected $router = '';
8-
private $indexFiles = ['index.html', 'index.php'];
13+
use Config;
14+
public $request;
915

1016
function __construct($config = '') {
11-
$this->init($config);
12-
}
17+
$this->initConfig($config);
1318

14-
public function getListFiles() {
15-
return $this->listFiles;
19+
$this->request = new Request([
20+
'host'=>$this->getHost(),
21+
'port'=>$this->getPort(),
22+
'path'=>$this->getPath(),
23+
'router'=>$this->getRouter(),
24+
'indexFiles'=>$this->getIndexFiles()
25+
]);
1626
}
1727

18-
public function init($config = '') {
19-
if (!empty($config)) {
20-
if (\is_string($config)) {
21-
$this->path = $config;
22-
} else if (\is_object($config)) {
23-
if (isset($config->path)) $this->path = $config->path;
24-
if (isset($config->router)) $this->router = $config->router;
25-
if (isset($config->indexFiles)) $this->indexFiles = $config->indexFiles;
26-
} else if (\is_array($config)) {
27-
$config = (object) $config;
28-
if (isset($config->path)) $this->path = $config->path;
29-
if (isset($config->router)) $this->router = $config->router;
30-
if (isset($config->indexFiles)) $this->indexFiles = $config->indexFiles;
31-
}
28+
public function showError(int $code = 404) {
29+
$page = $this->pageError($code);
30+
if (\file_exists($page)) {
31+
$page = $this->pageError(404);
3232
}
33+
include $page;
34+
die();
3335
}
3436

35-
public function getRouter($page) {
36-
$router = $this->router;
37-
if (\is_array($router)){
38-
foreach ($router as $regex => $fn)
39-
{
40-
if (preg_match('%'.$regex.'%', $page))
41-
{
42-
return dirname(__FILE__) . $fn;
43-
break;
44-
}
45-
}
37+
public function pageError(int $code=404) {
38+
switch($code) {
39+
case 404:
40+
return 'Error/notfound.php';
41+
break;
4642
}
47-
return $page;
4843
}
4944

50-
public function getIndexPage($page) {
51-
if (is_dir($page))
52-
{
53-
foreach ($this->indexFiles as $filename)
54-
{
55-
56-
$fn = $page.DIRECTORY_SEPARATOR.$filename;
57-
if (is_file($fn)) {
58-
return $fn;
59-
break;
45+
public function _autoload() {
46+
if (is_string($this->autoload)) {
47+
if (\file_exists($this->autoload)) {
48+
require_once $this->autoload;
49+
}
50+
} else if (\is_array($this->autoload)) {
51+
for($i = 0; $i < sizeof($this->autoload); $i++) {
52+
if (\file_exists($this->autoload[$i])) {
53+
require_once $this->autoload[$i];
6054
}
6155
}
62-
return self::pageError(404);
6356
}
64-
return $page;
6557
}
6658

67-
public function pageError(int $code=404) {
68-
$path = \dirname(__FILE__);
69-
switch($code) {
70-
case 404:
71-
return 'Error/notfound.php';
72-
break;
59+
public function run() {
60+
$page = rtrim($this->request->getPage(), '\\%');
61+
$ext = (new \SplFileInfo($page))->getExtension();
62+
$isClass = false;
63+
if (empty($ext)) {
64+
if ($this->getRewrite() && !empty($this->getFileClass())) {
65+
$page = $this->getFileClass();
66+
$isClass = true;
67+
} else {
68+
$page = $this->pageError(404);
69+
}
7370
}
71+
return $this->openPage($page, $isClass);
7472
}
7573

76-
public function openPage($page = null) {
77-
if (empty($page)) {
78-
$page = '';
79-
}
80-
$page = \ltrim(\rtrim(rtrim($page, '/'), '\\'));
81-
$page = $this->path . $page;
82-
if (!\file_exists($page)) {
83-
include_once self::pageError(404);
84-
return true;
85-
}
86-
$page = self::getRouter($page);
87-
$page = self::getIndexPage($page);
74+
public function openPage($page, $isClass = false) {
8875
$ext = (new \SplFileInfo($page))->getExtension();
8976
if (\strtolower($ext) == 'php') {
90-
include_once $page;
91-
return true;
77+
$this->_autoload();
78+
$exp_page = explode('\\', $page);
79+
$pagename = str_replace('.php', '', $exp_page[(sizeof($exp_page) - 1)]);
80+
$class_file = strtolower(str_replace('.php', '', $this->file_class));
81+
if ($pagename == $class_file) {
82+
$isClass = true;
83+
}
84+
if ($isClass && $this->getRewrite() && !empty($this->getFileClass())) {
85+
if (file_exists($page)) {
86+
$page = $page;
87+
} else if (file_exists($this->getPath() . $page)) {
88+
$page = $this->getPath().$page;
89+
} else {
90+
return $this->showError(404);
91+
}
92+
include_once $page;
93+
$initclass = Helps::getClassNameFile($page);
94+
$classname = !empty($initclass->namespace) ? '\\'. $initclass->namespace : '\\';
95+
$classname .= $initclass->class;
96+
$class = new $classname();
97+
$uri = $this->request->getUri();
98+
if (\is_callable(array($class, strtolower($uri)))) {
99+
$param = $this->request->argsURL($uri);
100+
return \call_user_func_array(array($class, $uri), $param);
101+
} else {
102+
return $this->showError(404);
103+
}
104+
} else {
105+
if (file_exists($page)) {
106+
include_once $page;
107+
} else {
108+
return $this->showError(404);
109+
}
110+
}
111+
die();
92112
} else {
93113
if ($ext == 'html' || $ext == 'htm') {
94114
$mimi = 'text/html';
@@ -100,11 +120,11 @@ public function openPage($page = null) {
100120
}else{
101121
$mimi = mime_content_type($page);
102122
}
103-
header('Content-Type: '. $mimi);
123+
header('Content-Type: '. $mimi);
104124
$fh = fopen($page, 'r');
105125
fpassthru($fh);
106126
fclose($fh);
107-
return true;
127+
die();
108128
}
109129
}
110130
}
@@ -124,6 +144,5 @@ function is_cli() : bool{
124144
if (!is_cli()) {
125145
$env = json_decode(\getenv('CONFIG_ENV'));
126146
$app = new App($env);
127-
$page = !empty($page) ? $page : $_SERVER['REQUEST_URI'];
128-
$app->openPage($page);
147+
$app->run();
129148
}

src/Request.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
namespace Appkita\SPARK;
3+
class Request {
4+
private $_base_url = '';
5+
private $_method = '';
6+
private $_uri_segment;
7+
private $_path = '';
8+
private $_page = '';
9+
private $_list_index = [];
10+
private $_router= [];
11+
12+
function __construct(array $config = []) {
13+
$this->_method = $_SERVER['REQUEST_METHOD'];
14+
$this->init($config);
15+
$this->initRequest();
16+
}
17+
18+
public function initRequest () {
19+
$page = \ltrim(\ltrim(\parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'), '\\');
20+
$this->_uri_segment = \explode("/", $page);
21+
$this->_page = $this->getRouter($page);
22+
if ($this->_page == $page) {
23+
$this->_page = rtrim($this->_path, '\\') .'\\'. $this->_page;
24+
$this->_page = $this->getIndexPage($this->_page);
25+
}
26+
$explode = \explode('/', $this->_page);
27+
$_page = [];
28+
for($i=0; $i < sizeof($explode); $i++) {
29+
$ext = (new \SplFileInfo($explode[$i]))->getExtension();
30+
if (empty($ext)) {
31+
array_push($_page, $explode[$i]);
32+
} else {
33+
array_push($_page, $explode[$i]);
34+
break;
35+
}
36+
}
37+
$this->_page = \implode(DIRECTORY_SEPARATOR, $_page);
38+
}
39+
40+
public function showNotFound() {
41+
$page = __DIR__ .DIRECTORY_SEPARATOR. 'Error/notfound.php';
42+
include $page;
43+
die();
44+
}
45+
46+
public function init(array $config) {
47+
if (isset($config['host'])) {
48+
$this->_base_url = $config['host'];
49+
}
50+
if (isset($config['path'])) {
51+
$this->_path = $config['path'];
52+
}
53+
if (isset($config['indexFiles'])) {
54+
$this->_list_index = $config['indexFiles'];
55+
}
56+
if (isset($config['router']) && \is_array($config['router'])) {
57+
$this->_router = $config['router'];
58+
}
59+
if (isset($config['port']) && !empty($config['port'])) {
60+
if ($port != '80') {
61+
$this->_base_url = $this->host.':'. $this->port;
62+
}
63+
}
64+
}
65+
66+
public function getRouter($page) {
67+
$router = $this->_router;
68+
if (\is_array($router)){
69+
foreach ($router as $regex => $fn)
70+
{
71+
if (preg_match('%'.$regex.'%', $page))
72+
{
73+
return $fn;
74+
break;
75+
}
76+
}
77+
}
78+
return $page;
79+
}
80+
81+
public function getIndexPage($page) {
82+
if (is_dir($page))
83+
{
84+
foreach ($this->_list_index as $filename)
85+
{
86+
$page = \rtrim($page, '\\');
87+
$page = \ltrim($page, '/');
88+
$fn = $page.DIRECTORY_SEPARATOR.$filename;
89+
if (is_file($fn)) {
90+
return $fn;
91+
break;
92+
}
93+
}
94+
return $this->showNotFound();
95+
}
96+
return $page;
97+
}
98+
99+
public function getClass() {
100+
return $this->_class;
101+
}
102+
103+
public function getPage() {
104+
return $this->_page;
105+
}
106+
107+
public function getUri(int $segment = 1) {
108+
$uri = $this->getSegment(($segment > 0 ? $segment : 1));
109+
if (empty($uri)) {
110+
$uri = 'index';
111+
} else {
112+
if(strtolower(substr($uri, -4)) == '.php') {
113+
$uri = $this->getUri(2);
114+
}
115+
}
116+
return $uri;
117+
}
118+
119+
public function method(bool $lower) {
120+
return $lower ? \strtolower($this->_method) : \strtoupper($this->_method);
121+
}
122+
123+
public function base_url() {
124+
$host = \ltrim(\ltrim($this->_base_url, '\\'), '/');
125+
return $host .'/';
126+
}
127+
128+
public function argsURL($uri) {
129+
$hasil = [];
130+
$seg = $this->getSegment(0);
131+
for ($i = 0; $i < \sizeof($seg); $i++) {
132+
$arg = $seg[$i];
133+
if (!empty($arg)) {
134+
if(strtolower(substr($arg, -4)) != '.php' && $arg != $uri) {
135+
array_push($hasil, $arg);
136+
}
137+
}
138+
}
139+
return $hasil;
140+
}
141+
142+
public function getSegment(int $segment = 0) {
143+
if ($segment > 0) {
144+
$segment = (int) $segment - 1;
145+
return isset($this->_uri_segment[$segment]) ? $this->_req($this->_uri_segment[$segment], false) : '';
146+
} else {
147+
return $this->_uri_segment;
148+
}
149+
}
150+
151+
public function getVar($key = '', $safe = true, $filter = FILTER_SANITIZE_STRING) {
152+
if (!empty($post = $this->GetPOST($key, $safe, $filter))) {
153+
return $post;
154+
} else if (!empty($get = $this->getGet($key, $safe, $filter))) {
155+
return $get;
156+
} else {
157+
return $this->getJSON($key. $safe, $filter);
158+
}
159+
}
160+
161+
public function getJSON($key = '', $safe = true, $filter = FILTER_SANITIZE_STRING) {
162+
$json = file_get_contents('php://input');
163+
$data = json_decode($json);
164+
if (!empty($key)) {
165+
if (isset($data->{$key})) {
166+
return $this->_req($data->key, $safe, $filter);
167+
} else {
168+
return '';
169+
}
170+
} else {
171+
return $data;
172+
}
173+
}
174+
175+
public function getPOST($key = '', $safe = true, $filter = FILTER_SANITIZE_STRING) {
176+
$get = empty($key) ? $_GET : (isset($_POST[$key]) ? $_POST[$key] : '');
177+
return $this->_req($get, $safe, $fitler);
178+
}
179+
180+
public function getGet($key = '', $safe = true, $filter = FILTER_SANITIZE_STRING) {
181+
$get = empty($key) ? $_GET : (isset($_GET[$key]) ? $_GET[$key] : '');
182+
return $this->_req($get, $safe, $fitler);
183+
}
184+
185+
private function _req($var, $safe = true, $filter = FILTER_SANITIZE_STRING) {
186+
if ($safe) {
187+
$var = \htmlspecialchars($get);
188+
}
189+
if ($filter && \is_string($var)) {
190+
return \filter_var($var, $filter);
191+
} else {
192+
return $var;
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)