Skip to content

Commit

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

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

$instance = new $namespace($this->io);
$instance = new $namespace();

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

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

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

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

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

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

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

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

$instance = new $namespace($this->req, $this->res, $view);
$instance = new $namespace();

switch ($instance->getAttr('mode')) {
case 'page':
Expand All @@ -241,6 +241,7 @@ private function loadController()
}

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

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

Expand All @@ -254,7 +255,7 @@ private function loadController()
}

$namespace = "{$namespace}\\{$handler}Controller";
$instance = new $namespace($this->req, $this->res, $view);
$instance = new $namespace();
$action = $this->getAttr('controller/error/action') . 'Action';

if (false === method_exists($instance, $action)) {
Expand All @@ -264,6 +265,13 @@ 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 @@ -295,7 +303,9 @@ private function loadController()
return false;
}

// Call Function: up -> xxxAction -> down
// Controller Flow
$instance->init();

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

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

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

/**
* Construct
* Initializer
*/
public function __construct($req = null, $res = null, $view = null)
final public function init()
{
$this->req = (null !== $req) ? $req : Req::init();
$this->res = (null !== $res) ? $res : Res::init();
$this->view = (null !== $view) ? $view : View::init();
$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();
});
}

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

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

/**
Expand Down

0 comments on commit 6fc969c

Please sign in to comment.