diff --git a/Command/GenerateCommand.php b/Command/GenerateCommand.php index 767cc80..479a41e 100644 --- a/Command/GenerateCommand.php +++ b/Command/GenerateCommand.php @@ -19,8 +19,32 @@ public function execute(InputInterface $input, OutputInterface $output) { $sitemap = $this->getContainer()->get('sitemap'); + $webPath = $this->getContainer()->getParameter('kernel.root_dir').'/../web'; + $templating = $this->getContainer()->get('templating'); + $this->getContainer()->get('sitemap.provider.chain')->populate($sitemap); - $output->write('Sitemap was sucessfully populated!', true); + + // TODO: Extract this to a service + + // Sitemaps + $pages = $sitemap->pages(); + for ($page = 1 ; $page <= $pages ; $page++) { + $sitemap->setPage($page); + $data = $templating->render('AvalancheSitemapBundle:Sitemap:sitemap.twig.xml', array( + 'sitemap' => $sitemap + )); + file_put_contents($webPath.'/sitemap-'.$page.'.xml', $data); + $output->write("Sitemap page $page was sucessfully generated!", true); + } + + // Siteindex + $data = $templating->render('AvalancheSitemapBundle:Sitemap:siteindex.twig.xml', array( + 'pages' => $sitemap->pages(), + 'sitemap' => $sitemap, + )); + file_put_contents($webPath.'/siteindex.xml', $data); + + $output->write('Siteindex was sucessfully generated!', true); } } diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php new file mode 100644 index 0000000..0d226ef --- /dev/null +++ b/Command/PopulateCommand.php @@ -0,0 +1,25 @@ +setName('sitemap:populate') + ->setDescription('Populate sitemap, using its data providers.'); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $sitemap = $this->getContainer()->get('sitemap'); + + $this->getContainer()->get('sitemap.provider.chain')->populate($sitemap); + $output->write('Sitemap was sucessfully populated!', true); + } +} diff --git a/Controller/SitemapController.php b/Controller/SitemapController.php index b928054..0fadfa1 100644 --- a/Controller/SitemapController.php +++ b/Controller/SitemapController.php @@ -23,6 +23,7 @@ public function sitemap() { $page = $this->request->query->get('page', 1); + // TODO: populate if necessary $this->sitemap->setPage($page); return $this->templating->renderResponse('AvalancheSitemapBundle:Sitemap:sitemap.twig.xml', array( diff --git a/DependencyInjection/AvalancheSitemapExtension.php b/DependencyInjection/AvalancheSitemapExtension.php index c097349..1f3fa1b 100644 --- a/DependencyInjection/AvalancheSitemapExtension.php +++ b/DependencyInjection/AvalancheSitemapExtension.php @@ -21,17 +21,24 @@ public function load(array $configs, ContainerBuilder $container) throw new InvalidArgumentException('Please provide a base_url in the sitemap configuration'); } + if (!isset($config['driver'])) { + throw new InvalidArgumentException('Please provide a driver in the sitemap configuration'); + } + $driver = $config['driver']; + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('sitemap.xml'); + + $container->setAlias('sitemap.url.repository', 'sitemap.url.'.$driver.'_repository'); - foreach (array('collection', 'database') as $key) { - if (isset($config[$key])) { - $container->setParameter(sprintf("sitemap.mongodb.%s", $key), $config[$key]); + if ('odm' == $driver) { + foreach (array('collection', 'database') as $key) { + if (isset($config[$key])) { + $container->setParameter(sprintf("sitemap.mongodb.%s", $key), $config[$key]); + } } } - if(isset($config['base_url'])) { - $container->setParameter('sitemap.base_url', $config['base_url']); - } + $container->setParameter('sitemap.base_url', $config['base_url']); } } diff --git a/Document/ArrayUrlRepository.php b/Document/ArrayUrlRepository.php new file mode 100644 index 0000000..4f3be51 --- /dev/null +++ b/Document/ArrayUrlRepository.php @@ -0,0 +1,54 @@ +urls[$url->getLoc()] = $url; + } + + public function findAllOnPage($page) + { + return array_slice( + $this->urls, + UrlRepositoryInterface::PER_PAGE_LIMIT * ($page - 1), + UrlRepositoryInterface::PER_PAGE_LIMIT, + true + ); + } + + public function findOneByLoc($loc) + { + $url = null; + if (isset($this->urls[$loc])) { + $url = $this->urls[$loc]; + } + return $url; + } + + public function remove(Url $url) + { + unset($this->urls[$url->getLoc()]); + } + + public function pages() + { + return max(ceil(count($this->urls) / UrlRepositoryInterface::PER_PAGE_LIMIT), 1); + } + + public function flush() + { + } + + public function getLastmod($page) + { + } + +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index eaf839a..90c695d 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -1,5 +1,5 @@ sitemap: - pattern: /sitemap.xml + pattern: /sitemap-{page}.xml defaults: { _controller: sitemap.controller:sitemap, _format: xml } requirements: { _method: GET, _format: xml } diff --git a/Resources/config/sitemap.xml b/Resources/config/sitemap.xml index a4c1060..b3f81ae 100644 --- a/Resources/config/sitemap.xml +++ b/Resources/config/sitemap.xml @@ -23,12 +23,15 @@ - %sitemap.url.class% + + %sitemap.mongodb.collection% diff --git a/Resources/views/Sitemap/sitemap_url.twig.xml b/Resources/views/Sitemap/sitemap_url.twig.xml index 59635ee..abfb110 100644 --- a/Resources/views/Sitemap/sitemap_url.twig.xml +++ b/Resources/views/Sitemap/sitemap_url.twig.xml @@ -1,4 +1,4 @@ -{{ url('sitemap', { 'page': page }) }} +{{ path('sitemap', { 'page': page })|sm_absolutize }} {% if sitemap.lastmod(page) %} sitemap.lastmod(page)|sm_format_date {% endif %}