AutowiringFailedException.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Exception;
  11. /**
  12. * Thrown when a definition cannot be autowired.
  13. */
  14. class AutowiringFailedException extends RuntimeException
  15. {
  16. private $serviceId;
  17. private $messageCallback;
  18. public function __construct(string $serviceId, $message = '', int $code = 0, \Throwable $previous = null)
  19. {
  20. $this->serviceId = $serviceId;
  21. if ($message instanceof \Closure
  22. && (\function_exists('xdebug_is_enabled') ? xdebug_is_enabled() : \function_exists('xdebug_info'))
  23. ) {
  24. $message = $message();
  25. }
  26. if (!$message instanceof \Closure) {
  27. parent::__construct($message, $code, $previous);
  28. return;
  29. }
  30. $this->messageCallback = $message;
  31. parent::__construct('', $code, $previous);
  32. $this->message = new class($this->message, $this->messageCallback) {
  33. private $message;
  34. private $messageCallback;
  35. public function __construct(&$message, &$messageCallback)
  36. {
  37. $this->message = &$message;
  38. $this->messageCallback = &$messageCallback;
  39. }
  40. public function __toString(): string
  41. {
  42. $messageCallback = $this->messageCallback;
  43. $this->messageCallback = null;
  44. try {
  45. return $this->message = $messageCallback();
  46. } catch (\Throwable $e) {
  47. return $this->message = $e->getMessage();
  48. }
  49. }
  50. };
  51. }
  52. public function getMessageCallback(): ?\Closure
  53. {
  54. return $this->messageCallback;
  55. }
  56. public function getServiceId()
  57. {
  58. return $this->serviceId;
  59. }
  60. }