<?php
namespace Moto\Twig\Cache; use RuntimeException; class Filesystem extends \Twig_Cache_Filesystem { protected $directory; protected $options; public function write($key, $content) { $dir = dirname($key); if (!is_dir($dir)) { if (false === @mkdir($dir, 0777, true)) { clearstatcache(true, $dir); if (!is_dir($dir)) { throw new RuntimeException(sprintf('Unable to create the cache directory (%s).', $dir)); } } } elseif (!is_writable($dir)) { throw new RuntimeException(sprintf('Unable to write in the cache directory (%s).', $dir)); } $tmpFile = $key . '.' . microtime(1) . '.' . mt_rand(1000000, 9000000) . '.tmp'; if (@file_put_contents($tmpFile, $content) === false) { throw new RuntimeException(sprintf('Failed to write cache file "%s".', $key)); } if (!@rename($tmpFile, $key)) { clearstatcache(true, $tmpFile); clearstatcache(true, $key); usleep(50); if (!@rename($tmpFile, $key)) { throw new RuntimeException(sprintf('Failed to write cache file "%s".', $key)); } } @chmod($key, 0666 & ~umask()); clearstatcache(true, $key); if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) { if (function_exists('opcache_invalidate')) { opcache_invalidate($key, true); } elseif (function_exists('apc_compile_file')) { apc_compile_file($key); } } return; } } 