Lotus.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. class Lotus
  3. {
  4. /**
  5. * Lotus Option array
  6. *
  7. * @var array array(
  8. * "proj_dir" =>
  9. * "app_name" =>
  10. * "autoload_dir" =>
  11. * );
  12. */
  13. public $option;
  14. public $devMode = true;
  15. public $defaultStoreDir;
  16. protected $proj_dir;
  17. protected $app_dir;
  18. protected $data_dir;
  19. protected $lotusRuntimeDir;
  20. protected $coreCacheHandle;
  21. public function __construct()
  22. {
  23. $this->lotusRuntimeDir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
  24. }
  25. public function init()
  26. {
  27. $underMVC = false;
  28. if (isset($this->option["proj_dir"]) && !empty($this->option["proj_dir"]))
  29. {
  30. $this->proj_dir = rtrim($this->option["proj_dir"], '\\/') . '/';
  31. if (isset($this->option["app_name"]) && !empty($this->option["app_name"]))
  32. {
  33. $this->app_dir = $this->proj_dir . "app/" . $this->option["app_name"] . "/";
  34. $this->data_dir = $this->proj_dir . "data/" . $this->option["app_name"] . "/";
  35. $underMVC = true;
  36. }
  37. else
  38. {
  39. trigger_error("Lotus option [app_name] is missing.");
  40. }
  41. }
  42. /**
  43. * Load core component
  44. */
  45. require_once $this->lotusRuntimeDir . "Store.php";
  46. require_once $this->lotusRuntimeDir . "StoreMemory.php";
  47. require_once $this->lotusRuntimeDir . "StoreFile.php";
  48. // var_dump($this->defaultStoreDir);exit;
  49. if ($this->defaultStoreDir)
  50. {
  51. if ($defaultStoreDir = realpath($this->defaultStoreDir))
  52. {
  53. LtStoreFile::$defaultStoreDir = $defaultStoreDir;
  54. }
  55. else
  56. {
  57. trigger_error("invalid [default store dir]: " . $this->defaultStoreDir);
  58. }
  59. }
  60. if (!$this->devMode)
  61. {
  62. /**
  63. * accelerate LtAutoloader, LtConfig
  64. */
  65. $this->coreCacheHandle = new LtStoreFile;
  66. $prefix = sprintf("%u", crc32(serialize($this->app_dir)));
  67. $this->coreCacheHandle->prefix = 'Lotus-' . $prefix;
  68. $this->coreCacheHandle->useSerialize = true;
  69. $this->coreCacheHandle->init();
  70. }
  71. /**
  72. * Init Autoloader, do this before init all other lotusphp component.
  73. */
  74. $this->prepareAutoloader();
  75. /**
  76. * init Config
  77. */
  78. $this->prepareConfig();
  79. /**
  80. * Run dispatcher when under MVC mode
  81. */
  82. if ($underMVC)
  83. {
  84. $this->runMVC();
  85. }
  86. }
  87. /**
  88. * Autoload all lotus components and user-defined libraries;
  89. */
  90. protected function prepareAutoloader()
  91. {
  92. require_once $this->lotusRuntimeDir . "Autoloader/Autoloader.php";
  93. $autoloader = new LtAutoloader;
  94. $autoloader->autoloadPath[] = $this->lotusRuntimeDir;
  95. if (isset($this->option["autoload_dir"]))
  96. {
  97. $autoloader->autoloadPath[] = $this->option["autoload_dir"];
  98. }
  99. if ($this->proj_dir)
  100. {
  101. is_dir($this->proj_dir . 'lib') && $autoloader->autoloadPath[] = $this->proj_dir . 'lib';
  102. is_dir($this->app_dir . 'action') && $autoloader->autoloadPath[] = $this->app_dir . 'action';
  103. is_dir($this->app_dir . 'lib') && $autoloader->autoloadPath[] = $this->app_dir . 'lib';
  104. }
  105. if (!$this->devMode)
  106. {
  107. $autoloader->storeHandle = $this->coreCacheHandle;
  108. }
  109. $autoloader->init();
  110. }
  111. protected function prepareConfig()
  112. {
  113. $this->configHandle = LtObjectUtil::singleton('LtConfig');
  114. if (!$this->devMode)
  115. {
  116. $configFile = 'conf/conf.php';
  117. $this->configHandle->storeHandle = $this->coreCacheHandle;
  118. }
  119. else
  120. {
  121. $configFile = 'conf/conf_dev.php';
  122. }
  123. $this->configHandle->init();
  124. if ($this->app_dir && is_file($this->app_dir . $configFile))
  125. {
  126. $this->configHandle->loadConfigFile($this->app_dir . $configFile);
  127. }
  128. }
  129. protected function runMVC()
  130. {
  131. $router = LtObjectUtil::singleton('LtRouter');
  132. $router->init();
  133. $dispatcher = LtObjectUtil::singleton('LtDispatcher');
  134. $dispatcher->viewDir = $this->app_dir . 'view/';
  135. $dispatcher->viewTplDir = $this->data_dir . 'templateView/';
  136. if (!$this->devMode)
  137. {
  138. $dispatcher->viewTplAutoCompile = false;
  139. }
  140. else
  141. {
  142. $dispatcher->viewTplAutoCompile = true;
  143. }
  144. $dispatcher->dispatchAction($router->module, $router->action);
  145. }
  146. }