Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DependencyInjection/OpenSkySitemapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('sitemap.xml');

$defaults = $container->getParameter('opensky.sitemap.defaults');
$properties = array_merge(array('changefreq', 'priority', 'lastmod'), $container->getParameter('opensky.sitemap.properties'));
foreach ($configs as $config) {
foreach (array('changefreq', 'priority', 'lastmod') as $prop) {
foreach ($properties as $prop) {
if (isset($config['default_' . $prop])) {
$defaults[$prop] = $config['default_' . $prop];
}
Expand All @@ -53,4 +54,4 @@ public function getAlias()
{
return 'opensky_sitemap';
}
}
}
3 changes: 0 additions & 3 deletions Dumper/Doctrine/ODM/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class MongoDB implements Dumper
*/
public function dump(Sitemap $sitemap)
{
// remove the old ones first...
$this->getDocumentCollection($sitemap)->remove(array());

// now flush the new ones
$dm = $this->getDocumentManager($sitemap);
$dm->flush(null, array('safe' => true));
Expand Down
Binary file added Event/.SitemapEvent.php.swp
Binary file not shown.
25 changes: 25 additions & 0 deletions Event/SitemapEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace OpenSky\Bundle\SitemapBundle\Event;

use Symfony\Component\EventDispatcher\Event;

class SitemapEvent extends Event
{
protected $arguments;

public function __construct(array $arguments)
{
$this->arguments = $arguments;
}

public function get($argument)
{
return $this->arguments[$argument];
}

public function has($argument)
{
return isset($this->arguments[$argument]);
}
}
10 changes: 10 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OpenSky\Bundle\SitemapBundle;

final class Events
{
const OPENSKY_SITEMAP_CREATE = 'opensky.sitemap.create';
const OPENSKY_SITEMAP_UPDATE = 'opensky.sitemap.update';
const OPENSKY_SITEMAP_DELETE = 'opensky.sitemap.delete';
}
19 changes: 17 additions & 2 deletions Listener/SitemapListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function update(Event $event)
$lastmod = $url->getLastmod();

$url->setLastmod($time);
$url->setChangefreq($this->getChangefreq($time->diff(\DateTime::createFromFormat(Url::LASTMOD_FORMAT, $lastmod))));
$url->setChangefreq($this->getChangefreq($time->diff(\DateTime::createFromFormat(Url::LASTMOD_FORMAT, $lastmod->format(Url::LASTMOD_FORMAT)))));

$this->dump($this->sitemap);
}
Expand All @@ -66,6 +66,21 @@ public function create(Event $event)
$this->dump($this->sitemap);
}

public function delete(Event $event)
{
$urlLoc = $event->get('loc');

if (!$this->sitemap->has($urlLoc)) {
throw new Exception\OutOfBoundsException('Url ' . $urlLoc . ' could not be found.');
}

$url = $this->sitemap->get($urlLoc);

$this->sitemap->delete($url);

$this->dump($this->sitemap);
}

protected function getChangefreq(\DateInterval $interval)
{
return ($interval->y >= 1) ? Url::YEARLY : (($interval->m >= 1) ? Url::MONTHLY : (($interval->d >= 7) ? Url::WEEKLY : Url::DAILY));
Expand All @@ -76,4 +91,4 @@ protected function dump(Sitemap $sitemap)
$this->dumper->dump($sitemap);
}

}
}
9 changes: 6 additions & 3 deletions Resources/config/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
<parameter key="opensky.sitemap.template.engine">twig</parameter>
<parameter key="opensky.sitemap.defaults" type="collection" />
<parameter key="opensky.sitemap.providers" type="collection" />
<parameter key="opensky.sitemap.properties" type="collection" />
</parameters>

<services>
<service alias="opensky.sitemap.sitemap" id="opensky.sitemap" />
<service id="opensky.sitemap.sitemap" class="%opensky.sitemap.class%">
<argument type="service" id="opensky.sitemap.sitemap.storage" />
<argument>%opensky.sitemap.defaults%</argument>
<argument>%opensky.sitemap.properties%</argument>
<call method="setUrlClass">
<argument>%opensky.sitemap.url.class%</argument>
</call>
Expand All @@ -36,10 +38,11 @@
</argument>
</service>
<service id="opensky.sitemap.listener" class="%opensky.sitemap.listener.class%">
<tag name="kernel.listener" event="opensky.sitemap.update" method="update" />
<tag name="kernel.listener" event="opensky.sitemap.create" method="create" />
<tag name="kernel.event_listener" event="opensky.sitemap.update" method="update" />
<tag name="kernel.event_listener" event="opensky.sitemap.create" method="create" />
<tag name="kernel.event_listener" event="opensky.sitemap.delete" method="delete" />
<argument type="service" id="opensky.sitemap.sitemap" />
<argument type="service" id="opensky.sitemap.dumper" />
</service>
</services>
</container>
</container>
2 changes: 1 addition & 1 deletion Resources/views/Sitemap/sitemap.xml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
</url>
{% endfor %}
</urlset>
{% endspaceless %}
{% endspaceless %}
18 changes: 14 additions & 4 deletions Sitemap/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Sitemap
* @var array
*/
protected $defaults = array();
/**
* @var array
*/
protected $properties = array();
/**
* @var OpenSky\Bundle\SitemapBundle\Sitemap\Storage\Storage
*/
Expand All @@ -31,10 +35,11 @@ class Sitemap
* @param OpenSky\Bundle\SitemapBundle\Sitemap\Storage\Storage $storage
* @param array $defaults
*/
public function __construct(Storage $storage, array $defaults = array())
public function __construct(Storage $storage, array $defaults = array(), array $properties = array())
{
$this->storage = $storage;
foreach (array('changefreq', 'priority', 'lastmod') as $prop) {
$this->properties = array_merge(array('changefreq', 'priority', 'lastmod'), $properties);
foreach ($this->properties as $prop) {
if (isset($defaults[$prop])) {
$this->defaults[$prop] = $defaults[$prop];
}
Expand Down Expand Up @@ -68,7 +73,7 @@ public function add($loc, array $info)
$info = array_merge($this->defaults, $info);
$urlClass = $this->getUrlClass();
$url = new $urlClass($loc);
foreach (array('changefreq', 'priority', 'lastmod') as $prop) {
foreach ($this->properties as $prop) {
if (isset($info[$prop])) {
$url->{'set' . ucfirst($prop)}($info[$prop]);
}
Expand All @@ -77,6 +82,11 @@ public function add($loc, array $info)
return $url;
}

public function delete($url)
{
$this->storage->remove($url);
}

/**
* @param string $loc
* @return OpenSky\Bundle\SitemapBundle\Sitemap\Url|null
Expand Down Expand Up @@ -122,4 +132,4 @@ public function getStorage()
return $this->storage;
}

}
}
12 changes: 11 additions & 1 deletion Sitemap/Storage/Doctrine/ODM/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public function save(Url $url)
$this->dm->persist($url);
}

/**
* Removes a Url in DocumentManager, does not call flush
*
* @param OpenSky\Bundle\SitemapBundle\Sitemap\Url $url
*/
public function remove(Url $url)
{
$this->dm->remove($url);
}

/**
* @return Doctrine\ODM\MongoDB\DocumentManager
*/
Expand All @@ -102,4 +112,4 @@ private function getSkip($page)
{
return ((int) $page - 1) * self::PAGE_LIMIT;
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"require": {
"php": ">=5.3.0",
"symfony/framework-bundle": ">=2.1.0,<2.3-dev"
"symfony/framework-bundle": ">=2.1.0,<2.7-dev"
},
"autoload": {
"psr-0": { "OpenSky\\Bundle\\SitemapBundle": "" }
Expand Down