Skip to content

Commit

Permalink
import dietcake
Browse files Browse the repository at this point in the history
  • Loading branch information
ttsuruoka committed Feb 1, 2012
0 parents commit ebf0714
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License

Copyright (c) 2010-2012 bunkyo-koki Co.,Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
86 changes: 86 additions & 0 deletions core/controller.php
@@ -0,0 +1,86 @@
<?php
class Controller
{
public $name; // コントローラ名
public $action; // アクション名
public $view; // ビュー

public $default_view_class = 'View'; // デフォルトのビュークラス名

public $output; // 出力結果

public function __construct($name)
{
$this->name = $name;
$this->view = new $this->default_view_class($this);
}

public function beforeFilter()
{
}

public function afterFilter()
{
}

public function dispatchAction()
{
if (!self::isAction($this->action)) {
// アクション名が予約語などで正しくないとき
throw new DCException('invalid action name');
}

if (!method_exists($this, '__call')) {
if (!method_exists($this, $this->action)) {
// アクションがコントローラに存在しないとき
throw new DCException('action does not exist');
}
$method = new ReflectionMethod($this, $this->action);
if (!$method->isPublic()) {
// アクションが public メソッドではないとき
throw new DCException('action is not public');
}
}

// アクションの実行
$this->{$this->action}();

$this->render();
}

// アクション名の妥当性を検証する
public static function isAction($action)
{
$methods = get_class_methods('Controller');
return !in_array($action, $methods);
}

// ビューに値を渡す
public function set($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $k => $v) {
$this->view->vars[$k] = $v;
}
} else {
$this->view->vars[$name] = $value;
}
}

public function beforeRender()
{
}

public function render($action = null)
{
static $is_rendered = false;

if ($is_rendered) {
return;
}

$this->beforeRender();
$this->view->render($action);
$is_rendered = true;
}
}
47 changes: 47 additions & 0 deletions core/dispatcher.php
@@ -0,0 +1,47 @@
<?php
class Dispatcher
{
public static function invoke()
{
list($controller_name, $action_name) = self::parseAction(Param::get(DC_ACTION));

$controller = self::getController($controller_name);

$controller->action = $action_name;
$controller->beforeFilter();
$controller->dispatchAction();
$controller->afterFilter();

echo $controller->output;
}

/**
* コントローラ/アクション名を取得する
*
* url は必ず http://example.com/index.php?dc_action=controller-name/action-name の形
*
*/
public static function parseAction($action)
{
$action = explode('/', $action);

if (count($action) < 2) {
throw new DCException('invalid url format');
}
$action_name = array_pop($action);
$controller_name = join("_", $action);

return array($controller_name, $action_name);
}

public static function getController($controller_name)
{
$controller_class = Inflector::camelize($controller_name) . 'Controller';

if (!class_exists($controller_class)) {
throw new DCException("{$controller_class} is not found");
}

return new $controller_class($controller_name);
}
}
4 changes: 4 additions & 0 deletions core/exception.php
@@ -0,0 +1,4 @@
<?php
class DCException extends Exception
{
}
13 changes: 13 additions & 0 deletions core/inflector.php
@@ -0,0 +1,13 @@
<?php
class Inflector
{
public static function camelize($str)
{
return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
}

public static function underscore($str)
{
return strtolower(preg_replace('/(?<=\\w)([A-Z]+)/', '_\\1', $str));
}
}
75 changes: 75 additions & 0 deletions core/model.php
@@ -0,0 +1,75 @@
<?php
class Model
{
public $id;
public $validation;
public $validation_errors;

public function __construct(array $data = array())
{
$this->set($data);
}

/**
* メンバの値をセットする
*
* 複数のメンバの値を一度にセットできます。
*
*/
public function set(array $data)
{
foreach ($data as $k => $v) {
$this->$k = $v;
}
}

/**
* メンバの値が正しいか検証する
*
* @return boolean 正しいとき true、それ以外のとき false
*/
public function validate()
{
$members = get_object_vars($this);
unset($members['validation']);
unset($members['validation_errors']);

$errors = 0;
foreach ($members as $member => $v) {
if (!isset($this->validation[$member])) continue;
foreach ($this->validation[$member] as $rule_name => $args) {
$validate_func = array_shift($args);
if (method_exists($this, $validate_func)) {
// 検証メソッドがモデルに存在するとき実行
$valid = call_user_func_array(array($this, $validate_func), array_merge(array($v), $args));
} elseif (function_exists($validate_func)) {
$valid = call_user_func_array($validate_func, array_merge(array($v), $args));
} else {
// 存在しない検証メソッドのときエラー
throw new DCException("{$validate_func} does not exist");
}
$this->validation_errors[$member][$rule_name] = $valid ? false : true;
if (!$valid) {
$errors++;
}
}
}
return $errors === 0 ? true : false;
}

/**
* メンバの値にエラーがあるか調べる
*
* @return boolean エラーがあるとき true、それ以外のとき false
*/
public function hasError()
{
if (empty($this->validation_errors)) return false;
foreach ($this->validation_errors as $v) {
foreach ($v as $w) {
if ($w) return true;
}
}
return false;
}
}
13 changes: 13 additions & 0 deletions core/param.php
@@ -0,0 +1,13 @@
<?php
class Param
{
public static function get($name, $default = null)
{
return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
}

public static function params()
{
return $_REQUEST;
}
}
48 changes: 48 additions & 0 deletions core/view.php
@@ -0,0 +1,48 @@
<?php
class View
{
public $controller; // コントローラへの参照
public $vars = array(); // 展開する変数
public static $ext = '.php';

public function __construct($controller)
{
$this->controller = $controller;
}

/**
* コンテンツをレンダリングする
*
* $this->render(); // 現在のコントローラ/アクションのビュー
* $this->render('edit'); // 現在のコントローラ、edit アクションのビュー
* $this->render('error/503'); // error コントローラ、503 アクションのビューをレンダリング
*
* @param string $action レンダリングするアクション名
* @return void
*/
public function render($action = null)
{
$action = is_null($action) ? $this->controller->action : $action;
if (strpos($action, '/') === false) {
$view_filename = VIEWS_DIR . $this->controller->name . '/' . $action . self::$ext;
} else {
$view_filename = VIEWS_DIR . $action . self::$ext;
}
$content = self::extract($view_filename, $this->vars);
$this->controller->output .= $content;
}

public static function extract($filename, $vars)
{
if (!file_exists($filename)) {
throw new DCException("{$filename} is not found");
}

extract($vars, EXTR_SKIP);
ob_start();
ob_implicit_flush(0);
include $filename;
$out = ob_get_clean();
return $out;
}
}
31 changes: 31 additions & 0 deletions dietcake.php
@@ -0,0 +1,31 @@
<?php
/**
* DietCake - Fastest MVC framework skeleton
*
* @copyright bunkyo-koki Co.,Ltd.
* @license MIT License
* @author Tatsuya Tsuruoka <http://github.com/ttsuruoka>
*/
mb_internal_encoding('UTF-8');
define('TIME_START', microtime(true));
define('DC_ACTION', 'dc_action');

define('DC_DIR', __DIR__.'/');
define('DC_CORE_DIR', DC_DIR.'core/');
define('CONFIG_DIR', APP_DIR.'config/');
define('CONTROLLERS_DIR', APP_DIR.'controllers/');
define('MODELS_DIR', APP_DIR.'models/');
define('VIEWS_DIR', APP_DIR.'views/');
define('HELPERS_DIR', APP_DIR.'helpers/');
define('TMP_DIR', APP_DIR.'tmp/');
define('LOGS_DIR', TMP_DIR.'logs/');
define('LIB_DIR', ROOT_DIR.'lib/');
define('VENDOR_DIR', ROOT_DIR.'vendor/');

require_once DC_CORE_DIR.'exception.php';
require_once DC_CORE_DIR.'inflector.php';
require_once DC_CORE_DIR.'param.php';
require_once DC_CORE_DIR.'model.php';
require_once DC_CORE_DIR.'view.php';
require_once DC_CORE_DIR.'controller.php';
require_once DC_CORE_DIR.'dispatcher.php';
30 changes: 30 additions & 0 deletions tests/ControllerTest.php
@@ -0,0 +1,30 @@
<?php
require_once dirname(__DIR__).'/core/controller.php';
require_once dirname(__DIR__).'/core/view.php';

class ControllerTest extends PHPUnit_Framework_TestCase
{
public function test_isAction()
{
$this->assertTrue(Controller::isAction('index'));
$this->assertTrue(Controller::isAction('view'));

$this->assertFalse(Controller::isAction('__construct'));
$this->assertFalse(Controller::isAction('beforeFilter'));
$this->assertFalse(Controller::isAction('isAction'));
$this->assertFalse(Controller::isAction('set'));
$this->assertFalse(Controller::isAction('render'));
}

public function test_set()
{
$controller = new Controller('');
$controller->set('foo', 100);
$controller->set('bar', array(1, 2));
$this->assertEquals(100, $controller->view->vars['foo']);
$this->assertEquals(array(1, 2), $controller->view->vars['bar']);

$controller->set(array('foo' => 200));
$this->assertEquals(200, $controller->view->vars['foo']);
}
}

0 comments on commit ebf0714

Please sign in to comment.