|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2016 Symfony CMF |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Cmf\Bundle\SeoBundle\Sitemap; |
| 13 | + |
| 14 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 15 | +use Doctrine\Common\Util\ClassUtils; |
| 16 | +use Doctrine\ODM\PHPCR\DocumentManager; |
| 17 | +use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; |
| 18 | +use Symfony\Cmf\Bundle\SeoBundle\Model\UrlInformation; |
| 19 | + |
| 20 | +/** |
| 21 | + * This guesser will add last modified date of an document to the url information, that can be rendered |
| 22 | + * to the sitemap. |
| 23 | + * |
| 24 | + * @author Maximilian Berghoff <[email protected]> |
| 25 | + */ |
| 26 | +class LastModifiedGuesser implements GuesserInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var ManagerRegistry |
| 30 | + */ |
| 31 | + private $managerRegistry; |
| 32 | + |
| 33 | + /** |
| 34 | + * LastModifiedGuesser constructor. |
| 35 | + * |
| 36 | + * @param ManagerRegistry $manager |
| 37 | + */ |
| 38 | + public function __construct(ManagerRegistry $manager) |
| 39 | + { |
| 40 | + $this->managerRegistry = $manager; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Updates UrlInformation with new values if they are not already set. |
| 45 | + * |
| 46 | + * @param UrlInformation $urlInformation The value object to update. |
| 47 | + * @param object $object The sitemap element to get values from. |
| 48 | + * @param string $sitemap Name of the sitemap being built. |
| 49 | + */ |
| 50 | + public function guessValues(UrlInformation $urlInformation, $object, $sitemap) |
| 51 | + { |
| 52 | + if (null !== $urlInformation->getLastModification()) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $className = ClassUtils::getRealClass(get_class($object)); |
| 57 | + $manager = $this->managerRegistry->getManagerForClass($className); |
| 58 | + if (!$manager instanceof DocumentManager) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + /** @var ClassMetadata $metadata */ |
| 63 | + $metadata = $manager->getClassMetadata($className); |
| 64 | + $mixins = $metadata->getMixins(); |
| 65 | + |
| 66 | + if (!in_array('mix:lastModified', $mixins)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $fieldName = $this->getFieldName($metadata); |
| 71 | + if (null === $fieldName) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $urlInformation->setLastModification($metadata->getFieldValue($object, $fieldName)); |
| 76 | + } |
| 77 | + |
| 78 | + private function getFieldName(ClassMetadata $metadata) |
| 79 | + { |
| 80 | + foreach ($metadata->getFieldNames() as $fieldName) { |
| 81 | + $field = $metadata->getField($fieldName); |
| 82 | + if ('jcr:lastModified' == $field['property']) { |
| 83 | + return $fieldName; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return; |
| 88 | + } |
| 89 | +} |
0 commit comments