Skip to content

Commit

Permalink
Fix event deprecation (prestaconcept#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas authored and yann-eugone committed Jul 8, 2019
1 parent 5ec8028 commit 61a8170
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 50 deletions.
165 changes: 116 additions & 49 deletions Event/SitemapPopulateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,135 @@
namespace Presta\SitemapBundle\Event;

use Presta\SitemapBundle\Service\UrlContainerInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\Event as BaseEvent;
use Symfony\Contracts\EventDispatcher\Event as ContractsBaseEvent;

/**
* Manage populate event
*
* @author depely
*/
class SitemapPopulateEvent extends Event
{
if (is_subclass_of('Symfony\Component\EventDispatcher\EventDispatcher', 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface')) {
/**
* @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent")
* Manage populate event
*
* @author depely
*/
const ON_SITEMAP_POPULATE = 'presta_sitemap.populate';
class SitemapPopulateEvent extends ContractsBaseEvent
{
/**
* @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent")
*/
const ON_SITEMAP_POPULATE = 'presta_sitemap.populate';

/**
* @var UrlContainerInterface
*/
protected $urlContainer;
/**
* @var UrlContainerInterface
*/
protected $urlContainer;

/**
* Allows creating EventListeners for particular sitemap sections, used when dumping
* @var string
*/
protected $section;
/**
* Allows creating EventListeners for particular sitemap sections, used when dumping
* @var string
*/
protected $section;

/**
* @param UrlContainerInterface $urlContainer
* @param string|null $section
*/
public function __construct(UrlContainerInterface $urlContainer, $section = null)
{
$this->urlContainer = $urlContainer;
$this->section = $section;
}
/**
* @param UrlContainerInterface $urlContainer
* @param string|null $section
*/
public function __construct(UrlContainerInterface $urlContainer, $section = null)
{
$this->urlContainer = $urlContainer;
$this->section = $section;
}

/**
* @deprecated in favor of `Presta\SitemapBundle\Event\SitemapPopulateEvent::getUrlContainer()`
*
* @return UrlContainerInterface
*/
public function getGenerator()
{
@trigger_error('getGenerator is deprecated since 1.5. Use getUrlContainer instead', E_USER_DEPRECATED);
/**
* @deprecated in favor of `Presta\SitemapBundle\Event\SitemapPopulateEvent::getUrlContainer()`
*
* @return UrlContainerInterface
*/
public function getGenerator()
{
@trigger_error('getGenerator is deprecated since 1.5. Use getUrlContainer instead', E_USER_DEPRECATED);

return $this->urlContainer;
}
return $this->urlContainer;
}

/**
* @return UrlContainerInterface
*/
public function getUrlContainer()
{
return $this->urlContainer;
}
/**
* @return UrlContainerInterface
*/
public function getUrlContainer()
{
return $this->urlContainer;
}

/**
* Section to be processed, null means any
*
* @return null|string
*/
public function getSection()
{
return $this->section;
}
}
} else {
/**
* Section to be processed, null means any
* Manage populate event
*
* @return null|string
* @author depely
*/
public function getSection()
class SitemapPopulateEvent extends BaseEvent
{
return $this->section;
/**
* @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent")
*/
const ON_SITEMAP_POPULATE = 'presta_sitemap.populate';

/**
* @var UrlContainerInterface
*/
protected $urlContainer;

/**
* Allows creating EventListeners for particular sitemap sections, used when dumping
* @var string
*/
protected $section;

/**
* @param UrlContainerInterface $urlContainer
* @param string|null $section
*/
public function __construct(UrlContainerInterface $urlContainer, $section = null)
{
$this->urlContainer = $urlContainer;
$this->section = $section;
}

/**
* @deprecated in favor of `Presta\SitemapBundle\Event\SitemapPopulateEvent::getUrlContainer()`
*
* @return UrlContainerInterface
*/
public function getGenerator()
{
@trigger_error('getGenerator is deprecated since 1.5. Use getUrlContainer instead', E_USER_DEPRECATED);

return $this->urlContainer;
}

/**
* @return UrlContainerInterface
*/
public function getUrlContainer()
{
return $this->urlContainer;
}

/**
* Section to be processed, null means any
*
* @return null|string
*/
public function getSection()
{
return $this->section;
}
}
}
8 changes: 7 additions & 1 deletion Service/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Presta\SitemapBundle\Sitemap\Urlset;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;

/**
* Abstract sitemap generator class
Expand Down Expand Up @@ -144,7 +145,12 @@ abstract protected function newUrlset($name, \DateTime $lastmod = null);
protected function populate($section = null)
{
$event = new SitemapPopulateEvent($this, $section);
$this->dispatcher->dispatch(SitemapPopulateEvent::ON_SITEMAP_POPULATE, $event);

if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($event, SitemapPopulateEvent::ON_SITEMAP_POPULATE);
} else {
$this->dispatcher->dispatch(SitemapPopulateEvent::ON_SITEMAP_POPULATE, $event);
}
}

/**
Expand Down

0 comments on commit 61a8170

Please sign in to comment.