WriteCacheFileTrait.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * @package Grav\Common\Twig
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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. trait WriteCacheFileTrait
  12. {
  13. protected static $umask;
  14. /**
  15. * This exists so template cache files use the same
  16. * group between apache and cli
  17. *
  18. * @param string $file
  19. * @param string $content
  20. */
  21. protected function writeCacheFile($file, $content)
  22. {
  23. if (empty($file)) {
  24. return;
  25. }
  26. if (!isset(self::$umask)) {
  27. self::$umask = Grav::instance()['config']->get('system.twig.umask_fix', false);
  28. }
  29. if (self::$umask) {
  30. $dir = dirname($file);
  31. if (!is_dir($dir)) {
  32. $old = umask(0002);
  33. Folder::create($dir);
  34. umask($old);
  35. }
  36. parent::writeCacheFile($file, $content);
  37. chmod($file, 0775);
  38. } else {
  39. parent::writeCacheFile($file, $content);
  40. }
  41. }
  42. }