AbstractFileConfiguration.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Migrations\Configuration;
  20. use Doctrine\DBAL\Migrations\MigrationException;
  21. /**
  22. * Abstract Migration Configuration class for loading configuration information
  23. * from a configuration file (xml or yml).
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @author Jonathan H. Wage <jonwage@gmail.com>
  29. */
  30. abstract class AbstractFileConfiguration extends Configuration
  31. {
  32. /**
  33. * The configuration file used to load configuration information
  34. *
  35. * @var string
  36. */
  37. private $file;
  38. /**
  39. * Whether or not the configuration file has been loaded yet or not
  40. *
  41. * @var boolean
  42. */
  43. private $loaded = false;
  44. /**
  45. * @var array of possible configuration properties in migrations configuration.
  46. */
  47. private $configurationProperties = [
  48. 'migrations_namespace' => 'setMigrationsNamespace',
  49. 'table_name' => 'setMigrationsTableName',
  50. 'column_name' => 'setMigrationsColumnName',
  51. 'organize_migrations' => 'setMigrationOrganisation',
  52. 'name' => 'setName',
  53. 'migrations_directory' => 'loadMigrationsFromDirectory',
  54. 'migrations' => 'loadMigrations',
  55. ];
  56. protected function setConfiguration(array $config)
  57. {
  58. foreach($config as $configurationKey => $configurationValue) {
  59. if (!isset($this->configurationProperties[$configurationKey])) {
  60. $msg = sprintf('Migrations configuration key "%s" does not exists.', $configurationKey);
  61. throw MigrationException::configurationNotValid($msg);
  62. }
  63. }
  64. foreach($this->configurationProperties as $configurationKey => $configurationSetter) {
  65. if (isset($config[$configurationKey])) {
  66. $this->{$configurationSetter}($config[$configurationKey]);
  67. }
  68. }
  69. }
  70. private function loadMigrationsFromDirectory($migrationsDirectory)
  71. {
  72. $this->setMigrationsDirectory($migrationsDirectory);
  73. $this->registerMigrationsFromDirectory($migrationsDirectory);
  74. }
  75. private function loadMigrations($migrations)
  76. {
  77. if (is_array($migrations)) {
  78. foreach ($migrations as $migration) {
  79. $this->registerMigration($migration['version'], $migration['class']);
  80. }
  81. }
  82. }
  83. private function setMigrationOrganisation($migrationOrganisation)
  84. {
  85. if (strcasecmp($migrationOrganisation, static::VERSIONS_ORGANIZATION_BY_YEAR) == 0) {
  86. $this->setMigrationsAreOrganizedByYear();
  87. } else if (strcasecmp($migrationOrganisation, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) == 0) {
  88. $this->setMigrationsAreOrganizedByYearAndMonth();
  89. } else {
  90. $msg = 'Unknown ' . var_export($migrationOrganisation, true) . ' for configuration "organize_migrations".';
  91. throw MigrationException::configurationNotValid($msg);
  92. }
  93. }
  94. /**
  95. * Load the information from the passed configuration file
  96. *
  97. * @param string $file The path to the configuration file
  98. *
  99. * @throws MigrationException Throws exception if configuration file was already loaded
  100. */
  101. public function load($file)
  102. {
  103. if ($this->loaded) {
  104. throw MigrationException::configurationFileAlreadyLoaded();
  105. }
  106. if (file_exists($path = getcwd() . '/' . $file)) {
  107. $file = $path;
  108. }
  109. $this->file = $file;
  110. if (!file_exists($file)) {
  111. throw new \InvalidArgumentException('Given config file does not exist');
  112. }
  113. $this->doLoad($file);
  114. $this->loaded = true;
  115. }
  116. protected function getDirectoryRelativeToFile($file, $input)
  117. {
  118. $path = realpath(dirname($file) . '/' . $input);
  119. return ($path !== false) ? $path : $input;
  120. }
  121. public function getFile()
  122. {
  123. return $this->file;
  124. }
  125. /**
  126. * Abstract method that each file configuration driver must implement to
  127. * load the given configuration file whether it be xml, yaml, etc. or something
  128. * else.
  129. *
  130. * @param string $file The path to a configuration file.
  131. */
  132. abstract protected function doLoad($file);
  133. }