1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\CoreBundle\Collector; |
15
|
|
|
|
16
|
|
|
use Sylius\Bundle\CoreBundle\Application\Kernel; |
17
|
|
|
use Sylius\Component\Channel\Context\ChannelNotFoundException; |
18
|
|
|
use Sylius\Component\Core\Context\ShopperContextInterface; |
19
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
20
|
|
|
use Sylius\Component\Currency\Context\CurrencyNotFoundException; |
21
|
|
|
use Sylius\Component\Locale\Context\LocaleNotFoundException; |
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
25
|
|
|
|
26
|
|
|
final class SyliusCollector extends DataCollector |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ShopperContextInterface |
30
|
|
|
*/ |
31
|
|
|
private $shopperContext; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ShopperContextInterface $shopperContext |
35
|
|
|
* @param array $bundles |
36
|
|
|
* @param string $defaultLocaleCode |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
ShopperContextInterface $shopperContext, |
40
|
|
|
array $bundles, |
41
|
|
|
string $defaultLocaleCode |
42
|
|
|
) { |
43
|
|
|
$this->shopperContext = $shopperContext; |
44
|
|
|
|
45
|
|
|
$this->data = [ |
46
|
|
|
'version' => Kernel::VERSION, |
47
|
|
|
'base_currency_code' => null, |
48
|
|
|
'currency_code' => null, |
49
|
|
|
'default_locale_code' => $defaultLocaleCode, |
50
|
|
|
'locale_code' => null, |
51
|
|
|
'extensions' => [ |
52
|
|
|
'SyliusAdminApiBundle' => ['name' => 'API', 'enabled' => false], |
53
|
|
|
'SyliusAdminBundle' => ['name' => 'Admin', 'enabled' => false], |
54
|
|
|
'SyliusShopBundle' => ['name' => 'Shop', 'enabled' => false], |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
foreach (array_keys($this->data['extensions']) as $bundleName) { |
59
|
|
|
if (isset($bundles[$bundleName])) { |
60
|
|
|
$this->data['extensions'][$bundleName]['enabled'] = true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getVersion(): string |
69
|
|
|
{ |
70
|
|
|
return $this->data['version']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getExtensions(): array |
77
|
|
|
{ |
78
|
|
|
return $this->data['extensions']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getCurrencyCode(): ?string |
85
|
|
|
{ |
86
|
|
|
return $this->data['currency_code']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getLocaleCode(): ?string |
93
|
|
|
{ |
94
|
|
|
return $this->data['locale_code']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getDefaultCurrencyCode(): ?string |
101
|
|
|
{ |
102
|
|
|
return $this->data['base_currency_code']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getDefaultLocaleCode(): ?string |
109
|
|
|
{ |
110
|
|
|
return $this->data['default_locale_code']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null): void |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
|
|
/** @var ChannelInterface $channel */ |
120
|
|
|
$channel = $this->shopperContext->getChannel(); |
121
|
|
|
|
122
|
|
|
$this->data['base_currency_code'] = $channel->getBaseCurrency()->getCode(); |
123
|
|
|
$this->data['currency_code'] = $this->shopperContext->getCurrencyCode(); |
124
|
|
|
} catch (ChannelNotFoundException | CurrencyNotFoundException $exception) { |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
try { |
128
|
|
|
$this->data['locale_code'] = $this->shopperContext->getLocaleCode(); |
129
|
|
|
} catch (LocaleNotFoundException $exception) { |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function reset(): void |
137
|
|
|
{ |
138
|
|
|
$this->data['base_currency_code'] = null; |
139
|
|
|
$this->data['currency_code'] = null; |
140
|
|
|
$this->data['locale_code'] = null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function getName(): string |
147
|
|
|
{ |
148
|
|
|
return 'sylius_core'; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|