|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016 |
|
6
|
|
|
* @package Slim |
|
7
|
|
|
* @subpackage Base |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Aimeos\Slim\Base; |
|
11
|
|
|
|
|
12
|
|
|
use Interop\Container\ContainerInterface; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Service providing the locale objects |
|
17
|
|
|
* |
|
18
|
|
|
* @package Slim |
|
19
|
|
|
* @subpackage Base |
|
20
|
|
|
*/ |
|
21
|
|
|
class Locale |
|
22
|
|
|
{ |
|
23
|
|
|
private $container; |
|
24
|
|
|
private $locale; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Initializes the object |
|
29
|
|
|
* |
|
30
|
|
|
* @param ContainerInterface $container Dependency container |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( ContainerInterface $container ) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->container = $container; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the locale item for the current request |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
42
|
|
|
* @param array $attributes Associative list of URL parameter |
|
43
|
|
|
* @return \Aimeos\MShop\Locale\Item\Iface Locale item object |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get( \Aimeos\MShop\Context\Item\Iface $context, array $attributes ) |
|
46
|
|
|
{ |
|
47
|
|
|
if( $this->locale === null ) |
|
48
|
|
|
{ |
|
49
|
|
|
$disableSites = $this->container->get( 'aimeos_config' )->get( 'disableSites', true ); |
|
50
|
|
|
|
|
51
|
|
|
$site = ( isset( $attributes['site'] ) ? $attributes['site'] : 'default' ); |
|
52
|
|
|
$lang = ( isset( $attributes['locale'] ) ? $attributes['locale'] : '' ); |
|
53
|
|
|
$currency = ( isset( $attributes['currency'] ) ? $attributes['currency'] : '' ); |
|
54
|
|
|
|
|
55
|
|
|
$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context ); |
|
56
|
|
|
$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->locale; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the locale item for the current request |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
67
|
|
|
* @param string $site Unique site code |
|
68
|
|
|
* @return \Aimeos\MShop\Locale\Item\Iface Locale item object |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
public function getBackend( \Aimeos\MShop\Context\Item\Iface $context, $site ) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' ); |
|
73
|
|
|
|
|
74
|
|
|
try |
|
75
|
|
|
{ |
|
76
|
|
|
$localeItem = $localeManager->bootstrap( $site, '', '', false ); |
|
77
|
|
|
$localeItem->setLanguageId( null ); |
|
78
|
|
|
$localeItem->setCurrencyId( null ); |
|
79
|
|
|
} |
|
80
|
|
|
catch( \Aimeos\MShop\Locale\Exception $e ) |
|
81
|
|
|
{ |
|
82
|
|
|
$localeItem = $localeManager->createItem(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $localeItem; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.