DoctrineStep.php 2.9 KB

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