From ad4d53a9024c43e67fda7322ba15352f2cbc9adc Mon Sep 17 00:00:00 2001 From: Ben Meynell Date: Thu, 24 Apr 2014 12:24:27 -0400 Subject: [PATCH 1/3] support for additional document properties --- DependencyInjection/OpenSkySitemapExtension.php | 5 +++-- Resources/config/sitemap.xml | 4 +++- Sitemap/Sitemap.php | 13 +++++++++---- composer.json | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) 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/Resources/config/sitemap.xml b/Resources/config/sitemap.xml index caf01e0..b697ed1 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% @@ -42,4 +44,4 @@ - \ No newline at end of file + diff --git a/Sitemap/Sitemap.php b/Sitemap/Sitemap.php index f901d0d..16673b7 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]); } @@ -122,4 +127,4 @@ public function getStorage() return $this->storage; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 709d8cf..9673e60 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.4-dev" }, "autoload": { "psr-0": { "OpenSky\\Bundle\\SitemapBundle": "" } From 102429d8ea577126335c322458f5ff591ce84ddd Mon Sep 17 00:00:00 2001 From: Ben Meynell Date: Sun, 27 Apr 2014 07:46:40 -0400 Subject: [PATCH 2/3] changed lastmode to DateTime + Added delete event and storage method + Added SitemapEvent --- Dumper/Doctrine/ODM/MongoDB.php | 3 --- Event/.SitemapEvent.php.swp | Bin 0 -> 12288 bytes Event/SitemapEvent.php | 25 +++++++++++++++++++++++ Events.php | 10 +++++++++ Listener/SitemapListener.php | 19 +++++++++++++++-- Resources/config/sitemap.xml | 5 +++-- Resources/views/Sitemap/sitemap.xml.twig | 2 +- Sitemap/Sitemap.php | 5 +++++ Sitemap/Storage/Doctrine/ODM/MongoDB.php | 12 ++++++++++- 9 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 Event/.SitemapEvent.php.swp create mode 100644 Event/SitemapEvent.php create mode 100644 Events.php 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 0000000000000000000000000000000000000000..5316e605f77e5b3a9ad370d6a5e313fcd155ab1b GIT binary patch literal 12288 zcmeI2yKWOf6oyae11LyHbOS_@BjIcy1s3KeE(Hamu%L;-T93!^3h(S{E^$^C;zg2* zm*5$opg=qVj{qVxaQ5QGB#Qee|484?<;-nAX$#AzzxnFfb8&lPi@01Q`ubzL^Zjy* zer^&)Hi?xpk+ga6tTBIh z{UP&<`Nn)OfosycwDe2T;F4U$e z$#H0{n9o}mYR_>VycI0&2ABV64^9#a->YzZxiV-Ry-(` WsM6)TWrBS7zjXO}`rQY4mi%8oz~f&4 literal 0 HcmV?d00001 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 b697ed1..396a57a 100644 --- a/Resources/config/sitemap.xml +++ b/Resources/config/sitemap.xml @@ -38,8 +38,9 @@ - - + + + 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 16673b7..d5e8431 100644 --- a/Sitemap/Sitemap.php +++ b/Sitemap/Sitemap.php @@ -82,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 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 +} From 835fe79845f6fa70b01bbb016758d7ef1bc60b80 Mon Sep 17 00:00:00 2001 From: Ben Meynell Date: Sat, 1 Aug 2015 16:45:29 -0700 Subject: [PATCH 3/3] updated symfony version to 2.7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9673e60..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.4-dev" + "symfony/framework-bundle": ">=2.1.0,<2.7-dev" }, "autoload": { "psr-0": { "OpenSky\\Bundle\\SitemapBundle": "" }