菜谱项目

SimpleAnnotationReader.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Common\Annotations;
  20. /**
  21. * Simple Annotation Reader.
  22. *
  23. * This annotation reader is intended to be used in projects where you have
  24. * full-control over all annotations that are available.
  25. *
  26. * @since 2.2
  27. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  28. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  29. */
  30. class SimpleAnnotationReader implements Reader
  31. {
  32. /**
  33. * @var DocParser
  34. */
  35. private $parser;
  36. /**
  37. * Constructor.
  38. *
  39. * Initializes a new SimpleAnnotationReader.
  40. */
  41. public function __construct()
  42. {
  43. $this->parser = new DocParser();
  44. $this->parser->setIgnoreNotImportedAnnotations(true);
  45. }
  46. /**
  47. * Adds a namespace in which we will look for annotations.
  48. *
  49. * @param string $namespace
  50. *
  51. * @return void
  52. */
  53. public function addNamespace($namespace)
  54. {
  55. $this->parser->addNamespace($namespace);
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function getClassAnnotations(\ReflectionClass $class)
  61. {
  62. return $this->parser->parse($class->getDocComment(), 'class '.$class->getName());
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getMethodAnnotations(\ReflectionMethod $method)
  68. {
  69. return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()');
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getPropertyAnnotations(\ReflectionProperty $property)
  75. {
  76. return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName());
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  82. {
  83. foreach ($this->getClassAnnotations($class) as $annot) {
  84. if ($annot instanceof $annotationName) {
  85. return $annot;
  86. }
  87. }
  88. return null;
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  94. {
  95. foreach ($this->getMethodAnnotations($method) as $annot) {
  96. if ($annot instanceof $annotationName) {
  97. return $annot;
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  106. {
  107. foreach ($this->getPropertyAnnotations($property) as $annot) {
  108. if ($annot instanceof $annotationName) {
  109. return $annot;
  110. }
  111. }
  112. return null;
  113. }
  114. }