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