【问题标题】:Change Twig cache file permissions to clear cache更改 Twig 缓存文件权限以清除缓存
【发布时间】:2019-02-01 08:45:47
【问题描述】:

在我正在处理的网络服务器上,Twig 1.27 使用 Apache 用户和权限 755 创建缓存文件。

$ ls -la cache/
total 4
drwxrwxrwx 5 apache       apache         33 Jan 31 09:40 .
drwxrwxrwx 5 apache       apache         4096 Jan 31 02:39 ..
drwxr-xr-x 2 apache       apache         81 Jan 31 09:40 08
drwxr-xr-x 2 apache       apache         81 Jan 31 09:40 4e
drwxr-xr-x 2 apache       apache         81 Jan 31 09:40 92

我想在不通过脚本获取su权限的情况下清除缓存。所以我查看了 Twig 文件,发现它实际上设置为使用权限 777 写入它们。

lib/Twig/Cache/Filesystem.php

public function write($key, $content)
{
    $dir = dirname($key);
    if (!is_dir($dir)) {
        if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {

为什么 Apache 不写入具有 777 权限的目录/文件? 或者,在 Twig 中是否有清除缓存的内置方法?

【问题讨论】:

    标签: php linux caching twig file-permissions


    【解决方案1】:

    没有清除缓存的内置方法,但是我没有更改 twig 的核心文件,我有另一个解决方案来解决这个问题。只需创建您自己的 Environment 扩展 Twig_Environment 并调整 writeCacheFile 函数并为您的自定义实例而不是默认的 Twig_Environment 创建一个实例。

    class Environment extends \Twig_Environment {
        protected function writeCacheFile($file, $content){
            $this->createDirectoryTree(dirname($file));
            parent::writeCacheFile($file, $content);
            chmod($file,0664);
        }
    
        protected function createDirectoryTree($folder) {
            if (is_dir($folder)) return;
    
            $folder = str_replace('/', DIRECTORY_SEPARATOR, $folder);
            $branches = array_filter(explode(DIRECTORY_SEPARATOR, $folder));
    
            $tree = DIRECTORY_SEPARATOR;
            if (strpos($folder, 'httpdocs') !== false)  while(!empty($branches) && strpos($tree, 'httpdocs') === false) $tree .= array_shift($branches).DIRECTORY_SEPARATOR;
    
            while(is_dir($tree)) $tree .= array_shift($branches).DIRECTORY_SEPARATOR;
            array_unshift($branches, pathinfo($tree, PATHINFO_FILENAME));
            $tree = realpath(dirname($tree)).DIRECTORY_SEPARATOR;
            if ($tree === null) return;
    
            $old_mask = umask(0);
            while(!empty($branches)) {
                $tree .= array_shift($branches).DIRECTORY_SEPARATOR;
                if (!@file_exists($tree)) @mkdir($tree, 0775);
            }
            umask($old_mask);
        }
    
    
    }
    

    注意:使用0777作为文件权限被认为是安全线程,不推荐

    【讨论】:

    • 嗨,DarkBee,我没有更改核心文件,我只是发现它已经应该写入权限为 777 的文件。所以我想知道,为什么它不这样做。
    • 迈克,你是对的。这就是我创建这个类的原因,试图回忆它的原因。让我再考虑一下
    • @Mike,我已经用我的真实课程更新了代码,看来我的服务器并没有足够的递归 mkdir twig 在他们的函数中使用 writeCacheFile
    • 感谢您更新您的答案。但是我会在哪里存储你的类,以便 Twig 加载它并扩展它自己的类?
    • 你是独立使用 twig 还是与其他框架一起使用?
    【解决方案2】:

    这不是我最初想到的解决问题的方式,但它确实有效。 我在我的 rc.local 中添加了一个脚本,它使用 inotifywait 检查我的 twig 模板文件夹中的现有文件是否发生了任何新文件或更改。如果为真,缓存将被清除。 我添加了 vi 交换文件的排除项,以在打开文件时不触发脚本。

    #!/bin/bash
    
    while true
    do
      inotifywait -e modify -r /var/www/project/tpl/ @/var/www/project/tpl/cache --excludei ".swp"
      rm -rf /var/www/project/tpl/cache/*
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 2018-03-15
      相关资源
      最近更新 更多