Skip to content

Commit

Permalink
Merge c92760b into 772e912
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag committed Apr 26, 2016
2 parents 772e912 + c92760b commit 8c27cf1
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 2 deletions.
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests/integration')
->in(__DIR__ . '/tests/functional')
;

Expand Down
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=5.4.0",
"arachne/di-helpers": "~0.4.0",
"kdyby/doctrine": "^3.0.0",
"kdyby/doctrine": "dev-events",
"nette/di": "^2.3.0"
},
"suggest": {
Expand All @@ -26,9 +26,11 @@
"php": ">=5.5.0",
"arachne/bootstrap": "~0.2.1",
"arachne/codeception": "~0.6.3",
"arachne/container-adapter": "@dev",
"arachne/entity-loader": "~0.8.3",
"codeception/codeception": "~2.2.0@rc",
"kdyby/console": "^2.4.0",
"symfony/doctrine-bridge": "^2.8.0",
"symfony/event-dispatcher": "^2.8.0",
"nette/di": "^2.3.0"
},
Expand All @@ -39,7 +41,14 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\Integration\\": "tests/integration/src",
"Tests\\Functional\\": "tests/functional/src"
}
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/enumag/Doctrine.git"
}
]
}
25 changes: 25 additions & 0 deletions src/DI/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
use Kdyby\Validator\DI\ValidatorExtension;
use Nette\Utils\AssertionException;
use Nette\Utils\Validators;
use ReflectionClass;

/**
* @author Jáchym Toušek <enumag@gmail.com>
*/
class DoctrineExtension extends CompilerExtension
{
const TAG_SUBSCRIBER = 'arachne.doctrine.subscriber';

/** @var array */
public $defaults = [
'validateOnFlush' => false,
Expand All @@ -42,6 +45,11 @@ public function loadConfiguration()

$builder = $this->getContainerBuilder();

if (!$this->getExtension('Kdyby\Events\DI\EventsExtension', false) && $this->getExtension('Arachne\ContainerAdapter\DI\ContainerAdapterExtension', false)) {
$builder->addDefinition($this->prefix('eventManager'))
->setClass('Symfony\Bridge\Doctrine\ContainerAwareEventManager');
}

if ($this->getExtension('Arachne\EntityLoader\DI\EntityLoaderExtension', false)) {
$builder->addDefinition($this->prefix('entityLoader.filterInResolver'))
->setClass('Arachne\Doctrine\EntityLoader\FilterInResolver')
Expand Down Expand Up @@ -123,5 +131,22 @@ public function beforeCompile()
'resolver' => '@' . $extension->get(EntityLoaderExtension::TAG_FILTER_OUT, false),
]);
}

$subscribers = $builder->findByTag(self::TAG_SUBSCRIBER);
if ($subscribers) {
if (!$builder->hasDefinition($this->prefix('eventManager'))) {
throw new AssertionException("Subscribers support requires 'Arachne\ContainerAdapter\DI\ContainerAdapterExtension' to be installed and 'Kdyby\Events\DI\EventsExtension' to NOT be installed.", E_USER_NOTICE);
}

$evm = $builder->getDefinition($this->prefix('eventManager'));
foreach ($subscribers as $name => $attributes) {
$subscriber = $builder->getDefinition($name);
$evm->addSetup('?->addEventListener(?, ?)', [
'@self',
(new ReflectionClass($subscriber->getEntity() ?: $subscriber->getClass()))->newInstanceWithoutConstructor()->getSubscribedEvents(),
$name, // Intentionally without @ for laziness.
]);
}
}
}
}
3 changes: 3 additions & 0 deletions tests/integration.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
error_level: "E_ALL"

class_name: IntegrationSuiteGuy
5 changes: 5 additions & 0 deletions tests/integration/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

define('TEMP_DIR', __DIR__ . '/../_temp/integration_' . md5(time()));

mkdir(TEMP_DIR);
20 changes: 20 additions & 0 deletions tests/integration/config/subscriber-exception.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extensions:
arachne.containeradapter: Arachne\ContainerAdapter\DI\ContainerAdapterExtension
arachne.doctrine: Arachne\Doctrine\DI\DoctrineExtension
nette.caching: Nette\Bridges\CacheDI\CacheExtension( %tempDir% )
kdyby.annotations: Kdyby\Annotations\DI\AnnotationsExtension
kdyby.events: Kdyby\Events\DI\EventsExtension
kdyby.doctrine: Kdyby\Doctrine\DI\OrmExtension

kdyby.doctrine:
driver: pdo_sqlite
memory: true

kdyby.events:
debugger: false

services:
subscriber:
class: Tests\Integration\Fixtures\Subscriber
tags:
- arachne.doctrine.subscriber
16 changes: 16 additions & 0 deletions tests/integration/config/subscriber.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extensions:
arachne.containeradapter: Arachne\ContainerAdapter\DI\ContainerAdapterExtension
arachne.doctrine: Arachne\Doctrine\DI\DoctrineExtension
nette.caching: Nette\Bridges\CacheDI\CacheExtension( %tempDir% )
kdyby.annotations: Kdyby\Annotations\DI\AnnotationsExtension
kdyby.doctrine: Kdyby\Doctrine\DI\OrmExtension

kdyby.doctrine:
driver: pdo_sqlite
memory: true

services:
subscriber:
class: Tests\Integration\Fixtures\Subscriber
tags:
- arachne.doctrine.subscriber
47 changes: 47 additions & 0 deletions tests/integration/src/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Tests\Integration;

use Arachne\Bootstrap\Configurator;
use Codeception\TestCase\Test;
use Doctrine\Common\EventManager;
use Symfony\Bridge\Doctrine\ContainerAwareEventManager;

/**
* @author Jáchym Toušek <enumag@gmail.com>
*/
class DoctrineExtensionTest extends Test
{
/**
* @expectedException Nette\Utils\AssertionException
* @expectedExceptionMessage Subscribers support requires 'Arachne\ContainerAdapter\DI\ContainerAdapterExtension' to be installed and 'Kdyby\Events\DI\EventsExtension' to NOT be installed.
*/
public function testSubscriberException()
{
$this->createContainer('subscriber-exception.neon');
}

public function testSubscriber()
{
$container = $this->createContainer('subscriber.neon');

/* @var $evm EventManager */
$evm = $container->getByType(EventManager::class);
$this->assertInstanceOf(ContainerAwareEventManager::class, $evm);

// Ensure laziness.
$this->assertFalse($container->isCreated('subscriber'));
$evm->dispatchEvent('fooEvent');
$this->assertFalse($container->isCreated('subscriber'));
$evm->dispatchEvent('barEvent');
$this->assertTrue($container->isCreated('subscriber'));
}

private function createContainer($file)
{
$config = new Configurator();
$config->setTempDirectory(TEMP_DIR);
$config->addConfig(__DIR__ . '/../config/' . $file, false);
return $config->createContainer();
}
}
22 changes: 22 additions & 0 deletions tests/integration/src/Fixtures/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests\Integration\Fixtures;

use Doctrine\Common\EventSubscriber;

/**
* @author Jáchym Toušek <enumag@gmail.com>
*/
class Subscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return [
'barEvent',
];
}

public function barEvent()
{
}
}

0 comments on commit 8c27cf1

Please sign in to comment.