LocalizedException.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Localized Exception
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Exception;
  9. use Magento\Framework\Phrase;
  10. use Magento\Framework\Phrase\Renderer\Placeholder;
  11. /**
  12. * Localized exception
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class LocalizedException extends \Exception
  18. {
  19. /**
  20. * @var \Magento\Framework\Phrase
  21. */
  22. protected $phrase;
  23. /**
  24. * @var string
  25. */
  26. protected $logMessage;
  27. /**
  28. * @param \Magento\Framework\Phrase $phrase
  29. * @param \Exception $cause
  30. * @param int $code
  31. */
  32. public function __construct(Phrase $phrase, \Exception $cause = null, $code = 0)
  33. {
  34. $this->phrase = $phrase;
  35. parent::__construct($phrase->render(), (int)$code, $cause);
  36. }
  37. /**
  38. * Get the un-processed message, without the parameters filled in
  39. *
  40. * @return string
  41. */
  42. public function getRawMessage()
  43. {
  44. return $this->phrase->getText();
  45. }
  46. /**
  47. * Get parameters, corresponding to placeholders in raw exception message
  48. *
  49. * @return array
  50. */
  51. public function getParameters()
  52. {
  53. return $this->phrase->getArguments();
  54. }
  55. /**
  56. * Get the un-localized message, but with the parameters filled in
  57. *
  58. * @return string
  59. */
  60. public function getLogMessage()
  61. {
  62. if ($this->logMessage === null) {
  63. $renderer = new Placeholder();
  64. $this->logMessage = $renderer->render([$this->getRawMessage()], $this->getParameters());
  65. }
  66. return $this->logMessage;
  67. }
  68. }