AnnotationDriver.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. * Copyright 2010 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SecurityExtraBundle\Metadata\Driver;
  18. use Doctrine\Common\Annotations\Reader;
  19. use JMS\SecurityExtraBundle\Annotation\RunAs;
  20. use JMS\SecurityExtraBundle\Annotation\SatisfiesParentSecurityPolicy;
  21. use JMS\SecurityExtraBundle\Annotation\Secure;
  22. use JMS\SecurityExtraBundle\Annotation\SecureParam;
  23. use JMS\SecurityExtraBundle\Annotation\SecureReturn;
  24. use JMS\SecurityExtraBundle\Metadata\ClassMetadata;
  25. use JMS\SecurityExtraBundle\Metadata\MethodMetadata;
  26. use Metadata\Driver\DriverInterface;
  27. use \ReflectionClass;
  28. use \ReflectionMethod;
  29. /**
  30. * Loads security annotations and converts them to metadata
  31. *
  32. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  33. */
  34. class AnnotationDriver implements DriverInterface
  35. {
  36. private $reader;
  37. private $converter;
  38. public function __construct(Reader $reader)
  39. {
  40. $this->reader = $reader;
  41. $this->converter = new AnnotationConverter();
  42. }
  43. public function loadMetadataForClass(ReflectionClass $reflection)
  44. {
  45. $metadata = new ClassMetadata($reflection->getName());
  46. foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED) as $method) {
  47. // check if the method was defined on this class
  48. if ($method->getDeclaringClass()->getName() !== $reflection->getName()) {
  49. continue;
  50. }
  51. $annotations = $this->reader->getMethodAnnotations($method);
  52. if ($annotations && null !== $methodMetadata = $this->converter->convertMethodAnnotations($method, $annotations)) {
  53. $metadata->addMethodMetadata($methodMetadata);
  54. }
  55. }
  56. return $metadata;
  57. }
  58. }