Skip to content

Commit

Permalink
Implement add sitemap URL providers to sitemap generator compiler pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
igor committed Feb 20, 2016
1 parent 33b9a0d commit bf4c04d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
10 changes: 9 additions & 1 deletion DarvinSitemapBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@

namespace Darvin\SitemapBundle;

use Darvin\SitemapBundle\DependencyInjection\Compiler\AddSitemapUrlProvidersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Sitemap bundle
*/
class DarvinSitemapBundle extends Bundle
{

/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new AddSitemapUrlProvidersPass());
}
}
58 changes: 58 additions & 0 deletions DependencyInjection/Compiler/AddSitemapUrlProvidersPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* @author Igor Nikolaev <[email protected]>
* @copyright Copyright (c) 2016, Darvin Studio
* @link https://www.darvin-studio.ru
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Darvin\SitemapBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Add sitemap URL providers to sitemap generator compiler pass
*/
class AddSitemapUrlProvidersPass implements CompilerPassInterface
{
const SITEMAP_GENERATOR_ALIAS = 'darvin_sitemap.generator';
const SITEMAP_URL_PROVIDER_TAG = 'darvin_sitemap.url_provider';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasAlias(self::SITEMAP_GENERATOR_ALIAS)) {
return;
}

$generatorId = (string) $container->getAlias(self::SITEMAP_GENERATOR_ALIAS);

if (!$container->hasDefinition($generatorId)) {
return;
}

$urlProviderIds = $container->findTaggedServiceIds(self::SITEMAP_URL_PROVIDER_TAG);

if (empty($urlProviderIds)) {
return;
}

$generatorDefinition = $container->getDefinition($generatorId);

foreach ($urlProviderIds as $id => $tags) {
$reference = new Reference($id);

foreach ($tags as $tag) {
$generatorDefinition->addMethodCall('addUrlProvider', array(
$reference,
));
}
}
}
}

0 comments on commit bf4c04d

Please sign in to comment.