Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #295 from symfony-cmf/last_modification
Browse files Browse the repository at this point in the history
implement last modification date guesser
  • Loading branch information
dbu authored Jun 9, 2016
2 parents 806cb9d + d329569 commit 0fe196f
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
1.3.0 (unreleased)
------------------

* **2016-06-09**: Build and register LastModificationGuesser for PHPCR to use a last modification date on a sitemap.
* **2016-04-12**: Build and register DepthGuesser for PHPCR to use depths information for structure sitemap

1.2.0
Expand Down
8 changes: 6 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public function getConfigTreeBuilder()
->end()
// validation needs to be on top, when no values are set a validation inside the content_listener array node will not be triggered
->validate()
->ifTrue(function ($v) { return $v['content_listener']['enabled'] && empty($v['content_listener']['content_key']); })
->ifTrue(function ($v) {
return $v['content_listener']['enabled'] && empty($v['content_listener']['content_key']);
})
->thenInvalid('Configure the content_listener.content_key or disable the content_listener when not using the CmfRoutingBundle DynamicRouter.')
->end()
->children()
Expand Down Expand Up @@ -136,7 +138,9 @@ private function addSonataAdminSection(NodeBuilder $treeBuilder)
->arrayNode('sonata_admin_extension')
->addDefaultsIfNotSet()
->beforeNormalization()
->ifTrue(function ($v) { return is_scalar($v); })
->ifTrue(function ($v) {
return is_scalar($v);
})
->then(function ($v) {
return array('enabled' => $v);
})
Expand Down
8 changes: 8 additions & 0 deletions Resources/config/phpcr-sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@

<tag name="cmf_seo.sitemap.guesser" position="-2"/>
</service>

<service
id="cmf_seo.sitemap.phpcr.last_modified_guesser"
class="Symfony\Cmf\Bundle\SeoBundle\Sitemap\LastModifiedGuesser">
<argument type="service" id="doctrine_phpcr" />

<tag name="cmf_seo.sitemap.guesser" position="-2"/>
</service>
</services>
</container>
89 changes: 89 additions & 0 deletions Sitemap/LastModifiedGuesser.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testDocumentOrder()
$this->assertEquals(
array(
'/test/content/sitemap-aware',
'/test/content/sitemap-aware-last-mod-date',
'/test/content/sitemap-aware-non-publish',
'/test/content/sitemap-aware-publish',
),
Expand Down
16 changes: 16 additions & 0 deletions Tests/Resources/DataFixtures/Phpcr/LoadSitemapData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPCR\Util\NodeHelper;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
use Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SitemapAwareContent;
use Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SitemapAwareWithLastModifiedDateContent;
use Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SitemapAwareWithPublishWorkflowContent;

class LoadSitemapData implements FixtureInterface
Expand Down Expand Up @@ -79,6 +80,21 @@ public function load(ObjectManager $manager)
$route->setContent($publishedContent);
$manager->persist($route);

$lastModifiedContent = new SitemapAwareWithLastModifiedDateContent();
$lastModifiedContent
->setIsVisibleForSitemap(true)
->setLastModified(new \DateTime('2016-07-06', new \DateTimeZone('Europe/Berlin')))
->setTitle('Sitemap Aware Content last mod date')
->setName('sitemap-aware-last-mod-date')
->setParentDocument($contentRoot)
->setBody('Content for that is sitemap aware, that has last modified date.');
$manager->persist($lastModifiedContent);

$route = new Route();
$route->setPosition($routeRoot, 'sitemap-aware-last-mod-date');
$route->setContent($lastModifiedContent);
$manager->persist($route);

$manager->flush();
}
}
104 changes: 104 additions & 0 deletions Tests/Resources/Document/SitemapAwareWithLastModifiedDateContent.php
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;
}
}
4 changes: 3 additions & 1 deletion Tests/Unit/SeoPresentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ public function testSeoAwareWithoutCurrentMetadata()
$content
->expects($this->once())
->method('setSeoMetadata')
->with($this->callback(function ($c) { return $c instanceof SeoMetadataInterface; }))
->with($this->callback(function ($c) {
return $c instanceof SeoMetadataInterface;
}))
;

$this->seoPresentation->updateSeoPage($content);
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Sitemap/GuesserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ abstract class GuesserTestCase extends \PHPUnit_Framework_Testcase
/**
* @var GuesserInterface
*/
private $guesser;
protected $guesser;

/**
* @var object
*/
private $data;
protected $data;

public function setUp()
{
Expand Down
Loading

0 comments on commit 0fe196f

Please sign in to comment.