|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Cmf\Bundle\SeoBundle\Loader; |
| 4 | + |
| 5 | +use Doctrine\Common\Annotations\Reader; |
| 6 | +use Symfony\Cmf\Bundle\SeoBundle\Loader\Annotation\MetaDescription; |
| 7 | +use Symfony\Cmf\Bundle\SeoBundle\Loader\Annotation\MetaKeywords; |
| 8 | +use Symfony\Cmf\Bundle\SeoBundle\Loader\Annotation\SeoMetadataAnnotation; |
| 9 | +use Symfony\Cmf\Bundle\SeoBundle\Loader\Annotation\Title; |
| 10 | +use Symfony\Cmf\Bundle\SeoBundle\Model\SeoMetadataInterface; |
| 11 | +use Symfony\Component\Config\Loader\Loader; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author Wouter de Jong <[email protected]> |
| 15 | + */ |
| 16 | +class AnnotationLoader extends Loader |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var Reader |
| 20 | + */ |
| 21 | + private $reader; |
| 22 | + |
| 23 | + public function __construct(Reader $reader) |
| 24 | + { |
| 25 | + $this->reader = $reader; |
| 26 | + } |
| 27 | + |
| 28 | + public function supports($resource, $type = null) |
| 29 | + { |
| 30 | + return is_object($resource) && $this->isAnnotated($resource); |
| 31 | + } |
| 32 | + |
| 33 | + public function load($content, $type = null) |
| 34 | + { |
| 35 | + $seoMetadata = SeoMetadataFactory::initializeSeoMetadata($content); |
| 36 | + |
| 37 | + $data = $this->getAnnotationsFromContent($content); |
| 38 | + |
| 39 | + // todo cache $data |
| 40 | + |
| 41 | + $classReflection = new \ReflectionClass($content); |
| 42 | + foreach ($data['properties'] as $propertyName => $annotations) { |
| 43 | + /** @var SeoMetadataAnnotation $annotation */ |
| 44 | + foreach ($annotations as $annotation) { |
| 45 | + $property = $classReflection->getProperty($propertyName); |
| 46 | + $property->setAccessible(true); |
| 47 | + |
| 48 | + $annotation->configureSeoMetadata($seoMetadata, $property->getValue($content)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + foreach ($data['methods'] as $methodName => $annotations) { |
| 53 | + /** @var SeoMetadataAnnotation $annotation */ |
| 54 | + foreach ($annotations as $annotation) { |
| 55 | + $annotation->configureSeoMetadata($seoMetadata, $content->{$methodName}()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return $seoMetadata; |
| 60 | + } |
| 61 | + |
| 62 | + private function getAnnotationsFromContent($content) |
| 63 | + { |
| 64 | + $classReflection = new \ReflectionClass($content); |
| 65 | + |
| 66 | + return [ |
| 67 | + 'properties' => $this->readProperties($classReflection->getProperties()), |
| 68 | + 'methods' => $this->readMethods($classReflection->getMethods()), |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param \ReflectionProperty[] $properties |
| 74 | + * |
| 75 | + * @return SeoMetadataAnnotation[][] |
| 76 | + */ |
| 77 | + private function readProperties(array $properties) |
| 78 | + { |
| 79 | + $propertyAnnotations = []; |
| 80 | + |
| 81 | + foreach ($properties as $reflectionProperty) { |
| 82 | + $annotations = $this->reader->getPropertyAnnotations($reflectionProperty); |
| 83 | + $propertyName = $reflectionProperty->getName(); |
| 84 | + |
| 85 | + foreach ($annotations as $annotation) { |
| 86 | + if ($annotation instanceof SeoMetadataAnnotation) { |
| 87 | + if (!isset($propertyAnnotations[$propertyName])) { |
| 88 | + $propertyAnnotations[$propertyName] = []; |
| 89 | + } |
| 90 | + |
| 91 | + $propertyAnnotations[$propertyName][] = $annotation; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return $propertyAnnotations; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param \ReflectionMethod[] $methods |
| 101 | + * |
| 102 | + * @return SeoMetadataAnnotation[][] |
| 103 | + */ |
| 104 | + private function readMethods(array $methods) |
| 105 | + { |
| 106 | + $methodAnnotations = []; |
| 107 | + |
| 108 | + foreach ($methods as $reflectionMethod) { |
| 109 | + $annotations = $this->reader->getMethodAnnotations($reflectionMethod); |
| 110 | + $methodName = $reflectionMethod->getName(); |
| 111 | + |
| 112 | + foreach ($annotations as $annotation) { |
| 113 | + if ($annotation instanceof SeoMetadataAnnotation) { |
| 114 | + if (!isset($methodAnnotations[$methodName])) { |
| 115 | + $methodAnnotations[$methodName] = []; |
| 116 | + } |
| 117 | + |
| 118 | + $methodAnnotations[$methodName][] = $annotation; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return $methodAnnotations; |
| 124 | + } |
| 125 | + |
| 126 | + private function isAnnotated($content) |
| 127 | + { |
| 128 | + return 0 !== count(call_user_func_array('array_merge', $this->getAnnotationsFromContent($content))); |
| 129 | + } |
| 130 | +} |
0 commit comments