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

Add possibility to change used entity manager. #502

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions DependencyInjection/Compiler/BuildEntityManagerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Payum\Bundle\PayumBundle\DependencyInjection\Compiler;

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

class BuildEntityManagerPass implements CompilerPassInterface
{
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
$payumConfig = $container->getExtensionConfig('payum')[0];

$serviceId = \sprintf('doctrine.orm.%s_entity_manager', $payumConfig['entity_manager']);

if ($container->has($serviceId)) {
$container->setAlias('payum.entity_manager', $serviceId);
}
}
}
10 changes: 8 additions & 2 deletions DependencyInjection/MainConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public function getConfigTreeBuilder()
$rootNode = $tb->root('payum');
}

$rootNode
->children()
->scalarNode('entity_manager')
->defaultValue('default')
->end();

$securityNode = $rootNode->children()
->arrayNode('security')->isRequired()
;
Expand All @@ -55,7 +61,7 @@ public function getConfigTreeBuilder()
->prototype('variable')
->treatNullLike([])
;

$this->addStoragesSection($rootNode);

return $tb;
Expand Down Expand Up @@ -260,4 +266,4 @@ protected function addDynamicGatewaysSection(ArrayNodeDefinition $dynamicGateway
);
}
}
}
}
40 changes: 28 additions & 12 deletions DependencyInjection/PayumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ class PayumExtension extends Extension implements PrependExtensionInterface
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->addStorageFactory(new FilesystemStorageFactory);
$this->addStorageFactory(new DoctrineStorageFactory);
$this->addStorageFactory(new CustomStorageFactory);
$this->addStorageFactory(new Propel1StorageFactory);
$this->addStorageFactory(new Propel2StorageFactory);

$this->addDefaultStorageFactories();
$mainConfig = $this->getConfiguration($configs, $container);

$config = $this->processConfiguration($mainConfig, $configs);
Expand Down Expand Up @@ -75,8 +70,12 @@ public function load(array $configs, ContainerBuilder $container)
*/
public function prepend(ContainerBuilder $container)
{
$this->addDefaultStorageFactories();
$bundles = $container->getParameter('kernel.bundles');

$configs = $container->getExtensionConfig($this->getAlias());
$payumConfig = $this->processConfiguration(new MainConfiguration($this->storagesFactories), $configs);

if (isset($bundles['DoctrineBundle'])) {
$config = array_merge(...$container->getExtensionConfig('doctrine'));

Expand All @@ -87,12 +86,16 @@ public function prepend(ContainerBuilder $container)

$container->prependExtensionConfig('doctrine', array(
'orm' => array(
'mappings' => array(
'payum' => array(
'is_bundle' => false,
'type' => 'xml',
'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping',
'prefix' => 'Payum\Core\Model',
'entity_managers' => array(
$payumConfig['entity_manager'] => array(
'mappings' => array(
'payum' => array(
'is_bundle' => false,
'type' => 'xml',
'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping',
'prefix' => 'Payum\Core\Model',
),
),
),
),
),
Expand Down Expand Up @@ -312,4 +315,17 @@ protected function findSelectedStorageFactoryNameInStorageConfig($storageConfig)
}
}
}

private function addDefaultStorageFactories()
{
try {
$this->addStorageFactory(new FilesystemStorageFactory);
$this->addStorageFactory(new DoctrineStorageFactory);
$this->addStorageFactory(new CustomStorageFactory);
$this->addStorageFactory(new Propel1StorageFactory);
$this->addStorageFactory(new Propel2StorageFactory);
} catch (\InvalidArgumentException $e) {

}
}
}
2 changes: 2 additions & 0 deletions PayumBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Payum\Bundle\PayumBundle;

use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildConfigsPass;
use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildEntityManagerPass;
use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildGatewayFactoriesBuilderPass;
use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildGatewayFactoriesPass;
use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildGatewaysPass;
Expand All @@ -20,5 +21,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new BuildStoragesPass);
$container->addCompilerPass(new BuildGatewayFactoriesPass);
$container->addCompilerPass(new BuildGatewayFactoriesBuilderPass());
$container->addCompilerPass(new BuildEntityManagerPass());
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ payum:
gateways:
offline:
factory: offline

entity_manager: default
```

_note_ if you're using Symfony 4+ then create `config/packages/payum.yaml` file with contents described above.
Expand Down
6 changes: 1 addition & 5 deletions Resources/config/storage/doctrine.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<argument><!-- should be set in DoctrineStorageFactory --></argument>
</service>

<service
id="payum.entity_manager"
alias="doctrine.orm.default_entity_manager"
public="false"
/>
<service id="payum.entity_manager" abstract="true" />
</services>
</container>
50 changes: 50 additions & 0 deletions Tests/DependencyInjection/Compiler/BuildEntityManagerPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Payum\Bundle\PayumBundle\Tests\DependencyInjection\Factory\Compiler;

use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildConfigsPass;
use Payum\Bundle\PayumBundle\DependencyInjection\Compiler\BuildEntityManagerPass;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class BuildEntityManagerPassTest extends \PHPUnit\Framework\TestCase
{
public function testShouldImplementCompilerPassInterface()
{
$rc = new \ReflectionClass(BuildConfigsPass::class);

$this->assertTrue($rc->implementsInterface(CompilerPassInterface::class));
}

public function testShouldAddAliasToContainer()
{
$service = new Definition();

$container = new ContainerBuilder();
$container->setDefinition('doctrine.orm.custom_entity_manager', $service);
$container->prependExtensionConfig('payum', [
'entity_manager' => 'custom',
]);

$pass = new BuildEntityManagerPass();

$pass->process($container);

$this->assertTrue($container->hasAlias('payum.entity_manager'));
}

public function testShouldNotAddAliasToContainer()
{
$container = new ContainerBuilder();
$container->prependExtensionConfig('payum', [
'entity_manager' => 'default',
]);

$pass = new BuildEntityManagerPass();

$pass->process($container);

$this->assertFalse($container->hasAlias('payum.entity_manager'));
}
}
Loading