AbstractFinder.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Finder;
  20. /**
  21. * Abstract base class for MigrationFinders
  22. *
  23. * @since 1.0.0-alpha3
  24. */
  25. abstract class AbstractFinder implements MigrationFinderInterface
  26. {
  27. protected static function requireOnce($path)
  28. {
  29. require_once $path;
  30. }
  31. protected function getRealPath($directory)
  32. {
  33. $dir = realpath($directory);
  34. if (false === $dir || !is_dir($dir)) {
  35. throw new \InvalidArgumentException(sprintf(
  36. 'Cannot load migrations from "%s" because it is not a valid directory',
  37. $directory
  38. ));
  39. }
  40. return $dir;
  41. }
  42. /**
  43. * Load the migrations and return an array of thoses loaded migrations
  44. * @param $files array of migration filename found
  45. * @param $namespace namespace of thoses migrations
  46. * @return array constructed with the migration name as key and the value is the fully qualified name of the migration
  47. */
  48. protected function loadMigrations($files, $namespace)
  49. {
  50. $migrations = [];
  51. uasort($files, $this->getFileSortCallback());
  52. foreach ($files as $file) {
  53. static::requireOnce($file);
  54. $className = basename($file, '.php');
  55. $version = (string) substr($className, 7);
  56. if ($version === '0') {
  57. throw new \InvalidArgumentException(sprintf(
  58. 'Cannot load a migrations with the name "%s" because it is a reserved number by doctrine migraitons' . PHP_EOL .
  59. 'It\'s used to revert all migrations including the first one.',
  60. $version
  61. ));
  62. }
  63. $migrations[$version] = sprintf('%s\\%s', $namespace, $className);
  64. }
  65. return $migrations;
  66. }
  67. /**
  68. * Return callable for files basename uasort
  69. *
  70. * @return callable
  71. */
  72. protected function getFileSortCallback(){
  73. return function ($a, $b) {
  74. return (basename($a) < basename($b)) ? -1 : 1;
  75. };
  76. }
  77. }