Extensions.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  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 Twig\Extra\TwigExtraBundle;
  11. use Twig\Extra\Cache\CacheExtension;
  12. use Twig\Extra\CssInliner\CssInlinerExtension;
  13. use Twig\Extra\Html\HtmlExtension;
  14. use Twig\Extra\Inky\InkyExtension;
  15. use Twig\Extra\Intl\IntlExtension;
  16. use Twig\Extra\Markdown\MarkdownExtension;
  17. use Twig\Extra\String\StringExtension;
  18. final class Extensions
  19. {
  20. private const EXTENSIONS = [
  21. 'cache' => [
  22. 'name' => 'cache',
  23. 'class' => CacheExtension::class,
  24. 'class_name' => 'CacheExtension',
  25. 'package' => 'twig/cache-extra',
  26. 'filters' => [],
  27. 'functions' => [],
  28. 'tags' => ['cache'],
  29. ],
  30. 'html' => [
  31. 'name' => 'html',
  32. 'class' => HtmlExtension::class,
  33. 'class_name' => 'HtmlExtension',
  34. 'package' => 'twig/html-extra',
  35. 'filters' => ['data_uri'],
  36. 'functions' => ['html_classes'],
  37. 'tags' => [],
  38. ],
  39. 'markdown' => [
  40. 'name' => 'markdown',
  41. 'class' => MarkdownExtension::class,
  42. 'class_name' => 'MarkdownExtension',
  43. 'package' => 'twig/markdown-extra',
  44. 'filters' => ['html_to_markdown', 'markdown_to_html'],
  45. 'functions' => [],
  46. 'tags' => [],
  47. ],
  48. 'intl' => [
  49. 'name' => 'intl',
  50. 'class' => IntlExtension::class,
  51. 'class_name' => 'IntlExtension',
  52. 'package' => 'twig/intl-extra',
  53. 'filters' => ['country_name', 'currency_name', 'currency_symbol', 'language_name', 'locale_name', 'timezone_name',
  54. 'format_currency', 'format_number', 'format_decimal_number', 'format_currency_number',
  55. 'format_percent_number', 'format_scientific_number', 'format_spellout_number', 'format_ordinal_number',
  56. 'format_duration_number', 'format_date', 'format_datetime', 'format_time',
  57. ],
  58. 'functions' => ['country_timezones'],
  59. 'tags' => [],
  60. ],
  61. 'cssinliner' => [
  62. 'name' => 'cssinliner',
  63. 'class' => CssInlinerExtension::class,
  64. 'class_name' => 'CssInlinerExtension',
  65. 'package' => 'twig/cssinliner-extra',
  66. 'filters' => ['inline_css'],
  67. 'functions' => [],
  68. 'tags' => [],
  69. ],
  70. 'inky' => [
  71. 'name' => 'inky',
  72. 'class' => InkyExtension::class,
  73. 'class_name' => 'InkyExtension',
  74. 'package' => 'twig/inky-extra',
  75. 'filters' => ['inky_to_html'],
  76. 'functions' => [],
  77. 'tags' => [],
  78. ],
  79. 'string' => [
  80. 'name' => 'string',
  81. 'class' => StringExtension::class,
  82. 'class_name' => 'StringExtension',
  83. 'package' => 'twig/string-extra',
  84. 'filters' => ['u'],
  85. 'functions' => [],
  86. 'tags' => [],
  87. ],
  88. ];
  89. public static function getClasses(): array
  90. {
  91. return array_column(self::EXTENSIONS, 'class', 'name');
  92. }
  93. public static function getFilter(string $name): array
  94. {
  95. foreach (self::EXTENSIONS as $extension) {
  96. if (\in_array($name, $extension['filters'])) {
  97. return [$extension['class_name'], $extension['package']];
  98. }
  99. }
  100. return [];
  101. }
  102. public static function getFunction(string $name): array
  103. {
  104. foreach (self::EXTENSIONS as $extension) {
  105. if (\in_array($name, $extension['functions'])) {
  106. return [$extension['class_name'], $extension['package']];
  107. }
  108. }
  109. return [];
  110. }
  111. public static function getTag(string $name): array
  112. {
  113. foreach (self::EXTENSIONS as $extension) {
  114. if (\in_array($name, $extension['tags'])) {
  115. return [$extension['class_name'], $extension['package']];
  116. }
  117. }
  118. return [];
  119. }
  120. }