Permalink
Browse files

Added CLI (command-line interface). Fixed project structure.

  • Loading branch information...
1 parent 15523e9 commit cebcb341e3aca5f6ba7c8aed5b83705e3d0a2099 @Xeoncross Xeoncross committed Dec 10, 2010
View
2 bootstrap.php
@@ -1,4 +1,4 @@
-<?php
+<?php defined('T') OR die();
/*
* EDIT THIS FILE TO SETUP SYSTEM STATE
* If posible, these should be set in the php.ini instead of here!
View
4 classes/controller.php
@@ -2,6 +2,6 @@
// Controller Class
class Controller
{
-protected function render(){$l=new View('layout');$l->set($this);echo$l;}
-function show_404(){header("HTTP/1.0 404 Not Found");$this->content=new View('404');$this->render();}
+function render(){$l=new View('layout');$l->set($this);echo$l;}
+function show_404(){header("HTTP/1.0 404 Not Found");$this->content=new View('404');}
}
View
23 classes/controller/home.php
@@ -4,8 +4,25 @@ class controller_home Extends Controller
// Welcome page!
public function index()
{
- $this->content = new View('welcome');
- $this->render();
+ $this->content = new View('home/index');
+ }
+
+ // Show how to pass parameters over the URI
+ public function param($value1 = NULL, $value2 = NULL)
+ {
+ $view = new View('home/param');
+
+ $view->value1 = $value1;
+ $view->value2 = $value2;
+ // OR
+ /*
+ $view->set(array(
+ 'value1' => $value1,
+ 'value2' => $value2
+ ));
+ */
+
+ $this->content = $view;
}
// Example exception handling
@@ -42,7 +59,5 @@ public function session()
// Save session
Cookie::set('session',$_SESSION);
-
- $this->render();
}
}
View
2 classes/view.php
@@ -1,5 +1,5 @@
<?php
-// Handles working with HTML ouput templates
+// Handles working with HTML output templates
class View
{
function __construct($v){$this->v=p("views/$v");}
View
7 cli.php
@@ -0,0 +1,7 @@
+<?php if(PHP_SAPI!=='cli')die();
+// Do your command-line stuff safely here
+define('T',microtime(TRUE));
+define('M',memory_get_usage());
+define('AJAX',0);
+require('functions.php');
+require('bootstrap.php');
View
2 config.sample.php
@@ -1,4 +1,4 @@
-<?php
+<?php defined('T') OR die();
// Global site configuration
$config = array(
View
2 functions.php
@@ -9,6 +9,6 @@ function dump($v){return'<pre>'.print_r($v,1).'</pre>';}
function post($k,$d='',$s=1){$v=v($_POST[$k],$d);return($s&&is_string($v))?$v:(!$s&&is_array($v)?$v:$d);}
function _log($m){file_put_contents('log/.'.date('Y-m-d'),time().' '.getenv('REMOTE_ADDR')." $m\n",FILE_APPEND);}
function h($s){return htmlspecialchars($s,ENT_QUOTES,'utf-8');}
-function redirect($u='',$m='location',$c=302){header($m=='refresh'?"Refresh:0;url=$u":"Location: $u",TRUE,$c);}
+function redirect($u='',$r=0,$c=302){header($r?"Refresh:0;url=$u":"Location: $u",TRUE,$c);}
function registry($k,$v=null){static$o;return(func_num_args()>1?$o[$k]=$v:(isset($o[$k])?$o[$k]:NULL));}
function utf8($s,$f='UTF-8'){return @iconv($f,$f,$s);}
View
7 index.php
@@ -8,6 +8,7 @@
set_error_handler(function($c,$e,$f=0,$l=0){$v=new View('error');$v->e=$e;$v->f=$f;$v->l=$l;echo$v;_log("$e [$f:$l]");});
function exception($e){$v=new View('exception');$v->e=$e;_log($e->getMessage().' '.$e->getFile());die($v);}
set_exception_handler('exception');
-register_shutdown_function(function(){if($e=error_get_last())exception(new ErrorException($e['message'],$e['type'],0,$e['file'],$e['line']));});
-$c='controller_'.(url(0)?:'home');$m=url(1)?:'index';if(!is_file(p("classes/$c"))||!($c=new$c)||!method_exists($c,$m)){$c=new controller;$m='show_404';}
-call_user_func_array(array($c,$m),array_slice(url(),2));
+register_shutdown_function(function(){if($e=error_get_last())exception(new ErrorException($e['message'],$e['type'],0,v($e['file']),$e['line']));});
+$c='controller_'.(url(0)?:'home');$m=url(1)?:'index';if(!is_file(p("classes/$c"))||!($c=new$c)||$m=='render'||!method_exists($c,$m)){$c=new controller;$m='show_404';}
+call_user_func_array(array($c,$m),array_slice(url(),2));
+$c->render();
View
5 views/404.php
@@ -1,7 +1,4 @@
-<?php
-function_exists('p') OR die('Forbidden');
-headers_sent() OR header('HTTP/1.0 404 Page Not Found');
-?>
+<?php defined('T') OR die(); headers_sent() OR header('HTTP/1.0 404 Page Not Found'); ?>
<h1>404 Not Found</h1>
<p>Sorry, we could not find the page you requested.</p>
<code><?php print implode('/', url());?></code>
View
1 views/debug.php
@@ -1,3 +1,4 @@
+<?php defined('T') OR die(); ?>
<div style="margin: 5em 0;padding:2em;background:#ECF5FA;color:#000;clear:both;">
<b>Benchmarks</b>
View
5 views/error.php
@@ -1,7 +1,4 @@
-<?php
-function_exists('p') OR die('Forbidden');
-headers_sent() OR header('HTTP/1.0 500 Internal Server Error');
-?>
+<?php defined('T') OR die(); headers_sent() OR header('HTTP/1.0 500 Internal Server Error'); ?>
<h1>System Error</h1>
<p><?php print $e; ?></p>
<p>In <?php print str_replace(dirname(__DIR__),'',$f); ?> on line <?php print $l; ?></p>
View
2 views/exception.php
@@ -1,4 +1,4 @@
-<?php function_exists('p') OR die(); headers_sent() OR header('HTTP/1.0 500 Internal Server Error'); ?>
+<?php defined('T') OR die(); headers_sent() OR header('HTTP/1.0 500 Internal Server Error'); ?>
<!doctype html>
<head>
<meta charset="utf-8">
View
3 views/home/index.php
@@ -0,0 +1,3 @@
+<?php defined('T') OR die(); ?>
+<h3>Welcome!</h3>
+<p>Thanks for trying out the 1kb MVC Framework!</p>
View
7 views/home/param.php
@@ -0,0 +1,7 @@
+<?php defined('T') OR die(); ?>
+<h3>URI Parameter Test</h3>
+<?php
+var_dump(h($value1));
+var_dump(h($value2));
+?>
+<a href="/home/param/343/desc">/home/param/343/desc</a>
View
2 views/layout.php
@@ -1,4 +1,4 @@
-<!DOCTYPE html><?php function_exists('p') OR die('Forbidden');?>
+<!DOCTYPE html><?php defined('T') OR die(); ?>
<html>
<head>
<meta charset="utf-8">
View
3 views/welcome.php
@@ -1,3 +0,0 @@
-<?php function_exists('p') OR die('Forbidden');?>
-<h3>Welcome!</h3>
-<p>Thanks for trying out the 1kb MVC Framework!</p>

0 comments on commit cebcb34

Please sign in to comment.