/
fango.php
1007 lines (860 loc) · 21.9 KB
/
fango.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Andrea Giardina <andrea.giardina@gmail.com>
* @link http://github.com/agiardina/Fango
*/
/**
* The main class
*/
class FangoBase {
/**
* The array of events, used by FangoEvent::lazyLoading to load
* an event on demand
*/
public $events = array();
function subscribe($event,$handler=null) {
$event->addObserver($this,$handler);
return $this;
}
function unsubscribe($event) {
$event->deleteObserver($this);
return $this;
}
/**
* Used to lazy loading events
*/
function __get($name) {
FangoEvent::lazyLoading($this,$name);
return $this->$name;
}
}
/**
* The main controller
*/
class Fango extends FangoBase {
/**
* @var string the default controller if no controller specified
*/
public $default_controller = 'default';
/**
* @var the default action if action not specified
*/
public $default_action = 'index';
/**
* @var the default action if action not found
*/
public $notfound_action = 'error404';
/**
* @var the controller after the routing
*/
public $controller;
/**
* @var the action after the routing
*/
public $action;
/**
* @var array the params
*/
public $params = array();
/**
* @var FangoDB
*/
public $db;
/**
* @var FangoEvent
*/
static $onNew;
/**
* @var FangoEvent
*/
public $beforeDispatch;
/**
* @var FangoEvent
*/
public $afterDispatch;
function __construct() {
$this->beforeDispatch = new FangoEvent('beforeDispatch');
$this->afterDispatch = new FangoEvent('afterDispatch');
self::$onNew->fire($this);
}
/**
* @param array $custom_rules
* @param $subject
* @return Fango
*/
function route($custom_rules = array(),$subject = '') {
//If subject has not been declared we guess that from $_SERVER
if (!$subject) {
$script = $_SERVER['PHP_SELF'];
$script = preg_replace("~\w*\.php$~",'',$script);
if (isset($_SERVER['REQUEST_URI'])) {
$subject = $_SERVER['REQUEST_URI'];
$subject = preg_replace('~\?.*$~','',$subject);
if (strpos($subject,$script)===0) {
$subject = substr($subject,strlen($script));
}
}
}
//Rules merged
$rules[] = '(\w+)/(\w+)/(.*)$ controller=$1,action=$2,params=$3';
$rules[] = '(\w+)/(\w+)/?$ controller=$1,action=$2';
$rules[] = '(\w+)\.html$ action=$1';
$rules[] = '(\w+)/?$ controller=$1';
if ($custom_rules) {
if (!is_array($custom_rules)) $custom_rules = array($custom_rules);
$rules = array_merge($custom_rules,$rules);
}
$controller = $this->default_controller;
$action = $this->default_action;
$params = array();
foreach ($rules as $rule) {
list($rule,$replacement) = @preg_split("/[\t\s]/",$rule,-1,PREG_SPLIT_NO_EMPTY); //Split the rule from the replace
if (preg_match("~$rule~",$subject)) {
$replacement = preg_replace("~$rule~",$replacement,$subject);
$a_conf = explode(',',$replacement);
foreach ($a_conf as $conf) {
list($var,$value) = explode('=',$conf);
if ($var == 'controller' && $value) {
$controller = $value;
} elseif ($var == 'action' && $action) {
$action = $value;
} elseif ($var == 'params') {
$_params = explode('/',$value);
foreach ($_params as $i=>$param) {
if ($i % 2 == 0) {
$param_name = $param;
} else {
$params[$param_name] = $param;
}
}
} else {
$params[$var] = $value;
}
}
break;
}
}
$this->controller = $controller;
$this->action = $action;
$this->params = $params;
if (isset($_REQUEST)) {
$this->params = array_merge($_REQUEST,$this->params);
}
return $this;
}
/**
* @param string $controller
* @param string $action
* @param array $params
*/
function dispatch($controller=null,$action=null,array $params=null) {
if ($controller)$this->controller = $controller;
if ($action) $this->action = $action;
if ($params!==null) $this->params = $params;
$e = $this->beforeDispatch->fire($this); //BeforeDispatch Event Fired
if ($e->preventDefault()) return;
$class_name = "{$this->controller}Controller";
$method_name = "{$this->action}Action";
//If the method doesn't exist use the default controller and method
if (!class_exists($class_name) || !method_exists($class_name, $method_name)) {
$this->controller = $this->default_controller;
$this->action = $this->notfound_action;
$class_name = "{$this->controller}Controller";
$method_name = "{$this->action}Action";
if (!class_exists($class_name)) {
$this->controller = 'Fango';
$class_name = 'FangoController';
}
}
$obj_controller = new $class_name($this);
$obj_controller->init();
$result = $obj_controller->$method_name();
$this->afterDispatch->fire($this,$result); //AfterDispatch Event Fired
}
/**
* Shortcut for fango->route()->dispatch();
* @param array $custom_rules
* @param string $subject
*/
function run($custom_rules = array(),$subject = '') {
return $this->route($custom_rules,$subject)->dispatch();
}
/**
* Return the params array (URL Params + $_REQUEST)
* @param array $params
* @return array
*/
function params(array $params=null) {
if ($params !== null) {
$this->params = $params;
}
return $this->params;
}
/**
* Setter/Getter for param
* @param string $param
* @param mixed $value
* @return mixed
*/
function param($param,$value=null) {
if ($value!==null) {
$this->params[$param] = $value;
}
if (isset($this->params[$param])) {
return $this->params[$param];
}
return null;
}
/** Setter/Getter for request, use param instead
* @param string $name of the request param
* @param mixed value
* @see param
* @deprecated
*/
function request($name,$value=null) {
return $this->param($name,$value);
}
}
class FangoController extends FangoBase {
/**
* @var Fango The fango front controller
*/
public $fango;
/**
* @var FangoEvent
*/
static $onNew;
/**
* @param Fango $fango
*/
function __construct(Fango $fango) {
$this->fango = $fango;
self::$onNew->fire($this);
}
/**
* Extend this method to run a code every time a controller is created
*/
function init() {}
/**
* shortcut for Fango::params()
*
* @see Fango::params()
* @param array $params
* @return array
*/
function params(array $params=null) {
return $this->fango->params($params);
}
/**
* shortcut for Fango::param()
*
* @see Fango::param()
* @param string $param
* @param mixed $value
* @return mixed
*/
function param($param,$value=null) {
return $this->fango->param($param,$value);
}
function error404Action() {
header("HTTP/1.0 404 Not Found");
echo "<h1>Page Not Found</h1>";
}
}
/**
* @method string getName() getName() return the name of the view
* @method string getTemplate() getTemplate() return the template path
* @method FangoView name() $name set the name of the view and return the view
* @method FangoView template() template($template) set the template path and return the view
*/
class FangoView extends FangoBase {
/**
* The name of the view, used by render as imput/select etc
* @var string
*/
protected $_name;
/**
* @var string
*/
protected $_template;
/**
* @var FangoEvent
*/
static $onNew;
/**
* @param string $name of the view
* @param string $template to render
*/
function __construct($name = null,$template=null) {
if ($name) $this->_name = $name;
if (!$template && $name) {
$this->_template = "templates/$name.phtml";
} elseif ($template) {
$this->_template = $template;
}
self::$onNew->fire($this);
}
/**
* Render the default view or the view passed as param
* @param string $template
* @return string
*/
function render($template=null) {
ob_start();
extract((array)$this);
if ($template) $this->_template = $template;
include $this->_template;
return ob_get_clean();
}
/**
* Call the render method
* @return string
*/
function __toString() {
return $this->render();
}
}
class FangoDB extends PDO {
/**
* @var FangoEvent
*/
static $onNew;
/**
* @var FangoDB
*/
static $db;
/**
* @see PDO::__construct
*/
static function connect($dsn, $username=null, $password=null,$driver_options=array()) {
self::$db = new FangoDB($dsn,$username,$password,$driver_options);
}
/**
* @see PDO::__construct
*/
function __construct($dsn,$username=null,$password=null,$driver_options=array()) {
parent::__construct($dsn,$username,$password,$driver_options);
self::$onNew->fire($this);
}
/**
* Instance a table model an inject it with the database
* @param string $table name
* @param string $pk name
* @param string $class the model class instance
* @return FangoModel
*/
function model($table,$pk = null) {
return new FangoModel($table,$pk,$this);
}
/**
* @param string $sql
* @param array $params
* @return PDOStatement
*/
function execute($sql,$params = null) {
$sth = $this->prepare($sql);
$sth->execute($params);
return $sth;
}
/**
* @param string $sql
* @param array $params
* @return array
*/
function getAll($sql,$params = null) {
$sth = $this->execute($sql,$params);
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
/**
* @param string $sql
* @param array $params
* @return array
*/
function getRow($sql,$params = null) {
$sth = $this->execute($sql,$params);
return $sth->fetch(PDO::FETCH_ASSOC);
}
/**
* @param string $sql
* @param array $params
* @return array
*/
function getCol($sql,$params = null) {
$sth = $this->execute($sql,$params);
return $sth->fetchAll(PDO::FETCH_COLUMN);
}
/**
* @param string $sql
* @param array $params
* @return mixed
*/
function getOne($sql,$params = null) {
$sth = $this->execute($sql,$params);
$res = $sth->fetch(PDO::FETCH_NUM);
if (is_array($res)) return array_shift($res);
}
}
class FangoModel extends FangoBase {
/**
*
* @var FangoDB
*/
public $db;
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $pk;
/**
* @var array
*/
protected $fields = array();
/**
* @var array
*/
protected $where = array();
/**
* @var array
*/
protected $order = array();
/**
* @var array
*/
protected $limit = array();
/**
* @var FangoEvent
*/
static $onNew;
/**
* @var array
*/
public $events = array('beforeUpdate','beforeInsert','afterUpdate','afterInsert');
/**
* @param string $name
* @param string $pk
* @param FangoDB $db
*/
function __construct($name,$pk=null,$db=null) {
$this->name = $name;
$this->pk = $pk;
$this->setDB($db);
self::$onNew->fire($this);
}
/**
* @param FangoDB $db
*/
function setDB($db=null) {
if (is_object($db)) {
$this->db = $db;
} elseif (is_object(FangoDB::$db)) {
$this->db = FangoDB::$db;
}
}
/**
* Set the fields to extract
* @param array $fields
* @return FangoModel
*/
function fields($fields) {
if (!is_array($fields)) $fields = func_get_args();
$this->fields = $fields;
return $this;
}
/**
* Add a where clause in the PDO format
* @param string $clause
* @param array $params
* @return FangoModel
*/
function where($clause,$params=null) {
if ($params !== null && !is_array($params)) {
$params = array($params);
}
$this->where['clause'][] = $clause;
if (isset($this->where['params'])) {
$this->where['params'] = array_merge($this->where['params'],$params);
} else {
$this->where['params'] = $params;
}
return $this;
}
/**
* Add an order clause
* @param string $order field
* @param string $direction (asc,desc)
* @return FangoModel
*/
function order($order,$direction=null) {
$this->order[] = array($order,$direction);
return $this;
}
/**
* Set limit and offset
* @param int $limit
* @param int $offset
* @return FangoModel
*/
function limit($limit,$offset = null) {
$this->limit = array($limit,$offset);
return $this;
}
/**
* @return array params to use with the where
*/
function params() {
if (isset($this->where['params'])) {
return $this->where['params'];
}
return array();
}
/**
* @return string the built select using fields, where, order and limit params
*/
function asSelect() {
$fields = '*';
$where = '';
$order = '';
$limit = '';
if ($this->fields) $fields = join(',',$this->fields);
if ($this->where) $where = 'WHERE ' . join (' AND ',$this->where['clause']);
if ($this->order) {
$order = "ORDER BY ";
foreach ($this->order as $ao) {
$order .= trim("{$ao[0]} {$ao[1]}") . ',';
}
$order = substr($order,0,-1);
}
if ($this->limit) {
$limit = "LIMIT " . $this->limit[0];
if ($this->limit[1]) {
$limit .= " OFFSET " . $this->limit[1];
}
}
$select = trim("SELECT $fields FROM {$this->name} $where $order $limit");
return $select;
}
/**
* @return array all rows
*/
function getAll() {
return $this->db->getAll($this, $this->params());
}
/**
* @return array the first row
*/
function getRow() {
return $this->db->getRow($this, $this->params());
}
/**
* @return array the first col
*/
function getCol() {
return $this->db->getCol($this, $this->params());
}
/**
* @return mixed first value of the first row
*/
function getOne() {
return $this->db->getOne($this, $this->params());
}
/**
* @return int the number of row of the built select
*/
function count() {
$sql = "SELECT count(*) FROM (".$this->asSelect().") AS A";
return $this->db->getOne($sql,$this->params());
}
/**
* Insert a row in the related table
* @param array $row
* @return PDOStatement
*/
function insert($row) {
$e = $this->beforeInsert->fire($this,$row); //BeforeInsert event fired
list($row) = $e->params;
if ($e->preventDefault()) return;
$keys = array_keys($row);
$fields = join(',',$keys);
$values = ':' . join(',:',$keys);
$sql = "INSERT INTO {$this->name} ($fields) VALUES($values)";
$stm = $this->db->execute($sql, $row);
$this->afterInsert->fire($this,$row); //AfterInsert event fired
return !(bool)$stm->errorCode();
}
/**
* Shortcut for PDO::lastInsertID
* @param string $seqname
* @return int
*/
function lastInsertID($seqname = null) {
return $this->db->lastInsertId($seqname);
}
/**
* Delete the row specified by the pk param in the related table
* @param mixed $pk
* @return DBOStatement
*/
function delete($pk) {
$e = $this->beforeDelete->fire($this,$pk);
list($pk) = $e->params;
if ($e->preventDefault()) return;
$this->requirePK();
list($where,$params) = $this->pkParts(null,$pk);
$sql = "DELETE FROM {$this->name} WHERE $where";
$stm = $this->db->execute($sql,$params);
$this->afterDelete->fire($this,$pk);
return !(bool)$stm->errorCode();
}
/**
* Update a row
* @param array $row
* @param mixed $pk
* @return PDOStatement
*/
function update($row,$pk=null) {
$e = $this->beforeUpdate->fire($this,$row,$pk); //BeforeUpdate event fired
list($row,$pk) = $e->params;
if ($e->preventDefault()) return;
$this->requirePK();
list($where,$pk_params) = $this->pkParts($row,$pk);
$sql = "UPDATE {$this->name} SET ";
foreach ($row as $field=>$value) {
$sql .= "{$field}=:{$field},";
}
$sql = substr($sql,0,-1) . " WHERE $where";
$params = array_merge($pk_params,$row);
$return = $this->db->execute($sql,$params);
$this->afterUpdate->fire($this,$row,$pk,$return); //AfterUpdate event fired
return $return;
}
/**
* The arg row is new?
* @param array $row to consider
* @param mixed $pk
* @return boolean
*/
function isNew($row,$pk=null) {
list($pk_where,$pk_values) = $this->pkParts($row,$pk);
$statement = "SELECT 1 FROM {$this->name} WHERE $pk_where";
return !$this->db->getOne($statement,$pk_values);
}
/**
* Reset fields, where, limit and order, useful to reuse the model to run a different query
* @param string $what
* @return FangoModel
*/
function reset($what = null) {
if (in_array($what,array('fields','where','limit','order'))) {
$this->$what = array();
} else {
$this->fields = array();
$this->where = array();
$this->limit = array();
$this->order = array();
}
return $this;
}
/**
* Return an array with pk where and params ready to be used to compose a query
* @param string $row
* @param mixed $pk_value
* @return array
*/
function pkParts($row,$pk_value = null) {
$this->requirePK();
$pk = $this->pk;
if (!is_array($pk)) $pk = array($pk);
if (!$pk_value) { //If no pk specified we read the pk from the row
$pk_value = array_intersect_key($row,array_flip($pk));
} elseif (!is_array($pk_value)) { //We need pk_value as key=>value
$pk_value = array($pk[0]=>$pk_value);
}
if (array_keys($pk_value) != $pk) throw new Exception('PK not valid');
$pk_params = array();
$pk_where = '';
foreach ($pk as $p) {
$pk_params["__PK__{$p}"] = $pk_value[$p]; //We had __PK__ to key to allow updating on pk (set id:id where :id=__PK__id)
$pk_where .= "{$p} = :__PK__{$p} AND ";
}
$pk_where = substr($pk_where,0,-5);
return array($pk_where,$pk_params );
}
/**
* throw an exception if pk is not defined
*/
protected function requirePK() {
if (!isset($this->db) | !isset($this->pk)) throw new Exception("DB or PK not defined");
}
/**
* return the model as select
* @return string
*/
function __toString() {
return $this->asSelect();
}
}
class FangoEvent extends FangoBase {
/**
* @var array the params passed to the obsers
*/
public $params;
/**
* The objects that has generated the event
* @var object
*/
public $target;
/**
* @var string event name
*/
public $name;
/**
* @var array list of observer for the events
*/
protected $observers = array();
/**
* @var boolean
*/
protected $prevent = false;
/**
* @param string $name the event name
*/
function __construct($name) {
$this->name = $name;
}
/**
* @param boolean $prevent
* @return boolean
*/
function preventDefault($prevent = null) {
if ($prevent !== null) $this->prevent = $prevent;
return $this->prevent;
}
/**
* @param FangoObserver $observer
*/
function addObserver($observer,$handler=null) {
if ($handler === null) $handler = $this->name;
if (!in_array($observer,$this->observers)) $this->observers[] = array($observer,$handler);
}
/**
* @param FangoObserver $observer
*/
function deleteObserver($observer) {
foreach ($this->observers as $key=>$stored_observer) {
if ($stored_obverser==$observer) {
unset($this->observers[$key]);
return true;
}
}
return false;
}
/**
* Fire the event
*/
function fire($target) {
$this->target = $target;
$this->params = func_get_args();
array_shift($this->params); //the param 0 is always the target
foreach ($this->observers as $a) {
list($observer,$method) = $a;
if (method_exists($observer,$method)) {
$observer->$method($this);
}
}
return $this;
}
/**
* @param Object $subject
*/
static function lazyLoading($subject,$event) {
if (isset($subject->events) && is_array($subject->events) && in_array($event,$subject->events)) {
$subject->$event = new FangoEvent($event);
return $subject->$event;
}
throw new Exception("Property $event doesn't exists");
}
}
//onNew events set to class level
Fango::$onNew = new FangoEvent('onNew');
FangoDB::$onNew = new FangoEvent('onNew');
FangoModel::$onNew = new FangoEvent('onNew');
FangoView::$onNew = new FangoEvent('onNew');
FangoController::$onNew = new FangoEvent('onNew');
//CREATE A BASE APPLICATION STRUCTURE
if (PHP_SAPI === 'cli') {
if (strtolower($argv[1]) === 'create') {
if (version_compare(PHP_VERSION, '5.3.0') < 0) {
throw new Exception("Version 5.3.0 minimum required");
}
if ($argc > 2) {
$dir = $argv[2];
} else {
$dir = dirname(PHP_SELF);
}
if (!is_dir($dir) || !is_writable($dir)) {
throw new Exception("The $dir is not writable");
}
$controllers_path = rtrim($dir,'/') . '/controllers';
$templates_path = rtrim($dir,'/') . '/templates';
$public_path = rtrim($dir,'/') . '/public';
if (is_dir($controllers_path) || is_dir($templates_path) || is_dir($public_path)) {
throw new Exception("Directory '$dir' is not empty");
} else{
mkdir($controllers_path);
mkdir($public_path);
mkdir($templates_path);
}
$index = <<<'TEXT'
<?php
ini_set('include_path','..:'. ini_get('include_path'));
require_once 'fango.php';
require_once 'controllers/default.php';
//FangoDB::connect('mysql:dbname=fango;host=127.0.0.1','root', 'password');
$fango = new Fango();
$fango->run();
TEXT;
$htaccess = <<<'TEXT'
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
TEXT;
$controller = <<<'TEXT'
<?php
class DefaultController extends FangoController{
function indexAction() {
$view = new FangoView('index');
$view->title = 'My First Page';
echo $view;
}
}
TEXT;
$view = <<<'TEXT'
<!doctype>
<html>
<h1><?=$title?></h1>
</html>
TEXT;