Skip to content

Instantly share code, notes, and snippets.

@kl3ryk
Created June 6, 2013 08:57
Show Gist options
  • Save kl3ryk/5720247 to your computer and use it in GitHub Desktop.
Save kl3ryk/5720247 to your computer and use it in GitHub Desktop.
<?php
namespace Absolvent\AbsolventBundle\EventListener\SitemapListener;
use Absolvent\AbsolventBundle\EventListener\SitemapListener;
use Absolvent\Common\Enum\CollegeCourseType;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
class CollegeRankingListener extends SitemapListener
{
protected $section = 'college_ranking';
protected $changefreq = UrlConcrete::CHANGEFREQ_WEEKLY;
protected $priority = 0.5;
protected $hasNextPage = true;
protected $onDemandOnly = true;
protected $items = [];
protected function getItems($page)
{
// selecting informations from db using $page parameter
return $this->items;
}
protected function getEntry($item)
{
$jobSearchUrl = $this->container->get('absolvent.url_generator');
return [
'url' => $jobSearchUrl->ranking($item)
];
}
// [...]
<?php
namespace Absolvent\AbsolventBundle\EventListener;
use Absolvent\Common\EventListener;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\SitemapListenerInterface;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\Routing\RouterInterface;
abstract class SitemapListener extends EventListener implements SitemapListenerInterface
{
protected $section;
protected $changefreq = UrlConcrete::CHANGEFREQ_WEEKLY;
protected $priority = 0.5;
protected $hasNextPage = false;
protected $onDemandOnly = false;
protected $generator;
public function populateSitemap(SitemapPopulateEvent $event)
{
$section = $event->getSection();
$this->generator = $event->getGenerator();
if ((false === is_null($section) && $section != $this->section) ||
(null === $section && $this->onDemandOnly)) {
return;
}
echo "Generating $this->section\n";
gc_enable();
$page = 0;
do {
$items = $this->getItems($page++);
$jobSearchUrl = $this->container->get('job.search.url');
foreach ($items as $item) {
$jobSearchUrl->reset();
$entry = $this->getEntry($item);
$this->addUrl($entry['url'],
array_key_exists('date', $entry) ? $entry['date'] : null,
$this->changefreq,
$this->priority,
$this->section);
}
unset($items);
gc_collect_cycles();
} while ($this->hasNextPage);
gc_disable();
echo "End Generating $this->section\n";
echo "-------------\n";
}
protected function addUrl($url, $date, $changefreq, $priority, $section)
{
$this
->generator
->addUrl(new UrlConcrete($url, $date, $changefreq, $priority), $section);
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::onSitemapPopulate => ['populateSitemap', 0],
];
}
abstract protected function getItems($page);
abstract protected function getEntry($item);
}
@iamdey
Copy link

iamdey commented Jun 6, 2013

I remember isset is faster that array_key_exists for micro optim @ L43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment