Skip to content

Commit

Permalink
1. refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Aug 8, 2018
1 parent cd4a284 commit 6610e31
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 68 deletions.
5 changes: 3 additions & 2 deletions example/CLI/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
// New Oni CLI Application Instance
$app = new Oni\CLI\App();

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

$app->run();
14 changes: 8 additions & 6 deletions example/Web/boot/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
// New Oni Web Application Instance
$app = new Oni\Web\App();

$app->setAttr('name', 'WebApp');
$app->setAttr('controller', "{$root}/controllers");
$app->setAttr('model', "{$root}/models");
$app->setAttr('view', "{$root}/views");
$app->setAttr('static', "{$root}/static");
$app->setAttr('cache', "{$root}/cache");
$app->setAttr('namespace', 'WebApp');
$app->setAttr('controller/namespace', 'WebApp\Controller');
$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->run();
93 changes: 78 additions & 15 deletions src/Oni/CLI/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,91 @@ class App extends Basic
*/
protected $io = null;

/**
* @var array
*/
private $_namespace_list = [];

/**
* Construct
*/
public function __construct() {
public function __construct()
{
// Set Default Attributes
$this->_attr = [
'name' => 'OniApp',
'task' => null, // Requied
'namespace' => 'OniApp',
'task/namespace' => null, // Requied
'task/path' => null, // Requied
'task/default' => 'Main'
];

$this->io = IO::init();

// Namespace Autoload Register
spl_autoload_register(function ($class_name) {
$class_name = trim($class_name, '\\');

foreach ($this->_namespace_list as $namespace => $path_list) {
$pattern = '/^' . str_replace('\\', '\\\\', $namespace) . '/';

if (!preg_match($pattern, $class_name)) {
continue;
}

$class_name = str_replace($namespace, '', trim($class_name, '\\'));
$class_name = str_replace('\\', '/', trim($class_name, '\\'));

foreach ($path_list as $path) {
if (!file_exists("{$path}/{$class_name}.php")) {
continue;
}

require "{$path}/{$class_name}.php";

return true;
}
}

return false;
});
}

/**
* Register Namespace
*
* @param string $namespace
* @param string $path
*/
public function registerNamespace($namespace, $path)
{
$namespace = trim($namespace, '\\');
$path = rtrim($path, '/');

if (!isset($this->_namespace_list[$namespace])) {
$this->_namespace_list[$namespace] = [];
}

$this->_namespace_list[$namespace][] = $path;
}

/**
* Run
*/
public function run()
{
// Register Task Classes
$namespace = $this->getAttr('task/namespace');
$path = $this->getAttr('task/path');

if (null !== $namespace && null !== $path) {
$this->registerNamespace($namespace, $path);
}

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

return true;
}
}
Expand All @@ -52,9 +117,8 @@ public function run()
*/
private function loadTask()
{
$current_path = $this->getAttr('task');
$current_namespace = $this->getAttr('name') . '\\Task';

$namespace = $this->getAttr('task/namespace');
$path = $this->getAttr('task/path');
$segments = $this->io->getArguments();

// Set Deafult Task
Expand All @@ -65,25 +129,24 @@ private function loadTask()
foreach ($segments as $segment) {
$segment = ucfirst($segment);

$current_path = "{$current_path}/{$segment}";
$current_namespace = "{$current_namespace}\\{$segment}";
$path = "{$path}/{$segment}";
$namespace = "{$namespace}\\{$segment}";
}

if (false === file_exists("{$current_path}Task.php")) {
if (false === file_exists("{$path}Task.php")) {
throw new Exception("Task is not found.");
}

// Require Task
require "{$current_path}Task.php";

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

if (false !== $instance->up()) {
$instance->run();
}

$instance->down();

return true;
}
}
3 changes: 2 additions & 1 deletion src/Oni/CLI/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class IO extends Basic
*
* This function is private, so this class is singleton pattern
*/
private function __construct() {
private function __construct()
{
$config_regex_rule = '/^-{2}(\w+(?:-\w+)?)(?:=(.+))?/';
$option_regex_rule = '/^-{1}(\w+)/';

Expand Down
3 changes: 2 additions & 1 deletion src/Oni/CLI/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ abstract class Task extends Basic
/**
* Construct
*/
public function __construct($io = null) {
public function __construct($io = null)
{
$this->io = (null !== $io) ? $io : IO::init();
}

Expand Down
Loading

0 comments on commit 6610e31

Please sign in to comment.