Skip to content

Commit

Permalink
1. refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Jun 9, 2022
1 parent 6c95e3c commit a1d290c
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 109 deletions.
7 changes: 2 additions & 5 deletions example/Web/controllers/AboutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,33 @@ class AboutController extends Controller
*/
public function up()
{
$this->view->setLayoutPath('index');
// pass
}

public function down()
{
$this->res->html($this->view->render());
// pass
}

/**
* Actions
*/
public function defaultAction()
{
$this->view->setContentPath('about/default');
$this->view->setData([
'title' => 'Oni - About / Default Page'
]);
}

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

public function mvvmAction()
{
$this->view->setContentPath('about/mvvm');
$this->view->setData([
'title' => 'Oni - About / MVVM Page'
]);
Expand Down
2 changes: 1 addition & 1 deletion example/Web/controllers/Api/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public function down()
*/
public function defaultAction()
{
$this->res->json($this->data);
return $this->data;
}
}
8 changes: 4 additions & 4 deletions example/Web/controllers/Api/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ public function down()
*/
public function getAction()
{
$this->res->json($this->data);
return $this->data;
}

public function postAction()
{
$this->res->json($this->data);
return $this->data;
}

public function putAction()
{
$this->res->json($this->data);
return $this->data;
}

public function deleteAction()
{
$this->res->json($this->data);
return $this->data;
}
}
6 changes: 2 additions & 4 deletions example/Web/controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ class MainController extends Controller
*/
public function up()
{
$this->view->setLayoutPath('index');
// pass
}

public function down()
{
$this->res->html($this->view->render());
// pass
}

/**
* Actions
*/
public function defaultAction($params = [])
{
$this->view->setContentPath('main/default');
$this->view->setData([
'title' => 'Oni - A Lightweight PHP Framework for Web & CLI',
'data' => [
Expand Down Expand Up @@ -62,7 +61,6 @@ public function defaultAction($params = [])

public function errorAction()
{
$this->view->setContentPath('main/error');
$this->view->setData([
'title' => 'Oni - Error Page'
]);
Expand Down
2 changes: 1 addition & 1 deletion example/Web/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<title><?=$title?></title>
</head>
<body>
<?=$this->loadContent()?>
<?=$this->loadLayout()?>
</body>
</html>
28 changes: 14 additions & 14 deletions src/Oni/CLI/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,39 @@ private function loadTask(): bool
$path = $this->getAttr('task/path');
$params = $this->io->getArguments();

while (0 < sizeof($params)) {
$param = $params[0];
$param = ucfirst($param);
$currentPath = null;

if (false === file_exists("{$path}/{$param}")
&& false === file_exists("{$path}/{$param}Task.php")
while (0 < count($params)) {
$tempPath = ucfirst($params[0]);
$tempPath = (null !== $currentPath) ? "{$currentPath}/{$tempPath}" : $tempPath;

if (false === file_exists("{$path}/{$tempPath}")
&& false === file_exists("{$path}/{$tempPath}Task.php")
) {
break;
}

$path = "{$path}/{$param}";
$namespace = "{$namespace}\\{$param}";
$currentPath = $tempPath;

array_shift($params);
}

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

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

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

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

$instance = new $currentNamaspece();

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

0 comments on commit a1d290c

Please sign in to comment.