Skip to content

Commit

Permalink
1. refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Jun 10, 2022
1 parent a1d290c commit 65d3171
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
vendor
example/WebApp/Cache
example/Web/caches/*
7 changes: 4 additions & 3 deletions example/Web/boot/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
$app->setAttr('controller/path', "{$root}/controllers");
$app->setAttr('model/namespace', 'WebApp\Model');
$app->setAttr('model/path', "{$root}/models");
$app->setAttr('view/path', "{$root}/views");
$app->setAttr('static/path', "{$root}/static");
$app->setAttr('cache/path', "{$root}/cache");
$app->setAttr('view/folders', [ "{$root}/views" ]);
$app->setAttr('static/folders', [ "{$root}/static" ]);
$app->setAttr('cache/path', "{$root}/caches");
$app->setAttr('cache/permission', 0777);

$app->run();
Empty file added example/Web/caches/.gitkeep
Empty file.
12 changes: 6 additions & 6 deletions src/Oni/CLI/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ private function loadTask(): bool

// Rewrite Task
if (null === $currentPath) {
$handler = ucfirst($this->getAttr('task/default/handler'));
$handlerName = ucfirst($this->getAttr('task/default/handler'));

if (false === file_exists("{$path}/{$handler}Task.php")) {
if (false === file_exists("{$path}/{$handlerName}Task.php")) {
return false;
}

$currentPath = $handler;
$currentPath = $handlerName;
}

// Task Flow
$currentNamaspece = implode('\\', explode('/', $currentPath));
$currentNamaspece = "{$namespace}\\{$currentNamaspece}Task";
$className = implode('\\', explode('/', $currentPath));
$className = "{$namespace}\\{$className}Task";

$instance = new $currentNamaspece();
$instance = new $className();

if (false !== $instance->up()) {
$instance->run($params);
Expand Down
182 changes: 108 additions & 74 deletions src/Oni/Web/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class App extends Basic
'model/namespace' => null, // Requied
'model/path' => null, // Requied

'view/path' => null, // Requied
'view/folders' => null, // Requied
'view/ext' => 'php',

'static/path' => null, // Requied
'static/folders' => null, // Requied

'cache/path' => null, // Requied
'cache/permission' => 0775, // rwxrwxr-x
'cache/time' => 300 // 300 sec = 5 min
];

Expand Down Expand Up @@ -132,21 +133,58 @@ public function run(): bool
*/
private function loadStatic(): bool
{
$path = $this->getAttr('static/path');
$folders = $this->getAttr('static/folders');
$uri = $this->req->uri();

if (false === is_string($path) || '' === $uri) {
if (false === is_array($folders) || '' === $uri) {
return false;
}

$currentPath = "{$path}/{$uri}";
$currentPath = null;

if (false === file_exists($currentPath)) {
foreach ($folders as $folder) {
if (false === file_exists("{$folder}/{$uri}")) {
continue;
}

$currentPath = "{$folder}/{$uri}";

break;
}

if (null === $currentPath) {
return false;
}

// Load Real File from Disk
return $this->loadRealFile($currentPath);
$mimeType = null;
$fileInfo = pathinfo($currentPath);

// Check File Ext
if (true === isset($fileInfo['extension'])) {

// Skip .php File
if ('php' === $fileInfo['extension']) {
return false;
}

// Check MIME Type
if (true === isset($this->_mimeMapping[$fileInfo['extension']])) {
$mimeType = $this->_mimeMapping[$fileInfo['extension']];
}
}

// Using Builtin Function to Check MIME Type
if (null === $mimeType) {
$mimeType = mime_content_type($currentPath);
}

// Set HTTP Header
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($currentPath));

echo file_get_contents($currentPath);

return true;
}

/**
Expand All @@ -159,7 +197,7 @@ private function loadCache(): bool
$path = $this->getAttr('cache/path');
$uri = $this->req->uri();

if (false === is_string($path) || '' === $uri) {
if (false === is_string($path)) {
return false;
}

Expand All @@ -176,46 +214,34 @@ private function loadCache(): bool
return false;
}

// Load Real File from Disk
return $this->loadRealFile($currentPath);
$this->res->html(file_get_contents($currentPath));

return true;
}

/**
* Load Real File
*
* @param string $currentPath
* Save Cache File
*
* @return bool
*/
private function loadRealFile(string $currentPath): bool
private function saveCache($html): bool
{
$mimeType = null;
$fileInfo = pathinfo($currentPath);

// Check File Ext
if (true === isset($fileInfo['extension'])) {

// Skip .php File
if ('php' === $fileInfo['extension']) {
return false;
}
$path = $this->getAttr('cache/path');
$uri = $this->req->uri();

// Check MIME Type
if (true === isset($this->_mimeMapping[$fileInfo['extension']])) {
$mimeType = $this->_mimeMapping[$fileInfo['extension']];
}
if (false === is_string($path)) {
return false;
}

// Using Builtin Function to Check MIME Type
if (null === $mimeType) {
$mimeType = mime_content_type($currentPath);
if (false === file_exists($path)) {
$permission = $this->getAttr('cache/permission');

mkdir($path, $permission, true);
}

// Set HTTP Header
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($currentPath));
$currentPath = "{$path}/" . md5($uri);

echo file_get_contents($currentPath);
file_put_contents($currentPath, $html);

return true;
}
Expand Down Expand Up @@ -251,61 +277,61 @@ private function loadController(): bool
}

// Rewrite Controller
$action = null;
$actionName = null;

if (null === $currentPath) {
$handler = ucfirst($this->getAttr('controller/default/handler'));
$handlerName = ucfirst($this->getAttr('controller/default/handler'));

if (false === file_exists("{$path}/{$handler}Controller.php")) {
$handler = ucfirst($this->getAttr('controller/error/handler'));
if (false === file_exists("{$path}/{$handlerName}Controller.php")) {
$handlerName = ucfirst($this->getAttr('controller/error/handler'));

if (false === file_exists("{$path}/{$handler}Controller.php")) {
if (false === file_exists("{$path}/{$handlerName}Controller.php")) {
http_response_code(400);

return false;
}

$action = $this->getAttr('controller/error/action');
$actionName = $this->getAttr('controller/error/action');
}

$currentPath = $handler;
$currentPath = $handlerName;
}

// Controller Flow
$currentNamaspece = implode('\\', explode('/', $currentPath));
$currentNamaspece = "{$namespace}\\{$currentNamaspece}Controller";
$className = implode('\\', explode('/', $currentPath));
$className = "{$namespace}\\{$className}Controller";

$instance = new $currentNamaspece();
$instance = new $className();

switch ($instance->getAttr('mode')) {
case 'page':
if (null === $action && 0 === count($params)) {
$action = $this->getAttr('controller/default/action');
if (null === $actionName && 0 === count($params)) {
$actionName = $this->getAttr('controller/default/action');
} else {
$action = array_shift($params);
$actionName = array_shift($params);
}

$currentAction = "{$action}Action";
$method = "{$actionName}Action";

if (false === method_exists($instance, $currentAction)) {
$handler = ucfirst($this->getAttr('controller/error/handler'));
if (false === method_exists($instance, $method)) {
$handlerName = ucfirst($this->getAttr('controller/error/handler'));

if (false === file_exists("{$path}/{$handler}Controller.php")) {
if (false === file_exists("{$path}/{$handlerName}Controller.php")) {
http_response_code(404);

return false;
}

$action = $this->getAttr('controller/error/action');
$currentPath = $handler;
$actionName = $this->getAttr('controller/error/action');
$currentPath = $handlerName;

$currentNamaspece = implode('\\', explode('/', $currentPath));
$currentNamaspece = "{$namespace}\\{$currentNamaspece}Controller";
$className = implode('\\', explode('/', $currentPath));
$className = "{$namespace}\\{$className}Controller";

$instance = new $currentNamaspece();
$currentAction = "{$action}Action";
$instance = new $className();
$method = "{$actionName}Action";

if (false === method_exists($instance, $currentAction)) {
if (false === method_exists($instance, $method)) {
http_response_code(404);

return false;
Expand All @@ -316,55 +342,63 @@ private function loadController(): bool

// Init View
$view = View::init();
$view->setAttr('path', $this->getAttr('view/path'));
$view->setAttr('folders', $this->getAttr('view/folders'));
$view->setAttr('ext', $this->getAttr('view/ext'));
$view->setLayoutPath(implode('/', array_map(function ($segment) {
return strtolower($segment);
}, explode('/', "{$currentPath}/{$action}"))));
}, explode('/', "{$currentPath}/{$actionName}"))));

$instance->$currentAction($params);
$instance->$method($params);

$this->res->html($view->render());
// Render HTML
$data = $view->render();

// Save Cache
if ('get' === $this->req->method()) {
$this->saveCache($data);
}

$this->res->html($data);
}

$instance->down();

break;
case 'ajax':
if (null === $action && 0 === count($params)) {
$action = $this->getAttr('controller/default/action');
if (null === $actionName && 0 === count($params)) {
$actionName = $this->getAttr('controller/default/action');
} else {
$action = array_shift($params);
$actionName = array_shift($params);
}

$currentAction = "{$action}Action";
$method = "{$actionName}Action";

if (false === method_exists($instance, $currentAction)) {
if (false === method_exists($instance, $method)) {
http_response_code(501);

return false;
}

if (false !== $instance->up()) {
$result = $instance->$currentAction($params);
$data = $instance->$method($params);

$this->res->json($result);
$this->res->json($data);
}

$instance->down();

break;
case 'rest':
$currentAction = $this->req->method() . 'Action';
$method = $this->req->method() . 'Action';

if (false === method_exists($instance, $currentAction)) {
if (false === method_exists($instance, $method)) {
http_response_code(501);

return false;
}

if (false !== $instance->up()) {
$result = $instance->$currentAction($params);
$result = $instance->$method($params);

$this->res->json($result);
}
Expand Down
10 changes: 7 additions & 3 deletions src/Oni/Web/Http/Res.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function redirect(string $path): void
public function html(string $data): void
{
header('Content-Type: text/html');
header('Content-Length: ' . strlen($data));

echo $data;
}
Expand All @@ -66,10 +67,13 @@ public function html(string $data): void
*/
public function json(array $data, ?integer $option = null): void
{
header('Content-Type: application/json');

echo (true === isset($option))
$data = (true === isset($option))
? json_encode($data, $option)
: json_encode($data);

header('Content-Type: application/json');
header('Content-Length: ' . strlen($data));

echo $data;
}
}
Loading

0 comments on commit 65d3171

Please sign in to comment.