Skip to content

Commit

Permalink
1. add web/view class
Browse files Browse the repository at this point in the history
2. add web/help class
3. update example
4. refactor
  • Loading branch information
scarwu committed Aug 14, 2018
1 parent b911ddd commit 4ef0afb
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 60 deletions.
21 changes: 15 additions & 6 deletions example/Web/controllers/AboutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@ class AboutController extends Controller
{
public function defaultAction()
{
$this->res->html('about/default', [
$this->view->setLayoutPath('index');
$this->view->setContentPath('about/default');

$this->res->html($this->view->render([
'title' => 'Oni - About / Default Page'
]);
]));
}

public function mvcAction()
{
$this->res->html('about/mvc', [
$this->view->setLayoutPath('index');
$this->view->setContentPath('about/mvc');

$this->res->html($this->view->render([
'title' => 'Oni - About / MVC Page'
]);
]));
}

public function mvvmAction()
{
$this->res->html('about/mvvm', [
$this->view->setLayoutPath('index');
$this->view->setContentPath('about/mvvm');

$this->res->html($this->view->render([
'title' => 'Oni - About / MVVM Page'
]);
]));
}
}
14 changes: 10 additions & 4 deletions example/Web/controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class MainController extends Controller
{
public function defaultAction()
{
$this->res->html('main/default', [
$this->view->setLayoutPath('index');
$this->view->setContentPath('main/default');

$this->res->html($this->view->render([
'title' => 'Oni - A Lightweight PHP Framework for Web & CLI',
'data' => [
'method' => $this->req->method(),
Expand All @@ -37,13 +40,16 @@ public function defaultAction()
'get' => $_GET
]
]
]);
]));
}

public function errorAction()
{
$this->res->html('main/error', [
$this->view->setLayoutPath('index');
$this->view->setContentPath('main/error');

$this->res->html($this->view->render([
'title' => 'Oni - Error Page'
]);
]));
}
}
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->view->content()?>
<?=$this->loadContent()?>
</body>
</html>
1 change: 0 additions & 1 deletion src/Oni/CLI/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\CLI;

use Exception;
use Oni\Basic;
use Oni\Loader;
use Oni\CLI\IO;
Expand Down
1 change: 0 additions & 1 deletion src/Oni/CLI/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\CLI;

use Exception;
use Oni\Basic;

class IO extends Basic
Expand Down
1 change: 0 additions & 1 deletion src/Oni/CLI/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\CLI;

use Exception;
use Oni\Basic;
use Oni\CLI\IO;

Expand Down
2 changes: 0 additions & 2 deletions src/Oni/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace Oni;

use Exception;

class Loader
{
/**
Expand Down
15 changes: 8 additions & 7 deletions src/Oni/Web/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace Oni\Web;

use Exception;
use Oni\Basic;
use Oni\Loader;
use Oni\Web\Req;
use Oni\Web\Res;
use Oni\Web\View;

class App extends Basic
{
Expand Down Expand Up @@ -72,10 +72,6 @@ public function run()
return true;
}

// Set Response Attrs
$this->res->setAttr('view/path', $this->getAttr('view/path'));
$this->res->setAttr('view/ext', $this->getAttr('view/ext'));

// Register Model Classes
$namespace = $this->getAttr('model/namespace');
$path = $this->getAttr('model/path');
Expand Down Expand Up @@ -229,7 +225,12 @@ private function loadController()
$action = null;
}

$instance = new $namespace($this->req, $this->res);
// 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);

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

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

if (false === method_exists($instance, $action)) {
Expand Down
10 changes: 8 additions & 2 deletions src/Oni/Web/Controller/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

namespace Oni\Web\Controller;

use Exception;
use Oni\Basic;
use Oni\Web\Req;
use Oni\Web\Res;
use Oni\Web\View;

abstract class Page extends Basic
{
/**
* @var array
*/
protected $view = null;

/**
* @var array
*/
Expand All @@ -30,7 +35,7 @@ abstract class Page extends Basic
/**
* Construct
*/
public function __construct($req = null, $res = null)
public function __construct($req = null, $res = null, $view = null)
{
// Set Default Attributes
$this->_attr = [
Expand All @@ -40,6 +45,7 @@ public function __construct($req = null, $res = null)
// Set Instance(s)
$this->req = (null !== $req) ? $req : Req::init();
$this->res = (null !== $res) ? $res : Res::init();
$this->view = (null !== $view) ? $view : View::init();
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Oni/Web/Controller/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\Web\Controller;

use Exception;
use Oni\Basic;
use Oni\Web\Req;
use Oni\Web\Res;
Expand Down
45 changes: 45 additions & 0 deletions src/Oni/Web/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Helper
*
* @package Oni
* @author Scar Wu
* @copyright Copyright (c) Scar Wu (https://scar.tw)
* @link https://github.com/scarwu/Oni
*/

namespace Oni\Web;

class Helper {

/**
* Create Link To
*
* @param string $link
* @param string $name
*
* @return string
*/
public static function linkTo($link, $name)
{
$link = self::linkEncode($link);

return "<a href=\"{$link}\">{$name}</a>";
}

/**
* Link Encode
*
* @param string $link
*
* @return string
*/
public static function linkEncode($link)
{
$segments = explode('/', $link);
$segments = array_map('rawurlencode', $segments);
$link = implode('/', $segments);

return $link;
}
}
1 change: 0 additions & 1 deletion src/Oni/Web/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\Web;

use Exception;
use Oni\Basic;

class Model extends Basic
Expand Down
5 changes: 1 addition & 4 deletions src/Oni/Web/Req.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

namespace Oni\Web;

use Exception;
use Oni\Basic;

class Req extends Basic
class Req extends
{
/**
* @var object
Expand Down
41 changes: 15 additions & 26 deletions src/Oni/Web/Res.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

namespace Oni\Web;

use Exception;
use Oni\Basic;

class Res extends Basic
class Res extends
{
/**
* @var object
Expand All @@ -25,14 +22,7 @@ class Res extends Basic
*
* This function is private, so this class is singleton pattern
*/
private function __construct()
{
// Set Default Attributes
$this->_attr = [
'view/path' => null,
'view/ext' => 'php'
];
}
private function __construct() {}

/**
* Initialize
Expand All @@ -46,27 +36,26 @@ public static function init()
return self::$_instance;
}

/**
* Redirect
*
* @param string $path
*/
public function redirect($path)
{
header("Location: {$path}");
}

/**
* Render HTML
*
* @param string $_name
* @param array $_data
* @param string $data
*/
public function html($_name, $_data = [])
public function html($data)
{
header('Content-Type: text/html');

$_path = $this->getAttr('view/path');
$_ext = $this->getAttr('view/ext');
$_fullpath = "{$_path}/{$_name}.{$_ext}";

if (file_exists($_fullpath)) {
foreach ($_data as $_key => $_value) {
$$_key = $_value;
}

include $_fullpath;
}
echo $data;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Oni/Web/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\Web;

use Exception;
use Oni\Basic;

class Router extends Basic
Expand Down
1 change: 0 additions & 1 deletion src/Oni/Web/Store/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\Web\Store;

use Exception;
use Memcached;
use Oni\Basic;

Expand Down
1 change: 0 additions & 1 deletion src/Oni/Web/Store/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Oni\Web\Store;

use Exception;
use PDO;
use Oni\Basic;

Expand Down
Loading

0 comments on commit 4ef0afb

Please sign in to comment.