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

Commit 0fe196f

Browse files
authored
Merge pull request #295 from symfony-cmf/last_modification
implement last modification date guesser
2 parents 806cb9d + d329569 commit 0fe196f

File tree

13 files changed

+369
-7
lines changed

13 files changed

+369
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44
1.3.0 (unreleased)
55
------------------
66

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

910
1.2.0

DependencyInjection/Configuration.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public function getConfigTreeBuilder()
7171
->end()
7272
// validation needs to be on top, when no values are set a validation inside the content_listener array node will not be triggered
7373
->validate()
74-
->ifTrue(function ($v) { return $v['content_listener']['enabled'] && empty($v['content_listener']['content_key']); })
74+
->ifTrue(function ($v) {
75+
return $v['content_listener']['enabled'] && empty($v['content_listener']['content_key']);
76+
})
7577
->thenInvalid('Configure the content_listener.content_key or disable the content_listener when not using the CmfRoutingBundle DynamicRouter.')
7678
->end()
7779
->children()
@@ -136,7 +138,9 @@ private function addSonataAdminSection(NodeBuilder $treeBuilder)
136138
->arrayNode('sonata_admin_extension')
137139
->addDefaultsIfNotSet()
138140
->beforeNormalization()
139-
->ifTrue(function ($v) { return is_scalar($v); })
141+
->ifTrue(function ($v) {
142+
return is_scalar($v);
143+
})
140144
->then(function ($v) {
141145
return array('enabled' => $v);
142146
})

Resources/config/phpcr-sitemap.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,13 @@
2121

2222
<tag name="cmf_seo.sitemap.guesser" position="-2"/>
2323
</service>
24+
25+
<service
26+
id="cmf_seo.sitemap.phpcr.last_modified_guesser"
27+
class="Symfony\Cmf\Bundle\SeoBundle\Sitemap\LastModifiedGuesser">
28+
<argument type="service" id="doctrine_phpcr" />
29+
30+
<tag name="cmf_seo.sitemap.guesser" position="-2"/>
31+
</service>
2432
</services>
2533
</container>

Sitemap/LastModifiedGuesser.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

Tests/Functional/Doctrine/Phpcr/SitemapDocumentProviderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testDocumentOrder()
4848
$this->assertEquals(
4949
array(
5050
'/test/content/sitemap-aware',
51+
'/test/content/sitemap-aware-last-mod-date',
5152
'/test/content/sitemap-aware-non-publish',
5253
'/test/content/sitemap-aware-publish',
5354
),

Tests/Resources/DataFixtures/Phpcr/LoadSitemapData.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPCR\Util\NodeHelper;
1717
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
1818
use Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SitemapAwareContent;
19+
use Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SitemapAwareWithLastModifiedDateContent;
1920
use Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SitemapAwareWithPublishWorkflowContent;
2021

2122
class LoadSitemapData implements FixtureInterface
@@ -79,6 +80,21 @@ public function load(ObjectManager $manager)
7980
$route->setContent($publishedContent);
8081
$manager->persist($route);
8182

83+
$lastModifiedContent = new SitemapAwareWithLastModifiedDateContent();
84+
$lastModifiedContent
85+
->setIsVisibleForSitemap(true)
86+
->setLastModified(new \DateTime('2016-07-06', new \DateTimeZone('Europe/Berlin')))
87+
->setTitle('Sitemap Aware Content last mod date')
88+
->setName('sitemap-aware-last-mod-date')
89+
->setParentDocument($contentRoot)
90+
->setBody('Content for that is sitemap aware, that has last modified date.');
91+
$manager->persist($lastModifiedContent);
92+
93+
$route = new Route();
94+
$route->setPosition($routeRoot, 'sitemap-aware-last-mod-date');
95+
$route->setContent($lastModifiedContent);
96+
$manager->persist($route);
97+
8298
$manager->flush();
8399
}
84100
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Tests\Resources\Document;
13+
14+
use Doctrine\Common\Collections\ArrayCollection;
15+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
16+
use Symfony\Cmf\Bundle\SeoBundle\SitemapAwareInterface;
17+
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
18+
use Symfony\Component\Routing\Route;
19+
20+
/**
21+
* @PHPCRODM\Document(referenceable=true, mixins={"mix:lastModified"})
22+
*
23+
* @author Maximilian Berghoff <[email protected]>
24+
*/
25+
class SitemapAwareWithLastModifiedDateContent extends ContentBase implements RouteReferrersReadInterface, SitemapAwareInterface
26+
{
27+
/**
28+
* @var ArrayCollection|Route[]
29+
*
30+
* @PHPCRODM\Referrers(
31+
* referringDocument="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route",
32+
* referencedBy="content"
33+
* )
34+
*/
35+
protected $routes;
36+
37+
/**
38+
* @var bool
39+
*
40+
* @PHPCRODM\Field(type="boolean",property="visible_for_sitemap")
41+
*/
42+
private $isVisibleForSitemap;
43+
44+
/**
45+
* @var \DateTime
46+
* @PHPCRODM\Field(type="date", property="jcr:lastModified")
47+
*/
48+
private $lastModified;
49+
50+
public function __construct()
51+
{
52+
$this->routes = new ArrayCollection();
53+
}
54+
55+
/**
56+
* @param string $sitemap
57+
*
58+
* @return bool
59+
*/
60+
public function isVisibleInSitemap($sitemap)
61+
{
62+
return $this->isVisibleForSitemap;
63+
}
64+
65+
/**
66+
* @param bool $isVisibleForSitemap
67+
*
68+
* @return SitemapAwareContent
69+
*/
70+
public function setIsVisibleForSitemap($isVisibleForSitemap)
71+
{
72+
$this->isVisibleForSitemap = $isVisibleForSitemap;
73+
74+
return $this;
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function getRoutes()
81+
{
82+
return $this->routes;
83+
}
84+
85+
/**
86+
* @return \DateTime
87+
*/
88+
public function getLastModified()
89+
{
90+
return $this->lastModified;
91+
}
92+
93+
/**
94+
* @param \DateTime $lastModified
95+
*
96+
* @return SitemapAwareWithLastModifiedDateContent
97+
*/
98+
public function setLastModified($lastModified)
99+
{
100+
$this->lastModified = $lastModified;
101+
102+
return $this;
103+
}
104+
}

Tests/Unit/SeoPresentationTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ public function testSeoAwareWithoutCurrentMetadata()
414414
$content
415415
->expects($this->once())
416416
->method('setSeoMetadata')
417-
->with($this->callback(function ($c) { return $c instanceof SeoMetadataInterface; }))
417+
->with($this->callback(function ($c) {
418+
return $c instanceof SeoMetadataInterface;
419+
}))
418420
;
419421

420422
$this->seoPresentation->updateSeoPage($content);

Tests/Unit/Sitemap/GuesserTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ abstract class GuesserTestCase extends \PHPUnit_Framework_Testcase
2222
/**
2323
* @var GuesserInterface
2424
*/
25-
private $guesser;
25+
protected $guesser;
2626

2727
/**
2828
* @var object
2929
*/
30-
private $data;
30+
protected $data;
3131

3232
public function setUp()
3333
{

0 commit comments

Comments
 (0)