Source

Fire / fire

Full commit
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
#!/usr/bin/php -q
<?php

/*
 * GLOBAL TODOs
 * ------------
 * FIXME - Fix the indentation in the MVC::controller() and MVC::model() methods
 * 
 */

error_reporting(E_ALL & E_STRICT);

/**
* Master class. Shows the help, contains all the configuration constants and all the static methods.
* 
* @package Fire
* @author Aziz Light
* @link http://bitbucket.org/azizlight/fire
* @link http://github.com/AzizLight/fire
* @copyright Copyright (c) 2010, Aziz Light
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class Fire
{
	/**
	 * The path to the CodeIgniter folder that will serve as model/template to create the new project.
	 */
	const codeigniter_path = '/Users/aziz/Sites/source/CodeIgniter_1.7.2';
	
	/**
	 * If the CodeIgniter folder referenced above is not the
	 * default one (the unchanged CodeIgniter folder you downloaded
	 * from the official site), then default_ci should be set to FALSE.
	 */
	const default_ci = TRUE;
	
	/**
	 * Set to FALSE if you want to set up the class manually and don't want to be asked
	 * questions when you create a new project.
	 */
	const verbose_mode = FALSE;
	
	/*
	|--NON-verbose mode configuration constants-------------------------------
	| Those constant will be used if verbose_mode is set to FALSE and will also
	| be used as default choice if verbose_mode is set to TRUE (default)
	|-------------------------------------------------------------------------
	*/
	/**
	 * If TRUE, the application folder will be moved out of the system folder.
	 */
	const move_application_folder = TRUE;
	
	/**
	 * If TRUE, an assets folder will be created at the application root (at the same
	 * level as the system folder and the index.php file).
	 */
	const create_assets_folder = TRUE;
	
	/**
	 * Name of the assets folder. NOTE - A number of subfolder will also be created, but
	 * this is configurable in the constructor (by initializing the $_assets_subfolders private property).
	 */
	const name_of_assets_folder = 'public';
	/*
	|-------------------------------------------------------------------------
	*/
	
	/**
	 * The name of the application folder. 
	 * Change that constant only if you changed the name of the application folder.
	 */
	const application_folder_name = 'application';
	
	/**
	 * The name of the system folder.
	 */
	const system_folder_name = 'system';
	
	/**
	 * Parent controller that will extend every other controller
	 */
	const parent_controller = 'Controller';
	
	/**
	 * The name of the constructor of the parent Controller.
	 * Can be __construct or Controller for instance.
	 */
	const parent_controller_constructor = 'Controller';
	
	/**
	 * Parent model that will extend every other model
	 */
	const parent_model = 'Model';
	
	/**
	 * The name of the constructor of the parent Model.
	 * Can be __construct or Model for instance.
	 */
	const parent_model_constructor = 'Model';
	
	/**
	 * Model suffix
	 */
	const model_suffix = '_model';
	
// ------------------------------------------------------------------------
	
	/**
	 * This static variable will contain all the controllers the user will
	 * create for each query.
	 *
	 * @access private
	 * @static
	 * @var array
	 */
	private static $_controllers = array();
	
	/**
	 * This static variable will contain all the models the user will
	 * create for each query.
	 *
	 * @access private
	 * @static
	 * @var array
	 */
	private static $_models = array();

	/**
	 * @access private
	 * @static
	 * @var array
	 */
	private static $_views = array();
	
	/**
	 * Array containing the list of subfolders that will
	 * be created in the assets folder
	 *
	 * @access private
	 * @static
	 * @var array
	 */
	protected static $_assets_subfolders = array(
		'cs',
		'js',
		'images',
	);
	
	/**
	 * The list of valid parameters that will tell fire
	 * what it needs to do
	 *
	 * @access private
	 * @static
	 * @var array
	 */
	private static $_valid_cli_params = array(
		'controller',
		'controller+',
		'+controller',
		'model',
		'view',
		'ci',
		'help',
	);
	
// ------------------------------------------------------------------------
	
	/**
	 * The Constructor!
	 *
	 * @access public
	 * @author Aziz Light
	 */
	public function __construct()
	{
		
	} // End of __construct
	
// ------------------------------------------------------------------------
	
	/**
	 * Validate a query. If duplicate arguments were passed to the command line
	 * the validation will fail!
	 *
	 * @todo - rewrite the doc for this method
	 * @access public
	 * @static
	 * @param array $args : Array of arguments. Typically, $argv;
	 * @return void|bool : Throws an Exception if validation fails. Otherwise, return TRUE.
	 * @author Aziz Light
	 */
	public static function init($args)
	{
		// First thing to do is to verify that the first parameter is a valid keyword!
		if (!in_array($args[1], self::$_valid_cli_params))
			throw new Exception($args[1] . ' is not a valid type! Type fire help for instructions on how to use this script!');
		
		$next_param = FALSE; // is the next keyword an action/method?
		$param = FALSE; // is the current keyword an action/method?
		$view_file = FALSE; // if the current keyword is an action and this variable is set to true, then a view will be created for that action.
		$last_type = '';
		$last_of_type = 0;

		$max = count($args);
		for ($i = 1; $i < $max; $i++)
		{
			// Check that there isn't two keywords one after the other.
			if (in_array($args[$i - 1], self::$_valid_cli_params) && in_array($args[$i], self::$_valid_cli_params))
			{
				throw new Exception('Duplicate keywords detected!');
			}
			
			if (in_array($args[$i], self::$_valid_cli_params))
			{
				if ($args[$i] == 'controller' || $args[$i] == 'controller+' || $args[$i] == 'model' || $args[$i] == 'view')
				{
					$last_type = $args[$i];
					$param = FALSE;
					$next_param = FALSE;

					if ($args[$i] != 'controller+')
					{
						$view_file = FALSE;
					}

					continue;
				}
				elseif ($args[$i] == 'help')
				{
					Fire::help();
				}
				elseif ($args[$i] == 'ci')
				{
					$fire = new Ci();
					$fire->create_project($args[$i + 1]);
					exit();
				}
			}
			else
			{
				if (substr($args[$i], -1) == ':')
				{
					$next_param = TRUE;
					$args[$i] = rtrim($args[$i], ':');
				}

				if (substr($args[$i], 0, 1) == '+')
				{
					if ($last_type == 'controller' || $last_type == 'controller+')
					{
						$last_type = '+controller';
						$view_file = TRUE;
					}
					$args[$i] = ltrim($args[$i], '+');
				}
			}
			
			if ($param)
			{
				if (substr($args[$i], -1) == '.')
				{
					$next_param = FALSE;
					$args[$i] = rtrim($args[$i], '.');
				}

				if ($last_type == 'controller' || $last_type == 'controller+')
				{
					self::$_controllers[$args[$last_of_type]][] = $args[$i];

					if ($view_file)
					{

						echo $args[$i] . "\n";
						self::$_views[$args[$last_of_type]][] = $args[$i];

						if ($next_param === FALSE)
						{
							$view_file = FALSE;
						}
					}
				}
				elseif ($last_type == 'model')
				{
					self::$_models[$args[$last_of_type]][] = $args[$i];
				}
				elseif ($last_type == 'view')
				{
					self::$_views[$args[$last_of_type]][] = $args[$i];
				}
			}
			elseif ($last_type == 'controller')
			{
				$last_of_type = $i;
				self::$_controllers[$args[$i]] = array();
			}
			elseif ($last_type == 'controller+' || $last_type == '+controller')
			{
				$last_of_type = $i;
				self::$_controllers[$args[$i]] = array();
				self::$_models[Inflector::singularize($args[$i])] = array();
				self::$_views[$args[$i]] = array();
				
				if ($last_type == '+controller')
				{
					$last_type = 'controller';
				}
			}
			elseif ($last_type == 'model')
			{
				$last_of_type = $i;
				self::$_models[$args[$i]] = array();
			}
			elseif ($last_type == 'view')
			{
				$last_of_type = $i;
				self::$_views[$args[$i]] = array();
			}

			if ($next_param === TRUE)
			{
				$param = TRUE;
			}
			else
				$param = FALSE;
		}
		
		self::_create();
		
		return TRUE;
	} // End of init
	
// ------------------------------------------------------------------------
	
	/**
	 * Create the controllers, the methods and the views.
	 * 
	 * @access private
	 * @static
	 * @return void
	 * @author Aziz Light
	 */
	private static function _create()
	{
		// Create the controllers
		foreach (self::$_controllers as $controller => $actions)
		{
			$fire = new MVC('controller', $controller);
			if (!empty($actions))
			{
				// Create the actions
				foreach ($actions as $action)
				{
					$fire->new_action($action);
				}
			}
			$fire->create();
			unset($fire);
		}

		// Create the models
		foreach (self::$_models as $model => $methods)
		{
			$fire = new MVC('model', $model);
			if (!empty($methods))
			{
				foreach ($methods as $method)
				{
					$fire->new_method($method);
				}
			}
			$fire->create();
			unset($fire);
		}

		// Create the views
		foreach (self::$_views as $views_folder => $views)
		{
			$fire = new MVC('view', $views_folder, $views);
			$fire->create();
			unset($fire);
		}

		return;
	} // End of create
	
// ------------------------------------------------------------------------
	
	/**
	 * Display some help and stop the execution
	 *
	 * @access public
	 * @static
	 * @return void
	 * @author Aziz Light
	 */
	public static function help()
	{
		// show some ... help!
		exit();
	} // End of help
	
// ------------------------------------------------------------------------
	
	/**
	 * Display a message. If it's an error message, also terminate
	 * the execution of the script.
	 * NOTE - This method is exactly the same in the Ci class and the MVC class; I might want to do something about that...
	 *
	 * @access protected
	 * @static
	 * @param string $type : The type of the message. Leave empty to send a regular message.
	 * @param string $message : The message.
	 * @return void
	 * @author Aziz Light
	 */
	protected static function _display_message($type = '', $message = '', $continue = FALSE)
	{
		if (empty($message))
		{
			$type = 'error';
			$message = 'You tried to display a message but didn\'t provide the message...';
		}
		
		$_message_type = array(
			'error', 'warning', 'notice',
		);
		
		$_label = '';
		if (in_array($type, $_message_type))
		{
			$_label = strtoupper($type) . ': ';
		}
		
		$_message  = "\n";
		$_message .= $_label . $message;
		$_message .= "\n";
		
		fwrite(STDOUT, $_message);
		unset($_label, $_message, $message);
		
		if ($type == 'error' && $continue === FALSE)
			exit();
		else
			return;
	} // End of _display_message
	
} // End of Fire class

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

/**
 * Create a base CodeIgniter project.
 *
 * @package Fire
 * @subpackage Ci
 * @author Aziz Light
 * @link http://bitbucket.org/azizlight/fire
 * @link http://github.com/AzizLight/fire
 * @copyright Copyright (c) 2010, Aziz Light
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
class Ci extends Fire
{
	/**
	 * Instance variable that exists for the sole purpose of passing the name
	 * of the created project to the message that the destructor will display
	 * if a project was successfully created.
	 *
	 * @access private
	 * @var string
	 */
	private $_name;
	
	/**
	 * The destructor method will not display a success message unless this variable is true;
	 *
	 * @access private
	 * @var string
	 */
	private $_success;
	
// ------------------------------------------------------------------------
	
	/**
	 * The Constructor!
	 *
	 * @access public
	 * @author Aziz Light
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->_success = FALSE;
	}
	
// ------------------------------------------------------------------------
	
	/**
	 * El Destructor!
	 *
	 * @access public
	 * @return void
	 * @author Aziz Light
	 */
	public function __destruct()
	{
		if ($this->_success === TRUE)
		{
			$_message = ucfirst($this->_name) . ' project successfully created!';
			self::_display_message('', $_message);
			unset($_message);
		}
	} // End of __destruct
	
// ------------------------------------------------------------------------
	
	/**
	 * Create a new project. Optionally, moves the application folder out of
	 * the system folder and creates the assets folder.
	 *
	 * @access public
	 * @param string $name : The name of the new project.
	 * @param string $destination : Optional. The destination of the new project. If not provided, will be set automatically to the current location.
	 * @return void
	 * @author Aziz Light
	 */
	public function create_project($name = '', $destination = '')
	{
		$name = trim($name);
		$destination = trim($destination);
		if (empty($destination))
		{
			$destination = getcwd() . '/';
		}
		else
		{
			if (substr($destination, - 1) != '/')
				$destination .= '/';
		}
		
		if (!empty($name) && is_dir($destination . $name))
		{
			$_notice = 'There is already a project named ' . $name . ' in this directory! Try Again.';
			self::_display_message('notice', $_notice);
			unset($_notice);
			$name = $this->_get_name($destination);
		}
		elseif (empty($name))
			$name = $this->_get_name($destination);
		$destination .= $name;
		
		/*
		 * TODO - Create the blocks that will let the user remove the user_guide and license.txt.
		 * TODO - Create the blocks that will let the user remove welcome controller + view. Fire also need to send a notice advising to change the default route.
		 */
		
		if ($this->_import_ci($destination))
		{
			// -Verbose-mode-----------------------------------------------------------
			if (self::verbose_mode)
			{
				// -Move-the-application folder--------------------------------------------
				// NOTE - If the application folder is not in the system folder, that would mean one of two things:
				//        - The application_folder_name constant is not the same as the application folder's name.
				//        - You have manually moved the application folder.
				//      - In both cases, the user won't be asked wether or not he wants to move the application
				//        folder out of the system folder.
				if (is_dir($destination . self::system_folder_name . '/' . self::application_folder_name))
				{
					$_default_answer = (self::move_application_folder) ? 'yes' : 'no';
					$_question  = "'Do you want to move the application folder outside of the system folder? [Default: ${_default_answer}]\n[yes/no] ";
					fwrite(STDOUT, $_question);
					
					$_answer = strtolower(trim(fgets(STDIN)));
					
					if ($_answer == 'yes' || $_answer == 'y' || (empty($_answer) && $_default_answer == 'yes'))
					{
						if (!$this->_move_application_folder($destination))
						{
							$_error = 'Unable to move the application folder...';
							self::_display_message('error', $_error, TRUE);
							unset($_error);
						}
					}
					elseif ($_answer != 'no' || $_answer != 'n')
					{
						$i = 0;
						do
						{
							fwrite(STDOUT, "I do not understand...\n$_question");
							$_answer = strtolower(trim(fgets(STDIN)));
							if ($_answer == 'yes' || $_answer == 'y' || (empty($_answer) && $_default_answer == 'yes'))
							{
								if (!$this->_move_application_folder($destination))
								{
									$_error = 'Unable to move the application folder...';
									self::_display_message('error', $_error, TRUE);
									unset($_error);
								}
								$i = 42;
							}
							elseif ($_answer == 'no' || $_answer == 'n')
							{
								$i = 42;
							}
							$i++;
						} while ($i < 3);
					
						if ($i < 42)
						{
							$_warning = 'No understandable answer was provided to the previous question...The choice will be made for you...';
							self::_display_message('warning', $_warning);
							unset($_warning);
						
							if (self::move_application_folder == 'yes')
							{
								if (!$this->_move_application_folder($destination))
								{
									$_error = 'Unable to move the application folder...';
									self::_display_message('error', $_error, TRUE);
									unset($_error);
								}
							}
						}
					}
				}
				unset($_answer, $_question, $_default_answer);
				// ------------------------------------------------------------------------
				
				// -Create-assets-folder---------------------------------------------------
				// NOTE - The assets folder will only be created if it's not already there.
				if (!is_dir($destination . self::name_of_assets_folder))
				{
					$_default_answer = (self::create_assets_folder) ? 'yes' : 'no';
					$_question  = "'Do you want to create an assets folder? [Default: ${_default_answer}\n[yes/no] ";
					fwrite(STDOUT, $_question);
					
					$_answer = strtolower(trim(fgets(STDIN)));
					if ($_answer == 'yes' || $_answer = 'y' || (empty($_answer) && $_default_answer == 'yes'))
					{
						if (!$this->_create_assets_folders($destination))
						{
							$_error = 'Unable to create the assets folder...';
							self::_display_message('error', $_error, TRUE);
							unset($_error);
						}
					}
					elseif ($_answer != 'no' || $_answer != 'n')
					{
						$i = 0;
						do
						{
							fwrite(STDOUT, "I do not understand...\n$_question");
							$_answer = strtolower(trim(fgets(STDIN)));
							if ($_answer == 'yes' || $_answer == 'y' || (empty($_answer) && $_default_answer == 'yes'))
							{
								if (!$this->_create_assets_folders($destination))
								{
									$_error = 'Unable to create the assets folder...';
									self::_display_message('error', $_error, TRUE);
									unset($_error);
								}
								$i = 42;
							}
							elseif ($_answer == 'no' || $_answer == 'n')
							{
								$i = 42;
							}
							$i++;
						} while ($i < 3);
						
						if ($i < 42)
						{
							$_warning = 'No understandable answer was provided to the previous question...The choice will be made for you...';
							self::_display_message('warning', $_warning);
							unset($_warning);
							
							if (self::create_assets_folder == 'yes')
							{
								if (!$this->_create_assets_folders($destination))
								{
									$_error = 'Unable to create the assets folder...';
									self::_display_message('error', $_error, TRUE);
									unset($_error);
								}
							}
						}
					}
					
					unset($_answer, $_question, $_default_answer);
				}
				// ------------------------------------------------------------------------
			} // End of Verbose mode
			else
			{
				// -Non-Verbose-Mode-------------------------------------------------------
				if (self::move_application_folder == 'yes')
				{
					if (is_dir($destination . '/' . self::system_folder_name . '/' . self::application_folder_name))
					{
						if (!$this->_move_application_folder($destination))
						{
							$_error = 'Unable to move the application folder...';
							self::_display_message('error', $_error, TRUE);
							unset($_error);
						}
					}
				}
				
				if (self::create_assets_folder == 'yes')
				{
					if (!is_dir($destination . '/' . self::name_of_assets_folder))
					{
						if (!$this->_create_assets_folders($destination))
						{
							$_error = 'Unable to create the assets folder...';
							self::_display_message('error', $_error, TRUE);
							unset($_error);
						}
					}
				}
			}
			
			$this->_name    = $name;
			$this->_success =  TRUE;
			return;
		}
		else
		{
			$_error = 'Unable to create the CodeIgniter project...';
			self::_display_message('error', $_error);
		}
	} // End of create_project
	
// ------------------------------------------------------------------------
	
	/**
	 * Prompts the user to enter a name for the new project he is
	 * trying to create.
	 * This method will only be called if the user tried to
	 * create a project and did not provide a name.
	 *
	 * @access public
	 * @param string $destination : The destination of the new project. Used to check if a project with the same name already exists.
	 * @return void|string : Returns the name or terminates the execution if no name was retrieved.
	 * @author Aziz Light
	 */
	private function _get_name($destination)
	{
		fwrite(STDOUT, 'Choose a name for your project: ');
		$name = trim(fgets(STDIN));
		
		$i = 0;
		$state = '';
		do
		{
			if (empty($name))
			{
				$_notice = 'You need to specify a name for your project! Try again.';
				self::_display_message('notice', $_notice);
				unset($_notice);
			}
			elseif (is_dir($destination . $name))
			{
				$_notice = 'There is already a project named ' . $name . ' in this directory! Try Again.';
				self::_display_message('notice', $_notice);
				unset($_notice);
				$name = '';
				$state = 'new';
			}
			else
			{
				$i = 42;
				break;
			}
			
			fwrite(STDOUT, 'Choose a ' . $state . ' name for your project: ');
			$name = trim(fgets(STDIN));
			$i++;
		} while ($i < 3);
		
		if ($i < 42)
		{
			$_error = 'You didn\'t specify a name for the new project you are trying to create!';
			self::_display_message('error', $_error);
		}
		return $name;
	} // End of _get_name
	
// ------------------------------------------------------------------------
	
	/**
	 * Copy the CodeIgniter folder to create a new project.
	 * It's a pain in the ass to transfer permissions when copying a file/folder
	 * in php so here is what will happen:
	 *     - All the files will have a permission of 644.
	 *     - All the folders will have a permission of 755.
	 * 
	 * I did not create this method, It's Sina Salek who left it as comment
	 * on php.net {@link http://www.php.net/manual/en/function.copy.php#91256}
	 * and then info ]t[ intalo [.] de modified
	 * it {@link http://www.php.net/manual/en/function.copy.php#93953}
	 * 
	 * @access private
	 * @param string $dest : The destination folder.
	 * @param string $source : The source file/folder.
	 * @param string $folderPermission : The permission that will be given to all folders.
	 * @param string $filePermission  : The permission that will be given to all files.
	 * @return bool
	 * @author Sina Salek
	 */
	private function _import_ci($dest = '', $source = self::codeigniter_path, $folderPermission = 0755, $filePermission = 0644)
	{
		$result = FALSE;
		
		if (is_file($source))
		{
			if (is_dir($dest))
			{
				if ($dest[strlen($dest) - 1] != '/')
				    $__dest = $dest . '/';
				$__dest .= basename($source);
			}
			else
			{
				$__dest = $dest;
			}
			
			$result = copy($source, $__dest);
			chmod($__dest, $filePermission);
		} 
		elseif (is_dir($source))
		{
			if (!is_dir($dest))
			{
				@mkdir($dest, $folderPermission);
				chmod($dest, $folderPermission);
			}
			
			if ($source[strlen($source) - 1] != '/')
				$source = $source . '/';
			if ($dest[strlen($dest) - 1] != '/')
				$dest = $dest . '/';
			
			$result = TRUE;
			$dirHandle = opendir($source);
			while ($file = readdir($dirHandle))
			{
				if ($file != '.' && $file != '..')
				{
					$result = $this->_import_ci($dest . $file, $source . $file, $folderPermission, $filePermission);
				}
			}
			closedir($dirHandle);
		}
		else
		{
			$result = FALSE;
		}
		
		return $result;
	} // End of _import_ci
	
// ------------------------------------------------------------------------
	
	/**
	 * Move the application folder up one level (to the same level as the system folder).
	 *
	 * @access private
	 * @param string $application_path : The path where the new project has been created.
	 * @return bool
	 * @author Aziz Light
	 */
	private function _move_application_folder($project_path)
	{
		if (substr($project_path, - 1) != '/')
			$project_path .= '/';
			
		if (!is_dir($project_path . self::system_folder_name . '/' . self::application_folder_name))
		{
			$_notice  = "The application folder wasn't found in the system folder.\n";
			$_notice .= "- Check that the application_folder_name constant is the same as the application folder's name.\n";
			$_notice .= "- If it's the same, that would probably mean that you manually moved the application folder. In that case, you should set the move_application_folder constant to FALSE.";
			self::_display_message($_notice);
			unset($_notice);
			
			return TRUE;
		}
		
		return (@rename($project_path . self::system_folder_name . '/' . self::application_folder_name, $project_path . self::application_folder_name));
	} // End of _move_application_folder
	
// ------------------------------------------------------------------------
	
	/**
	 * Create the assets folder and subfolders.
	 * If the method fails to create one of the folders,
	 * all the previously created folders will be deleted.
	 *
	 * @access private
	 * @param string $project_path : The path where the new project has been created.
	 * @return bool
	 * @author Aziz Light
	 */
	private function _create_assets_folders($project_path)
	{
		if (substr($project_path, - 1) != '/')
			$project_path .= '/';
		
		if (!@mkdir($project_path . self::name_of_assets_folder, 0755))
		{
			return FALSE;
		}
		
		foreach (self::$_assets_subfolders as $asset)
		{
			if (!@mkdir($project_path . self::name_of_assets_folder . '/' . $asset, 0755))
			{
				// if (!$this->_remove_assets_folder($project_path))
				if (!$this->_recursive_remove_directory($project_path))
				{
					$_error = 'Unable to remove the assets folder...';
					self::_display_message('error', $_error, TRUE);
					unset($_error);
				}
				return FALSE;
			}
		}
		
		return TRUE;
	} // End of _create_assets_folders
	
// ------------------------------------------------------------------------
	
	/**
	 * Remove a folder and all of its contents.
	 *
	 * @access private
	 * @param string $directory : The path to the folder to remove.
	 * @param bool $empty : If TRUE, the directory and subdirectories will be emptied but not removed.
	 * @return bool
	 * @author lixlpixel
	 * @link http://lixlpixel.org/recursive_function/php/recursive_directory_delete/
	 */
	private function _recursive_remove_directory($directory, $empty = FALSE)
	{
		if (substr($directory, -1) == '/')
		{
			$directory = substr($directory, 0, -1);
		}
		if (!file_exists($directory) || !is_dir($directory))
		{
			return FALSE;
		}
		elseif (is_readable($directory))
		{
			$handle = opendir($directory);
			while (FALSE !== ($item = readdir($handle)))
			{
				if ($item != '.' && $item != '..')
				{
					$path = $directory . '/' . $item;
					if (is_dir($path))
					{
						$this->_recursive_remove_directory($path);
					}
					else
					{
						unlink($path);
					}
				}
			}
			closedir($handle);
			if ($empty == FALSE)
			{
				if (!rmdir($directory))
				{
					return FALSE;
				}
			}
		}
		return TRUE;
	} // End of _recursive_remove_directory
	
} // End of Ci class

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

/**
 * A CodeIgniter CLI tool to easily create models, controllers and views.
 *
 * @package Fire
 * @subpackage MVC
 * @author Aziz Light
 * @link http://bitbucket.org/azizlight/fire
 * @link http://github.com/AzizLight/fire
 * @copyright Copyright (c) 2010, Aziz Light
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
class MVC extends Fire
{
	/**
	 * The type of element you want to create. ie: 'controller', 'model', 'view'.
	 * It can also be 'help'.
	 *
	 * @access private
	 * @var string
	 */
	private $_type;
	
	/**
	 * The name of the the element you want to create
	 * or the name of the help section you want to display.
	 *
	 * @access private
	 * @var string
	 */
	private $_name;
	
	/**
	 * This array will typically contain the views that will be
	 * created in the views folder. Later on, actions and methods
	 * might also be passed to this array.
	 *
	 * @access private
	 * @var array
	 */
	private $_extras;
	
	/**
	 * Array of actions/methods to be added to the controller or model.
	 *
	 * @access private
	 * @var array
	 */
	private $_actions;
	
	/**
	 * Array of mandatory params. This is variable is re-initialized everytime a new
	 * action is created and is destroyed everytime a new controller or model is created.
	 *
	 * @access private
	 * @var array
	 */
	private $_params;
	
	/**
	 * The destructor method will not display a success message unless this variable is true.
	 *
	 * @access private
	 * @var bool
	 */
	private $_success;
	
	/**
	 * This variable is only used with views. It's there so that the destructor
	 * doesn't say that a view folder was created even though the folder was
	 * there before the execution of the script.
	 * 
	 * @access private
	 * @var bool
	 */
	private $_new_views_folder;
	
	/**
	 * The location of the user. This variable will basically tell if the user is
	 * in the application folder or the project root.
	 *
	 * @todo - I'm not yet sure what type this static variable should be; set as string for the time being.
	 * @access private
	 * @static
	 * @var string
	 */
	private static $_location;
	
// ------------------------------------------------------------------------
	
	/**
	 * The Constructor!
	 * If the user is in the right directory,
	 * initialises the type and name instance variables if they are valid.
	 *
	 * @access public
	 * @param string $type : The type of file to create. ie: controller or model.
	 * @param string $name : The name of the file the user wants to create.
	 * @param array $extras : This is typically an array of views. The option to pass actions/methods to the constructor via this parameter may be available later on.
	 * @return void
	 * @author Aziz Light
	 */
	public function __construct($type, $name = '', $extras = array())
	{
		parent::__construct();
		
		$this->_success = FALSE;
		$this->_new_views_folder = FALSE;
		
		if ($type == 'help' || ($this->_check_location() === TRUE && $this->_check_type($type) === TRUE))
		{
			$this->_type = $type;
			$this->_name = $name;
			$this->_extras = $extras;
			$this->_actions = array();
		}
	} // End of __construct
	
// ------------------------------------------------------------------------
	
	/**
	 * El Destructor!
	 * Prints a success message if nothing went wrong.
	 *
	 * @access public
	 * @return void
	 * @author Aziz Light
	 */
	public function __destruct()
	{
		if ($this->_success === TRUE)
		{
			if ($this->_type == 'view')
			{
				if ($this->_new_views_folder)
					$_message = ucfirst($this->_name) . " views folder successfully created!";
				elseif (empty($this->_extras))
					$_message = "The {$this->_name} views folder already exists!";

				foreach ($this->_extras as $view)
				{
					$_message .= "\n" . ucfirst($view) . " view successfully created in the " . ucfirst($this->_name) . " views folder!";
				}
				self::_display_message('', $_message);
				unset($_message);
			}
			else
			{
				$_message = ucfirst($this->_name) . ' ' . ucfirst($this->_type) . ' successfully created!';
				self::_display_message('', $_message);
				unset($_message);
			}
		}
	} // End of __destruct
	
// ------------------------------------------------------------------------
	
	/**
	 * Controller template
	 *
	 * @access private
	 * @param string $name : The name of the controller. Should be plural.
	 * @return void
	 * @author Aziz Light
	 */
	private  function _controller()
	{
		$class_name = ucfirst($this->_name);
		$parent_controller = self::parent_controller;
		$parent_controller_constructor = self::parent_controller_constructor;
		$application_folder_name = self::application_folder_name;
		
$controller = <<<CONTROLLER
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class {$class_name} extends {$parent_controller}
{
	/**
	 * The Constructor!
	 **/
	public function __construct()
	{
		parent::{$parent_controller_constructor}();
	}
CONTROLLER;

		$controller = $this->_add_actions($controller);

$controller .= <<<CONTROLLER
	
} // End of the {$class_name} controller

/* End of file {$this->_name}.php */
/* Location ./{$application_folder_name}/controllers/{$this->_name}.php */
CONTROLLER;
		
		return $controller;
	} // End of _controller
	
// ------------------------------------------------------------------------
	
	/**
	 * Model Template
	 *
	 * @access private
	 * @param string $name : The name of the model without the suffix. Should be singular.
	 * @return void
	 * @author Aziz Light
	 */
	private function _model()
	{
		$class_name = ucfirst($this->_name);
		$parent_model = self::parent_model;
		$parent_model_constructor = self::parent_model_constructor;
		$application_folder_name = self::application_folder_name;
		
$model = <<<MODEL
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class {$class_name} extends {$parent_model}
{
	/**
	 * The Constructor!
	 **/
	public function __construct()
	{
		parent::{$parent_model_constructor}();
	}
MODEL;
		$model  = $this->_add_actions($model);
$model .= <<<MODEL
} // End of the {$class_name}

/* End of file {$this->_name}.php */
/* Location ./{$application_folder_name}/models/{$this->_name}.php */
MODEL;
		
		return $model;
	} // End of _model
	
// ------------------------------------------------------------------------

	/**
	 * Create the views subfolder if it doesn't exist and then create the
	 * view files. Additionally creates an index.html file for restricting
	 * direct access to the views subfolders.
	 *
	 * @access private
	 * @param string $name : The name of the views folder.
	 * @param array $view : The view files to create.
	 * @return bool
	 * @author Aziz Light
	 */
	private function _view($name = '', $views = array())
	{
		$folder = self::$_location . '/views/' . $name . '/';
		
		// If the views folder doesn't exist, try to create it.
		if (!is_dir($folder))
		{
			if(!@mkdir($folder, 0755))
			{
				$_error = 'Unable to create the views folder...';
				self::_display_message($_error, TRUE);
				unset($_error, $folder);
				
				return FALSE;
			}
			$this->_new_views_folder = TRUE;
		}
		else
		{
			$this->_success = TRUE;
			$this->_new_views_folder = FALSE;
			exit();
		}
		
		// Create the index.html file to restrict direct access to the views folder.
		if (!file_exists($folder . 'index.html'))
		{
			if (($fp = @fopen ($folder . 'index.html', 'wb')))
			{
$index_contents = <<<EOT
<html>
<head>
	<title>403 Forbidden</title>
</head>
<body>

<p>Directory access is forbidden.</p>

</body>
</html>
EOT;
				flock($fp, LOCK_EX);
				fwrite($fp, $index_contents);
				flock($fp, LOCK_UN);
				fclose($fp);
			}
		}

		// Create teh view files.
		if (!empty($views))
		{
			foreach ($views as $view)
			{
				if (file_exists($folder . $view . '.php'))
				{
					$_warning = 'The ' . $view . ' view file already exists!';
					self::_display_message('warning', $_warning);
					unset($_warning);

					$i = 0;
					do
					{
						fwrite(STDOUT, "Do you want to overwrite {$folder}{$view}.php ? [Default: no]\n[yes/no] ");
						$_answer = trim(fgets(STDIN));

						if ($_answer == 'no' || $_answer == 'n')
						{
							break;
						}
						elseif ($_answer == 'yes' || $_answer == 'y' || empty($_answer))
						{
							$i = 42;
						}
						else
						{
							fwrite(STDOUT, "I don't understand...\n");
							$i++;
						}
					} while ($i < 3);
					unset($_answer);

				}
				else
				{
					$i = 42;
				}

				if ($i > 3)
				{
					if (($fp = @fopen ($folder . $view . '.php', 'wb')))
					{
						fclose($fp);
					}
					else
					{
						$_error = 'Unable to create the ' . $view . ' view...';
						self::_display_message('error', $_error, TRUE);
					}
				}
			}
		}

		$this->_success = TRUE;
		return TRUE;
	}

// ------------------------------------------------------------------------

	/**
	 * Create a new action.
	 *
	 * @access public
	 * @param string $name : The name of the action.
	 * @return string : The action.
	 * @author Aziz Light
	 */
	public function new_action($name)
	{
		if (empty($name))
		{
			fwrite(STDOUT, 'Please enter the name of the action you want to create: ');
			$name = fgets(STDIN);
			$name = trim($name);
			if (empty($name))
			{
				$_error = 'You did not provide a name for the action you want to create!';
				self::_display_message('error', $_error);
			}
		}

		$_access = (substr($name, 0, 1) == '_') ? 'private' : 'public';

$_action = <<<ACTION
// ------------------------------------------------------------------------
	
	{$_access} function {$name}()
	{

	} // End of {$name}
	
ACTION;

		$this->_actions[] = $_action;

		return TRUE;
	} // End of new_action

// ------------------------------------------------------------------------

	/**
	 * Alias of the new_action method.
	 * Models don't have actions they have methods.
	 *
	 * @see MVC::new_action()
	 * @access public
	 * @param string $name : The name of the action.
	 * @return string : The action.
	 * @author Aziz Light
	 */
	public function new_method($name)
	{
		return $this->new_action($name);
	}

// ------------------------------------------------------------------------
	
	/**
	 * Creates a new file in the project.
	 *
	 * @access public
	 * @return void
	 * @author Aziz Light
	 */
	public function create()
	{
		if (empty($this->_name))
		{
			$this->_name = $this->_get_name();
			if (empty($this->_name))
			{
				$_error  = 'You didn\'t specify a name for the ' . $this->_type;
				$_error .= ($this->_type == 'view') ? 's folder': '';
				$_error .= ' you want to create...';
				self::_display_message('error', $_error);
			}
		}

		if ($this->_type != 'view')
		{
			$config['type'    ] = $this->_type;
			$config['filename'] = $this->_name;
			$config['template'] = $this->{'_' . $config['type']}();
		}

		switch ($this->_type)
		{
			// CHANGED - Removed the default case. Put it back it there is error poping out...there won't (shouldn't) be any!
			case 'controller':
				$this->_make($config);
				break;
			case 'model':
				$config['filename'] = $config['filename'] . self::model_suffix;
				$this->_make($config);
				break;
			case 'view':
				$this->_view($this->_name, $this->_extras);
				break;
		}
	} // End of create
		
// ------------------------------------------------------------------------
	
	/**
	 * Add a parameter to a controller's action or a model's medthod
	 *
	 * @access public
	 * @param string $name : The name of the parameter (the name of the variable)
	 * @return bool
	 * @author Aziz Light
	 */
	public function new_param($name)
	{
		
	} // End of new_param
	
// ------------------------------------------------------------------------
	
	/**
	 * Check that the user is in the application folder.
	 *
	 * @access private
	 * @return bool|void
	 * @author Aziz Light
	 */
	private function _check_location()
	{
		$location = FolderScanner::check_location(self::application_folder_name, self::system_folder_name);
		if ($location === FALSE)
		{
			$_error = "No CodeIgniter project detected at your location.\nYou must either be in the root of a CodeIgniter project or in the application folder of a project!";
			self::_display_message('error', $_error);
		}
		self::$_location = $location;
		
		return TRUE;
	} // End of _check_location
	
// ------------------------------------------------------------------------
	
	/**
	 * Check that a type is valid.
	 *
	 * @access private
	 * @param string $type
	 * @return bool|void
	 * @author Aziz Light
	 */
	private function _check_type($type = '')
	{
		$_types = array(
			'controller', 'model', 'view',
		);
		
		if (in_array(strtolower($type), $_types))
		{
			return TRUE;
		}
		else
		{
			$_error = 'You specified an unknown type!!';
			self::_display_message('error', $_error);
		}
	} // End of _check_type
	
// ------------------------------------------------------------------------
	
	/**
	 * If the user did not provide a name as argument, ask to provide a name.
	 *
	 * @access private
	 * @return string : The name provided by the user.
	 * @author Aziz Light
	 */
	private function _get_name()
	{
		$message  = 'Please enter the name of the ' . $this->_type;
		$message .= ($this->type == 'view') ? 's folder': '';
		$message .= ': ';
		fwrite(STDOUT, $message);
		return trim(fgets(STDIN));
	} // End of _get_name
	
// ------------------------------------------------------------------------
	
	/**
	 * Creates a controller or a model and terminates the execution of the script.
	 *
	 * @access private
	 * @param array $config : Contains the type of file (controller or model), its name and its contents.
	 * @return void
	 * @author Aziz Light
	 */
	private function _make($config)
	{
		$file = self::$_location . '/' . $config['type'] . 's/' . $config['filename'] . '.php';
		
		if (file_exists($file))
		{
			$_warning = "${file} already exists!!";
			self::_display_message('warning', $_warning);
			unset($_warning);
			
			$i = 0;
			do {
				fwrite(STDOUT, "Do you want to overwrite ${file} ? [Default: yes]\n[yes/no] ");
				$_answer = trim(fgets(STDIN));
				if ($_answer == 'no' || $_answer == 'n')
				{
					break;
				}
				elseif ($_answer == 'yes' || $_answer == 'y' || empty($_answer))
				{
					$i = 42;
				}
				else
				{
					fwrite(STDOUT, "I don't understand...\n");
					$i++;
				}
				
			} while ($i < 3);
			unset($_answer);
		}
		else
		{
			$i = 42;
		}
		
		if ($i > 3)
		{
			if (($fp = @fopen ($file, 'wb'))) // NOTE - Here I added a set of parenthesis to stop getting the "Assignment in condition" warning.
			{
				flock($fp, LOCK_EX);
				fwrite($fp, $config['template']);
				flock($fp, LOCK_UN);
				fclose($fp);
				
				$this->_success = TRUE;
				return;
			}
			else
			{
				$_error = 'Unable to create the ' . ucfirst($config['filename']) . ' ' . $config['type'] . '...';
				self::_display_message('error', $_error);
			}
		}
	} // End of _make
	
// ------------------------------------------------------------------------
	
	/**
	 * Adds actions to the template.
	 *
	 * @access private
	 * @param string $template : The template's "head". Take a look at the controller template to see where the _add_actions() method is called.
	 * @return void|string : Returns the template with the added actions (if there is any).
	 * @author Aziz Light
	 */
	private function _add_actions($template = NULL)
	{
		if (empty($template))
		{
			$_error = 'You need to pass a template as parameter to add actions to it...';
			self::_display_message('error', $_error);
		}
		elseif (empty($this->_actions))
		{
			return $template;
		}
		else
		{
			foreach ($this->_actions as $_action)
			{
				$template .= $_action;
			}
			
			return $template;
		}
	} // End of _add_actions
	
} // End of MVC class

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

/**
 * A folder scanner.
 *
 * @package Fire
 * @subpackage FolderScanner
 * @author Aziz Light
 * @link http://bitbucket.org/azizlight/fire
 * @link http://github.com/AzizLight/fire
 * @copyright Copyright (c) 2010, Aziz Light
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
class FolderScanner
{
	/**
	 * Get the current location. Returns the path of the current location
	 * and the breadcrumbs trail to that location.
	 *
	 * @access public
	 * @static
	 * @return object
	 * @author Aziz Light
	 */
	public static function get_location()
	{
		$location = new stdClass();
		$location->path = getcwd();
		$location->dirs = explode('/', $location->path);
		$location->dirs[0] = '/';
		
		return $location;
	} // End of get_location
	
// ------------------------------------------------------------------------
	
	/**
	 * List the files and folders that are in a given directory (by default the current location)
	 *
	 * @access public
	 * @static
	 * @param string $dir : The directory to scan.
	 * @return array : The list of files and folders in an array.
	 * @author Aziz Light
	 */
	public static function ls($dir = '')
	{
		if (empty($dir))
			$dir = getcwd();
		
		$handle = opendir($dir);
		$folders = array();
		while (FALSE !== ($f = readdir($handle)))
		{
			if ($f != '.' && $f != '..')
				$folders[] = $f;
		}
		closedir($handle);
		
		return $folders;
	} // End of ls
	
// ------------------------------------------------------------------------
	
	/**
	 * Check that the user is either in the root of a CodeIgniter project or in
	 * the application folder of a CodeIgniter project and returns the path to the
	 * application folder.
	 *
	 * @access public
	 * @static
	 * @return bool|string : Returns the path to the application folder or FALSE if it wasn't found.
	 * @author Aziz Light
	 */
	public static function check_location($application_folder = 'application', $system_folder = 'system')
	{
		$location = self::get_location();
		
		if (!in_array($application_folder, $location->dirs))
		{
			$folders = self::ls();
			if (!in_array($application_folder, $folders))
			{
				if (!in_array($system_folder, $folders))
					return FALSE;
				
				$folders = self::ls($system_folder);
				if (!in_array($application_folder, $folders))
					return FALSE;
				
				return $location->path . '/' . $system_folder . '/' . $application_folder;
			}
			
			return $location->path . '/' . $application_folder;
		}
		
		return $location->path;
	} // End of check_location
} // End of FolderScanner

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

/**
* Inflector for pluralize and singularize English nouns.
* 
* This Inflector is a port of Ruby on Rails Inflector.
* 
* It can be really helpful for developers that want to
* create frameworks based on naming conventions rather than
* configurations.
* 
* It was ported to PHP for the Akelos Framework, a
* multilingual Ruby on Rails like framework for PHP that will
* be launched soon.
* 
* Note from Aziz Light:
*  I modified this class a bit, I actually removed all of its
*  methods except singularize. I kept the method in the Inflector
*  class because in the future I might add back some other methods to it.
* 
* @author Bermi Ferrer Martinez <bermi akelos com>
* @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
* @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
* @since 0.1
* @version $Revision 0.1 $
*/
class Inflector
{	
	/**
	* Singularizes English nouns.
	* 
	* @access public
	* @static
	* @param string $word : English noun to singularize
	* @return string : Singular noun.
	*/
	public static function singularize($word)
	{
		$singular = array(
			'/(quiz)zes$/i' => '\\1',
			'/(matr)ices$/i' => '\\1ix',
			'/(vert|ind)ices$/i' => '\\1ex',
			'/^(ox)en/i' => '\\1',
			'/(alias|status)es$/i' => '\\1',
			'/([octop|vir])i$/i' => '\\1us',
			'/(cris|ax|test)es$/i' => '\\1is',
			'/(shoe)s$/i' => '\\1',
			'/(o)es$/i' => '\\1',
			'/(bus)es$/i' => '\\1',
			'/([m|l])ice$/i' => '\\1ouse',
			'/(x|ch|ss|sh)es$/i' => '\\1',
			'/(m)ovies$/i' => '\\1ovie',
			'/(s)eries$/i' => '\\1eries',
			'/([^aeiouy]|qu)ies$/i' => '\\1y',
			'/([lr])ves$/i' => '\\1f',
			'/(tive)s$/i' => '\\1',
			'/(hive)s$/i' => '\\1',
			'/([^f])ves$/i' => '\\1fe',
			'/(^analy)ses$/i' => '\\1sis',
			'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\\1\\2sis',
			'/([ti])a$/i' => '\\1um',
			'/(n)ews$/i' => '\\1ews',
			'/s$/i' => '',
		);
		
		$uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
		
		$irregular = array(
			'person' => 'people',
			'man' => 'men',
			'child' => 'children',
			'sex' => 'sexes',
			'move' => 'moves'
		);
		
		$lowercased_word = strtolower($word);
		foreach ($uncountable as $_uncountable)
		{
			if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable)
			{
				return $word;
			}
		}
		
		foreach ($irregular as $_plural => $_singular)
		{
			if (preg_match('/(' . $_singular . ')$/i', $word, $arr))
			{
				return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word);
			}
		}
		
		foreach ($singular as $rule => $replacement)
		{
			if (preg_match($rule, $word))
			{
				return preg_replace($rule, $replacement, $word);
			}
		}
		return $word;
	} // End of singularize
	
} // End of Inflector

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------

// -Query-Validation-------------------------------------------------------
try {
	Fire::init($argv);
} catch (Exception $e) {
	exit('ERROR: ' . $e->getMessage() . "\n");
}
// ------------------------------------------------------------------------