Printer.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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. public function endProduction()
  70. {
  71. $this->_indent--;
  72. $this->println(')');
  73. }
  74. /**
  75. * Prints text indented with spaces depending on current indentation level.
  76. *
  77. * @param string $str text
  78. */
  79. public function println($str)
  80. {
  81. if ( ! $this->_silent) {
  82. echo str_repeat(' ', $this->_indent), $str, "\n";
  83. }
  84. }
  85. }