Skip to content

Commit

Permalink
Added compiler extension for Nette
Browse files Browse the repository at this point in the history
  • Loading branch information
tg666 committed Sep 30, 2019
1 parent c65ce2c commit 8e0ce04
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Bridge/Nette/SluggableExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\DoctrineSluggable\Bridge\Nette;

use Nette;
use Doctrine;
use SixtyEightPublishers;

final class SluggableExtension extends Nette\DI\CompilerExtension
{
/** @var bool */
private $registerSubscriberIntoEntityManager;

/**
* @param bool $registerSubscriberIntoEntityManager
*/
public function __construct(bool $registerSubscriberIntoEntityManager = FALSE)
{
$this->registerSubscriberIntoEntityManager = $registerSubscriberIntoEntityManager;
}

/**
* {@inheritdoc}
*/
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('storage'))
->setType(SixtyEightPublishers\DoctrineSluggable\SluggableDefinitionStorage::class);

$builder->addDefinition($this->prefix('event_subscriber'))
->setType(SixtyEightPublishers\DoctrineSluggable\SluggableEventSubscriber::class);

$builder->addDefinition($this->prefix('helper'))
->setType(SixtyEightPublishers\DoctrineSluggable\Helper\UniqueSlugHelper::class);
}

/**
* {@inheritdoc}
*/
public function beforeCompile(): void
{
if (FALSE === $this->registerSubscriberIntoEntityManager) {
return;
}

$builder = $this->getContainerBuilder();

$builder->getDefinition($builder->getByType(Doctrine\ORM\EntityManagerInterface::class))
->addSetup('?->getEventManager()->addEventSubscriber(?)', [
'@self',
$builder->getDefinition($this->prefix('event_subscriber')),
]);
}
}

0 comments on commit 8e0ce04

Please sign in to comment.