/framework/Core/lib/Horde/Core/Bundle.php

https://github.com/ewandor/horde · PHP · 187 lines · 109 code · 20 blank · 58 comment · 10 complexity · 971a23181ddae38f8b08dcc1e1f2f13c MD5 · raw file

  1. <?php
  2. /**
  3. * Base class for the Horde bundle API.
  4. *
  5. * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @author Jan Schneider <chuck@horde.org>
  11. * @package Core
  12. */
  13. abstract class Horde_Core_Bundle
  14. {
  15. /**
  16. * @var Horde_Core_Cli
  17. */
  18. protected $_cli;
  19. /**
  20. * @var Horde_Config
  21. */
  22. protected $_config;
  23. /**
  24. * Path to the PEAR configuration file.
  25. *
  26. * @var string
  27. */
  28. protected $_pearconf;
  29. /**
  30. * Constructor.
  31. */
  32. public function __construct(Horde_Core_Cli $cli, $pearconf = null)
  33. {
  34. $this->_cli = $cli;
  35. $this->_pearconf = $pearconf;
  36. }
  37. /**
  38. * Creates and loads a basic conf.php configuration file.
  39. */
  40. public function init()
  41. {
  42. // Check if conf.php is writeable.
  43. if ((file_exists(HORDE_BASE . '/config/conf.php') &&
  44. !is_writable(HORDE_BASE . '/config/conf.php')) ||
  45. !is_writable(HORDE_BASE . '/config')) {
  46. $this->_cli->message(Horde_Util::realPath(HORDE_BASE . '/config/conf.php') . ' is not writable.', 'cli.error');
  47. }
  48. // We need a valid conf.php to instantiate the registry.
  49. if (!file_exists(HORDE_BASE . '/config/conf.php')) {
  50. copy(HORDE_BASE . '/config/conf.php.dist', HORDE_BASE . '/config/conf.php');
  51. }
  52. // Initialization
  53. $umask = umask();
  54. Horde_Registry::appInit('horde', array('nocompress' => true, 'authentication' => 'none'));
  55. $this->_config = new Horde_Config();
  56. umask($umask);
  57. }
  58. /**
  59. * Asks for the database settings and creates the SQL configuration.
  60. */
  61. public function configDb()
  62. {
  63. $this->_cli->writeln();
  64. $this->_cli->writeln($this->_cli->bold('Configuring database settings'));
  65. $this->_cli->writeln();
  66. $sql_config = $this->_config->configSQL('');
  67. $vars = new Horde_Variables();
  68. new Horde_Config_Form($vars, 'horde', true);
  69. $this->_cli->question(
  70. $vars,
  71. 'sql',
  72. 'phptype',
  73. $sql_config['switch']['custom']['fields']['phptype']);
  74. $this->writeConfig($vars);
  75. }
  76. /**
  77. * Creates or updates the database tables.
  78. *
  79. * @throws Horde_Db_Exception
  80. */
  81. public function migrateDb()
  82. {
  83. $this->_cli->writeln();
  84. echo 'Creating and updating database tables...';
  85. $migration = new Horde_Core_Db_Migration(null, $this->_pearconf);
  86. // Try twice to work around unresolved migration dependencies.
  87. for ($i = 0; $i < 2; $i++) {
  88. $error = null;
  89. foreach ($migration->apps as $app) {
  90. $migrator = $migration->getMigrator($app);
  91. if ($migrator->getTargetVersion() <= $migrator->getCurrentVersion()) {
  92. continue;
  93. }
  94. try {
  95. $migrator->up();
  96. } catch (Exception $error) {
  97. if ($i) {
  98. throw $error;
  99. }
  100. }
  101. }
  102. if (!$error) {
  103. break;
  104. }
  105. }
  106. $this->_cli->writeln($this->_cli->green(' done.'));
  107. }
  108. /**
  109. * Asks for the administrator settings and creates the authentication
  110. * configuration.
  111. */
  112. public function configAuth()
  113. {
  114. $vars = new Horde_Variables();
  115. new Horde_Config_Form($vars, 'horde', true);
  116. $this->_cli->writeln();
  117. $this->_cli->writeln($this->_cli->bold('Configuring administrator settings'));
  118. $admin_user = $this->_configAuth($vars);
  119. $vars->auth__admins = $admin_user;
  120. $this->writeConfig($vars);
  121. }
  122. /**
  123. * Asks for the administrator settings.
  124. */
  125. abstract protected function _configAuth(Horde_Variables $vars);
  126. /**
  127. * Writes the current configuration to the conf.php file.
  128. *
  129. * @throws Horde_Exception
  130. */
  131. public function writeConfig(Horde_Variables $vars)
  132. {
  133. $this->_cli->writeln();
  134. echo 'Writing main configuration file...';
  135. $php_config = $this->_config->generatePHPConfig($vars, $GLOBALS['conf']);
  136. $configFile = $this->_config->configFile();
  137. $fp = fopen($configFile, 'w');
  138. if (!$fp) {
  139. throw new Horde_Exception('Cannot write configuration file ' . Horde_Util::realPath($configFile));
  140. }
  141. fwrite($fp, $php_config);
  142. fclose($fp);
  143. // Reload configuration.
  144. include $configFile;
  145. $GLOBALS['conf'] = $conf;
  146. $this->_cli->writeln($this->_cli->green(' done.'));
  147. }
  148. /**
  149. * Creates default configuration files for all installed applications.
  150. *
  151. * @throws Horde_Exception
  152. */
  153. public function writeAllConfigs()
  154. {
  155. foreach ($GLOBALS['registry']->listAllApps() as $app) {
  156. if ($app == 'horde' ||
  157. !file_exists($GLOBALS['registry']->get('fileroot', $app) . '/config/conf.xml')) {
  158. continue;
  159. }
  160. $config = new Horde_Config($app);
  161. $configFile = $config->configFile();
  162. if (!$config->writePHPConfig(new Horde_Variables())) {
  163. throw new Horde_Exception('Cannot write configuration file ' . Horde_Util::realPath($configFile));
  164. }
  165. }
  166. }
  167. }