Currency.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Locale;
  7. class Currency implements \Magento\Framework\Locale\CurrencyInterface
  8. {
  9. /**
  10. * Default currency
  11. */
  12. const DEFAULT_CURRENCY = 'USD';
  13. /**#@+
  14. * Currency Options
  15. */
  16. const CURRENCY_OPTION_SYMBOL = 'symbol';
  17. const CURRENCY_OPTION_CURRENCY = 'currency';
  18. const CURRENCY_OPTION_NAME = 'name';
  19. const CURRENCY_OPTION_DISPLAY = 'display';
  20. /**
  21. * @var array
  22. */
  23. protected static $_currencyCache = [];
  24. /**
  25. * Core event manager proxy
  26. *
  27. * @var \Magento\Framework\Event\ManagerInterface
  28. */
  29. protected $_eventManager = null;
  30. /**
  31. * @var \Magento\Framework\Locale\ResolverInterface
  32. */
  33. protected $_localeResolver;
  34. /**
  35. * @var \Magento\Framework\CurrencyFactory
  36. */
  37. protected $_currencyFactory;
  38. /**
  39. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  40. * @param ResolverInterface $localeResolver
  41. * @param \Magento\Framework\CurrencyFactory $currencyFactory
  42. */
  43. public function __construct(
  44. \Magento\Framework\Event\ManagerInterface $eventManager,
  45. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  46. \Magento\Framework\CurrencyFactory $currencyFactory
  47. ) {
  48. $this->_eventManager = $eventManager;
  49. $this->_localeResolver = $localeResolver;
  50. $this->_currencyFactory = $currencyFactory;
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function getDefaultCurrency()
  56. {
  57. return self::DEFAULT_CURRENCY;
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function getCurrency($currency)
  63. {
  64. \Magento\Framework\Profiler::start('locale/currency');
  65. if (!isset(self::$_currencyCache[$this->_localeResolver->getLocale()][$currency])) {
  66. $options = [];
  67. try {
  68. $currencyObject = $this->_currencyFactory->create(
  69. ['options' => $currency, 'locale' => $this->_localeResolver->getLocale()]
  70. );
  71. } catch (\Exception $e) {
  72. $currencyObject = $this->_currencyFactory->create(
  73. ['options' => $this->getDefaultCurrency(), 'locale' => $this->_localeResolver->getLocale()]
  74. );
  75. $options[self::CURRENCY_OPTION_NAME] = $currency;
  76. $options[self::CURRENCY_OPTION_CURRENCY] = $currency;
  77. $options[self::CURRENCY_OPTION_SYMBOL] = $currency;
  78. }
  79. $options = new \Magento\Framework\DataObject($options);
  80. $this->_eventManager->dispatch(
  81. 'currency_display_options_forming',
  82. ['currency_options' => $options, 'base_code' => $currency]
  83. );
  84. $currencyObject->setFormat($options->toArray());
  85. self::$_currencyCache[$this->_localeResolver->getLocale()][$currency] = $currencyObject;
  86. }
  87. \Magento\Framework\Profiler::stop('locale/currency');
  88. return self::$_currencyCache[$this->_localeResolver->getLocale()][$currency];
  89. }
  90. }