Printer.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. namespace Doctrine\ORM\Query;
  22. /**
  23. * A parse tree printer for Doctrine Query Language parser.
  24. *
  25. * @author Janne Vanhala <jpvanhal@cc.hut.fi>
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link http://www.phpdoctrine.org
  28. * @since 2.0
  29. * @version $Revision$
  30. */
  31. class Printer
  32. {
  33. /**
  34. * Current indentation level
  35. *
  36. * @var int
  37. */
  38. protected $_indent = 0;
  39. /**
  40. * Defines whether parse tree is printed (default, false) or not (true).
  41. *
  42. * @var bool
  43. */
  44. protected $_silent;
  45. /**
  46. * Constructs a new parse tree printer.
  47. *
  48. * @param bool $silent Parse tree will not be printed if true.
  49. */
  50. public function __construct($silent = false)
  51. {
  52. $this->_silent = $silent;
  53. }
  54. /**
  55. * Prints an opening parenthesis followed by production name and increases
  56. * indentation level by one.
  57. *
  58. * This method is called before executing a production.
  59. *
  60. * @param string $name production name
  61. */
  62. public function startProduction($name)
  63. {
  64. $this->println('(' . $name);
  65. $this->_indent++;
  66. }
  67. /**
  68. * Decreases indentation level by one and prints a closing parenthesis.
  69. *
  70. * This method is called after executing a production.
  71. */
  72. public function endProduction()
  73. {
  74. $this->_indent--;
  75. $this->println(')');
  76. }
  77. /**
  78. * Prints text indented with spaces depending on current indentation level.
  79. *
  80. * @param string $str text
  81. */
  82. public function println($str)
  83. {
  84. if ( ! $this->_silent) {
  85. echo str_repeat(' ', $this->_indent), $str, "\n";
  86. }
  87. }
  88. }