Unnamed Fossil Project

Check-in [151e6e55c6]
Login

Check-in [151e6e55c6]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added some demos. Some changes in autoloading and router.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 151e6e55c63e8dc2d0810803e6d44a2a7982a641
User & Date: Spravce 2012-10-07 13:30:31.750
Context
2012-10-07
18:23
Improved exception handling. Production mode now by default. check-in: bb6b91d687 user: Spravce tags: trunk
13:30
Added some demos. Some changes in autoloading and router. check-in: 151e6e55c6 user: Spravce tags: trunk
2012-10-06
22:09
Rewriten form scratch. check-in: 932dcf243f user: Spravce tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to brickyard.nw.
84
85
86
87
88
89
90

91







92
93
94
95
96
97
98
We will require it if it exists.

<<autoload>>==
if (file_exists($filename)){
	require $filename;
@


Elseway we will throw readable exception.








<<autoload>>==
} else {
	throw new Exception('Class ' . $className . ' not found! Tried to find it in ' . $filename . '.');
}
@








>
|
>
>
>
>
>
>
>







84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
We will require it if it exists.

<<autoload>>==
if (file_exists($filename)){
	require $filename;
@

If class not exists in file, we will inform developer.

<<autoload>>==
	if (!class_exists($className, false)){
		throw new Exception('Class ' . $className . ' expected to be in ' . $filename . '!');
	}
@

If file notexists, we will inform developer too.

<<autoload>>==
} else {
	throw new Exception('Class ' . $className . ' not found! Tried to find it in ' . $filename . '.');
}
@

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

Before showing any debug message we will erase output.

<<show debug message>>==
ob_clean();
@

Compose this message and showit to user.

<<show debug message>>==
$out="<html><head><title>error</title></head><body><h1>:-(</h1>";
$out.="<div>" . nl2br( $e->getMessage() ) . "</div>";
$out.="<pre>" . $e->getTraceAsString() . "</pre>";
$out.="</body></html>";
echo $out;







|







118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

Before showing any debug message we will erase output.

<<show debug message>>==
ob_clean();
@

Compose this message and show it to user.

<<show debug message>>==
$out="<html><head><title>error</title></head><body><h1>:-(</h1>";
$out.="<div>" . nl2br( $e->getMessage() ) . "</div>";
$out.="<pre>" . $e->getTraceAsString() . "</pre>";
$out.="</body></html>";
echo $out;
165
166
167
168
169
170
171



















172
173
174
175
176
177
178


179
180
181
182
183
184
185
public $path = '.';

public function __construct(){
	$this->router = new brickyard_router_default;
	$this->path = dirname(__FILE__);
}
@




















###Brickyard class definition

<<class brickyard>>==
class brickyard
{
	<<defaults>>


	
	public function autoload($className){
		<<autoload>>
	}
	
	<<error handling>>
	







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
public $path = '.';

public function __construct(){
	$this->router = new brickyard_router_default;
	$this->path = dirname(__FILE__);
}
@

##Setters and getters

Today we have setter and getter only for router.

<<getters>>==
public function getRouter()
{
	return $this->router;
}
@

<<setters>>==
public function setRouter($router)
{
	$this->router = $router;
}
@


###Brickyard class definition

<<class brickyard>>==
class brickyard
{
	<<defaults>>
	<<getters>>
	<<setters>>
	
	public function autoload($className){
		<<autoload>>
	}
	
	<<error handling>>
	
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	
	public function run(){
		<<run>>
	}
}
@

#Brickyard router

##Interface

<<brickyard router interface>>==
interface brickyard_router_interface
{
	<<router interface code>>
}
@







|

|







222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
	
	public function run(){
		<<run>>
	}
}
@

##Brickyard router

###Interface

<<brickyard router interface>>==
interface brickyard_router_interface
{
	<<router interface code>>
}
@
224
225
226
227
228
229
230






231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

253
254
255
256
257

258
259
260
261
262

263
264
265















266
267
268
269
270
271
272

<<router interface code>>==
public function getArgs();
@

Return an array of arguments. It can be empty if there are none arguments.







##Default implementation

This is default router which works out of the box.

Today unexplained.

<<brickyard default router>>==
class brickyard_router_default
{
	public $controller = "home";
	public $method = "index";
	public $args = array();
	
	function analyze()
	{
		$path=( isset($_SERVER["PATH_INFO"]) ? explode("/",$_SERVER["PATH_INFO"]) : array() );
		if (count($path)>1){$this->controller=$path[1];}
		if (count($path)>2){$this->method=$path[2];}
		if (count($path)>3){$this->args=array_slice($path,3);}
	}
	
	public function getController(){

		$this->analyze();
		return $this->controller;
	}
	
	public function getMethod(){

		$this->analyze();
		return $this->method;
	}
	
	public function getArgs(){

		$this->analyze();
		return $this->args;
	}















}
@



#Appendix








>
>
>
>
>
>
|






|








|
|



|
>




|
>




|
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

<<router interface code>>==
public function getArgs();
@

Return an array of arguments. It can be empty if there are none arguments.

<<router interface code>>==
public function getLink($controller = null, $method = null, $args=array() );
@

Creates an link.

###Default implementation

This is default router which works out of the box.

Today unexplained.

<<brickyard default router>>==
class brickyard_router_default implements brickyard_router_interface
{
	public $controller = "home";
	public $method = "index";
	public $args = array();
	
	function analyze()
	{
		$path=( isset($_SERVER["PATH_INFO"]) ? explode("/",$_SERVER["PATH_INFO"]) : array() );
		if (count($path)>1 and $path[1]!=''){$this->controller=$path[1];}
		if (count($path)>2  and $path[2]!=''){$this->method=$path[2];}
		if (count($path)>3){$this->args=array_slice($path,3);}
	}
	
	public function getController()
	{
		$this->analyze();
		return $this->controller;
	}
	
	public function getMethod()
	{
		$this->analyze();
		return $this->method;
	}
	
	public function getArgs()
	{
		$this->analyze();
		return $this->args;
	}
	
	public function getLink($controller = null, $method = null, $args=array() )
	{
		$url = $_SERVER["SCRIPT_NAME"];
		if ($controller){
			$url .= '/' . $controller;
			if ($method){
				$url .= '/' . $method;
				if (count($args)>0){
					$url .= '/' . implode('/', $args);
				}
			}
		}
		return $url;
	}
}
@



#Appendix

Changes to brickyard.php.
10
11
12
13
14
15
16










17
18
19
20
21
22
23



24
25
26
27
28
29
30
	public $path = '.';
	
	public function __construct(){
		$this->router = new brickyard_router_default;
		$this->path = dirname(__FILE__);
	}
	










	
	public function autoload($className){
		$filename=$this->path.DIRECTORY_SEPARATOR;
		$filename.=str_replace("_", DIRECTORY_SEPARATOR, $className);
		$filename.=".php";
		if (file_exists($filename)){
			require $filename;



		} else {
			throw new Exception('Class ' . $className . ' not found! Tried to find it in ' . $filename . '.');
		}
		
	}
	
	function error_handler($errno, $errstr, $errfile, $errline ) {







>
>
>
>
>
>
>
>
>
>







>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	public $path = '.';
	
	public function __construct(){
		$this->router = new brickyard_router_default;
		$this->path = dirname(__FILE__);
	}
	
	public function getRouter()
	{
		return $this->router;
	}
	
	public function setRouter($router)
	{
		$this->router = $router;
	}
	
	
	public function autoload($className){
		$filename=$this->path.DIRECTORY_SEPARATOR;
		$filename.=str_replace("_", DIRECTORY_SEPARATOR, $className);
		$filename.=".php";
		if (file_exists($filename)){
			require $filename;
			if (!class_exists($className, false)){
				throw new Exception('Class ' . $className . ' expected to be in ' . $filename . '!');
			}
		} else {
			throw new Exception('Class ' . $className . ' not found! Tried to find it in ' . $filename . '.');
		}
		
	}
	
	function error_handler($errno, $errstr, $errfile, $errline ) {
81
82
83
84
85
86
87


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120


121
122
123
124
125
126
127
128
129
130


131
132
133
134
135
136
137
138
139
140


141
142
143
144
145
146
147
148































	public function getController();

	public function getMethod();

	public function getArgs();



	

}

class brickyard_router_default

{

	public $controller = "home";

	public $method = "index";

	public $args = array();

	

	function analyze()

	{

		$path=( isset($_SERVER["PATH_INFO"]) ? explode("/",$_SERVER["PATH_INFO"]) : array() );

		if (count($path)>1){$this->controller=$path[1];}

		if (count($path)>2){$this->method=$path[2];}

		if (count($path)>3){$this->args=array_slice($path,3);}

	}

	

	public function getController(){



		$this->analyze();

		return $this->controller;

	}

	

	public function getMethod(){



		$this->analyze();

		return $this->method;

	}

	

	public function getArgs(){



		$this->analyze();

		return $this->args;

	}

}





































>
>




|

















|

|







|
>
>









|
>
>









|
>
>







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

	public function getController();

	public function getMethod();

	public function getArgs();

	public function getLink($controller = null, $method = null, $args=array() );

	

}

class brickyard_router_default implements brickyard_router_interface

{

	public $controller = "home";

	public $method = "index";

	public $args = array();

	

	function analyze()

	{

		$path=( isset($_SERVER["PATH_INFO"]) ? explode("/",$_SERVER["PATH_INFO"]) : array() );

		if (count($path)>1 and $path[1]!=''){$this->controller=$path[1];}

		if (count($path)>2  and $path[2]!=''){$this->method=$path[2];}

		if (count($path)>3){$this->args=array_slice($path,3);}

	}

	

	public function getController()

	{

		$this->analyze();

		return $this->controller;

	}

	

	public function getMethod()

	{

		$this->analyze();

		return $this->method;

	}

	

	public function getArgs()

	{

		$this->analyze();

		return $this->args;

	}

	

	public function getLink($controller = null, $method = null, $args=array() )

	{

		$url = $_SERVER["SCRIPT_NAME"];

		if ($controller){

			$url .= '/' . $controller;

			if ($method){

				$url .= '/' . $method;

				if (count($args)>0){

					$url .= '/' . implode('/', $args);

				}

			}

		}

		return $url;

	}

}
Added c/demos.php.












































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
class c_demos
{
	function _getLink($name, $method, $args=array())
	{
		$router = $this->framework->getRouter();
		return '<a href="' . $router->getLink('demos', $method, $args). '">' . $name . '</a>';
	}
	
	function index()
	{
		echo '<h1>Demos</h1>';
		echo '<ul>';
		$router = $this->framework->getRouter();
		echo '<li>' . $this->_getLink('exception handling', 'exceptions') . '</li>';
		echo '<li>' . $this->_getLink('source code brownser', 'source', array('c_demos', 'source')) . '</li>';
		echo '<li>' . $this->_getLink('this controller source', 'source', array('c_demos')) . '</li>';
		echo '</ul>';
	}
	
	function exceptions()
	{
		try {
			throw new Exception(
				'In develMode exception reports looks like this page.' . PHP_EOL .
				'If you like this report, you can get it by calling $this->framework->bluescreen($e); like in this page.'
			);
		} catch (Exception $e) {
			$this->framework->bluescreen($e);
		}
	}
	
	function source($controller="c_demos", $method=null)
	{
		if ($method){
			$code = new ReflectionMethod($controller, $method);
		} else {
			$code = new ReflectionClass($controller);
		}
		$sourceName = $code->getFileName();
		if ($sourceName){
			$file = file($sourceName);
			$start = $code->getStartLine();
			$stop = $code->getEndLine();
			echo '<pre>';
			for ($i = $start-1; $i<$stop; $i++){
				echo htmlspecialchars($file[$i]);
			}
			echo '</pre>';
		} else {
			throw new Exception("Class " . $controller . " is unreachable for source brownser.");
		}
	}
}