RecursiveRegexFinder.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * A MigrationFinderInterface implementation that uses a RegexIterator along with a
  22. * RecursiveDirectoryIterator.
  23. *
  24. * @since 1.0.0-alpha3
  25. */
  26. final class RecursiveRegexFinder extends AbstractFinder implements MigrationDeepFinderInterface
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function findMigrations($directory, $namespace = null)
  32. {
  33. $dir = $this->getRealPath($directory);
  34. return $this->loadMigrations($this->getMatches($this->createIterator($dir)), $namespace);
  35. }
  36. /**
  37. * Create a recursive iterator to find all the migrations in the subdirectories.
  38. * @param $dir
  39. * @return \RegexIterator
  40. */
  41. private function createIterator($dir)
  42. {
  43. return new \RegexIterator(
  44. new \RecursiveIteratorIterator(
  45. new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
  46. \RecursiveIteratorIterator::LEAVES_ONLY
  47. ),
  48. $this->getPattern(),
  49. \RegexIterator::GET_MATCH
  50. );
  51. }
  52. private function getPattern()
  53. {
  54. return sprintf('#^.+\\%sVersion[^\\%s]{1,255}\\.php$#i', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
  55. }
  56. /**
  57. * Transform the recursiveIterator result array of array into the expected array of migration file
  58. * @param $iteratorFilesMatch
  59. * @return array
  60. */
  61. private function getMatches($iteratorFilesMatch)
  62. {
  63. $files = [];
  64. foreach ($iteratorFilesMatch as $file) {
  65. $files[] = $file[0];
  66. }
  67. return $files;
  68. }
  69. }