Skip to content

Commit

Permalink
WIP to allow usage without Doctrine ODM
Browse files Browse the repository at this point in the history
  • Loading branch information
mbontemps committed Sep 18, 2011
1 parent 6d4e87b commit 0c0743e
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 10 deletions.
26 changes: 25 additions & 1 deletion Command/GenerateCommand.php
Expand Up @@ -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('<info>Sitemap was sucessfully populated!</info>', 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("<info>Sitemap page $page was sucessfully generated!</info>", 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('<info>Siteindex was sucessfully generated!</info>', true);
}
}
25 changes: 25 additions & 0 deletions Command/PopulateCommand.php
@@ -0,0 +1,25 @@
<?php

namespace Avalanche\Bundle\SitemapBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Avalanche\Bundle\SitemapBundle\Sitemap\Provider;

class PopulateCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->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('<info>Sitemap was sucessfully populated!</info>', true);
}
}
1 change: 1 addition & 0 deletions Controller/SitemapController.php
Expand Up @@ -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(
Expand Down
19 changes: 13 additions & 6 deletions DependencyInjection/AvalancheSitemapExtension.php
Expand Up @@ -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']);
}
}
54 changes: 54 additions & 0 deletions Document/ArrayUrlRepository.php
@@ -0,0 +1,54 @@
<?php

namespace Avalanche\Bundle\SitemapBundle\Document;

use Avalanche\Bundle\SitemapBundle\Sitemap\Url;
use Avalanche\Bundle\SitemapBundle\Sitemap\UrlRepositoryInterface;

class ArrayUrlRepository implements UrlRepositoryInterface
{
private $urls = array();

public function add(Url $url)
{
$this->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)
{
}

}
2 changes: 1 addition & 1 deletion 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 }

Expand Down
5 changes: 4 additions & 1 deletion Resources/config/sitemap.xml
Expand Up @@ -23,12 +23,15 @@
<argument type="service" id="templating"></argument>
<argument type="service" id="request"></argument>
</service>
<service id="sitemap.url.repository"
<service id="sitemap.url.odm_repository"
class="Avalanche\Bundle\SitemapBundle\Document\UrlRepository"
factory-service="doctrine.odm.mongodb.document_manager"
factory-method="getRepository">
<argument>%sitemap.url.class%</argument>
</service>
<service id="sitemap.url.array_repository"
class="Avalanche\Bundle\SitemapBundle\Document\ArrayUrlRepository">
</service>
<service id="sitemap.url.mapper" class="%sitemap.url.mapper.class%">
<tag name="doctrine.odm.mongodb.default_event_listener" event="loadClassMetadata" />
<argument>%sitemap.mongodb.collection%</argument>
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Sitemap/sitemap_url.twig.xml
@@ -1,4 +1,4 @@
<loc>{{ url('sitemap', { 'page': page }) }}</loc>
<loc>{{ path('sitemap', { 'page': page })|sm_absolutize }}</loc>
{% if sitemap.lastmod(page) %}
<lastmod>sitemap.lastmod(page)|sm_format_date</lastmod>
{% endif %}

0 comments on commit 0c0743e

Please sign in to comment.