Commits

xiaoai pu committed 2903db5

init

Comments (0)

Files changed (17)

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>joanna</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+	</buildSpec>
+	<natures>
+	</natures>
+</projectDescription>

app/controller/index.php

+<?php
+namespace app\controller;
+
+final class index extends \system\super\controller
+{
+	
+	public function init($params=null)
+	{
+		parent::init();
+		$this->view = new \app\view\events;
+	}
+	
+	public function run()
+	{
+		$this->view->title('index');
+
+		$this->view->header();
+		$this->view->article();
+		$this->view->footer();
+	}
+	
+	
+}
+<?php
+namespace app\view;
+
+final class CLI extends \system\super\view
+{
+	public function title($title)
+	{
+		$this->title = $title .' - joanna';
+	}
+	
+	public function link()
+	{
+		$this->link = '';
+	}
+	
+	public function header()
+	{
+		$this->header = 'hi,';	
+	}
+	
+	public function article()
+	{
+		$this->article = 'This script run as CLI';
+		foreach(get_included_files() as $file){
+			$this->article .= "\n$file";
+		}
+	}
+	
+	public function footer()
+	{
+		$this->footer = 'use '. round(microtime(true)-START_TIME, 6);
+	}
+}

app/view/CLI/layout.php

+<?php
+namespace app\view\CLI;
+?>
+       <?php echo $this->title; ?>       
+
+<?php echo $this->header; ?>
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+<?php echo $this->article; ?>
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+<?php echo $this->footer; ?>
+

app/view/events.php

+<?php
+namespace app\view;
+
+final class events extends \system\super\view
+{
+	public function title($title)
+	{
+		$this->title = $title;
+		if( $this->title!=='' ){
+			$this->title = htmlentities($this->title.' | ');
+		}
+		$this->title .= 'Events';
+	}
+	
+	public function link()
+	{
+		$this->link = '';
+	}
+	
+	public function header()
+	{
+		$this->header = 'hi,';
+	}
+	
+	public function article()
+	{
+		$this->article = 'This script run as fast-cgi';
+	}	
+	public function footer()
+	{
+		$this->footer = 'use '. round(microtime(true)-START_TIME, 6);
+	}
+}

app/view/events/layout.php

+<?php
+namespace app\view\events;
+?>
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8" />
+<title><?php echo $this->title; ?></title>
+</head>
+<body>
+<header>
+<?php echo $this->header; ?>
+
+</header>
+<article>
+<?php echo $this->article; ?>
+
+</article>
+<footer>
+<?php echo $this->footer; ?>
+
+</footer>
+</body>
+</html>
+<?php
+namespace {
+
+	define('PATH', '/joanna');
+	define('DIR', __DIR__);
+	define('APP', '\app');
+	
+	$CLASS = array();
+	$CLASS['router'] = 'system\\router\\dispatch';
+	$CLASS['cache']  = 'system\\cache\\file';
+	
+	function __autoload($classname)
+	{
+		$file = str_replace('\\', '/', $classname);
+		require $file . \configure::EXT;
+	}
+
+	
+	final class configure
+	{
+		const EXT = '.php';
+		
+		public static $router = array(
+				'' => 'index',
+				'__404__' => 'page404'
+		);
+		
+		public static $database = array(
+				'dsn' => 'mysql:localhost;dbname=events',
+				'username' => 'root',
+				'password' => 'chilema'
+		);
+		
+	}
+	
+}
+<?php
+isset($_SERVER['REQUEST_TIME_FLOAT']) && ($_SERVER['REQUEST_TIME_FLOAT']=microtime(true));
+define('START_TIME', $_SERVER['REQUEST_TIME_FLOAT']);
+
+require 'bootstrap.php';
+
+
+
+$router = new $CLASS['router'];
+$controllername = $router->controller();
+$router->location($controllername);
+
+$params = $router->params();
+// $router->filter($params);
+
+$controllerclass = APP .'\\controller\\'. $controllername;
+
+$controller = new $controllerclass();
+
+unset($controllerclass, $controllername);
+
+$controller->init($params);
+$controller->run();
+$controller->output();
+
+$gzip = new \system\module\gzip();
+if( $gzip->check() ){
+	$gzip->run($controller->output);
+}
+
+echo $controller->output;

system/module/debug.php

+<?php
+namespace system\module;
+
+final class debug extends \system\super\ 

system/module/gzip.php

+<?php
+namespace system\module;
+
+final class gzip extends \system\super\module
+{
+	public function check()
+	{
+		if( isset($_SERVER['HTTP_ACCEPT_ENCODING']) && 
+			strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')!==false ){
+			return true;
+		}
+		return false;
+	}
+	
+	public function run(&$output)
+	{
+		// $output .= var_export( strlen($output), true);
+		$output .= implode(PHP_EOL, get_included_files());
+		$output = gzencode($output, 9);
+		header('Content-Encoding: gzip');
+		// header('X-Content-Length: '. strlen($output));
+	}
+}

system/router/command.php

+<?php
+namespace system\router;
+
+
+final class command extends \system\super\router
+{
+	public function controller()
+	{
+		global $argv;
+		
+		$controller = '';
+		if( isset($argv[1]) ){
+			$controller = $argv[1];
+			if( isset($argv[2]) && $argv[2]!=='NULL' ){
+				$controller .= '\\'.$argv[2];
+			}
+		}
+		return $controller;
+	}
+	
+	public function params()
+	{
+		global $argv;
+		
+		return array_slice($argv, 3);
+	}
+	
+}

system/router/dispatch.php

+<?php
+namespace system\router;
+
+
+final class dispatch extends \system\super\router
+{
+	public function controller()
+	{
+		$path = $this->getPath();
+		return $path;
+	}
+
+	public function params()
+	{
+		return $_GET;
+	}
+	
+	
+	private function getPath()
+	{
+		$path = $_SERVER['REQUEST_URI'];
+		if(strpos($path, PATH) === 0 ){
+			$path = substr($path, strlen(PATH));
+		}
+		$path = trim($path, '/');
+		return $path;
+	}
+
+}

system/super/controller.php

+<?php
+namespace system\super;
+
+abstract class controller
+{
+	protected $view;
+	public $output;
+	
+	protected function init($params=null)
+	{
+		
+	}
+	
+	abstract function run();
+	
+	public function output()
+	{
+		if( ! ($this->view instanceof \system\super\view) ){
+			throw new Exception('The view class must extends \system\super\view');
+		}
+		$this->output = (string)$this->view;
+	}
+}

system/super/model.php

+<?php
+namespace system\super;
+
+abstract class model
+{
+	public function __call($name, $param)
+	{
+		if( defined('CACHE') && class_exists(CACHE) ){
+			$data = $GLOBALS['cache']->get($name);
+			if( $data !== null ){
+				return $data;
+			}
+		}
+		$this->$name($param);
+	}
+}

system/super/module.php

+<?php
+namespace system\super;
+
+abstract class module
+{
+	
+}

system/super/router.php

+<?php
+namespace system\super;
+
+abstract class router
+{
+	/**
+	 * return the controller class name
+	 * @return string
+	 */
+	abstract function controller();
+	
+	/**
+	 * return the controller params
+	 * @return array
+	 */
+	abstract function params();
+	
+	
+	public function location(&$controller)
+	{
+		if( array_key_exists($controller, \configure::$router) ){
+			$controller = \configure::$router[$controller];
+		}
+	}
+	
+}

system/super/view.php

+<?php
+namespace system\super;
+
+abstract class view
+{
+	protected $data = array();
+	protected $layout;
+		
+	public function __get($name)
+	{
+		if( array_key_exists($name, $this->data) ){
+			if( is_array($this->data[$name]) ){
+				$this->data[$name] = call_user_func($this->data[$name]);
+			}
+			return $this->data[$name];
+		}else{
+			exit('View: '. $name .' Not found');
+		}
+	}
+	
+	public function __set($name, $value)
+	{
+		$this->data[$name] = $value;
+	}
+	
+	public function __toString()
+	{
+		ob_start();
+		require DIR.'/'.str_replace('\\', '/', get_class($this)).'/layout.php';
+		$str = ob_get_contents();
+		ob_end_clean();
+		return $str;
+	}
+	
+	public function encode($html)
+	{
+		return htmlspecialchars($html);
+	}
+	
+	/**
+	abstract function title();
+	abstract function link();
+	abstract function header();
+	abstract function article();
+	abstract function footer();
+	*/
+}