DoctrineStep.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\DistributionBundle\Configurator\Step;
  11. use Sensio\Bundle\DistributionBundle\Configurator\Form\DoctrineStepType;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14. * Doctrine Step.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class DoctrineStep implements StepInterface
  19. {
  20. /**
  21. * @Assert\Choice(callback="getDriverKeys")
  22. */
  23. public $driver;
  24. /**
  25. * @Assert\NotBlank
  26. */
  27. public $host;
  28. /**
  29. * @Assert\Min(0)
  30. */
  31. public $port;
  32. /**
  33. * @Assert\NotBlank
  34. */
  35. public $name;
  36. /**
  37. * @Assert\NotBlank
  38. */
  39. public $user;
  40. public $password;
  41. public function __construct(array $parameters)
  42. {
  43. foreach ($parameters as $key => $value) {
  44. if (0 === strpos($key, 'database_')) {
  45. $parameters[substr($key, 9)] = $value;
  46. $key = substr($key, 9);
  47. $this->$key = $value;
  48. }
  49. }
  50. }
  51. /**
  52. * @see StepInterface
  53. */
  54. public function getFormType()
  55. {
  56. return new DoctrineStepType();
  57. }
  58. /**
  59. * @see StepInterface
  60. */
  61. public function checkRequirements()
  62. {
  63. $messages = array();
  64. if (!class_exists('\PDO')) {
  65. $messages[] = 'PDO extension is mandatory.';
  66. } else {
  67. $drivers = \PDO::getAvailableDrivers();
  68. if (0 == count($drivers)) {
  69. $messages[] = 'Please install PDO drivers.';
  70. }
  71. }
  72. return $messages;
  73. }
  74. /**
  75. * @see StepInterface
  76. */
  77. public function checkOptionalSettings()
  78. {
  79. return array();
  80. }
  81. /**
  82. * @see StepInterface
  83. */
  84. public function update(StepInterface $data)
  85. {
  86. $parameters = array();
  87. foreach ($data as $key => $value) {
  88. $parameters['database_'.$key] = $value;
  89. }
  90. return $parameters;
  91. }
  92. /**
  93. * @see StepInterface
  94. */
  95. public function getTemplate()
  96. {
  97. return 'SensioDistributionBundle:Configurator/Step:doctrine.html.twig';
  98. }
  99. /**
  100. * @return array
  101. */
  102. static public function getDriverKeys()
  103. {
  104. return array_keys(static::getDrivers());
  105. }
  106. /**
  107. * @return array
  108. */
  109. static public function getDrivers()
  110. {
  111. return array(
  112. 'pdo_mysql' => 'MySQL (PDO)',
  113. 'pdo_sqlite' => 'SQLite (PDO)',
  114. 'pdo_pgsql' => 'PosgreSQL (PDO)',
  115. 'oci8' => 'Oracle (native)',
  116. 'ibm_db2' => 'IBM DB2 (native)',
  117. 'pdo_oci' => 'Oracle (PDO)',
  118. 'pdo_ibm' => 'IBM DB2 (PDO)',
  119. 'pdo_sqlsrv' => 'SQLServer (PDO)',
  120. );
  121. }
  122. }