PageRenderTime 21ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/qeephp/extended/qreflection/module.php

http://enaj.googlecode.com/
PHP | 348 lines | 217 code | 26 blank | 105 comment | 9 complexity | 54afe97f58c1474edc5b85a1a9f3cf13 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * ???????
  4. */
  5. class QReflection_Module
  6. {
  7. /**
  8. * ????????????
  9. *
  10. * @var QReflection_Application
  11. */
  12. protected $_app;
  13. /**
  14. * ??????
  15. *
  16. * @var string
  17. */
  18. protected $_module_dir;
  19. /**
  20. * ????
  21. *
  22. * @var string
  23. */
  24. protected $_module_name;
  25. /**
  26. * ??????????
  27. *
  28. * @var QColl
  29. */
  30. protected $_controllers;
  31. /**
  32. * ??????????
  33. *
  34. * @var array of controller name
  35. */
  36. protected $_controllers_name;
  37. /**
  38. * ??????????
  39. *
  40. * @var QGenerator_Controller
  41. */
  42. protected $_controller_generator;
  43. /**
  44. * ?????????
  45. *
  46. * @var QColl
  47. */
  48. protected $_models;
  49. /**
  50. * ?????????
  51. *
  52. * @var array of model name
  53. */
  54. protected $_models_name;
  55. /**
  56. * ?????????
  57. *
  58. * @var QGenerator_Model
  59. */
  60. protected $_model_generator;
  61. /**
  62. * ????
  63. *
  64. * @param QReflection_Application $app
  65. * @param string $module_name
  66. * @param string $module_dir
  67. */
  68. function __construct(QReflection_Application $app, $module_name, $module_dir)
  69. {
  70. if (!is_dir($module_dir))
  71. {
  72. throw new QReflection_ModuleNotExistsException($app, $module_name);
  73. }
  74. if (empty($module_name))
  75. {
  76. $module_name = QReflection_Application::DEFAULT_MODULE_NAME;
  77. }
  78. $this->_app = $app;
  79. $this->_module_dir = $module_dir;
  80. $this->_module_name = $module_name;
  81. }
  82. /**
  83. * ????????????????
  84. *
  85. * @return QReflection_Application
  86. */
  87. function app()
  88. {
  89. return $this->_app;
  90. }
  91. /**
  92. * ?????????
  93. *
  94. * @return string
  95. */
  96. function moduleName()
  97. {
  98. return $this->_module_name;
  99. }
  100. /**
  101. * ?????????
  102. *
  103. * @return string
  104. */
  105. function moduleDir()
  106. {
  107. return $this->_module_dir;
  108. }
  109. /**
  110. * ????????????
  111. *
  112. * @return boolean
  113. */
  114. function isDefaultModule()
  115. {
  116. return $this->_module_name == QReflection_Application::DEFAULT_MODULE_NAME;
  117. }
  118. /**
  119. * ?????????????
  120. *
  121. * @return array of controller name
  122. */
  123. function controllersName()
  124. {
  125. if (is_null($this->_controllers_name))
  126. {
  127. $dir = rtrim($this->_module_dir, '/\\') . DS . 'controller';
  128. $this->_controllers_name = array();
  129. $files = Helper_FileSys::recursionGlob($dir, '*_controller.php');
  130. sort($files, SORT_STRING);
  131. $dir = rtrim(realpath($dir), '/\\') . DS;
  132. $offset = strlen($dir);
  133. foreach ($files as $file)
  134. {
  135. $file = realpath($file);
  136. $name = substr(substr($file, $offset), 0, -15);
  137. $names = explode(DS, $name);
  138. $namespace = isset($names[1]) ? "{$names[0]}::" : '';
  139. $controller_name = array_pop($names);
  140. $this->_controllers_name[] = "{$namespace}{$controller_name}";
  141. }
  142. }
  143. return $this->_controllers_name;
  144. }
  145. /**
  146. * ?????????????
  147. *
  148. * @return array of QReflection_Controller
  149. */
  150. function controllers()
  151. {
  152. if (is_null($this->_controllers))
  153. {
  154. $this->_controllers = new QColl('QReflection_Controller');
  155. foreach ($this->controllersName() as $name)
  156. {
  157. $this->_controllers[$name] = new QReflection_Controller($this, $name);
  158. }
  159. }
  160. return $this->_controllers;
  161. }
  162. /**
  163. * ????????????
  164. *
  165. * @param string $udi
  166. *
  167. * @return QReflection_Controller
  168. */
  169. function controller($udi)
  170. {
  171. $controllers = $this->controllers();
  172. if (isset($controllers[$udi]))
  173. {
  174. return $controllers[$udi];
  175. }
  176. throw new QReflection_ModuleNotExistsException($this, $controller_name);
  177. }
  178. /**
  179. * ????????????
  180. *
  181. * @param string $controller_name
  182. * @param string $namespace
  183. *
  184. * @return boolean
  185. */
  186. function hasController($controller_name, $namespace = null)
  187. {
  188. $udi = strtolower($controller_name);
  189. $names = explode('::', $controller_name);
  190. if (!isset($names[1]))
  191. {
  192. if ($namespace) $udi = strtolower($namespace) . '::' . $udi;
  193. }
  194. $names = $this->controllersName();
  195. return in_array($udi, $names);
  196. }
  197. /**
  198. * ????????????
  199. *
  200. * @return array of model name
  201. */
  202. function modelsName()
  203. {
  204. if (is_null($this->_models_name))
  205. {
  206. $dir = rtrim($this->_module_dir, '/\\') . DS . 'model';
  207. $files = Helper_FileSys::recursionGlob($dir, '*.php');
  208. $this->_models_name = array();
  209. foreach ($files as $file)
  210. {
  211. $info = QReflection_Model::testModelFile($file);
  212. if ($info == false) continue;
  213. $this->_models_name[$file] = $info['class'];
  214. }
  215. asort($this->_models_name, SORT_STRING);
  216. }
  217. return $this->_models_name;
  218. }
  219. /**
  220. * ????????????
  221. *
  222. * @return QColl
  223. */
  224. function models()
  225. {
  226. if (is_null($this->_models))
  227. {
  228. $this->_models = new QColl('QReflection_Model');
  229. foreach ($this->modelsName() as $path => $class)
  230. {
  231. $this->_models[$class] = new QReflection_Model($this, $class, $path);
  232. }
  233. }
  234. return $this->_models;
  235. }
  236. /**
  237. * ???????????
  238. *
  239. * @param string $model_name
  240. *
  241. * @retrun QReflection_Model
  242. */
  243. function model($model_name)
  244. {
  245. return $this->models[$model_name];
  246. }
  247. /**
  248. * ???????????
  249. *
  250. * @param string $model_name
  251. *
  252. * @return boolean
  253. */
  254. function hasModel($model_name)
  255. {
  256. $names = $this->modelsName();
  257. return in_array($model_name, $names);
  258. }
  259. /**
  260. * ???????????????????????
  261. *
  262. * @param string $controller_name
  263. * @param string $namespace
  264. *
  265. * @return QGenerator_Controller
  266. */
  267. function generateController($controller_name, $namespace = null)
  268. {
  269. return $this->controllerGenerator()->generate($controller_name, $namespace);
  270. }
  271. /**
  272. * ??????????????
  273. *
  274. * @return QGenerator_Controller
  275. */
  276. function controllerGenerator()
  277. {
  278. if (is_null($this->_controller_generator))
  279. {
  280. $this->_controller_generator = new QGenerator_Controller($this);
  281. }
  282. return $this->_controller_generator;
  283. }
  284. /**
  285. * ??????????????????????
  286. *
  287. * @param string $model_name
  288. * @param string $table_name
  289. * @param QDB_Adapter_Abstract $dbo
  290. *
  291. * @return QGenerator_Controller
  292. */
  293. function generateModel($model_name, $table_name, $dbo = null)
  294. {
  295. return $this->modelGenerator()->generate($model_name, $table_name, $dbo);
  296. }
  297. /**
  298. * ?????????????
  299. *
  300. * @return QGenerator_Model
  301. */
  302. function modelGenerator()
  303. {
  304. if (is_null($this->_model_generator))
  305. {
  306. $this->_model_generator = new QGenerator_Model($this);
  307. }
  308. return $this->_model_generator;
  309. }
  310. }