FileLoaderLoadException.php 3.5 KB

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