PageRenderTime 59ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix-scripts/commands-single/createapp.cmd.php

https://bitbucket.org/jelix/jelix-trunk/
PHP | 224 lines | 177 code | 31 blank | 16 comment | 13 complexity | 9a70ae5bc3198d42a3b8171a158bea15 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * @package jelix-scripts
  4. * @author Laurent Jouanneau
  5. * @contributor Loic Mathaud
  6. * @contributor Gildas Givaja (bug #83)
  7. * @contributor Christophe Thiriot
  8. * @contributor Bastien Jaillot
  9. * @contributor Dominique Papin, Olivier Demah
  10. * @copyright 2005-2011 Laurent Jouanneau, 2006 Loic Mathaud, 2007 Gildas Givaja, 2007 Christophe Thiriot, 2008 Bastien Jaillot, 2008 Dominique Papin
  11. * @copyright 2011 Olivier Demah
  12. * @link http://www.jelix.org
  13. * @licence GNU General Public Licence see LICENCE file or http://www.gnu.org/licenses/gpl.html
  14. */
  15. class createappCommand extends JelixScriptCommand {
  16. protected $commonOptions = array();
  17. public $name = 'createapp';
  18. public $allowed_options=array('-nodefaultmodule'=>false,
  19. '-withcmdline'=>false,
  20. '-wwwpath'=>true);
  21. public $allowed_parameters=array('path'=>true);
  22. public $syntaxhelp = "[-nodefaultmodule] [-withcmdline] [-wwwpath a_path]";
  23. public $help='';
  24. public $applicationRequirement = 1;
  25. function __construct(){
  26. $this->help= array(
  27. 'fr'=>"
  28. Crיe une nouvelle application avec tous les rיpertoires nיcessaires et un module
  29. du mךme nom que l'application.
  30. Si l'option -nodefaultmodule est prיsente, le module n'est pas crיי.
  31. Si l'option -withcmdline est prיsente, crיe un point d'entrיe afin de
  32. dיvelopper des scripts en ligne de commande.
  33. Si l'option -wwwpath est prיsente, sa valeur dיfinit le document root de votre application.
  34. wwwpath doit ךtre relatif au rיpertoire de l'application (valeur par dיfaut www/).
  35. le rיpertoire de la future application doit ךtre indiquיe en paramטtre.
  36. ",
  37. 'en'=>"
  38. Create a new application with all directories and one module named as your application.
  39. If you give -nodefaultmodule option, it won't create the module.
  40. If you give the -withcmdline option, it will create an entry point dedicated to
  41. command line scripts.
  42. If you give the -wwwpath option, it will replace your application default document root.
  43. wwwpath must be relative to your application directory (default value is 'www/').
  44. The application directory should be indicated as parameter
  45. ",
  46. );
  47. }
  48. public function run() {
  49. require_once (LIB_PATH.'clearbricks/jelix.inc.php');
  50. require_once (JELIX_LIB_PATH.'installer/jInstaller.class.php');
  51. $appPath = $this->getParam('path');
  52. $appPath = $this->getRealPath($appPath);
  53. $appName = basename($appPath);
  54. $appPath .= '/';
  55. if (file_exists($appPath)) {
  56. throw new Exception("this application is already created");
  57. }
  58. $this->config = JelixScript::loadConfig($appName);
  59. $this->config->infoIDSuffix = $this->config->newAppInfoIDSuffix;
  60. $this->config->infoWebsite = $this->config->newAppInfoWebsite;
  61. $this->config->infoLicence = $this->config->newAppInfoLicence;
  62. $this->config->infoLicenceUrl = $this->config->newAppInfoLicenceUrl;
  63. $this->config->newAppInfoLocale = $this->config->newAppInfoLocale;
  64. $this->config->newAppInfoCopyright = $this->config->newAppInfoCopyright;
  65. $this->config->initAppPaths($appPath);
  66. jApp::setEnv('jelix-scripts');
  67. jApp::initLegacy();
  68. JelixScript::checkTempPath();
  69. if ($p = $this->getOption('-wwwpath')) {
  70. $wwwpath = path::real($appPath.$p, false).'/';
  71. }
  72. else {
  73. $wwwpath = jApp::wwwPath();
  74. }
  75. $this->createDir($appPath);
  76. $this->createDir(jApp::tempBasePath());
  77. $this->createDir($wwwpath);
  78. $varPath = jApp::varPath();
  79. $configPath = jApp::configPath();
  80. $this->createDir($varPath);
  81. $this->createDir(jApp::logPath());
  82. $this->createDir($configPath);
  83. $this->createDir($configPath.'index/');
  84. $this->createDir($varPath.'overloads/');
  85. $this->createDir($varPath.'themes/');
  86. $this->createDir($varPath.'themes/default/');
  87. $this->createDir($varPath.'uploads/');
  88. $this->createDir($varPath.'sessions/');
  89. $this->createDir($varPath.'mails/');
  90. $this->createDir($appPath.'install');
  91. $this->createDir($appPath.'modules');
  92. $this->createDir($appPath.'plugins');
  93. $this->createDir($appPath.'plugins/coord/');
  94. $this->createDir($appPath.'plugins/tpl/');
  95. $this->createDir($appPath.'plugins/tpl/common');
  96. $this->createDir($appPath.'plugins/tpl/html');
  97. $this->createDir($appPath.'plugins/tpl/text');
  98. $this->createDir($appPath.'plugins/db/');
  99. $this->createDir($appPath.'plugins/auth/');
  100. $this->createDir($appPath.'responses');
  101. $this->createDir($appPath.'tests');
  102. $this->createDir(jApp::scriptsPath());
  103. $param = array();
  104. $param['default_id'] = $appName.$this->config->infoIDSuffix;
  105. if($this->getOption('-nodefaultmodule')) {
  106. $param['tplname'] = 'jelix~defaultmain';
  107. $param['modulename'] = 'jelix';
  108. }
  109. else {
  110. // note: since module name are used for name of generated name,
  111. // only this characters are allowed
  112. $param['modulename'] = preg_replace('/([^a-zA-Z_0-9])/','_',$appName);
  113. $param['tplname'] = $param['modulename'].'~main';
  114. }
  115. $param['config_file'] = 'index/config.ini.php';
  116. $param['rp_temp'] = $this->getRelativePath($appPath, jApp::tempBasePath());
  117. $param['rp_var'] = $this->getRelativePath($appPath, jApp::varPath());
  118. $param['rp_log'] = $this->getRelativePath($appPath, jApp::logPath());
  119. $param['rp_conf'] = $this->getRelativePath($appPath, $configPath);
  120. $param['rp_www'] = $this->getRelativePath($appPath, $wwwpath);
  121. $param['rp_cmd'] = $this->getRelativePath($appPath, jApp::scriptsPath());
  122. $param['rp_jelix'] = $this->getRelativePath($appPath, JELIX_LIB_PATH);
  123. $param['rp_app'] = $this->getRelativePath($wwwpath, $appPath);
  124. $this->createFile($appPath.'.htaccess', 'htaccess_deny', $param);
  125. $this->createFile($appPath.'project.xml','project.xml.tpl', $param);
  126. $this->createFile($appPath.'cmd.php','cmd.php.tpl', $param);
  127. $this->createFile($configPath.'defaultconfig.ini.php', 'var/config/defaultconfig.ini.php.tpl', $param);
  128. $this->createFile($configPath.'profiles.ini.php', 'var/config/profiles.ini.php.tpl', $param);
  129. //$this->createFile(JELIX_APP_CONFIG_PATH.'installer.ini.php', 'var/config/installer.ini.php.tpl', $param);
  130. $this->createFile($configPath.'index/config.ini.php', 'var/config/index/config.ini.php.tpl', $param);
  131. $this->createFile($appPath.'responses/myHtmlResponse.class.php', 'responses/myHtmlResponse.class.php.tpl', $param);
  132. $this->createFile($appPath.'install/installer.php','installer/installer.php.tpl',$param);
  133. $this->createFile($appPath.'tests/runtests.php','tests/runtests.php', $param);
  134. $this->createFile($wwwpath.'index.php', 'www/index.php.tpl',$param);
  135. $this->createFile($wwwpath.'.htaccess', 'htaccess_allow',$param);
  136. $param['php_rp_temp'] = $this->convertRp($param['rp_temp']);
  137. $param['php_rp_var'] = $this->convertRp($param['rp_var']);
  138. $param['php_rp_log'] = $this->convertRp($param['rp_log']);
  139. $param['php_rp_conf'] = $this->convertRp($param['rp_conf']);
  140. $param['php_rp_www'] = $this->convertRp($param['rp_www']);
  141. $param['php_rp_cmd'] = $this->convertRp($param['rp_cmd']);
  142. $param['php_rp_jelix'] = $this->convertRp($param['rp_jelix']);
  143. $this->createFile($appPath.'application.init.php','application.init.php.tpl',$param);
  144. $installer = new jInstaller(new textInstallReporter('warning'));
  145. $installer->installApplication();
  146. $moduleok = true;
  147. if (!$this->getOption('-nodefaultmodule')) {
  148. try {
  149. $cmd = JelixScript::getCommand('createmodule', $this->config);
  150. $cmd->initOptParam(array('-addinstallzone'=>true), array('module'=>$param['modulename']));
  151. $cmd->run();
  152. $this->createFile($appPath.'modules/'.$param['modulename'].'/templates/main.tpl', 'module/main.tpl.tpl', $param);
  153. } catch (Exception $e) {
  154. $moduleok = false;
  155. echo "The module has not been created because of this error: ".$e->getMessage()."\nHowever the application has been created\n";
  156. }
  157. }
  158. if ($this->getOption('-withcmdline')) {
  159. if(!$this->getOption('-nodefaultmodule') && $moduleok){
  160. $agcommand = JelixScript::getCommand('createctrl', $this->config);
  161. $options = array('-cmdline'=>true);
  162. $agcommand->initOptParam($options,array('module'=>$param['modulename'], 'name'=>'default','method'=>'index'));
  163. $agcommand->run();
  164. }
  165. $agcommand = JelixScript::getCommand('createentrypoint', $this->config);
  166. $options = array('-type'=>'cmdline');
  167. $parameters = array('name'=>$param['modulename']);
  168. $agcommand->initOptParam($options, $parameters);
  169. $agcommand->run();
  170. }
  171. }
  172. protected function convertRp($rp) {
  173. if(strpos($rp, './') === 0)
  174. $rp = substr($rp, 2);
  175. if (strpos($rp, '../') !== false) {
  176. return 'realpath($appPath.\''.$rp."').'/'";
  177. }
  178. else if (DIRECTORY_SEPARATOR == '/' && $rp[0] == '/') {
  179. return "'".$rp."'";
  180. }
  181. else if (DIRECTORY_SEPARATOR == '\\' && preg_match('/^[a-z]\:/i', $rp)) { // windows
  182. return "'".$rp."'";
  183. }
  184. else {
  185. return '$appPath.\''.$rp."'";
  186. }
  187. }
  188. }