This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from symfony-cmf/last_modification
implement last modification date guesser
- Loading branch information
Showing
13 changed files
with
369 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\SeoBundle\Sitemap; | ||
|
||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\Common\Util\ClassUtils; | ||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; | ||
use Symfony\Cmf\Bundle\SeoBundle\Model\UrlInformation; | ||
|
||
/** | ||
* This guesser will add last modified date of an document to the url information, that can be rendered | ||
* to the sitemap. | ||
* | ||
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de> | ||
*/ | ||
class LastModifiedGuesser implements GuesserInterface | ||
{ | ||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
private $managerRegistry; | ||
|
||
/** | ||
* LastModifiedGuesser constructor. | ||
* | ||
* @param ManagerRegistry $manager | ||
*/ | ||
public function __construct(ManagerRegistry $manager) | ||
{ | ||
$this->managerRegistry = $manager; | ||
} | ||
|
||
/** | ||
* Updates UrlInformation with new values if they are not already set. | ||
* | ||
* @param UrlInformation $urlInformation The value object to update. | ||
* @param object $object The sitemap element to get values from. | ||
* @param string $sitemap Name of the sitemap being built. | ||
*/ | ||
public function guessValues(UrlInformation $urlInformation, $object, $sitemap) | ||
{ | ||
if (null !== $urlInformation->getLastModification()) { | ||
return; | ||
} | ||
|
||
$className = ClassUtils::getRealClass(get_class($object)); | ||
$manager = $this->managerRegistry->getManagerForClass($className); | ||
if (!$manager instanceof DocumentManager) { | ||
return; | ||
} | ||
|
||
/** @var ClassMetadata $metadata */ | ||
$metadata = $manager->getClassMetadata($className); | ||
$mixins = $metadata->getMixins(); | ||
|
||
if (!in_array('mix:lastModified', $mixins)) { | ||
return; | ||
} | ||
|
||
$fieldName = $this->getFieldName($metadata); | ||
if (null === $fieldName) { | ||
return; | ||
} | ||
|
||
$urlInformation->setLastModification($metadata->getFieldValue($object, $fieldName)); | ||
} | ||
|
||
private function getFieldName(ClassMetadata $metadata) | ||
{ | ||
foreach ($metadata->getFieldNames() as $fieldName) { | ||
$field = $metadata->getField($fieldName); | ||
if ('jcr:lastModified' == $field['property']) { | ||
return $fieldName; | ||
} | ||
} | ||
|
||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Tests/Resources/Document/SitemapAwareWithLastModifiedDateContent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2016 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; | ||
use Symfony\Cmf\Bundle\SeoBundle\SitemapAwareInterface; | ||
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface; | ||
use Symfony\Component\Routing\Route; | ||
|
||
/** | ||
* @PHPCRODM\Document(referenceable=true, mixins={"mix:lastModified"}) | ||
* | ||
* @author Maximilian Berghoff <Maximilian.Berghoff@mayflower.de> | ||
*/ | ||
class SitemapAwareWithLastModifiedDateContent extends ContentBase implements RouteReferrersReadInterface, SitemapAwareInterface | ||
{ | ||
/** | ||
* @var ArrayCollection|Route[] | ||
* | ||
* @PHPCRODM\Referrers( | ||
* referringDocument="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route", | ||
* referencedBy="content" | ||
* ) | ||
*/ | ||
protected $routes; | ||
|
||
/** | ||
* @var bool | ||
* | ||
* @PHPCRODM\Field(type="boolean",property="visible_for_sitemap") | ||
*/ | ||
private $isVisibleForSitemap; | ||
|
||
/** | ||
* @var \DateTime | ||
* @PHPCRODM\Field(type="date", property="jcr:lastModified") | ||
*/ | ||
private $lastModified; | ||
|
||
public function __construct() | ||
{ | ||
$this->routes = new ArrayCollection(); | ||
} | ||
|
||
/** | ||
* @param string $sitemap | ||
* | ||
* @return bool | ||
*/ | ||
public function isVisibleInSitemap($sitemap) | ||
{ | ||
return $this->isVisibleForSitemap; | ||
} | ||
|
||
/** | ||
* @param bool $isVisibleForSitemap | ||
* | ||
* @return SitemapAwareContent | ||
*/ | ||
public function setIsVisibleForSitemap($isVisibleForSitemap) | ||
{ | ||
$this->isVisibleForSitemap = $isVisibleForSitemap; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRoutes() | ||
{ | ||
return $this->routes; | ||
} | ||
|
||
/** | ||
* @return \DateTime | ||
*/ | ||
public function getLastModified() | ||
{ | ||
return $this->lastModified; | ||
} | ||
|
||
/** | ||
* @param \DateTime $lastModified | ||
* | ||
* @return SitemapAwareWithLastModifiedDateContent | ||
*/ | ||
public function setLastModified($lastModified) | ||
{ | ||
$this->lastModified = $lastModified; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.