1: <?php
2:
3: /**
4: * @filesource Kotchasan/Kotchasan.php
5: *
6: * @copyright 2016 Goragod.com
7: * @license http://www.kotchasan.com/license/
8: *
9: * @see http://www.kotchasan.com/
10: */
11: use Kotchasan\Config;
12: use Kotchasan\Http\Request;
13:
14: /**
15: * Kotchasan PHP Framework.
16: *
17: * @author Goragod Wiriya <admin@goragod.com>
18: *
19: * @since 1.0
20: */
21: class Kotchasan extends Kotchasan\KBase
22: {
23: /**
24: * default charset (แนะนำ utf-8).
25: *
26: * @var string
27: */
28: public $char_set = 'utf-8';
29: /**
30: * Controller หลัก
31: *
32: * @var string
33: */
34: public $defaultController = 'Index\Index\Controller';
35: /**
36: * Router หลัก
37: *
38: * @var string
39: */
40: public $defaultRouter = 'Kotchasan\Router';
41: /**
42: * @var Singleton สำหรับเรียกใช้ class นี้เพียงครั้งเดียวเท่านั้น
43: */
44: private static $instance = null;
45:
46: /**
47: * สร้าง Application สามารถเรียกใช้ได้ครั้งเดียวเท่านั้น.
48: *
49: * @param Config|string|null $cfg ถ้าไม่กำหนดมาจะใช้ค่าเริ่มต้นของคชสาร
50: *
51: * @return \static
52: */
53: public static function createWebApplication($cfg = null)
54: {
55: if (null === self::$instance) {
56: self::$instance = new static($cfg);
57: }
58:
59: return self::$instance;
60: }
61:
62: /**
63: * แสดงผลหน้าเว็บไซต์.
64: */
65: public function run()
66: {
67: $router = new $this->defaultRouter();
68: $router->init($this->defaultController);
69: }
70:
71: /**
72: * create Singleton.
73: *
74: * @param Config|string|null $cfg
75: */
76: private function __construct($cfg)
77: {
78: /* Request Class with Apache HTTP headers */
79: self::$request = new Request(true);
80: /* config */
81: if (empty($cfg)) {
82: self::$cfg = Config::create();
83: } elseif (is_string($cfg)) {
84: self::$cfg = $cfg::create();
85: } else {
86: self::$cfg = $cfg;
87: }
88: /* charset */
89: ini_set('default_charset', $this->char_set);
90: if (extension_loaded('mbstring')) {
91: mb_internal_encoding($this->char_set);
92: }
93: /* time zone */
94: @date_default_timezone_set(self::$cfg->timezone);
95: /* custom init site */
96: if (is_string($cfg) && method_exists($cfg, 'init')) {
97: $cfg::init(self::$cfg);
98: }
99: }
100: }
101: