File.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Backup\Filesystem\Iterator;
  7. /**
  8. * File lines iterator
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class File extends \SplFileObject
  13. {
  14. /**
  15. * The statement that was last read during iteration
  16. *
  17. * @var string
  18. */
  19. protected $_currentStatement = '';
  20. /**
  21. * Return current sql statement
  22. *
  23. * @return string
  24. */
  25. public function current()
  26. {
  27. return $this->_currentStatement;
  28. }
  29. /**
  30. * Iterate to next sql statement in file
  31. *
  32. * @return void
  33. */
  34. public function next()
  35. {
  36. $this->_currentStatement = '';
  37. while (!$this->eof()) {
  38. $line = $this->fgets();
  39. if (strlen(trim($line))) {
  40. $this->_currentStatement .= $line;
  41. if ($this->_isLineLastInCommand($line)) {
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * Return to first statement
  49. *
  50. * @return void
  51. */
  52. public function rewind()
  53. {
  54. parent::rewind();
  55. $this->next();
  56. }
  57. /**
  58. * Check whether provided string is comment
  59. *
  60. * @param string $line
  61. * @return bool
  62. */
  63. protected function _isComment($line)
  64. {
  65. return $line[0] == '#' || substr($line, 0, 2) == '--';
  66. }
  67. /**
  68. * Check is line a last in sql command
  69. *
  70. * @param string $line
  71. * @return bool
  72. */
  73. protected function _isLineLastInCommand($line)
  74. {
  75. $cleanLine = trim($line);
  76. $lineLength = strlen($cleanLine);
  77. $returnResult = false;
  78. if ($lineLength > 0) {
  79. $lastSymbolIndex = $lineLength - 1;
  80. if ($cleanLine[$lastSymbolIndex] == ';') {
  81. $returnResult = true;
  82. }
  83. }
  84. return $returnResult;
  85. }
  86. }