菜谱项目

UndefinedFunctionFatalErrorHandler.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Debug\FatalErrorHandler;
  11. use Symfony\Component\Debug\Exception\UndefinedFunctionException;
  12. use Symfony\Component\Debug\Exception\FatalErrorException;
  13. /**
  14. * ErrorHandler for undefined functions.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class UndefinedFunctionFatalErrorHandler implements FatalErrorHandlerInterface
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function handleError(array $error, FatalErrorException $exception)
  24. {
  25. $messageLen = strlen($error['message']);
  26. $notFoundSuffix = '()';
  27. $notFoundSuffixLen = strlen($notFoundSuffix);
  28. if ($notFoundSuffixLen > $messageLen) {
  29. return;
  30. }
  31. if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) {
  32. return;
  33. }
  34. $prefix = 'Call to undefined function ';
  35. $prefixLen = strlen($prefix);
  36. if (0 !== strpos($error['message'], $prefix)) {
  37. return;
  38. }
  39. $fullyQualifiedFunctionName = substr($error['message'], $prefixLen, -$notFoundSuffixLen);
  40. if (false !== $namespaceSeparatorIndex = strrpos($fullyQualifiedFunctionName, '\\')) {
  41. $functionName = substr($fullyQualifiedFunctionName, $namespaceSeparatorIndex + 1);
  42. $namespacePrefix = substr($fullyQualifiedFunctionName, 0, $namespaceSeparatorIndex);
  43. $message = sprintf('Attempted to call function "%s" from namespace "%s".', $functionName, $namespacePrefix);
  44. } else {
  45. $functionName = $fullyQualifiedFunctionName;
  46. $message = sprintf('Attempted to call function "%s" from the global namespace.', $functionName);
  47. }
  48. $candidates = array();
  49. foreach (get_defined_functions() as $type => $definedFunctionNames) {
  50. foreach ($definedFunctionNames as $definedFunctionName) {
  51. if (false !== $namespaceSeparatorIndex = strrpos($definedFunctionName, '\\')) {
  52. $definedFunctionNameBasename = substr($definedFunctionName, $namespaceSeparatorIndex + 1);
  53. } else {
  54. $definedFunctionNameBasename = $definedFunctionName;
  55. }
  56. if ($definedFunctionNameBasename === $functionName) {
  57. $candidates[] = '\\'.$definedFunctionName;
  58. }
  59. }
  60. }
  61. if ($candidates) {
  62. sort($candidates);
  63. $last = array_pop($candidates).'"?';
  64. if ($candidates) {
  65. $candidates = 'e.g. "'.implode('", "', $candidates).'" or "'.$last;
  66. } else {
  67. $candidates = '"'.$last;
  68. }
  69. $message .= "\nDid you mean to call ".$candidates;
  70. }
  71. return new UndefinedFunctionException($message, $exception);
  72. }
  73. }