LimeLexerVariables.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Lime test framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Extracts all global variables from a source file.
  13. *
  14. * This lexer includes all global variables that are not inside annotations,
  15. * except variables from the scope of the annotations passed to the constructor,
  16. * which are included as well.
  17. *
  18. * @package Lime
  19. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  20. * @version SVN: $Id: LimeLexerVariables.php 23701 2009-11-08 21:23:40Z bschussek $
  21. */
  22. class LimeLexerVariables extends LimeLexerAnnotationAware
  23. {
  24. protected
  25. $includedAnnotations = array(),
  26. $variables = array();
  27. /**
  28. * Constructor.
  29. *
  30. * @param array $allowedAnnotations The list of allowed annotation names
  31. * @param array $includedAnnotations The list of annotation names whose
  32. * variables are considered global
  33. */
  34. public function __construct(array $allowedAnnotations = array(), array $includedAnnotations = array())
  35. {
  36. parent::__construct($allowedAnnotations);
  37. $this->includedAnnotations = $includedAnnotations;
  38. }
  39. /**
  40. * (non-PHPdoc)
  41. * @see LimeLexer#process($text, $id)
  42. */
  43. protected function process($text, $id)
  44. {
  45. if ($id == T_VARIABLE && !$this->inClass() && !$this->inFunction()
  46. && (!$this->inAnnotation() || in_array($this->getCurrentAnnotation(), $this->includedAnnotations)))
  47. {
  48. $this->variables[] = $text;
  49. }
  50. }
  51. /**
  52. * (non-PHPdoc)
  53. * @see LimeLexer#getResult()
  54. */
  55. protected function getResult()
  56. {
  57. return array_unique($this->variables);
  58. }
  59. }