PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/bonfire/modules/migrations/controllers/Developer.php

http://github.com/ci-bonfire/Bonfire
PHP | 192 lines | 98 code | 26 blank | 68 comment | 13 complexity | 309cac3cd151545ec897a59e556bb073 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('BASEPATH') || exit('No direct script access allowed');
  2. /**
  3. * Bonfire
  4. *
  5. * An open source project to allow developers to jumpstart their development of
  6. * CodeIgniter applications
  7. *
  8. * @package Bonfire
  9. * @author Bonfire Dev Team
  10. * @copyright Copyright (c) 2011 - 2014, Bonfire Dev Team
  11. * @license http://opensource.org/licenses/MIT
  12. * @link http://cibonfire.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. /**
  17. * Manage the database migrations in Bonfire.
  18. *
  19. * @package Bonfire\Modules\Migrations\Controllers\Developer
  20. * @author Bonfire Dev Team
  21. * @link http://cibonfire.com/docs/migrations
  22. */
  23. class Developer extends Admin_Controller
  24. {
  25. /**
  26. * Setup the permissions and load the language file
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. $this->auth->restrict('Site.Developer.View');
  34. $this->auth->restrict('Bonfire.Database.Manage');
  35. // Load the database lang file because Migrations is on the database subnav
  36. $this->lang->load('database/database');
  37. $this->lang->load('migrations');
  38. $this->load->library('Migrations');
  39. Assets::add_module_css('migrations', 'migrations');
  40. Template::set_block('sub_nav', 'database/developer/_sub_nav');
  41. }
  42. /**
  43. * Display the list of migrations available at core, application, and module
  44. * level
  45. *
  46. * @return void
  47. */
  48. public function index()
  49. {
  50. if (isset($_POST['migrate'])) {
  51. $core = $this->input->post('core_only') ? '' : Migrations::APP_MIGRATION_PREFIX;
  52. if ($version = $this->input->post('migration')) {
  53. $this->migrate_to($version, $core);
  54. }
  55. }
  56. Template::set('installed_version', $this->migrations->getVersion(Migrations::APP_MIGRATION_PREFIX));
  57. Template::set('latest_version', $this->migrations->getVersion(Migrations::APP_MIGRATION_PREFIX, true));
  58. Template::set('core_installed_version', $this->migrations->getVersion(Migrations::CORE_MIGRATIONS));
  59. Template::set('core_latest_version', $this->migrations->getVersion(Migrations::CORE_MIGRATIONS, true));
  60. Template::set('core_migrations', $this->migrations->getAvailableVersions());
  61. Template::set('app_migrations', $this->migrations->getAvailableVersions(Migrations::APP_MIGRATION_PREFIX));
  62. Template::set('mod_migrations', $this->get_module_versions());
  63. Template::set('toolbar_title', lang('migrations_title_index'));
  64. Template::render();
  65. }
  66. /**
  67. * Migrate the selected migration type to a specific migration number
  68. *
  69. * @param int $version The version number to migrate to
  70. * @param string $type The migration type (core, app_, MODULE_)
  71. *
  72. * @return void
  73. */
  74. private function migrate_to($version, $type)
  75. {
  76. $result = $this->migrations->version($version, $type);
  77. $errorMessage = $this->migrations->getErrorMessage();
  78. if ($result !== false && strlen($errorMessage) == 0) {
  79. if ($result === 0) {
  80. Template::set_message(lang('migrations_uninstall_success'), 'success');
  81. log_activity(
  82. $this->auth->user_id(),
  83. sprintf(lang('migrations_act_uninstall_success'), $type, $version, $this->input->ip_address()),
  84. 'migrations'
  85. );
  86. } else {
  87. Template::set_message(sprintf(lang('migrations_migrate_success'), $result), 'success');
  88. log_activity(
  89. $this->auth->user_id(),
  90. sprintf(lang('migrations_act_migrate_success'), empty($type) ? 'core' : $type, $version, $this->input->ip_address()),
  91. 'migrations'
  92. );
  93. }
  94. } else {
  95. log_message(lang('migrations_migrate_error') . "\n{$errorMessage}", 'error');
  96. Template::set_message(lang('migrations_migrate_error') . "<br />{$errorMessage}", 'error');
  97. }
  98. }
  99. /**
  100. * Migrate a module to a particular version
  101. *
  102. * @return void
  103. */
  104. public function migrate_module($module = '')
  105. {
  106. if (isset($_POST['migrate'])) {
  107. $file = $this->input->post('version');
  108. if (empty($file)) {
  109. Template::set_message(lang('migrations_module_none'), 'info');
  110. redirect(SITE_AREA . '/developer/migrations');
  111. }
  112. $version = $file !== 'uninstall' ? (int) substr($file, 0, 3) : 0;
  113. // Do the migration
  114. $this->migrate_to($version, "{$module}_");
  115. log_activity(
  116. $this->auth->user_id(),
  117. sprintf(lang('migrations_act_module'), $module, $version, $this->input->ip_address()),
  118. 'migrations'
  119. );
  120. }
  121. redirect(SITE_AREA . '/developer/migrations');
  122. }
  123. /**
  124. * Get all versions available for the modules
  125. *
  126. * @return array Array of available versions for each module
  127. */
  128. private function get_module_versions()
  129. {
  130. $modules = Modules::files(null, 'migrations');
  131. if ($modules === false) {
  132. return false;
  133. }
  134. // Sort modules by key (module directory name)
  135. ksort($modules);
  136. // Get the installed version of all of the modules (modules which have
  137. // not been installed will not be included)
  138. $installedVersions = $this->migrations->getModuleVersions();
  139. $modVersions = array();
  140. // Add the migration data for each module
  141. foreach ($modules as $module => &$mod) {
  142. if (! array_key_exists('migrations', $mod)) {
  143. continue;
  144. }
  145. // Sort module migrations in reverse order
  146. arsort($mod['migrations']);
  147. /**
  148. * @internal Calculating the latest version from the migration list
  149. * saves ~20% of the load time when a lot of modules (tested with >
  150. * 50) are listed. However, it requires the controller to know more
  151. * about the format of the migration filenames than may be desirable.
  152. * If that is the case, the 'latest_version' key below can be
  153. * populated with the result of:
  154. * $this->migrations->getVersion("{$module}_", true)
  155. */
  156. // Add the installed version, latest version, and list of migrations
  157. $modVersions[$module] = array(
  158. 'installed_version' => isset($installedVersions["{$module}_"]) ? $installedVersions["{$module}_"] : 0,
  159. 'latest_version' => intval(substr(current($mod['migrations']), 0, 3), 10),
  160. 'migrations' => $mod['migrations'],
  161. );
  162. }
  163. return $modVersions;
  164. }
  165. }
  166. /* end /migrations/controllers/developer.php */