/src/backend/PartKeepr/Setup/Setup.php

https://github.com/aslizdmr/PartKeepr · PHP · 176 lines · 143 code · 11 blank · 22 comment · 5 complexity · edd7237933593ab67681708dd41c13e5 MD5 · raw file

  1. <?php
  2. namespace PartKeepr\Setup;
  3. use PartKeepr\PartKeepr,
  4. PartKeepr\Util\Configuration as PartKeeprConfiguration;
  5. class Setup {
  6. /**
  7. * Specifies if setup runs in console mode.
  8. * @var boolean
  9. */
  10. private $console = false;
  11. /**
  12. * Defines if the setup runs in verbose mode.
  13. * @var boolean
  14. */
  15. private static $verbose = false;
  16. /**
  17. * Runs the migration with all steps
  18. */
  19. public function run () {
  20. $this->runStep("all");
  21. }
  22. /**
  23. * Sets console mode.
  24. *
  25. * In this mode, messages are directly written to the console.
  26. */
  27. public function setConsole () {
  28. $this->console = true;
  29. }
  30. /**
  31. * Runs a specific setup step, or all steps.
  32. *
  33. * @param string $step The step to execute
  34. * @throws \Exception
  35. */
  36. public function runStep ($step) {
  37. $entityManager = PartKeepr::getEM();
  38. $aSteps = array(
  39. "schema" => new SchemaSetup($entityManager),
  40. "adminuser" => new UserSetup($entityManager),
  41. "partunit" => new PartUnitSetup($entityManager),
  42. "footprint" => new FootprintSetup($entityManager),
  43. "partcategory" => new PartCategorySetup($entityManager),
  44. "siprefix" => new SiPrefixSetup($entityManager),
  45. "unit" => new UnitSetup($entityManager),
  46. "manufacturer" => new ManufacturerSetup($entityManager),
  47. "miscsettings" => new MiscSettingsSetup($entityManager)
  48. );
  49. $aActions = array(
  50. "configfile" => new ConfigFileSetup($entityManager)
  51. );
  52. if ($step == "all") {
  53. foreach ($aSteps as $step) {
  54. $step->setConsole($this->console);
  55. $step->run();
  56. }
  57. } else {
  58. if (array_key_exists($step, $aSteps)) {
  59. return $aSteps[$step]->run();
  60. }
  61. if (array_key_exists($step, $aActions)) {
  62. return $aActions[$step]->run();
  63. }
  64. throw new \Exception(sprintf("Setup step %s doesn't exist", $step));
  65. }
  66. }
  67. /**
  68. * Tests for APC. Throws an exception if APC is missing or not active.
  69. * @throws \Exception
  70. */
  71. public function testAPC () {
  72. if (!extension_loaded("apc")) {
  73. throw new \Exception(PartKeepr::i18n("The extension 'apc' is not loaded. Make sure that it is installed (see http://php.net/manual/en/apc.installation.php) and that it is enabled (set apc.enabled=1 in your php.ini)."));
  74. }
  75. }
  76. /**
  77. * Tests for suitable memory_limit settings
  78. * @todo stub
  79. */
  80. public function testMemoryLimit () {
  81. //echo ini_get("memory_limit");
  82. }
  83. /**
  84. * Sets the verbose flag
  85. * @param boolean $verbose True if verbose output is wanted, false otherwise
  86. */
  87. public static function setVerbose ($verbose) {
  88. Setup::$verbose = $verbose;
  89. }
  90. /**
  91. * Outputs a progress message.
  92. *
  93. * @param string $string The string to output
  94. * @param boolean $verbose True if the string should only be printed if verbosity is turned on
  95. */
  96. public static function progress ($string, $verbose = false) {
  97. if (!$verbose || ($verbose && Setup::$verbose)) {
  98. echo $string."\n";
  99. }
  100. }
  101. /**
  102. * Loads the given YAML file. Due to an API brach between Doctrine 2.0.5 and Doctrine 2.0.6,
  103. * we need to work it around.
  104. * @param string $file The path of the file to load
  105. * @return array The parsed YAML file
  106. */
  107. public static function loadYAML ($file) {
  108. return \Symfony\Component\Yaml\Yaml::parse($file);
  109. }
  110. /**
  111. * Sets the database configuration array from $_REQUEST
  112. */
  113. public static function setDatabaseConfigurationFromRequest () {
  114. if (isset($_REQUEST["dbname"])) {
  115. PartKeeprConfiguration::setOption("partkeepr.database.dbname", $_REQUEST["dbname"]);
  116. }
  117. if (isset($_REQUEST["user"])) {
  118. PartKeeprConfiguration::setOption("partkeepr.database.username", $_REQUEST["user"]);
  119. }
  120. if (isset($_REQUEST["password"])) {
  121. PartKeeprConfiguration::setOption("partkeepr.database.password", $_REQUEST["password"]);
  122. }
  123. if (isset($_REQUEST["host"])) {
  124. PartKeeprConfiguration::setOption("partkeepr.database.host", $_REQUEST["host"]);
  125. }
  126. if (isset($_REQUEST['port'])) {
  127. PartKeeprConfiguration::setOption("partkeepr.database.port", $_REQUEST["port"]);
  128. }
  129. switch ($_REQUEST["driver"]) {
  130. case "mysql":
  131. PartKeeprConfiguration::setOption("partkeepr.database.driver","pdo_mysql");
  132. break;
  133. case "pgsql":
  134. PartKeeprConfiguration::setOption("partkeepr.database.driver","pdo_pgsql");
  135. break;
  136. default:
  137. throw new \Exception(sprintf("Invalid driver %s specified.", $_REQUEST["driver"]));
  138. break;
  139. }
  140. }
  141. /**
  142. * Runs some checks for the CLI setup
  143. */
  144. public function runCLIChecks () {
  145. if (PartKeeprConfiguration::getOption("partkeepr.database.driver") == "pdo_mysql") {
  146. $dbname = PartKeeprConfiguration::getOption("partkeepr.database.dbname");
  147. if (!SchemaSetup::mysqlHasUTF8Encoding(PartKeepr::getEM()->getConnection(), $dbname )) {
  148. echo "Error: The database $dbname hasn't got the UTF-8 encoding. You need to set the database encoding to UTF-8. Aborting.\n";
  149. die;
  150. }
  151. }
  152. }
  153. }