DoctrineStep.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  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:NotBlank
  30. */
  31. public $name;
  32. /**
  33. * @assert:NotBlank
  34. */
  35. public $user;
  36. public $password;
  37. public function __construct(array $parameters)
  38. {
  39. foreach ($parameters as $key => $value) {
  40. if (0 === strpos($key, 'database_')) {
  41. $parameters[substr($key, 9)] = $value;
  42. $key = substr($key, 9);
  43. $this->$key = $value;
  44. }
  45. }
  46. }
  47. /**
  48. * @see StepInterface
  49. */
  50. public function getFormType()
  51. {
  52. return new DoctrineStepType();
  53. }
  54. /**
  55. * @see StepInterface
  56. */
  57. public function checkRequirements()
  58. {
  59. $messages = array();
  60. if (!class_exists('\PDO')) {
  61. $messages[] = 'PDO extension is mandatory.';
  62. } else {
  63. $drivers = \PDO::getAvailableDrivers();
  64. if (0 == count($drivers)) {
  65. $messages[] = 'Please install PDO drivers.';
  66. }
  67. }
  68. return $messages;
  69. }
  70. /**
  71. * @see StepInterface
  72. */
  73. public function checkOptionalSettings()
  74. {
  75. return array();
  76. }
  77. /**
  78. * @see StepInterface
  79. */
  80. public function update(StepInterface $data)
  81. {
  82. $parameters = array();
  83. foreach ($data as $key => $value) {
  84. $parameters['database_'.$key] = $value;
  85. }
  86. return $parameters;
  87. }
  88. /**
  89. * @see StepInterface
  90. */
  91. public function getTemplate()
  92. {
  93. return 'SymfonyWebConfiguratorBundle:Step:doctrine.html.twig';
  94. }
  95. /**
  96. * @return array
  97. */
  98. static public function getDriverKeys()
  99. {
  100. return array_keys(static::getDrivers());
  101. }
  102. /**
  103. * @return array
  104. */
  105. static public function getDrivers()
  106. {
  107. return array(
  108. 'pdo_mysql' => 'MySQL (PDO)',
  109. 'pdo_sqlite' => 'SQLite (PDO)',
  110. 'pdo_pgsql' => 'PosgreSQL (PDO)',
  111. 'oci8' => 'Oracle (native)',
  112. 'ibm_db2' => 'IBM DB2 (native)',
  113. 'pdo_oci' => 'Oracle (PDO)',
  114. 'pdo_ibm' => 'IBM DB2 (PDO)',
  115. 'pdo_sqlsrv' => 'SQLServer (PDO)',
  116. );
  117. }
  118. }