Unnamed Fossil Project

Check-in [bb6b91d687]
Login

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

Overview
Comment:Improved exception handling. Production mode now by default.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:bb6b91d687197c72371b4fae0e3fd7508df826bd
User & Date: Spravce 2012-10-07 18:23:36
Context
2012-11-17
12:44
Some minor changes. Added view. check-in: fc60da8312 user: Spravce tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added 403.html.

















>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
<html>
<head>
<title>Forbidden</title>
</head>
<body>
<h1>Forbidden</h1>
</body>
</html>

Added 404.html.

















>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
<html>
<head>
<title>Not found</title>
</head>
<body>
<h1>Not found</h1>
</body>
</html>

Changes to brickyard.html.

7
8
9
10
11
12
13


14
15
16
17
18
19
20
..
65
66
67
68
69
70
71

72










73
74
75
76
77
78
79
..
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...
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
...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
...
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
...
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
</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>


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


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









>
>







 







>
|
>
>
>
>
>
>
>
>
>
>







 







|







 







|







 







|
>
>
>
>
>
>
>
>
>
>




|











>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>

<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
>




|




|


|







 







<
|
<







 







|










|

|
>
>

>
>
>
>
>


|


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









>
>





>
>
|
|
>
|
>


>
>
>
>
>












|

|







 







>
>
>
>
>
>
>
>
|







|








|
|



|
>




|
>




|
>



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




>

>

<
>

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







 







>
>







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
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
..
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
...
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
...
221
222
223
224
225
226
227

228

229
230
231
232
233
234
235
...
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
363
364
365
366
367
368
369
370
371
372
373
374
...
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
...
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
</head>
<body>

<h1>Brickyard framework</h1>

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

<p>It aims to be simpler and smaller than others. Not suitable for enterprise.</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>


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


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


<p>If it's fail we issue an 404 (not found) exception.</p>


<pre id="run" title="run">
} catch(brickyard_exception_autoload $e) {
    throw new brickyard_exception_404($e-&gt;getMessage() );
}
</pre>


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

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


<pre id="run" title="run">
$call=array($controllerInstance, $methodName);
if (is_callable($call)){
    call_user_func_array($call,$args);
}else{
    throw new brickyard_exception_404('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;libPath . 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>If class not exists in file, we will inform developer.</p>


<pre id="autoload" title="autoload">
    if (!class_exists($className, false)){
        throw new brickyard_exception_autoload('Class ' . $className . ' expected to be in ' . $filename . '!');
    }
</pre>


<p>If file notexists, we will inform developer too.</p>


<pre id="autoload" title="autoload">
} else {
    throw new brickyard_exception_autoload('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){
    $this-&gt;bluescreen($e);
} else {
    &lt;&lt;detect type of exception&gt;&gt;
    &lt;&lt;show error message&gt;&gt;
}
</pre>


<p>Firstly we detect type of exception.</p>


<pre id="detect type of exception" title="detect type of exception">
if ($e instanceof brickyard_exception_404){
    $err = 404;
} elseif ($e instanceof brickyard_exception_403){
    $err = 403;
} else {

    $err = 'error';
}   
</pre>


<p>And then we show static html page with some innocent error message.</p>

<p>But also in this case we need to check if error page is present.</p>


<pre id="show error message" title="show error message">
if (file_exists($this-&gt;libPath . DIRECTORY_SEPARATOR . $err . '.html')){
    ob_clean();
    echo file_get_contents($this-&gt;libPath . DIRECTORY_SEPARATOR . $err . '.html');
}else{
    echo &quot;An error occured. Also error page is missing.&quot;;
}
</pre>


<h2>Bluescreen</h2>

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


<pre id="bluescreen" title="bluescreen">
ob_clean();
</pre>


<p>Compose this message and show it to user.</p>


<pre id="bluescreen" title="bluescreen">
$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">

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

</pre>


<h3>Init function</h3>

<p>We register autoload.</p>

................................................................................
</pre>


<p>And exception handling.</p>


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



<h3>Default values</h3>

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


<pre id="defaults" title="defaults">
public $develMode = false;
public $router = null;
public $libPath = '.';
public $indexPath = '.';
</pre>


<p>Some default instances are created in constructor.</p>


<pre id="defaults" title="defaults">
public function __construct(){
    $this-&gt;router = new brickyard_router_default;
    $this-&gt;libPath = dirname(__FILE__);
}
</pre>


<h2>Setters and getters</h2>

<p>Today we have setter and getter or router.</p>


<pre id="getters" title="getters">
public function getRouter()
{
    return $this-&gt;router;
}
</pre>



<pre id="setters" title="setters">
public function setRouter($router)
{
    $this-&gt;router = $router;
}
</pre>


<p>And for indexPath.</p>


<pre id="getters" title="getters">
public function getIndexPath()
{
    return $this-&gt;indexPath;
}
</pre>



<pre id="setters" title="setters">
public function setIndexPath($indexFilePath)
{
    $this-&gt;indexPath = dirname($indexFilePath);
}
</pre>



<h3>Brickyard class definition</h3>


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

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

    function error_handler($errno, $errstr, $errfile, $errline )
    {
        &lt;&lt;error handling&gt;&gt;
    }

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

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

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

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


<h2>Brickyard router</h2>

<h3>Interface</h3>


<pre id="brickyard router interface" title="brickyard router interface">
interface brickyard_router_interface
{
    &lt;&lt;router interface code&gt;&gt;
}
................................................................................
<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>


<pre id="router interface code" title="router interface code">
public function getLink($controller = null, $method = null, $args=array() );
</pre>


<p>Creates an link.</p>

<h3>Default implementation</h3>

<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 implements brickyard_router_interface
{
    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 and $path[1]!=''){$this-&gt;controller=$path[1];}
        if (count($path)&gt;2  and $path[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;
    }

    public function getLink($controller = null, $method = null, $args=array() )
    {
        $url = $_SERVER[&quot;SCRIPT_NAME&quot;];
        if ($controller){
            $url .= '/' . $controller;
            if ($method){
                $url .= '/' . $method;
                if (count($args)&gt;0){
                    $url .= '/' . implode('/', $args);
                }
            }
        }
        return $url;
    }
}
</pre>


<h1>Appendix</h1>

<h2>exceptions</h2>


<p>Brickyard provides some useful exceptions.</p>


<pre id="exceptions" title="exceptions">
class brickyard_exception_autoload extends Exception{}
</pre>


<p>Throwned when something goes wrong in autoloader. </p>


<pre id="exceptions" title="exceptions">
class brickyard_exception_404 extends Exception{}
</pre>


<p>Throwned when 404 (not found) page should be shown.</p>


<pre id="exceptions" title="exceptions">
class brickyard_exception_403 extends Exception{}
</pre>


<p>Throwned when 403 (forbidden) page should be shown.</p>

<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;exceptions&gt;&gt;

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

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


Changes to brickyard.nw.

1
2
3
4


5
6
7
8
9
10
11
..
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
..
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
...
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
...
206
207
208
209
210
211
212


213
214

215

216
217





218
219
220
221
222
223
224
...
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
# 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>>==
................................................................................
$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>>==
................................................................................
	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 . '.');
}
@

###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 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;
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>>==
................................................................................
<<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__);
}
@

##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
{
................................................................................
	<<getters>>
	<<setters>>
	
	public function autoload($className){
		<<autoload>>
	}
	


	<<error handling>>
	

	public function bluescreen($e){

		<<exception handling>>
	}





	
	public function init(){
		<<init>>
	}
	
	public function run(){
		<<run>>
................................................................................
			}
		}
		return $url;
	}
}
@





#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. 
@




>
>







 







>
|
>
>
>
>
>
>
>
>







 







|











|







 







|







|









>
>
>
|
>
>
>
>
>
>
>
>
>
>

<
>
|

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



|





|













<
|
<







 







|








|

|
>
|
>
>
>
>


|





|







 







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







 







>
>
|
|
>
|
>


>
>
>
>
>







 







>

>

<
>

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













>
>










1
2
3
4
5
6
7
8
9
10
11
12
13
..
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
..
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
...
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
...
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
...
382
383
384
385
386
387
388
389
390
391
392

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# Brickyard framework

Yet another php framework.

It aims to be simpler and smaller than others. Not suitable for enterprise.

##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>>==
................................................................................
$methodName = $this->router->getMethod();
$args = $this->router->getArgs();
@

Tries to autoload controller.

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

If it's fail we issue an 404 (not found) exception.

<<run>>==
} catch(brickyard_exception_autoload $e) {
	throw new brickyard_exception_404($e->getMessage() );
}
@

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 brickyard_exception_404('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->libPath . DIRECTORY_SEPARATOR;
$filename.=str_replace("_", DIRECTORY_SEPARATOR, $className);
$filename.=".php";
@

We will require it if it exists.

<<autoload>>==
................................................................................
	require $filename;
@

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

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

If file notexists, we will inform developer too.

<<autoload>>==
} else {
	throw new brickyard_exception_autoload('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){
	$this->bluescreen($e);
} else {
	<<detect type of exception>>
	<<show error message>>
}
@

Firstly we detect type of exception.

<<detect type of exception>>==
if ($e instanceof brickyard_exception_404){
	$err = 404;
} elseif ($e instanceof brickyard_exception_403){
	$err = 403;
} else {

	$err = 'error';
}	
@

And then we show static html page with some innocent error message.

But also in this case we need to check if error page is present.

<<show error message>>==
if (file_exists($this->libPath . DIRECTORY_SEPARATOR . $err . '.html')){
	ob_clean();
	echo file_get_contents($this->libPath . DIRECTORY_SEPARATOR . $err . '.html');
}else{
	echo "An error occured. Also error page is missing.";
}
@

##Bluescreen

Before showing any debug message we will erase output.

<<bluescreen>>==
ob_clean();
@

Compose this message and show it to user.

<<bluescreen>>==
$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>>==

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

@

###Init function

We register autoload.

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

And exception handling.

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


###Default values

For now we are in devel mode permanently.

<<defaults>>==
public $develMode = false;
public $router = null;
public $libPath = '.';
public $indexPath = '.';
@

Some default instances are created in constructor.

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

##Setters and getters

Today we have setter and getter or router.

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

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

And for indexPath.

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

<<setters>>==
public function setIndexPath($indexFilePath)
{
	$this->indexPath = dirname($indexFilePath);
}
@


###Brickyard class definition

<<class brickyard>>==
class brickyard
{
................................................................................
	<<getters>>
	<<setters>>
	
	public function autoload($className){
		<<autoload>>
	}
	
	function error_handler($errno, $errstr, $errfile, $errline )
	{
		<<error handling>>
	}
	
	public function exception_handler($e)
	{
		<<exception handling>>
	}
	
	public function bluescreen($e)
	{
		<<bluescreen>>
	}
	
	public function init(){
		<<init>>
	}
	
	public function run(){
		<<run>>
................................................................................
			}
		}
		return $url;
	}
}
@

#Appendix

##exceptions


Brickyard provides some useful exceptions.

<<exceptions>>==
class brickyard_exception_autoload extends Exception{}
@

Throwned when something goes wrong in autoloader. 

<<exceptions>>==
class brickyard_exception_404 extends Exception{}
@

Throwned when 404 (not found) page should be shown.

<<exceptions>>==
class brickyard_exception_403 extends Exception{}
@

Throwned when 403 (forbidden) page should be shown.

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

<<exceptions>>

<<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
<?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 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 ) {
	
		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();








|

|
<
>


|





>
>
>
>






>
>
>
>



|





|


|




|
|

|


>
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
|
|
|
|
|
|
|
<
<
<
<






|








>
|
>
>
>





|




>
>
>
>
>
>







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
<?php
//
// Brickyard framework by Severak
//
//I am not yet decided about license. But I prefer WTFPL.
class brickyard
{
	public $develMode = false;
	public $router = null;
	public $libPath = '.';

	public $indexPath = '.';
	public function __construct(){
		$this->router = new brickyard_router_default;
		$this->libPath = dirname(__FILE__);
	}
	
	public function getRouter()
	{
		return $this->router;
	}
	public function getIndexPath()
	{
		return $this->indexPath;
	}
	
	public function setRouter($router)
	{
		$this->router = $router;
	}
	public function setIndexPath($indexFilePath)
	{
		$this->indexPath = dirname($indexFilePath);
	}
	
	
	public function autoload($className){
		$filename=$this->libPath . DIRECTORY_SEPARATOR;
		$filename.=str_replace("_", DIRECTORY_SEPARATOR, $className);
		$filename.=".php";
		if (file_exists($filename)){
			require $filename;
			if (!class_exists($className, false)){
				throw new brickyard_exception_autoload('Class ' . $className . ' expected to be in ' . $filename . '!');
			}
		} else {
			throw new brickyard_exception_autoload('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 exception_handler($e)
	{
		if ($this->develMode){
			$this->bluescreen($e);
		} else {
			if ($e instanceof brickyard_exception_404){
				$err = 404;
			} elseif ($e instanceof brickyard_exception_403){
				$err = 403;
			} else {
				$err = 'error';
			}	
			
			if (file_exists($this->libPath . DIRECTORY_SEPARATOR . $err . '.html')){
				ob_clean();
				echo file_get_contents($this->libPath . DIRECTORY_SEPARATOR . $err . '.html');
			}else{
				echo "An error occured. Also error page is missing.";
			}
			
		}
		
	}
	
	public function bluescreen($e)

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




		
	}
	
	public function init(){
		spl_autoload_register(array($this,"autoload"));
		set_error_handler(array($this,"error_handler"));
		set_exception_handler(array($this,"exception_handler"));
		
	}
	
	public function run(){
		ob_start();
		$controllerName = "c_" . $this->router->getController();
		$methodName = $this->router->getMethod();
		$args = $this->router->getArgs();
		try {
			$controllerInstance = new $controllerName;
		} catch(brickyard_exception_autoload $e) {
			throw new brickyard_exception_404($e->getMessage() );
		}
		$controllerInstance->framework=$this;
		$call=array($controllerInstance, $methodName);
		if (is_callable($call)){
			call_user_func_array($call,$args);
		}else{
			throw new brickyard_exception_404('Method ' . $methodName . ' is invalid!');
		}
		
	}
}

class brickyard_exception_autoload extends Exception{}

class brickyard_exception_404 extends Exception{}

class brickyard_exception_403 extends Exception{}

interface brickyard_router_interface

{

	public function getController();

Changes to c/demos.php.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
		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()







<







7
8
9
10
11
12
13

14
15
16
17
18
19
20
		return '<a href="' . $router->getLink('demos', $method, $args). '">' . $name . '</a>';
	}
	
	function index()
	{
		echo '<h1>Demos</h1>';
		echo '<ul>';

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

Changes to 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";
	}
}



|
>

|
<
<
<
<
1
2
3
4
5
6
7




<?php
class c_home{
	function index(){
		echo "<h1>It's working!</h1>";
		echo 'see some <a href="' .$this->framework->getRouter()->getLink('demos') . '">demos</a>';
	}
}




Added error.html.



















>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>Error</h1>
<p>An error occured!</p>
</body>
</html>