Unnamed Fossil Project

Check-in [932dcf243f]
Login

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

Overview
Comment:Rewriten form scratch.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:932dcf243ff209de5becfd9f5a7bd06a0168279e
User & Date: Spravce 2012-10-06 22:09:23
Context
2012-10-07
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
18:40
Added rollfile to external dependencies. check-in: e7b2706104 user: Spravce tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added brickyard.html.





















































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Brickyard framework</title>
	<link rel="stylesheet" type="text/css" href="default.css" />
</head>
<body>

<h1>Brickyard framework</h1>

<p>Yet another php framework.</p>

<h2>Usage</h2>

<p>The usage is really simple. Minimal index.php file (an entry point and setup) look like this:</p>

<p>Firstly you need to include framework file and get one instance.</p>


<pre id="index.php" title="index.php">
&lt;?php
require &quot;brickyard.php&quot;;
$f=new brickyard();
</pre>


<p>Init function enables autoloading and exception handling.</p>


<pre id="index.php" title="index.php">
$f-&gt;init();
</pre>


<p>Finally run function will call router and start right controller and method. </p>


<pre id="index.php" title="index.php">
$f-&gt;run();
</pre>


<h2>How it works?</h2>

<p>Here follows brickyard core explained.</p>

<h3>Main function</h3>

<p>It starts output control. It's useful for error pages.</p>


<pre id="run" title="run">
ob_start();
</pre>


<p>Get from router current controller, method and it's arguments. Note that controller has _c prefix before it's class name.</p>


<pre id="run" title="run">
$controllerName = &quot;c_&quot; . $this-&gt;router-&gt;getController();
$methodName = $this-&gt;router-&gt;getMethod();
$args = $this-&gt;router-&gt;getArgs();
</pre>


<p>Tries to autoload controller.</p>


<pre id="run" title="run">
$controllerInstance = new $controllerName;
</pre>


<p>Hand over framework instance to controller.</p>

<pre id="run" title="run">
$controllerInstance-&gt;framework=$this;
</pre>


<p>And finally call controller's method or throw Exception.</p>


<pre id="run" title="run">
$call=array($controllerInstance, $methodName);
if (is_callable($call)){
    call_user_func_array($call,$args);
}else{
    throw new Exception('Method ' . $methodName . ' is invalid!');
}
</pre>


<h3>Autoloading</h3>

<p>Class name is translated to filename. By this rules:</p>

<ul>
    <li>underscores in class names are replaced by directory separators</li>
    <li>last segment will have .php file extension</li>
</ul>


<pre id="autoload" title="autoload">
$filename=$this-&gt;path.DIRECTORY_SEPARATOR;
$filename.=str_replace(&quot;_&quot;, DIRECTORY_SEPARATOR, $className);
$filename.=&quot;.php&quot;;
</pre>


<p>We will require it if it exists.</p>


<pre id="autoload" title="autoload">
if (file_exists($filename)){
    require $filename;
</pre>


<p>Elseway we will throw readable exception.</p>


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


<h3>Exception handling</h3>

<p>If framework is in develMode, we will show debug message.</p>


<pre id="exception handling" title="exception handling">
if ($this-&gt;develMode){
    &lt;&lt;show debug message&gt;&gt;
} else {
    echo 'Silent error!';
}
</pre>


<p>Before showing any debug message we will erase output.</p>


<pre id="show debug message" title="show debug message">
ob_clean();
</pre>


<p>Compose this message and showit to user.</p>


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


<h3>Error handling</h3>

<p>This is simple rethrowing to ErrorException.</p>


<pre id="error handling" title="error handling">
function error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
</pre>


<h3>Init function</h3>

<p>We register autoload.</p>


<pre id="init" title="init">
spl_autoload_register(array($this,&quot;autoload&quot;));
</pre>


<p>Error handling.</p>

<pre id="init" title="init">
set_error_handler(array($this,&quot;error_handler&quot;));
</pre>


<p>And exception handling.</p>


<pre id="init" title="init">
set_exception_handler(array($this,&quot;bluescreen&quot;));
</pre>



<h3>Default values</h3>

<p>For now we are in devel mode permanently.</p>


<pre id="defaults" title="defaults">
public $develMode = true;
public $router = null;
public $path = '.';

public function __construct(){
    $this-&gt;router = new brickyard_router_default;
    $this-&gt;path = dirname(__FILE__);
}
</pre>


<h3>Brickyard class definition</h3>


<pre id="class brickyard" title="class brickyard">
class brickyard
{
    &lt;&lt;defaults&gt;&gt;

    public function autoload($className){
        &lt;&lt;autoload&gt;&gt;
    }

    &lt;&lt;error handling&gt;&gt;

    public function bluescreen($e){
        &lt;&lt;exception handling&gt;&gt;
    }

    public function init(){
        &lt;&lt;init&gt;&gt;
    }

    public function run(){
        &lt;&lt;run&gt;&gt;
    }
}
</pre>


<h1>Brickyard router</h1>

<h2>Interface</h2>


<pre id="brickyard router interface" title="brickyard router interface">
interface brickyard_router_interface
{
    &lt;&lt;router interface code&gt;&gt;
}
</pre>


<p>We have an interface for router. Later there will be setRouter function which will enable using of more complicated routers than default one.</p>


<pre id="router interface code" title="router interface code">
public function getController();
</pre>


<p>Gets controller name or null if not known.</p>


<pre id="router interface code" title="router interface code">
public function getMethod();
</pre>


<p>Gets method name or null if not known.</p>


<pre id="router interface code" title="router interface code">
public function getArgs();
</pre>


<p>Return an array of arguments. It can be empty if there are none arguments.</p>

<h2>Default implementation</h2>

<p>This is default router which works out of the box.</p>

<p>Today unexplained.</p>


<pre id="brickyard default router" title="brickyard default router">
class brickyard_router_default
{
    public $controller = &quot;home&quot;;
    public $method = &quot;index&quot;;
    public $args = array();

    function analyze()
    {
        $path=( isset($_SERVER[&quot;PATH_INFO&quot;]) ? explode(&quot;/&quot;,$_SERVER[&quot;PATH_INFO&quot;]) : array() );
        if (count($path)&gt;1){$this-&gt;controller=$path[1];}
        if (count($path)&gt;2){$this-&gt;method=$path[2];}
        if (count($path)&gt;3){$this-&gt;args=array_slice($path,3);}
    }

    public function getController(){
        $this-&gt;analyze();
        return $this-&gt;controller;
    }

    public function getMethod(){
        $this-&gt;analyze();
        return $this-&gt;method;
    }

    public function getArgs(){
        $this-&gt;analyze();
        return $this-&gt;args;
    }
}
</pre>




<h1>Appendix</h1>

<h2>brickyard.php file layout</h2>

<p>I will put it everything to the one file. It's very practical.</p>

<p>Layout of this file follows here.</p>


<pre id="brickyard.php" title="brickyard.php">
&lt;?php
//
// Brickyard framework by Severak
//
&lt;&lt;license&gt;&gt;
&lt;&lt;class brickyard&gt;&gt;

&lt;&lt;brickyard router interface&gt;&gt;

&lt;&lt;brickyard default router&gt;&gt;
</pre>


<h2>License</h2>

<pre id="license" title="license">
//I am not yet decided about license. But I prefer WTFPL. 
</pre>


</body></html>

Added brickyard.nw.















































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Brickyard framework

Yet another php framework.

##Usage

The usage is really simple. Minimal index.php file (an entry point and setup) look like this:

Firstly you need to include framework file and get one instance.

<<index.php>>==
<?php
require "brickyard.php";
$f=new brickyard();
@

Init function enables autoloading and exception handling.

<<index.php>>==
$f->init();
@

Finally run function will call router and start right controller and method. 

<<index.php>>==
$f->run();
@

##How it works?

Here follows brickyard core explained.

###Main function

It starts output control. It's useful for error pages.

<<run>>==
ob_start();
@

Get from router current controller, method and it's arguments. Note that controller has _c prefix before it's class name.

<<run>>==
$controllerName = "c_" . $this->router->getController();
$methodName = $this->router->getMethod();
$args = $this->router->getArgs();
@

Tries to autoload controller.

<<run>>==
$controllerInstance = new $controllerName;
@

Hand over framework instance to controller.
<<run>>==
$controllerInstance->framework=$this;
@

And finally call controller's method or throw Exception.

<<run>>==
$call=array($controllerInstance, $methodName);
if (is_callable($call)){
	call_user_func_array($call,$args);
}else{
	throw new Exception('Method ' . $methodName . ' is invalid!');
}
@

###Autoloading

Class name is translated to filename. By this rules:

 * underscores in class names are replaced by directory separators
 * last segment will have .php file extension

<<autoload>>==
$filename=$this->path.DIRECTORY_SEPARATOR;
$filename.=str_replace("_", DIRECTORY_SEPARATOR, $className);
$filename.=".php";
@

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 . '.');
}
@

###Exception handling

If framework is in develMode, we will show debug message.

<<exception handling>>==
if ($this->develMode){
	<<show debug message>>
} else {
	echo 'Silent error!';
}
@

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;
exit;
@

###Error handling

This is simple rethrowing to ErrorException.

<<error handling>>==
function error_handler($errno, $errstr, $errfile, $errline ) {
	throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
@

###Init function

We register autoload.

<<init>>==
spl_autoload_register(array($this,"autoload"));
@

Error handling.
<<init>>==
set_error_handler(array($this,"error_handler"));
@

And exception handling.

<<init>>==
set_exception_handler(array($this,"bluescreen"));
@


###Default values

For now we are in devel mode permanently.

<<defaults>>==
public $develMode = true;
public $router = null;
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>>
	
	public function bluescreen($e){
		<<exception handling>>
	}
	
	public function init(){
		<<init>>
	}
	
	public function run(){
		<<run>>
	}
}
@

#Brickyard router

##Interface

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

We have an interface for router. Later there will be setRouter function which will enable using of more complicated routers than default one.

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

Gets controller name or null if not known.

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

Gets method name or null if not known.

<<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

##brickyard.php file layout

I will put it everything to the one file. It's very practical.

Layout of this file follows here.

<<brickyard.php>>==
<?php
//
// Brickyard framework by Severak
//
<<license>>
<<class brickyard>>

<<brickyard router interface>>

<<brickyard default router>>
@

##License
<<license>>==
//I am not yet decided about license. But I prefer WTFPL. 
@

Changes to brickyard.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

55
56
57
58
59
60

61
62
63
64
65
66
67
68














69







70
71


72
73



74

75

76

77
78
79
80



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


<?php
class brickyard{
	public $errors=array();
	public $controller="home";
	public $method="index";
	public $debug=false;
	
	public function __construct(){
		$this->file_path=dirname(__FILE__);
		$this->router=new brickyard_router();
		$this->view=new brickyard_view();
		$this->view->framework=$this;
	}
	
	function init(){
		set_error_handler(array($this,"error_handler"));
		spl_autoload_register(array($this,"autoload"));
	}
	
	function autoload($name){
		$filename=$this->file_path.DIRECTORY_SEPARATOR;
		$filename.=str_replace("_",DIRECTORY_SEPARATOR,$name);
		$filename.=".php";
		include $filename;



	}
	
	function error_handler($errno, $errstr, $errfile, $errline ) {

		throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
	}
	



	function bluescreen($message){
		if ($this->debug){
			ob_clean();
			$out="<html><head><title>error</title></head><body><h1>:-(</h1>";
			$out.="<p>".$message."</p>";

			$out.="<pre>"; 
			echo $out;
			debug_print_backtrace();
			$out="</pre>";
			$out.="</body></html>";
			echo $out;
			exit;
		}


	}










	public function run(){
		ob_start();


		$route=$this->router->analyze();
		if ($route["controller"]!=""){ $this->controller=$route["controller"]; }
		if ($route["method"]!=""){ $this->method=$route["method"]; }
		$controller_path=$this->file_path.DIRECTORY_SEPARATOR."c".DIRECTORY_SEPARATOR.$this->controller.".php";
		if (file_exists($controller_path)){
			include $controller_path;
			$controller_instance=new $this->controller;

			$controller_instance->framework=$this;
			$call=array($controller_instance,$this->method);
			if (is_callable($call)){
				call_user_func_array($call,$route["args"]);
			}else{
				$this->bluescreen("Invalid method ".$this->method."!");

			}
		}else{
			$this->bluescreen("Invalid controller ".$this->controller."!");
		}
		//var_dump($this->errors);
	}
}















class brickyard_router{







	public $base_url="/";
	


	public function analyze(){
		$ret=array("controller"=>"","method"=>"","args"=>array());



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

		if (count($path)>1){$ret["controller"]=$path[1];}

		if (count($path)>2){$ret["method"]=$path[2];}

		$ret["args"]=array_slice($path,3);
		return $ret;
	}
	



	public function route($r){

		$url=$this->base_url;
		if (isset($r["controller"])){

			$url.=$r["controller"];
			if (isset($r["method"])){
				$url.="/".$r["method"];
				if (isset($r["args"])){
					$url.="/".join($r["args"],"/");
				}
			}
		}






		return $url;
	}
}

class brickyard_view{
	public $data=array();
	

	public function assign($key,$value=""){
		$this->data[$key]=$value;
	}

	
	public function display($view){
		extract($this->data);
		include "v/".$view.".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
55
56
57
58
59
60
61
62
63
64
65






66
67
68
69
70
71

72
73


74

75
76
77
78
79
80
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
<?php
//
// Brickyard framework by Severak
//
//I am not yet decided about license. But I prefer WTFPL.
class brickyard
{
	public $develMode = true;
	public $router = null;
	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 ) {
	
		throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
	
	}
	
	
	
	public function bluescreen($e){
		if ($this->develMode){
			ob_clean();
			$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;
			exit;
			
		} else {
			echo 'Silent error!';
		}
		
	}
	
	public function init(){
		spl_autoload_register(array($this,"autoload"));
		set_error_handler(array($this,"error_handler"));
		set_exception_handler(array($this,"bluescreen"));
		
	}
	
	public function run(){
		ob_start();
		$controllerName = "c_" . $this->router->getController();
		$methodName = $this->router->getMethod();
		$args = $this->router->getArgs();






		$controllerInstance = new $controllerName;
		$controllerInstance->framework=$this;
		$call=array($controllerInstance, $methodName);
		if (is_callable($call)){
			call_user_func_array($call,$args);
		}else{

			throw new Exception('Method ' . $methodName . ' is invalid!');
		}


		

	}
}

interface brickyard_router_interface

{

	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;

	}

}

Added c/home.php.





















>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
<?php
class c_home{
	function index(){
		echo "Working!";
	}
	
	function demo(){
		echo "some demo will go here";
	}
}

Changes to c/wiki.php.

1
2
3
4
5
6
7
8
9
<?php
class wiki{


	function index(){
		$this->page("index");
	}
	
	function page($pg){

|







1
2
3
4
5
6
7
8
9
<?php
class c_wiki{


	function index(){
		$this->page("index");
	}
	
	function page($pg){

Changes to index.php.

1
2
3
4
5
6
7
8
9
<?php
require "brickyard.php";
$framework=new brickyard();
$framework->init();
$framework->debug=true;
$framework->controller="wiki";
$framework->router->base_url="/b/index.php/";
$framework->run();
var_dump($framework->errors);


|
|
<
<
<
|
<
1
2
3
4



5

<?php
require "brickyard.php";
$f=new brickyard();
$f->init();



$f->run();