FileExtensionEscapingStrategy.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2015 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. /**
  11. * Default autoescaping strategy based on file names.
  12. *
  13. * This strategy sets the HTML as the default autoescaping strategy,
  14. * but changes it based on the filename.
  15. *
  16. * Note that there is no runtime performance impact as the
  17. * default autoescaping strategy is set at compilation time.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class Twig_FileExtensionEscapingStrategy
  22. {
  23. /**
  24. * Guesses the best autoescaping strategy based on the file name.
  25. *
  26. * @param string $filename The template file name
  27. *
  28. * @return string|false The escaping strategy name to use or false to disable
  29. */
  30. public static function guess($filename)
  31. {
  32. if (in_array(substr($filename, -1), array('/', '\\'))) {
  33. return 'html'; // return html for directories
  34. }
  35. if ('.twig' === substr($filename, -5)) {
  36. $filename = substr($filename, 0, -5);
  37. }
  38. $extension = pathinfo($filename, PATHINFO_EXTENSION);
  39. switch ($extension) {
  40. case 'js':
  41. return 'js';
  42. case 'css':
  43. return 'css';
  44. case 'txt':
  45. return false;
  46. default:
  47. return 'html';
  48. }
  49. }
  50. }