Printer.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query;
  20. use function str_repeat;
  21. /**
  22. * A parse tree printer for Doctrine Query Language parser.
  23. *
  24. * @link http://www.phpdoctrine.org
  25. */
  26. class Printer
  27. {
  28. /**
  29. * Current indentation level
  30. *
  31. * @var int
  32. */
  33. protected $_indent = 0;
  34. /**
  35. * Defines whether parse tree is printed (default, false) or not (true).
  36. *
  37. * @var bool
  38. */
  39. protected $_silent;
  40. /**
  41. * Constructs a new parse tree printer.
  42. *
  43. * @param bool $silent Parse tree will not be printed if true.
  44. */
  45. public function __construct($silent = false)
  46. {
  47. $this->_silent = $silent;
  48. }
  49. /**
  50. * Prints an opening parenthesis followed by production name and increases
  51. * indentation level by one.
  52. *
  53. * This method is called before executing a production.
  54. *
  55. * @param string $name Production name.
  56. *
  57. * @return void
  58. */
  59. public function startProduction($name)
  60. {
  61. $this->println('(' . $name);
  62. $this->_indent++;
  63. }
  64. /**
  65. * Decreases indentation level by one and prints a closing parenthesis.
  66. *
  67. * This method is called after executing a production.
  68. *
  69. * @return void
  70. */
  71. public function endProduction()
  72. {
  73. $this->_indent--;
  74. $this->println(')');
  75. }
  76. /**
  77. * Prints text indented with spaces depending on current indentation level.
  78. *
  79. * @param string $str The text.
  80. *
  81. * @return void
  82. */
  83. public function println($str)
  84. {
  85. if (! $this->_silent) {
  86. echo str_repeat(' ', $this->_indent), $str, "\n";
  87. }
  88. }
  89. }