-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathCurrency.php
104 lines (90 loc) · 3.09 KB
/
Currency.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Locale;
class Currency implements \Magento\Framework\Locale\CurrencyInterface
{
/**
* Default currency
*/
const DEFAULT_CURRENCY = 'USD';
/**#@+
* Currency Options
*/
const CURRENCY_OPTION_SYMBOL = 'symbol';
const CURRENCY_OPTION_CURRENCY = 'currency';
const CURRENCY_OPTION_NAME = 'name';
const CURRENCY_OPTION_DISPLAY = 'display';
/**
* @var array
*/
protected static $_currencyCache = [];
/**
* Core event manager proxy
*
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $_eventManager = null;
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
protected $_localeResolver;
/**
* @var \Magento\Framework\CurrencyFactory
*/
protected $_currencyFactory;
/**
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param ResolverInterface $localeResolver
* @param \Magento\Framework\CurrencyFactory $currencyFactory
*/
public function __construct(
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
\Magento\Framework\CurrencyFactory $currencyFactory
) {
$this->_eventManager = $eventManager;
$this->_localeResolver = $localeResolver;
$this->_currencyFactory = $currencyFactory;
}
/**
* @inheritdoc
*/
public function getDefaultCurrency()
{
return self::DEFAULT_CURRENCY;
}
/**
* @inheritdoc
*/
public function getCurrency($currency)
{
\Magento\Framework\Profiler::start('locale/currency');
if (!isset(self::$_currencyCache[$this->_localeResolver->getLocale()][$currency])) {
$options = [];
try {
$currencyObject = $this->_currencyFactory->create(
['options' => $currency, 'locale' => $this->_localeResolver->getLocale()]
);
} catch (\Exception $e) {
$currencyObject = $this->_currencyFactory->create(
['options' => $this->getDefaultCurrency(), 'locale' => $this->_localeResolver->getLocale()]
);
$options[self::CURRENCY_OPTION_NAME] = $currency;
$options[self::CURRENCY_OPTION_CURRENCY] = $currency;
$options[self::CURRENCY_OPTION_SYMBOL] = $currency;
}
$options = new \Magento\Framework\DataObject($options);
$this->_eventManager->dispatch(
'currency_display_options_forming',
['currency_options' => $options, 'base_code' => $currency]
);
$currencyObject->setFormat($options->toArray());
self::$_currencyCache[$this->_localeResolver->getLocale()][$currency] = $currencyObject;
}
\Magento\Framework\Profiler::stop('locale/currency');
return self::$_currencyCache[$this->_localeResolver->getLocale()][$currency];
}
}