Skip to content

Commit

Permalink
1. refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Aug 14, 2018
1 parent dde3866 commit b911ddd
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 113 deletions.
2 changes: 1 addition & 1 deletion example/CLI/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@

$app->setAttr('task/namespace', 'CLIApp\Task');
$app->setAttr('task/path', "{$root}/tasks");
$app->setAttr('task/default', 'Help');
$app->setAttr('task/default/handler', 'Help');

$app->run();
37 changes: 37 additions & 0 deletions example/Web/controllers/AboutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* About Controller
*
* @package Oni
* @author Scar Wu
* @copyright Copyright (c) Scar Wu (https://scar.tw)
* @link https://github.com/scarwu/Oni
*/

namespace WebApp\Controller;

use Oni\Web\Controller\Page as Controller;

class AboutController extends Controller
{
public function defaultAction()
{
$this->res->html('about/default', [
'title' => 'Oni - About / Default Page'
]);
}

public function mvcAction()
{
$this->res->html('about/mvc', [
'title' => 'Oni - About / MVC Page'
]);
}

public function mvvmAction()
{
$this->res->html('about/mvvm', [
'title' => 'Oni - About / MVVM Page'
]);
}
}
19 changes: 12 additions & 7 deletions example/Web/controllers/Api/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@

class TestController extends Controller
{
private $option;
private $data;

public function up()
{
$this->option = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE;
$this->data = [
'method' => $this->req->method(),
'method' => $this->req->method(),
'protocol' => $this->req->protocol(),
'scheme' => $this->req->scheme(),
'host' => $this->req->host(),
'uri' => $this->req->uri(),
'isAjax' => $this->req->isAjax(),
'contentLength' => $this->req->contentLength(),
'contentType' => $this->req->contentType(),
'body' => $this->req->body(),
'query' => $this->req->query(),
'content' => $this->req->content(),
'file' => $this->req->file(),
Expand All @@ -36,21 +41,21 @@ public function up()

public function getAction()
{
$this->res->json($this->data, $this->option);
var_dump($this->data);
}

public function postAction()
{
$this->res->json($this->data, $this->option);
var_dump($this->data);
}

public function putAction()
{
$this->res->json($this->data, $this->option);
var_dump($this->data);
}

public function deleteAction()
{
$this->res->json($this->data, $this->option);
var_dump($this->data);
}
}
27 changes: 21 additions & 6 deletions example/Web/controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,33 @@ class MainController extends Controller
{
public function defaultAction()
{
$this->res->html('index', [
$this->res->html('main/default', [
'title' => 'Oni - A Lightweight PHP Framework for Web & CLI',
'method' => $this->req->method(),
'query' => json_encode($this->req->query()),
'content' => json_encode($this->req->content()),
'file' => json_encode($this->req->file())
'data' => [
'method' => $this->req->method(),
'protocol' => $this->req->protocol(),
'scheme' => $this->req->scheme(),
'host' => $this->req->host(),
'uri' => $this->req->uri(),
'isAjax' => $this->req->isAjax(),
'contentLength' => $this->req->contentLength(),
'contentType' => $this->req->contentType(),
'body' => $this->req->body(),
'query' => $this->req->query(),
'content' => $this->req->content(),
'file' => $this->req->file(),
'native' => [
'server' => $_SERVER,
'post' => $_POST,
'get' => $_GET
]
]
]);
}

public function errorAction()
{
$this->res->html('error',[
$this->res->html('main/error', [
'title' => 'Oni - Error Page'
]);
}
Expand Down
1 change: 1 addition & 0 deletions example/Web/views/about/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><?=$title?></h1>
1 change: 1 addition & 0 deletions example/Web/views/about/mvc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><?=$title?></h1>
1 change: 1 addition & 0 deletions example/Web/views/about/mvvm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><?=$title?></h1>
8 changes: 0 additions & 8 deletions example/Web/views/error.php

This file was deleted.

6 changes: 1 addition & 5 deletions example/Web/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
<title><?=$title?></title>
</head>
<body>
<h1><?=$title?></h1>
<p>Method: <?=$method?></p>
<p>Query: <?=$query?></p>
<p>Content: <?=$content?></p>
<p>File: <?=$file?></p>
<?=$this->view->content()?>
</body>
</html>
2 changes: 2 additions & 0 deletions example/Web/views/main/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1><?=$title?></h1>
<?php var_dump($data); ?>
1 change: 1 addition & 0 deletions example/Web/views/main/error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><?=$title?></h1>
2 changes: 1 addition & 1 deletion src/Oni/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final public function setAttr($key, $value)
*
* @param string $key
*
* @return string|null
* @return mixed
*/
final public function getAttr($key)
{
Expand Down
36 changes: 18 additions & 18 deletions src/Oni/CLI/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public function __construct()
{
// Set Default Attributes
$this->_attr = [
'namespace' => 'OniApp',
'task/namespace' => null, // Requied
'task/path' => null, // Requied
'task/default' => 'Main'
'task/namespace' => null, // Requied
'task/path' => null, // Requied
'task/default/handler' => 'Main',
'task/error/handler' => 'Main'
];

// Set Instance
$this->io = IO::init();
}

Expand All @@ -43,21 +44,19 @@ public function __construct()
*/
public function run()
{
// Register Task Classes
// Register Task Classes & Load
$namespace = $this->getAttr('task/namespace');
$path = $this->getAttr('task/path');

if (null !== $namespace && null !== $path) {
Loader::append($namespace, $path);
}

// Load Task
if (null !== $this->getAttr('task/namespace')
&& null !== $this->getAttr('task/path')
&& $this->loadTask()) {

return true;
if ($this->loadTask()) {
return true;
}
}

return false;
}

/**
Expand Down Expand Up @@ -89,18 +88,19 @@ private function loadTask()

// Rewrite Task
if (false === file_exists("{$path}Task.php")) {
$default = $this->getAttr('task/default');
$default = ucfirst($default);
$namespace = $this->getAttr('task/namespace');
$path = $this->getAttr('task/path');
$handler = ucfirst($this->getAttr('task/default/handler'));

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

$namespace = "{$namespace}\\{$default}";
$namespace = "{$namespace}\\{$handler}Task";
} else {
$namespace = "{$namespace}Task";
}

// New Task Instance
$namespace = "{$namespace}Task";
$instance = new $namespace($this->io);

if (false !== $instance->up()) {
Expand Down
Loading

0 comments on commit b911ddd

Please sign in to comment.