PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/src/applications/appcenter/service/srv/PwGenerateApplication.php

https://github.com/cuijinquan/nextwind
PHP | 369 lines | 276 code | 29 blank | 64 comment | 16 complexity | fe61ab23e7cafb630a1ea2ea5ebff715 MD5 | raw file
  1. <?php
  2. Wind::import('APPCENTER:service.srv.helper.PwApplicationHelper');
  3. /**
  4. * 生成demo代码
  5. *
  6. * @author Shi Long <long.shi@alibaba-inc.com>
  7. * @copyright ©2003-2103 phpwind.com
  8. * @license http://www.windframework.com
  9. * @version $Id: PwGenerateApplication.php 24585 2013-02-01 04:02:37Z jieyin $
  10. * @package appcenter.service.srv
  11. */
  12. class PwGenerateApplication {
  13. protected $name;
  14. protected $description;
  15. protected $alias;
  16. protected $version;
  17. protected $pwversion;
  18. protected $installation_service;
  19. protected $need_admin;
  20. protected $need_service;
  21. protected $baseDir;
  22. protected $defaultDir;
  23. protected $manifest;
  24. protected $author;
  25. protected $email;
  26. protected $website;
  27. /**
  28. *
  29. * @param field_type $website
  30. */
  31. public function setWebsite($website) {
  32. $this->website = $website;
  33. }
  34. /**
  35. *
  36. * @param field_type $need_service
  37. */
  38. public function setNeed_service($need_service) {
  39. $this->need_service = $need_service;
  40. }
  41. /**
  42. *
  43. * @param field_type $author
  44. */
  45. public function setAuthor($author) {
  46. $this->author = $author;
  47. }
  48. /**
  49. *
  50. * @param field_type $email
  51. */
  52. public function setEmail($email) {
  53. $this->email = $email;
  54. }
  55. /**
  56. *
  57. * @param field_type $name
  58. */
  59. public function setName($name) {
  60. $this->name = $name;
  61. }
  62. /**
  63. *
  64. * @param field_type $description
  65. */
  66. public function setDescription($description) {
  67. $this->description = $description;
  68. }
  69. /**
  70. *
  71. * @param field_type $alias
  72. */
  73. public function setAlias($alias) {
  74. $this->alias = $alias;
  75. }
  76. /**
  77. *
  78. * @param field_type $version
  79. */
  80. public function setVersion($version) {
  81. $this->version = $version;
  82. }
  83. /**
  84. *
  85. * @param field_type $pwversion
  86. */
  87. public function setPwversion($pwversion) {
  88. $this->pwversion = $pwversion;
  89. }
  90. /**
  91. *
  92. * @param field_type $installation_service
  93. */
  94. public function setInstallation_service($installation_service) {
  95. $this->installation_service = $installation_service;
  96. }
  97. /**
  98. *
  99. * @param field_type $need_admin
  100. */
  101. public function setNeed_admin($need_admin) {
  102. $this->need_admin = $need_admin;
  103. }
  104. public function __construct() {
  105. $this->defaultDir = Wind::getRealDir('APPCENTER:service.data.source');
  106. }
  107. public function generate() {
  108. $this->baseDir = Wind::getRealDir('EXT:' . $this->alias);
  109. if (is_dir($this->baseDir)) return new PwError('APPCENTER:alias.exist');
  110. WindFolder::mkRecur($this->baseDir);
  111. Wind::import('APPCENTER:service.srv.helper.PwSystemHelper');
  112. $writable = PwSystemHelper::checkWriteAble($this->baseDir . '/');
  113. if (!$writable) {
  114. return new PwError('APPCENTER:generate.copy.fail');
  115. }
  116. PwApplicationHelper::copyRecursive($this->defaultDir, $this->baseDir, array('service'));
  117. $manifest = WindFile::read($this->baseDir . '/Manifest.xml');
  118. $this->manifest = $this->_strtr($manifest);
  119. $this->_generateAdmin();
  120. WindFile::write($this->baseDir . '/Manifest.xml', $this->manifest);
  121. $this->_resolveTemplate();
  122. $this->_generateService();
  123. return true;
  124. }
  125. public function generateBaseInfo() {
  126. $this->baseDir = Wind::getRealDir('EXT:' . $this->alias);
  127. $file = $this->baseDir . '/Manifest.xml';
  128. Wind::import('WIND:parser.WindXmlParser');
  129. $parser = new WindXmlParser();
  130. $manifest = $parser->parse($file);
  131. $manifest['application']['name'] = $this->name;
  132. $manifest['application']['description'] = $this->description;
  133. $manifest['application']['version'] = $this->version;
  134. $manifest['application']['pw-version'] = $this->pwversion;
  135. $manifest['application']['author-name'] = $this->author;
  136. $manifest['application']['author-email'] = $this->email;
  137. $manifest['application']['website'] = $this->website;
  138. $parser = new WindXmlParser();
  139. $manifest = str_replace('><', ">\n\t<", $parser->parseToXml(array('manifest' => $manifest), Wind::getApp()->getResponse()->getCharset()));
  140. if (!WindFile::write($file, $manifest)) {
  141. return new PwError('APPCENTER:generate.copy.fail');
  142. }
  143. }
  144. /**
  145. * 生成钩子
  146. *
  147. * @param unknown_type $hookname
  148. * @return PwError
  149. */
  150. public function generateHook($hookname) {
  151. $prefix = substr($hookname, 0, 2);
  152. if ('s_' == $prefix) {
  153. return $this->generateSimpleHook($hookname);
  154. } elseif ('m_' == $prefix) {
  155. return $this->generateServiceHook($hookname);
  156. } else {
  157. return new PwError('APPCENTER:generate.unsupport.hook');
  158. }
  159. }
  160. public function generateServiceHook($hookname) {
  161. $hookInfo = Wekit::load('hook.PwHooks')->fetchByName($hookname);
  162. list($description, , $interface) = explode("\r\n", $hookInfo['document']);
  163. if (!$interface) return new PwError('APPCENTER:generate.unsupport.hook');
  164. $this->baseDir = Wind::getRealDir('EXT:' . $this->alias);
  165. $thing = substr($hookname, 2);
  166. $classname = $this->_ucwords($this->alias . $thing) . 'Do';
  167. $interfacename = Wind::import($interface);
  168. $reflection = new ReflectionClass($interfacename);
  169. $extends = ($reflection->isInterface() ? 'implements ' : 'extends ') . $interfacename;
  170. $manifest = Wind::getComponent('configParser')->parse($this->baseDir . '/Manifest.xml');
  171. $hook = array(
  172. 'app_' . $this->alias => array(
  173. 'class' => 'EXT:' . $this->alias . '.service.srv.' . $classname,
  174. 'description' => 'this is another ' . $hookname));
  175. $manifest['inject-services'][$hookname] = $hook;
  176. Wind::import('WIND:parser.WindXmlParser');
  177. $parser = new WindXmlParser();
  178. $manifest = str_replace('><', ">\n\t<", $parser->parseToXml(array('manifest' => $manifest), Wind::getApp()->getResponse()->getCharset()));
  179. if (!WindFile::write($this->baseDir . '/Manifest.xml', $manifest)) {
  180. return new PwError('APPCENTER:generate.copy.fail');
  181. }
  182. $class = WindFile::read(dirname($this->defaultDir) . '/servicehook');
  183. $class = strtr($class,
  184. array(
  185. '{{interface}}' => $interface,
  186. '{{classname}}' => $classname,
  187. '{{extends}}' => $extends,
  188. '{{interfacename}}' => $interfacename,
  189. '{{classname}}' => $classname,
  190. '{{author}}' => $this->author,
  191. '{{email}}' => $this->email,
  192. '{{website}}' => $this->website,
  193. '{{description}}' => $description));
  194. WindFolder::mkRecur($this->baseDir . '/service/srv/');
  195. if (!WindFile::write($this->baseDir . '/service/srv/' . $classname . '.php', $class)) {
  196. return new PwError('APPCENTER:generate.copy.fail');
  197. }
  198. }
  199. /**
  200. * 生成简单钩子
  201. *
  202. * @param unknown_type $hookname
  203. */
  204. public function generateSimpleHook($hookname) {
  205. $hookInfo = Wekit::load('hook.PwHooks')->fetchByName($hookname);
  206. list($description, $doc) = explode("\r\n", $hookInfo['document']);
  207. if (!$doc) return new PwError('APPCENTER:generate.unsupport.hook');
  208. preg_match_all('/\$[a-zA-Z_][a-zA-Z0-9_]*/', $doc, $matches);
  209. $param = implode(', ', $matches[0]);
  210. $doc = str_replace("\n", "\n\t * ", $doc);
  211. $this->baseDir = Wind::getRealDir('EXT:' . $this->alias);
  212. $manifest = Wind::getComponent('configParser')->parse($this->baseDir . '/Manifest.xml');
  213. $thing = substr($hookname, 2);
  214. $classname = $this->_ucwords($this->alias . '_' . $thing) . 'Do';
  215. $method = WindUtility::lcfirst($this->_ucwords($this->alias)) . 'Do';
  216. $hook = array(
  217. 'app_' . $this->alias => array(
  218. 'class' => 'EXT:' . $this->alias . '.service.srv.' . $classname,
  219. 'loadway' => 'load',
  220. 'method' => $method,
  221. 'description' => 'this is another ' . $hookname));
  222. $manifest['inject-services'][$hookname] = $hook;
  223. Wind::import('WIND:parser.WindXmlParser');
  224. $parser = new WindXmlParser();
  225. $manifest = str_replace('><', ">\n\t<", $parser->parseToXml(array('manifest' => $manifest), Wind::getApp()->getResponse()->getCharset()));
  226. if (!WindFile::write($this->baseDir . '/Manifest.xml', $manifest)) {
  227. return new PwError('APPCENTER:generate.copy.fail');
  228. }
  229. $class = WindFile::read(dirname($this->defaultDir) . '/simplehook');
  230. $class = strtr($class,
  231. array(
  232. '{{method}}' => $method,
  233. '{{classname}}' => $classname,
  234. '{{document}}' => $doc,
  235. '{{param}}' => $param,
  236. '{{classname}}' => $classname,
  237. '{{author}}' => $this->author,
  238. '{{email}}' => $this->email,
  239. '{{website}}' => $this->website,
  240. '{{description}}' => $description));
  241. WindFolder::mkRecur($this->baseDir . '/service/srv/');
  242. if (!WindFile::write($this->baseDir . '/service/srv/' . $classname . '.php', $class)) {
  243. return new PwError('APPCENTER:generate.copy.fail');
  244. }
  245. }
  246. private function _resolveTemplate() {
  247. $index = $this->baseDir . '/template/index_run.htm';
  248. $admin = $this->baseDir . '/template/admin/manage_run.htm';
  249. WindFile::write($index, strtr(WindFile::read($index), array('{{alias}}' => $this->alias)));
  250. WindFile::write($admin, strtr(WindFile::read($admin), array('{{alias}}' => $this->alias)));
  251. }
  252. private function _strtr($string) {
  253. return strtr($string,
  254. array(
  255. '{{charset}}' => Wind::getApp()->getResponse()->getCharset(),
  256. '{{name}}' => $this->name,
  257. '{{alias}}' => $this->alias,
  258. '{{version}}' => $this->version,
  259. '{{pw_version}}' => $this->pwversion,
  260. '{{author}}' => $this->author,
  261. '{{email}}' => $this->email,
  262. '{{website}}' => $this->website,
  263. '{{description}}' => $this->description,
  264. '{{installation_service}}' => $this->installation_service));
  265. }
  266. private function _generateAdmin() {
  267. if ($this->need_admin) {
  268. $classname = $this->_ucwords($this->alias) . '_ConfigDo';
  269. $hook = array(
  270. 's_admin_menu' => array(
  271. 'app_' . $this->alias => array(
  272. 'class' => 'EXT:' . $this->alias . '.service.srv.' . $classname,
  273. 'loadway' => 'load',
  274. 'method' => 'getAdminMenu',
  275. 'description' => $this->name . 'admin menu')));
  276. Wind::import('WIND:parser.WindXmlParser');
  277. $parser = new WindXmlParser();
  278. $hook = preg_replace('/<\?xml[^\?]+\?>/i', '', $parser->parseToXml($hook, Wind::getApp()->getResponse()->getCharset()));
  279. $hook = str_replace('><', ">\n\t<", $hook);
  280. $this->manifest = strtr($this->manifest, array('{{inject_services}}' => $hook));
  281. $class = WindFile::read(dirname($this->defaultDir) . '/hook.admin');
  282. $class = strtr($class,
  283. array(
  284. '{{alias}}' => $this->alias,
  285. '{{name}}' => $this->name,
  286. '{{classname}}' => $classname,
  287. '{{author}}' => $this->author,
  288. '{{email}}' => $this->email,
  289. '{{website}}' => $this->website));
  290. WindFolder::mkRecur($this->baseDir . '/service/srv/');
  291. WindFile::write($this->baseDir . '/service/srv/' . $classname . '.php', $class);
  292. } else {
  293. $this->manifest = strtr($this->manifest, array('{{inject_services}}' => ''));
  294. WindFolder::clearRecur($this->baseDir . '/admin', true);
  295. WindFolder::clearRecur($this->baseDir . '/template/admin', true);
  296. }
  297. }
  298. protected function _generateService() {
  299. if (!$this->need_service) return true;
  300. $prefix = 'app_' . $this->alias . '_table';
  301. $classFrefix = str_replace(' ', '', ucwords('app_ ' . $this->alias . '_ ' . $this->alias));
  302. WindFolder::mkRecur($this->baseDir . '/service/dao/');
  303. WindFolder::mkRecur($this->baseDir . '/service/dm/');
  304. $dao_file = $this->defaultDir . '/service/dao/defaultdao';
  305. $class_dao = $classFrefix . 'Dao';
  306. $dao = strtr(WindFile::read($dao_file),
  307. array(
  308. '{{classname}}' => $class_dao,
  309. '{{prefix}}' => $prefix,
  310. '{{author}}' => $this->author,
  311. '{{email}}' => $this->email,
  312. '{{website}}' => $this->website));
  313. WindFile::write($this->baseDir . '/service/dao/' . $class_dao . '.php', $dao);
  314. $dm_file = $this->defaultDir . '/service/dm/defaultdm';
  315. $class_dm = $classFrefix . 'Dm';
  316. $dm = strtr(WindFile::read($dm_file),
  317. array(
  318. '{{classname}}' => $class_dm,
  319. '{{author}}' => $this->author,
  320. '{{email}}' => $this->email,
  321. '{{website}}' => $this->website));
  322. WindFile::write($this->baseDir . '/service/dm/' . $class_dm . '.php', $dm);
  323. $ds_file = $this->defaultDir . '/service/defaultds';
  324. $class_ds = $classFrefix;
  325. $ds = strtr(WindFile::read($ds_file),
  326. array(
  327. '{{classname}}' => $class_ds,
  328. '{{alias}}' => $this->alias,
  329. '{{class_dm}}' => $class_dm,
  330. '{{class_dao}}' => $class_dao,
  331. '{{author}}' => $this->author,
  332. '{{email}}' => $this->email,
  333. '{{website}}' => $this->website));
  334. WindFile::write($this->baseDir . '/service/' . $class_ds . '.php', $ds);
  335. }
  336. protected function _ucwords($str) {
  337. return 'App_' . str_replace('_ ', '_', ucwords(str_replace('_', '_ ', $str)));
  338. }
  339. }
  340. ?>