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
|
|
|
namespace Sylius\Component\Core\Provider; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
15
|
|
|
use Sylius\Component\Channel\Context\ChannelNotFoundException; |
16
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
17
|
|
|
use Sylius\Component\Locale\Context\LocaleNotFoundException; |
18
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
19
|
|
|
use Sylius\Component\Locale\Provider\LocaleProviderInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Kamil Kokot <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class ChannelBasedLocaleProvider implements LocaleProviderInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ChannelContextInterface |
28
|
|
|
*/ |
29
|
|
|
private $channelContext; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $defaultLocaleCode; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ChannelContextInterface $channelContext |
38
|
|
|
* @param string $defaultLocaleCode |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ChannelContextInterface $channelContext, $defaultLocaleCode) |
41
|
|
|
{ |
42
|
|
|
$this->channelContext = $channelContext; |
43
|
|
|
$this->defaultLocaleCode = $defaultLocaleCode; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getAvailableLocalesCodes() |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
/** @var ChannelInterface $channel */ |
53
|
|
|
$channel = $this->channelContext->getChannel(); |
54
|
|
|
|
55
|
|
|
return $channel |
56
|
|
|
->getLocales() |
57
|
|
|
->filter(function (LocaleInterface $locale) { |
58
|
|
|
return $locale->isEnabled(); |
59
|
|
|
}) |
60
|
|
|
->map(function (LocaleInterface $locale) { |
61
|
|
|
return $locale->getCode(); |
62
|
|
|
}) |
63
|
|
|
->toArray() |
64
|
|
|
; |
65
|
|
|
} catch (ChannelNotFoundException $exception) { |
66
|
|
|
return [$this->defaultLocaleCode]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getDefaultLocaleCode() |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
/** @var ChannelInterface $channel */ |
77
|
|
|
$channel = $this->channelContext->getChannel(); |
78
|
|
|
|
79
|
|
|
return $channel->getDefaultLocale()->getCode(); |
80
|
|
|
} catch (ChannelNotFoundException $exception) { |
81
|
|
|
return $this->defaultLocaleCode; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|