UndefinedCallableHandler.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Bridge\Twig;
  11. use Symfony\Bundle\FullStack;
  12. use Twig\Error\SyntaxError;
  13. /**
  14. * @internal
  15. */
  16. class UndefinedCallableHandler
  17. {
  18. private const FILTER_COMPONENTS = [
  19. 'humanize' => 'form',
  20. 'trans' => 'translation',
  21. 'yaml_encode' => 'yaml',
  22. 'yaml_dump' => 'yaml',
  23. ];
  24. private const FUNCTION_COMPONENTS = [
  25. 'asset' => 'asset',
  26. 'asset_version' => 'asset',
  27. 'dump' => 'debug-bundle',
  28. 'expression' => 'expression-language',
  29. 'form_widget' => 'form',
  30. 'form_errors' => 'form',
  31. 'form_label' => 'form',
  32. 'form_help' => 'form',
  33. 'form_row' => 'form',
  34. 'form_rest' => 'form',
  35. 'form' => 'form',
  36. 'form_start' => 'form',
  37. 'form_end' => 'form',
  38. 'csrf_token' => 'form',
  39. 'logout_url' => 'security-http',
  40. 'logout_path' => 'security-http',
  41. 'is_granted' => 'security-core',
  42. 'link' => 'web-link',
  43. 'preload' => 'web-link',
  44. 'dns_prefetch' => 'web-link',
  45. 'preconnect' => 'web-link',
  46. 'prefetch' => 'web-link',
  47. 'prerender' => 'web-link',
  48. 'workflow_can' => 'workflow',
  49. 'workflow_transitions' => 'workflow',
  50. 'workflow_has_marked_place' => 'workflow',
  51. 'workflow_marked_places' => 'workflow',
  52. ];
  53. private const FULL_STACK_ENABLE = [
  54. 'form' => 'enable "framework.form"',
  55. 'security-core' => 'add the "SecurityBundle"',
  56. 'security-http' => 'add the "SecurityBundle"',
  57. 'web-link' => 'enable "framework.web_link"',
  58. 'workflow' => 'enable "framework.workflows"',
  59. ];
  60. public static function onUndefinedFilter(string $name): bool
  61. {
  62. if (!isset(self::FILTER_COMPONENTS[$name])) {
  63. return false;
  64. }
  65. self::onUndefined($name, 'filter', self::FILTER_COMPONENTS[$name]);
  66. return true;
  67. }
  68. public static function onUndefinedFunction(string $name): bool
  69. {
  70. if (!isset(self::FUNCTION_COMPONENTS[$name])) {
  71. return false;
  72. }
  73. self::onUndefined($name, 'function', self::FUNCTION_COMPONENTS[$name]);
  74. return true;
  75. }
  76. private static function onUndefined(string $name, string $type, string $component)
  77. {
  78. if (class_exists(FullStack::class) && isset(self::FULL_STACK_ENABLE[$component])) {
  79. throw new SyntaxError(sprintf('Did you forget to %s? Unknown %s "%s".', self::FULL_STACK_ENABLE[$component], $type, $name));
  80. }
  81. throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown %s "%s".', $component, $type, $name));
  82. }
  83. }