Selaa lähdekoodia

submodule, better error handling, router and response class enhancements

Nathaniel van Diepen 8 vuotta sitten
vanhempi
commit
f2d1b1909e
10 muutettua tiedostoa jossa 62 lisäystä ja 27 poistoa
  1. 3 0
      .gitmodules
  2. 19 22
      index.php
  3. 1 0
      js/juju
  4. 3 0
      js/user.js
  5. 1 1
      lib/response.class.php
  6. 6 0
      lib/router.class.php
  7. 6 2
      lib/user.class.php
  8. 2 2
      paths/user.php
  9. 15 0
      templates/error.php
  10. 6 0
      templates/user.php

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "js/juju"]
+	path = js/juju
+	url = https://github.com/Eeems/juju.git

+ 19 - 22
index.php

@@ -2,35 +2,32 @@
 	error_reporting(E_ALL);
 	ini_set('display_errors', 'Off');
 	set_error_handler(function($errno, $errstr, $errfile, $errline){
-		header('Content-Type: application/json');
-		die(
-			json_encode(
-				array(
-					'number'=> $errno,
-					'msg'=> $errstr,
-					'file'=> $errfile,
-					'line'=> $errline,
+		Router::write(
+			Bugs::template('error')
+				->run(new Arguments(array(
+					'error'=> array(
+						'type'=> $errno,
+						'message'=> $errstr,
+						'file'=> $errfile,
+						'line'=> $errline
+					),
 					'backtrace'=> debug_backtrace(),
 					'included'=> get_included_files()
-				)
-			)
+				)))
 		);
+		die();
 	},E_ALL);
 	register_shutdown_function(function(){
 		$error = error_get_last();
-		if ($error['type'] == 1) {
-			header('Content-Type: application/json');
-			die(
-				json_encode(
-					array(
-						'number'=> $error['type'],
-						'msg'=> $error['message'],
-						'file'=> $error['file'],
-						'line'=> $error['line'],
-						'backtrace'=> array(),
+		if(!is_null($error) && $error['type'] == 1){
+			Router::clear();
+			Router::write(
+				Bugs::template('error')
+					->run(new Arguments(array(
+						'error'=> $error,
+						'backtrace'=> debug_backtrace(),
 						'included'=> get_included_files()
-					)
-				)
+					)))
 			);
 		} 
 	});

+ 1 - 0
js/juju

@@ -0,0 +1 @@
+Subproject commit 51685e6c9f4c2429e6567bf2aab6df1dc8f6ef69

+ 3 - 0
js/user.js

@@ -0,0 +1,3 @@
+(function(){
+	
+});

+ 1 - 1
lib/response.class.php

@@ -16,7 +16,7 @@
 			if($this->open){
 				$this->body = '';
 			}
-			return this;
+			return $this;
 		}
 		public function write($chunk){
 			if($this->open){

+ 6 - 0
lib/router.class.php

@@ -80,6 +80,12 @@
 				}
 			}
 		}
+		public static function write($chunk){
+			if(!isset(static::$responses[0])){
+				array_push($responses,new Response($_SERVER['REQUEST_URI']));
+			}
+			static::$responses[0]->write($chunk);
+		}
 	}
 	register_shutdown_function(function(){
 		Router::shutdown();

+ 6 - 2
lib/user.class.php

@@ -17,8 +17,12 @@
 				FROM users
 				WHERE id = ?;
 			",'i',$this->id)->assoc_result;
-			foreach($cache as $key => $value){
-				$this->cache[$key] = $value;
+			if($cache){
+				foreach($cache as $key => $value){
+					$this->cache[$key] = $value;
+				}
+			}else{
+				trigger_error("User with id {$id} does not exist");
 			}
 		}
 		public function jsonSerialize(){

+ 2 - 2
paths/user.php

@@ -1,14 +1,14 @@
 <?php
 	Bugs::actions(
-		'test'
+		'view_profile'
 	);
 	Router::paths(array(
 		'/~{user}'=>function($res,$args){
-			Bugs::activity('test','test');
 			$res->write(
 				Bugs::template('user')
 					->run(Bugs::user($args->user))
 			);
+			Bugs::activity('view_profile',$args->user);
 		},
 		'/user/{user}'=>function($res,$args){
 			$res->header(

+ 15 - 0
templates/error.php

@@ -0,0 +1,15 @@
+<?php
+	global $context;
+?>
+<!doctype html>
+	<head>
+		<meta charset="utf8"/>
+		<title>Error</title>
+	</head>
+	<body>
+		<h2>
+			<?=$context->error['message']?>
+		</h2>
+		<pre><?=print_r($context->backtrace,true)?></pre>
+	</body>
+</html>

+ 6 - 0
templates/user.php

@@ -6,6 +6,12 @@
 	<head>
 		<meta charset="utf8"/>
 		<title>User - <?=$context->name;?></title>
+		<script src="js/juju/core.js"></script>
+		<script src="js/juju/page.js"></script>
+		<script src="js/juju/dom.js"></script>
+		<script src="js/juju/keyboard.js"></script>
+		<script src="js/juju/mouse.js"></script>
+		<script src="js/user.js"></script>
 	</head>
 	<body>
 		<form>