Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Kdyby/Events optional #266

Closed
wants to merge 3 commits into from
Closed

Make Kdyby/Events optional #266

wants to merge 3 commits into from

Conversation

enumag
Copy link
Member

@enumag enumag commented Apr 17, 2016

#239

I wanted to add some tests but unfortunately I was not even able to run the current tests - PHP just crashed. :-( Which means this is highly experimental.

@enumag
Copy link
Member Author

enumag commented Apr 17, 2016

Travis is failing because of some incompatibility in dev Nette.

@enumag
Copy link
Member Author

enumag commented Apr 17, 2016

Failing because of Kdyby/Console#54.




protected function processRepositories()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to keep this as a separate method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. It's just that beforeCompile will only call this method and nothing else which seems a bit odd. But sure. :-)

@fprochazka
Copy link
Member

I've cherrypicked and fixed few missing occurences of ->class, 3e8bd5d. Thanks!

@enumag enumag force-pushed the events branch 2 times, most recently from 18701c1 to eff881f Compare April 20, 2016 20:06
@enumag
Copy link
Member Author

enumag commented Apr 21, 2016

Travis finally passed after rebase. Which means I at least didn't break any tests. I'm still unable to run the tests on localhost though so writing a new test is not easy.

@enumag enumag mentioned this pull request Apr 23, 2016
31 tasks
@enumag
Copy link
Member Author

enumag commented Apr 24, 2016

Finally managed to run the tests on localhost by using -p php (from .travis.yml).

In the end I didn't write any new tests, just changed the old ones little bit:

  1. Replaced EventsExtension with a simple service Doctrine\Common\EventManager for all tests.
  2. Identified tests broken in step 1 - Events.compatibility.phpt and TargetEntityMapping.phpt.
  3. These tests do require Kdyby/Events so I refactored them to use different configuration.

@enumag
Copy link
Member Author

enumag commented Apr 24, 2016

@fprochazka With the tests updated I'm satisfied with this PR as it is. Can you review the changes please? :-)

@enumag
Copy link
Member Author

enumag commented May 6, 2016

Refactored Kdyby/Events detection since you recently wanted a method instead of a property in similar case in Kdyby/Console.

@@ -64,6 +69,12 @@ protected function createMemoryManager()



protected function configure(Configurator $config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not used anywhere

fprochazka pushed a commit that referenced this pull request Jul 8, 2016
[Closes #266] [Closes #239] [Ref #238]
fprochazka pushed a commit that referenced this pull request Jul 8, 2016
[Closes #266] [Closes #239] [Ref #238]
@fprochazka fprochazka closed this in 55a7714 Jul 8, 2016
@fprochazka
Copy link
Member

@enumag Thank you, merged! I've made some minor changes

diff --git a/src/Kdyby/Doctrine/DI/OrmExtension.php b/src/Kdyby/Doctrine/DI/OrmExtension.php
index 7155fd4..0da259f 100644
--- a/src/Kdyby/Doctrine/DI/OrmExtension.php
+++ b/src/Kdyby/Doctrine/DI/OrmExtension.php
@@ -351,24 +351,26 @@ class OrmExtension extends Nette\DI\CompilerExtension
                        $this->targetEntityMappings = Nette\Utils\Arrays::mergeTree($this->targetEntityMappings, $config['targetEntityMappings']);
                }

-               $entityManagerArguments = [
-                       $connectionService = $this->processConnection($name, $defaults, $isDefault),
-                       $this->prefix('@' . $name . '.ormConfiguration'),
-               ];
-
                if ($this->isKdybyEventsPresent()) {
                        $builder->addDefinition($this->prefix($name . '.evm'))
                                ->setClass('Kdyby\Events\NamespacedEventManager', [Kdyby\Doctrine\Events::NS . '::'])
                                ->addSetup('$dispatchGlobalEvents', [TRUE]) // for BC
                                ->setAutowired(FALSE);

-                       $entityManagerArguments[] = $this->prefix('@' . $name . '.evm');
+               } else {
+                       $builder->addDefinition($this->prefix($name . '.evm'))
+                               ->setClass('Doctrine\Common\EventManager')
+                               ->setAutowired(FALSE);
                }

                // entity manager
                $entityManager = $builder->addDefinition($managerServiceId = $this->prefix($name . '.entityManager'))
                        ->setClass('Kdyby\Doctrine\EntityManager')
-                       ->setFactory('Kdyby\Doctrine\EntityManager::create', $entityManagerArguments)
+                       ->setFactory('Kdyby\Doctrine\EntityManager::create', [
+                               $connectionService = $this->processConnection($name, $defaults, $isDefault),
+                               $this->prefix('@' . $name . '.ormConfiguration'),
+                               $this->prefix('@' . $name . '.evm'),
+                       ])
                        ->addTag(self::TAG_ENTITY_MANAGER)
                        ->addTag('kdyby.doctrine.entityManager')
                        ->setAutowired($isDefault)
@@ -523,19 +525,13 @@ class OrmExtension extends Nette\DI\CompilerExtension

                // connection
                $options = array_diff_key($config, array_flip(['types', 'resultCache', 'connection', 'logging']));
-
-               $connectionArguments = [
-                       $options,
-                       $this->prefix('@' . $name . '.dbalConfiguration'),
-               ];
-
-               if ($this->isKdybyEventsPresent()) {
-                       $connectionArguments[] = $this->prefix('@' . $name . '.evm');
-               }
-
                $connection = $builder->addDefinition($connectionServiceId = $this->prefix($name . '.connection'))
                        ->setClass('Kdyby\Doctrine\Connection')
-                       ->setFactory('Kdyby\Doctrine\Connection::create', $connectionArguments)
+                       ->setFactory('Kdyby\Doctrine\Connection::create', [
+                               $options,
+                               $this->prefix('@' . $name . '.dbalConfiguration'),
+                               $this->prefix('@' . $name . '.evm'),
+                       ])
                        ->addSetup('setSchemaTypes', [$schemaTypes])
                        ->addSetup('setDbalTypes', [$dbalTypes])
                        ->addTag(self::TAG_CONNECTION)
diff --git a/tests/KdybyTests/Doctrine/ORMTestCase.php b/tests/KdybyTests/Doctrine/ORMTestCase.php
index 0d6a521..b6d0f4f 100644
--- a/tests/KdybyTests/Doctrine/ORMTestCase.php
+++ b/tests/KdybyTests/Doctrine/ORMTestCase.php
@@ -69,12 +69,6 @@ abstract class ORMTestCase extends Tester\TestCase



-       protected function configure(Configurator $config)
-       {
-       }
-
-
-
        /**
         * @param string $className
         * @param array $props
diff --git a/tests/KdybyTests/nette-reset.neon b/tests/KdybyTests/nette-reset.neon
index 0e29325..cc18417 100644
--- a/tests/KdybyTests/nette-reset.neon
+++ b/tests/KdybyTests/nette-reset.neon
@@ -31,5 +31,3 @@ session:
 services:
        cacheStorage:
                class: Nette\Caching\Storages\MemoryStorage
-       evm:
-               class: Doctrine\Common\EventManager

@enumag
Copy link
Member Author

enumag commented Jul 8, 2016

If I'm not mistaken those minor changes break what I intended - I need the evm to be autowired, not use some dummy replacement

@fprochazka
Copy link
Member

fprochazka commented Jul 8, 2016

I know, but I don't want the users to have to configure some magic service, that should be configured by Kdyby.
If you wanna provide custom implementation of Doctrine\Common\EventManager, you can override the evm service for the EM.

Now you can

  • use this with Kdyby/Events and it works
  • use this without Kdyby/Events and it works
  • use this without Kdyby/Events, with custom implementation of EventManager and it works.

IMHO it's cleaner that way and it makes sense that the third requires you to touch the evm service in config.

@enumag
Copy link
Member Author

enumag commented Jul 8, 2016

I can't agree. I have an extension providing a simple ContainerAwareEventManager and wanted to use it in Kdyby Doctrine. However my extension should not be aware of Kdyby/Doctrine at all (it's independent) - you're forcing me to specifically look for OrmExtension and hack it. That's not clean at all.

@fprochazka
Copy link
Member

First of all

I don't want the users to have to configure some magic service, that should be configured by Kdyby.

I hope you understand that. I wanna avoid this at all costs.

By just simply accepting what you've proposed, It would be simple for you, but I'd cripple the UX of the lib for everyone else.


About your argument - I don't consider it hacking. But I understand why you don't like it.

What about adding another case - if Kdyby/Events is not registered and a service of type Doctrine\Common\EventManager can be found in config, the evm service will be an alias to that service.

@enumag
Copy link
Member Author

enumag commented Jul 8, 2016

How about adding the evm service only if there is no autowired EventManager?

EDIT: Well that alias means pretty much the same thing so I don't care which one of these two solutions we use.

@fprochazka
Copy link
Member

fprochazka commented Jul 8, 2016

Imho it would be better to do something like

$evm = $builder->addDefinition($this->prefix($name . '.evm'))
    ->setClass('Doctrine\Common\EventManager')
    ->setAutowired(FALSE);

if ($customEvmService = $builder->getByType('Doctrine\Common\EventManager')) {
    $evm->setFactory('@' . $customEvmService);
}

but that's a detail :)

@enumag
Copy link
Member Author

enumag commented Jul 8, 2016

@fprochazka Yeah. This is fine by me. :-)

@fprochazka
Copy link
Member

My point is that you don't have to introduce more if's in the rest of the code :)

@enumag
Copy link
Member Author

enumag commented Jul 8, 2016

I'm aware... I didn't like the ifs in my PR either. :-)

@fprochazka
Copy link
Member

I'm lazy to write a test for it right now. You mind doing it and sending a PR? :bowtie:

@enumag
Copy link
Member Author

enumag commented Jul 8, 2016

Well... You're well aware that I won't refuse. :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants