Skip to content

Commit

Permalink
1. refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Feb 10, 2022
1 parent 6fc969c commit 3e892ad
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 321 deletions.
8 changes: 2 additions & 6 deletions src/Oni/CLI/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class App extends Basic
*/
public function __construct()
{
$this->io = $this->initDI('io', function () {
return IO::init();
});
$this->io = IO::init();
}

/**
Expand Down Expand Up @@ -103,10 +101,8 @@ private function loadTask()
$namespace = "{$namespace}Task";
}

$instance = new $namespace();

// Task Flow
$instance->init();
$instance = new $namespace();

if (false !== $instance->up()) {
$instance->run($params);
Expand Down
8 changes: 3 additions & 5 deletions src/Oni/CLI/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ abstract class Task extends Basic
protected $io = null;

/**
* Initializer
* Construct
*/
final public function init()
public function __construct()
{
$this->io = $this->initDI('io', function () {
return IO::init();
});
$this->io = IO::init();
}

/**
Expand Down
49 changes: 0 additions & 49 deletions src/Oni/Core/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,6 @@

abstract class Basic
{
/**
* @var array
*/
private static $_di = [];

/**
* Set DI
*
* @param string $key
* @param object $object
*
* @return object
*/
final public function setDI($key, $object)
{
self::$_di[$key] = $object;
}

/**
* Get DI
*
* @param string $key
*
* @return object
*/
final public function getDI($key)
{
return (true === isset(self::$_di[$key]))
? self::$_di[$key] : null;
}

/**
* Init DI
*
* @param string $key
* @param function $callback
*
* @return object
*/
final public function initDI($key, $callback = null)
{
if (false === isset(self::$_di[$key])) {
self::$_di[$key] = true === is_callable($callback)
? $callback() : null;
}

return self::$_di[$key];
}

/**
* @var array
*/
Expand Down
13 changes: 0 additions & 13 deletions src/Oni/Core/DI.php

This file was deleted.

25 changes: 7 additions & 18 deletions src/Oni/Web/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ class App extends Basic
*/
public function __construct()
{
$this->req = $this->initDI('req', function () {
return Req::init();
});

$this->res = $this->initDI('res', function () {
return Res::init();
});
$this->req = Req::init();
$this->res = Res::init();
}

/**
Expand Down Expand Up @@ -228,10 +223,15 @@ private function loadController()
$namespace = "{$namespace}Controller";
}

// Controller Flow
$instance = new $namespace();

switch ($instance->getAttr('mode')) {
case 'page':
$view = View::init();
$view->setAttr('path', $this->getAttr('view/path'));
$view->setAttr('ext', $this->getAttr('view/ext'));

if (null === $action) {
if (0 === count($params)) {
$action = $this->getAttr('controller/default/action') . 'Action';
Expand All @@ -241,7 +241,6 @@ private function loadController()
}

if (false === method_exists($instance, $action)) {

$namespace = $this->getAttr('controller/namespace');
$path = $this->getAttr('controller/path');

Expand All @@ -265,13 +264,6 @@ private function loadController()
}
}

// Set View Attrs
$view = View::init();
$view->setAttr('path', $this->getAttr('view/path'));
$view->setAttr('ext', $this->getAttr('view/ext'));

$this->setDI('view', $view);

break;
case 'ajax':
if (null === $action) {
Expand Down Expand Up @@ -303,9 +295,6 @@ private function loadController()
return false;
}

// Controller Flow
$instance->init();

if (false !== $instance->up()) {
$instance->$action($params);
}
Expand Down
12 changes: 4 additions & 8 deletions src/Oni/Web/Controller/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ abstract class Ajax extends Basic
protected $res = null;

/**
* Initializer
* Construct
*/
final public function init()
public function __construct()
{
$this->req = $this->initDI('req', function () {
return Req::init();
});
$this->res = $this->initDI('res', function () {
return Res::init();
});
$this->req = Req::init();
$this->res = Res::init();
}

/**
Expand Down
16 changes: 5 additions & 11 deletions src/Oni/Web/Controller/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,13 @@ abstract class Page extends Basic
protected $view = null;

/**
* Initializer
* Construct
*/
final public function init()
public function __construct()
{
$this->req = $this->initDI('req', function () {
return Req::init();
});
$this->res = $this->initDI('res', function () {
return Res::init();
});
$this->view = $this->initDI('view', function () {
return View::init();
});
$this->req = Req::init();
$this->res = Res::init();
$this->view = View::init();
}

/**
Expand Down
12 changes: 4 additions & 8 deletions src/Oni/Web/Controller/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ abstract class Rest extends Basic
protected $res = null;

/**
* Initializer
* Construct
*/
final public function init()
public function __construct()
{
$this->req = $this->initDI('req', function () {
return Req::init();
});
$this->res = $this->initDI('res', function () {
return Res::init();
});
$this->req = Req::init();
$this->res = Res::init();
}

/**
Expand Down
Loading

0 comments on commit 3e892ad

Please sign in to comment.