forked from Sylius/Sylius
/
SyliusShopExtension.php
111 lines (97 loc) · 3.97 KB
/
SyliusShopExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ShopBundle\DependencyInjection;
use Sylius\Bundle\CoreBundle\Checkout\CheckoutRedirectListener;
use Sylius\Bundle\CoreBundle\Checkout\CheckoutResolver;
use Sylius\Bundle\CoreBundle\Checkout\CheckoutStateUrlGenerator;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
final class SyliusShopExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $config, ContainerBuilder $container): void
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $config);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$loader->load(sprintf('services/integrations/locale/%s.xml', $config['locale_switcher']));
$container->setParameter('sylius_shop.firewall_context_name', $config['firewall_context_name']);
$container->setParameter(
'sylius_shop.product_grid.include_all_descendants',
$config['product_grid']['include_all_descendants']
);
$this->configureCheckoutResolverIfNeeded($config['checkout_resolver'], $container);
}
/**
* @param array $config
* @param ContainerBuilder $container
*/
private function configureCheckoutResolverIfNeeded(array $config, ContainerBuilder $container): void
{
if (!$config['enabled']) {
return;
}
$checkoutResolverDefinition = new Definition(CheckoutResolver::class,
[
new Reference('sylius.context.cart'),
new Reference('sylius.router.checkout_state'),
new Definition(RequestMatcher::class, [$config['pattern']]),
new Reference('sm.factory'),
]
);
$checkoutResolverDefinition->addTag('kernel.event_subscriber');
$checkoutStateUrlGeneratorDefinition = new Definition(
CheckoutStateUrlGenerator::class,
[
new Reference('router'),
$config['route_map'],
]
);
$container->setDefinition('sylius.resolver.checkout', $checkoutResolverDefinition);
$container->setDefinition('sylius.listener.checkout_redirect', $this->registerCheckoutRedirectListener($config));
$container->setDefinition('sylius.router.checkout_state', $checkoutStateUrlGeneratorDefinition);
}
/**
* @param array $config
*
* @return Definition
*/
private function registerCheckoutRedirectListener(array $config): Definition
{
$checkoutRedirectListener = new Definition(CheckoutRedirectListener::class, [
new Reference('request_stack'),
new Reference('sylius.router.checkout_state'),
new Definition(RequestMatcher::class, [$config['pattern']]),
]);
$checkoutRedirectListener
->addTag('kernel.event_listener', [
'event' => 'sylius.order.post_address',
'method' => 'handleCheckoutRedirect',
])
->addTag('kernel.event_listener', [
'event' => 'sylius.order.post_select_shipping',
'method' => 'handleCheckoutRedirect',
])
->addTag('kernel.event_listener', [
'event' => 'sylius.order.post_payment',
'method' => 'handleCheckoutRedirect',
])
;
return $checkoutRedirectListener;
}
}