WriteCacheFileTrait.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * @package Grav\Common\Twig
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Twig;
  9. use Grav\Common\Filesystem\Folder;
  10. use Grav\Common\Grav;
  11. use function dirname;
  12. /**
  13. * Trait WriteCacheFileTrait
  14. * @package Grav\Common\Twig
  15. */
  16. trait WriteCacheFileTrait
  17. {
  18. /** @var bool */
  19. protected static $umask;
  20. /**
  21. * This exists so template cache files use the same
  22. * group between apache and cli
  23. *
  24. * @param string $file
  25. * @param string $content
  26. * @return void
  27. */
  28. protected function writeCacheFile($file, $content)
  29. {
  30. if (empty($file)) {
  31. return;
  32. }
  33. if (!isset(self::$umask)) {
  34. self::$umask = Grav::instance()['config']->get('system.twig.umask_fix', false);
  35. }
  36. if (self::$umask) {
  37. $dir = dirname($file);
  38. if (!is_dir($dir)) {
  39. $old = umask(0002);
  40. Folder::create($dir);
  41. umask($old);
  42. }
  43. parent::writeCacheFile($file, $content);
  44. chmod($file, 0775);
  45. } else {
  46. parent::writeCacheFile($file, $content);
  47. }
  48. }
  49. }