FileLoaderLoadException.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 integer $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. if (null === $sourceResource) {
  27. $message = sprintf('Cannot load resource "%s".', $this->varToString($resource));
  28. } else {
  29. $message = sprintf('Cannot import resource "%s" from "%s".', $this->varToString($resource), $this->varToString($sourceResource));
  30. }
  31. // Is the resource located inside a bundle?
  32. if ('@' === $resource[0]) {
  33. $parts = explode(DIRECTORY_SEPARATOR, $resource);
  34. $bundle = substr($parts[0], 1);
  35. $message .= ' '.sprintf('Make sure the "%s" bundle is correctly registered and loaded in the application kernel class.', $bundle);
  36. } elseif ($previous) {
  37. // include the previous exception, to help the user see what might be the underlying cause
  38. $message .= ' '.sprintf('(%s)', $previous->getMessage());
  39. }
  40. parent::__construct($message, $code, $previous);
  41. }
  42. protected function varToString($var)
  43. {
  44. if (is_object($var)) {
  45. return sprintf('Object(%s)', get_class($var));
  46. }
  47. if (is_array($var)) {
  48. $a = array();
  49. foreach ($var as $k => $v) {
  50. $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  51. }
  52. return sprintf("Array(%s)", implode(', ', $a));
  53. }
  54. if (is_resource($var)) {
  55. return sprintf('Resource(%s)', get_resource_type($var));
  56. }
  57. if (null === $var) {
  58. return 'null';
  59. }
  60. if (false === $var) {
  61. return 'false';
  62. }
  63. if (true === $var) {
  64. return 'true';
  65. }
  66. return (string) $var;
  67. }
  68. }