Skip to content

Commit 9e95376

Browse files
committed
symfony omnipay bundle commit
1 parent c1d7039 commit 9e95376

29 files changed

+9173
-52
lines changed

.gitignore

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,4 @@
1-
# Cache and logs (Symfony2)
2-
/app/cache/*
3-
/app/logs/*
4-
!app/cache/.gitkeep
5-
!app/logs/.gitkeep
6-
7-
# Email spool folder
8-
/app/spool/*
9-
10-
# Cache, session files and logs (Symfony3)
11-
/var/cache/*
12-
/var/logs/*
13-
/var/sessions/*
14-
!var/cache/.gitkeep
15-
!var/logs/.gitkeep
16-
!var/sessions/.gitkeep
17-
18-
# Logs (Symfony4)
19-
/var/log/*
20-
!var/log/.gitkeep
21-
22-
# Parameters
23-
/app/config/parameters.yml
24-
/app/config/parameters.ini
25-
26-
# Managed by Composer
27-
/app/bootstrap.php.cache
28-
/var/bootstrap.php.cache
29-
/bin/*
30-
!bin/console
31-
!bin/symfony_requirements
1+
/.idea
322
/vendor/
33-
34-
# Assets and user uploads
35-
/web/bundles/
36-
/web/uploads/
37-
38-
# PHPUnit
39-
/app/phpunit.xml
40-
/phpunit.xml
41-
42-
# Build data
43-
/build/
44-
45-
# Composer PHAR
463
/composer.phar
47-
48-
# Backup entities generated with doctrine:generate:entities command
49-
**/Entity/*~
50-
51-
# Embedded web-server pid file
52-
/.web-server-pid
4+
.phpunit.result.cache

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [1.0.0] - 2022-06-06
10+
11+
### Added
12+
- 1st release
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Janwebdev\OmnipayBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
class GatewayTagCompilerPass implements CompilerPassInterface
10+
{
11+
public function process(ContainerBuilder $container)
12+
{
13+
if (!$container->hasDefinition('omnipay')) {
14+
return;
15+
}
16+
17+
$definition = $container->findDefinition('omnipay');
18+
19+
$taggedGateways = $container->findTaggedServiceIds('omnipay.gateway');
20+
foreach ($taggedGateways as $id => $tags) {
21+
foreach ($tags as $tag) {
22+
$args = [new Reference($id)];
23+
24+
// Reference the gateway by the alias if provided
25+
if (isset($tag['alias'])) {
26+
$args[] = $tag['alias'];
27+
}
28+
29+
$definition->addMethodCall('registerGateway', $args);
30+
}
31+
}
32+
}
33+
}

DependencyInjection/Configuration.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Janwebdev\OmnipayBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
class Configuration implements ConfigurationInterface
9+
{
10+
public function getConfigTreeBuilder()
11+
{
12+
$treeBuilder = new TreeBuilder('omnipay');
13+
$rootNode = $treeBuilder->getRootNode();
14+
15+
$rootNode
16+
->children()
17+
->arrayNode('gateways')
18+
->useAttributeAsKey('name')
19+
->prototype('variable')
20+
->end()
21+
->end()
22+
->scalarNode('default')
23+
->defaultNull()
24+
->end()
25+
->booleanNode('init_on_boot')
26+
->defaultFalse()
27+
->end()
28+
->arrayNode('disabled')
29+
->prototype('scalar')
30+
->end()
31+
->end();
32+
33+
return $treeBuilder;
34+
}
35+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Janwebdev\OmnipayBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\Config\FileLocator;
8+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9+
use Symfony\Component\DependencyInjection\Loader;
10+
11+
class OmnipayExtension extends Extension
12+
{
13+
public function load(array $configs, ContainerBuilder $container)
14+
{
15+
$configuration = new Configuration();
16+
$config = $this->processConfiguration($configuration, $configs);
17+
18+
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
19+
$loader->load('services.yaml');
20+
21+
$gateways = $config['gateways'];
22+
$gatewayNames = array_keys($gateways);
23+
24+
// Add configuration to the Omnipay service
25+
$omnipay = $container->getDefinition('omnipay');
26+
$omnipay->setPublic(true);
27+
$omnipay->addArgument($gateways);
28+
29+
if ($disabledGateways = $config['disabled']) {
30+
$omnipay->addMethodCall('setDisabledGateways', [$disabledGateways]);
31+
}
32+
33+
if ($defaultGateway = $config['default']) {
34+
$allowedValues = array_diff($gatewayNames, $disabledGateways);
35+
36+
if (!in_array($defaultGateway, $gatewayNames, true)) {
37+
throw new InvalidConfigurationException(sprintf(
38+
'You cannot specify non-existing gateway (%s) as default. Allowed values: %s',
39+
$defaultGateway,
40+
implode(', ', $allowedValues)
41+
));
42+
}
43+
44+
if (in_array($defaultGateway, $disabledGateways, true)) {
45+
throw new InvalidConfigurationException(sprintf(
46+
'You cannot specify disabled gateway (%s) as default. Allowed values: %s',
47+
$defaultGateway,
48+
implode(', ', $allowedValues)
49+
));
50+
}
51+
52+
$omnipay->addMethodCall('setDefaultGatewayName', [$defaultGateway]);
53+
}
54+
55+
if ($initOnBoot = $config['init_on_boot']) {
56+
$omnipay->addMethodCall('initOnBoot', [$initOnBoot]);
57+
}
58+
}
59+
}

Manager/OmnipayManager.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Janwebdev\OmnipayBundle\Manager;
4+
5+
use Omnipay\Common\Http\Client;
6+
use Omnipay\Common\GatewayFactory;
7+
use Omnipay\Common\GatewayInterface;
8+
use Omnipay\Common\Helper;
9+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
10+
11+
class OmnipayManager
12+
{
13+
/**
14+
* @var GatewayFactory
15+
*/
16+
protected GatewayFactory $gatewayFactory;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected array $config;
22+
23+
/**
24+
* @var GatewayInterface[]
25+
*/
26+
protected array $storage;
27+
28+
/**
29+
* @var GatewayInterface[]
30+
*/
31+
protected array $registeredGateways = [];
32+
33+
/**
34+
* @var string[]
35+
*/
36+
protected array $disabledGateways = [];
37+
38+
/**
39+
* @var string|null
40+
*/
41+
protected ?string $defaultGatewayName = null;
42+
43+
/**
44+
* @var bool
45+
*/
46+
protected bool $initOnBoot = false;
47+
48+
public function __construct(GatewayFactory $gatewayFactory, array $config = [])
49+
{
50+
$this->gatewayFactory = $gatewayFactory;
51+
$this->config = $config;
52+
}
53+
54+
public function get(string $gatewayName): GatewayInterface
55+
{
56+
if (!isset($this->storage[$gatewayName])) {
57+
$gateway = $this->createGateway($gatewayName);
58+
$this->storage[$gatewayName] = $gateway;
59+
}
60+
61+
return $this->storage[$gatewayName];
62+
}
63+
64+
protected function createGateway(string $gatewayName): GatewayInterface
65+
{
66+
if (isset($this->registeredGateways[$gatewayName])) {
67+
$gateway = $this->registeredGateways[$gatewayName];
68+
} else {
69+
$gateway = $this->gatewayFactory->create($gatewayName, new Client());
70+
}
71+
72+
return $gateway->initialize($this->getGatewayConfig($gatewayName));
73+
}
74+
75+
protected function getGatewayConfig(string $gatewayName): array
76+
{
77+
return $this->config[$gatewayName] ?? [];
78+
}
79+
80+
public function registerGateway(GatewayInterface $gatewayInstance, ?string $alias = null): void
81+
{
82+
$name = $alias ?? Helper::getGatewayClassName(get_class($gatewayInstance));
83+
84+
if (in_array($name, $this->disabledGateways, true)) {
85+
return;
86+
}
87+
88+
$this->registeredGateways[$name] = $gatewayInstance;
89+
90+
if ($this->initOnBoot) {
91+
$gatewayInstance->initialize($this->getGatewayConfig($name));
92+
$this->storage[$name] = $gatewayInstance;
93+
}
94+
}
95+
96+
public function setDisabledGateways(array $disabledGateways): void
97+
{
98+
$this->disabledGateways = $disabledGateways;
99+
}
100+
101+
public function getDefaultGateway(): GatewayInterface
102+
{
103+
if (null === $this->defaultGatewayName) {
104+
throw new InvalidConfigurationException('Default gateway is not configured');
105+
}
106+
107+
return $this->get($this->defaultGatewayName);
108+
}
109+
110+
public function setDefaultGatewayName(string $defaultGatewayName): void
111+
{
112+
$this->defaultGatewayName = $defaultGatewayName;
113+
}
114+
115+
public function initOnBoot(bool $initOnBoot): void
116+
{
117+
$this->initOnBoot = $initOnBoot;
118+
}
119+
}

OmnipayBundle.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Janwebdev\OmnipayBundle;
4+
5+
use Janwebdev\OmnipayBundle\DependencyInjection\Compiler\GatewayTagCompilerPass;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\HttpKernel\Bundle\Bundle;
8+
9+
class OmnipayBundle extends Bundle
10+
{
11+
public function build(ContainerBuilder $container): void
12+
{
13+
parent::build($container);
14+
15+
$container->addCompilerPass(new GatewayTagCompilerPass());
16+
}
17+
}

0 commit comments

Comments
 (0)