diff --git a/DependencyInjection/OpenSkySitemapExtension.php b/DependencyInjection/OpenSkySitemapExtension.php index 849889d..aadce16 100644 --- a/DependencyInjection/OpenSkySitemapExtension.php +++ b/DependencyInjection/OpenSkySitemapExtension.php @@ -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]; } @@ -53,4 +54,4 @@ public function getAlias() { return 'opensky_sitemap'; } -} \ No newline at end of file +} diff --git a/Dumper/Doctrine/ODM/MongoDB.php b/Dumper/Doctrine/ODM/MongoDB.php index b247e8f..6374ec4 100644 --- a/Dumper/Doctrine/ODM/MongoDB.php +++ b/Dumper/Doctrine/ODM/MongoDB.php @@ -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)); diff --git a/Event/.SitemapEvent.php.swp b/Event/.SitemapEvent.php.swp new file mode 100644 index 0000000..5316e60 Binary files /dev/null and b/Event/.SitemapEvent.php.swp differ diff --git a/Event/SitemapEvent.php b/Event/SitemapEvent.php new file mode 100644 index 0000000..7faadfe --- /dev/null +++ b/Event/SitemapEvent.php @@ -0,0 +1,25 @@ +arguments = $arguments; + } + + public function get($argument) + { + return $this->arguments[$argument]; + } + + public function has($argument) + { + return isset($this->arguments[$argument]); + } +} diff --git a/Events.php b/Events.php new file mode 100644 index 0000000..ae7830f --- /dev/null +++ b/Events.php @@ -0,0 +1,10 @@ +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); } @@ -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)); @@ -76,4 +91,4 @@ protected function dump(Sitemap $sitemap) $this->dumper->dump($sitemap); } -} \ No newline at end of file +} diff --git a/Resources/config/sitemap.xml b/Resources/config/sitemap.xml index caf01e0..396a57a 100644 --- a/Resources/config/sitemap.xml +++ b/Resources/config/sitemap.xml @@ -15,6 +15,7 @@ twig + @@ -22,6 +23,7 @@ %opensky.sitemap.defaults% + %opensky.sitemap.properties% %opensky.sitemap.url.class% @@ -36,10 +38,11 @@ - - + + + - \ No newline at end of file + diff --git a/Resources/views/Sitemap/sitemap.xml.twig b/Resources/views/Sitemap/sitemap.xml.twig index aba407c..89442f3 100644 --- a/Resources/views/Sitemap/sitemap.xml.twig +++ b/Resources/views/Sitemap/sitemap.xml.twig @@ -10,4 +10,4 @@ {% endfor %} -{% endspaceless %} \ No newline at end of file +{% endspaceless %} diff --git a/Sitemap/Sitemap.php b/Sitemap/Sitemap.php index f901d0d..d5e8431 100644 --- a/Sitemap/Sitemap.php +++ b/Sitemap/Sitemap.php @@ -22,6 +22,10 @@ class Sitemap * @var array */ protected $defaults = array(); + /** + * @var array + */ + protected $properties = array(); /** * @var OpenSky\Bundle\SitemapBundle\Sitemap\Storage\Storage */ @@ -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]; } @@ -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]); } @@ -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 @@ -122,4 +132,4 @@ public function getStorage() return $this->storage; } -} \ No newline at end of file +} diff --git a/Sitemap/Storage/Doctrine/ODM/MongoDB.php b/Sitemap/Storage/Doctrine/ODM/MongoDB.php index c713f37..bf9be9a 100644 --- a/Sitemap/Storage/Doctrine/ODM/MongoDB.php +++ b/Sitemap/Storage/Doctrine/ODM/MongoDB.php @@ -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 */ @@ -102,4 +112,4 @@ private function getSkip($page) { return ((int) $page - 1) * self::PAGE_LIMIT; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 709d8cf..e793dee 100644 --- a/composer.json +++ b/composer.json @@ -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": "" }