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 Dependency Injection integration #996

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"league/uri": "^6.4",
"league/uri-components": "^2.2",
"twig/twig": "^2.4 || ^3.0",
"php-http/discovery": "^1.19"
"php-http/discovery": "^1.19",
"psr/container": "^2.0",
"php-di/php-di": "^7.0"
},
"require-dev": {
"ext-curl": "*",
Expand Down
7 changes: 7 additions & 0 deletions src/Payum/Core/Action/PrependActionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Payum\Core\Action;

interface PrependActionInterface
{
}
349 changes: 220 additions & 129 deletions src/Payum/Core/CoreGatewayFactory.php

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/Payum/Core/DI/ContainerConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Payum\Core\DI;

use Payum\Core\Gateway;
use Psr\Container\ContainerInterface;

interface ContainerConfiguration
{
/**
* @return array<string, mixed>
*/
public function configureContainer(): array;

public function createGateway(ContainerInterface $container): Gateway;
}
7 changes: 7 additions & 0 deletions src/Payum/Core/Extension/PrependExtensionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Payum\Core\Extension;

interface PrependExtensionInterface
{
}
6 changes: 3 additions & 3 deletions src/Payum/Core/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
class Gateway implements GatewayInterface
{
/**
* @var Action\ActionInterface[]
* @var list<class-string<Action\ActionInterface>|Action\ActionInterface>
*/
protected $actions = [];
protected array $actions = [];

/**
* @var mixed[]
Expand All @@ -35,7 +35,7 @@ class Gateway implements GatewayInterface
/**
* @var Context[]
*/
protected $stack = [];
protected array $stack = [];

public function __construct()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Payum/Core/GatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Payum\Core;

use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\DI\ContainerConfiguration;

class GatewayFactory implements GatewayFactoryInterface
class GatewayFactory implements GatewayFactoryInterface /*, ContainerConfiguration */ // This class will implement ContainerConfiguration from version 3.0, and will remove the GatewayFactoryInterface
{
/**
* @var GatewayFactoryInterface
Expand Down
22 changes: 22 additions & 0 deletions src/Payum/Core/GatewayFactoryConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Payum\Core;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Extension\ExtensionInterface;
use Psr\Container\ContainerInterface;

interface GatewayFactoryConfigInterface
{
public function createGateway(ContainerInterface $container): Gateway;

/**
* @return array<string, class-string<ActionInterface>>|list<class-string<ActionInterface>>
*/
public function getActions(): array;

/**
* @return list<ExtensionInterface|class-string<ExtensionInterface>>
*/
public function getExtensions(): array;
}
27 changes: 26 additions & 1 deletion src/Payum/Core/PayumBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Payum\Core;

use DI\ContainerBuilder;
use LogicException;
use Omnipay\Omnipay;
use Payum\AuthorizeNet\Aim\AuthorizeNetAimGatewayFactory;
use Payum\Be2Bill\Be2BillDirectGatewayFactory;
use Payum\Be2Bill\Be2BillOffsiteGatewayFactory;
use Payum\Core\Bridge\PlainPhp\Security\HttpRequestVerifier;
use Payum\Core\Bridge\PlainPhp\Security\TokenFactory;
use Payum\Core\DI\ContainerConfiguration;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\Extension\GenericTokenFactoryExtension;
use Payum\Core\Extension\StorageExtension;
Expand Down Expand Up @@ -46,6 +48,7 @@
use function in_array;
use function strtolower;
use function sys_get_temp_dir;
use function trigger_error;

class PayumBuilder
{
Expand Down Expand Up @@ -315,10 +318,32 @@ public function getPayum(): Payum
if ($this->gatewayConfigs) {
$gateways = $this->gateways;
foreach ($this->gatewayConfigs as $name => $gatewayConfig) {
$containerBuilder = new ContainerBuilder();

$containerBuilder->addDefinitions($gatewayConfig);
$containerBuilder->addDefinitions([
'payum.security.token_storage' => fn () => $this->tokenStorage,
]);

$gatewayFactory = $registry->getGatewayFactory($gatewayConfig['factory']);
unset($gatewayConfig['factory']);

$gateways[$name] = $gatewayFactory->create($gatewayConfig);
if ($gatewayFactory instanceof ContainerConfiguration) {
$containerBuilder->addDefinitions($gatewayFactory->configureContainer());

$gateways[$name] = $gatewayFactory->createGateway($containerBuilder->build());
} else {
@trigger_error(
sprintf(
'Not implementing %s for gateway factory %s is deprecated since 2.0.',
ContainerConfiguration::class,
$gatewayFactory::class,
),
E_USER_DEPRECATED
);

$gateways[$name] = $gatewayFactory->create($gatewayConfig);
}
}

$registry = $this->buildRegistry($gateways, $this->storages, $gatewayFactories);
Expand Down
Loading