LoaderLoadException.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Config\Exception;
  11. /**
  12. * Exception class for when a resource cannot be loaded or imported.
  13. *
  14. * @author Ryan Weaver <ryan@thatsquality.com>
  15. */
  16. class LoaderLoadException extends \Exception
  17. {
  18. /**
  19. * @param string $resource The resource that could not be imported
  20. * @param string|null $sourceResource The original resource importing the new resource
  21. * @param int|null $code The error code
  22. * @param \Throwable|null $previous A previous exception
  23. * @param string|null $type The type of resource
  24. */
  25. public function __construct(string $resource, string $sourceResource = null, ?int $code = 0, \Throwable $previous = null, string $type = null)
  26. {
  27. $message = '';
  28. if ($previous) {
  29. // Include the previous exception, to help the user see what might be the underlying cause
  30. // Trim the trailing period of the previous message. We only want 1 period remove so no rtrim...
  31. if ('.' === substr($previous->getMessage(), -1)) {
  32. $trimmedMessage = substr($previous->getMessage(), 0, -1);
  33. $message .= sprintf('%s', $trimmedMessage).' in ';
  34. } else {
  35. $message .= sprintf('%s', $previous->getMessage()).' in ';
  36. }
  37. $message .= $resource.' ';
  38. // show tweaked trace to complete the human readable sentence
  39. if (null === $sourceResource) {
  40. $message .= sprintf('(which is loaded in resource "%s")', $resource);
  41. } else {
  42. $message .= sprintf('(which is being imported from "%s")', $sourceResource);
  43. }
  44. $message .= '.';
  45. // if there's no previous message, present it the default way
  46. } elseif (null === $sourceResource) {
  47. $message .= sprintf('Cannot load resource "%s".', $resource);
  48. } else {
  49. $message .= sprintf('Cannot import resource "%s" from "%s".', $resource, $sourceResource);
  50. }
  51. // Is the resource located inside a bundle?
  52. if ('@' === $resource[0]) {
  53. $parts = explode(\DIRECTORY_SEPARATOR, $resource);
  54. $bundle = substr($parts[0], 1);
  55. $message .= sprintf(' Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.', $bundle);
  56. $message .= sprintf(' If the bundle is registered, make sure the bundle path "%s" is not empty.', $resource);
  57. } elseif (null !== $type) {
  58. // maybe there is no loader for this specific type
  59. if ('annotation' === $type) {
  60. $message .= ' Make sure annotations are installed and enabled.';
  61. } else {
  62. $message .= sprintf(' Make sure there is a loader supporting the "%s" type.', $type);
  63. }
  64. }
  65. parent::__construct($message, $code, $previous);
  66. }
  67. protected function varToString($var)
  68. {
  69. if (\is_object($var)) {
  70. return sprintf('Object(%s)', \get_class($var));
  71. }
  72. if (\is_array($var)) {
  73. $a = [];
  74. foreach ($var as $k => $v) {
  75. $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  76. }
  77. return sprintf('Array(%s)', implode(', ', $a));
  78. }
  79. if (\is_resource($var)) {
  80. return sprintf('Resource(%s)', get_resource_type($var));
  81. }
  82. if (null === $var) {
  83. return 'null';
  84. }
  85. if (false === $var) {
  86. return 'false';
  87. }
  88. if (true === $var) {
  89. return 'true';
  90. }
  91. return (string) $var;
  92. }
  93. }