AbstractFileConfiguration.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * Load the information from the passed configuration file
  46. *
  47. * @param string $file The path to the configuration file
  48. *
  49. * @return void
  50. *
  51. * @throws MigrationException Throws exception if configuration file was already loaded
  52. */
  53. public function load($file)
  54. {
  55. if ($this->loaded) {
  56. throw MigrationException::configurationFileAlreadyLoaded();
  57. }
  58. if (file_exists($path = getcwd() . '/' . $file)) {
  59. $file = $path;
  60. }
  61. $this->file = $file;
  62. $this->doLoad($file);
  63. $this->loaded = true;
  64. }
  65. protected function getDirectoryRelativeToFile($file, $input)
  66. {
  67. $path = realpath(dirname($file) . '/' . $input);
  68. if ($path !== false) {
  69. $directory = $path;
  70. } else {
  71. $directory = $input;
  72. }
  73. return $directory;
  74. }
  75. public function getFile()
  76. {
  77. return $this->file;
  78. }
  79. /**
  80. * Abstract method that each file configuration driver must implement to
  81. * load the given configuration file whether it be xml, yaml, etc. or something
  82. * else.
  83. *
  84. * @param string $file The path to a configuration file.
  85. */
  86. abstract protected function doLoad($file);
  87. }