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