【问题标题】:Cache files permissions fix for Kohana Twig moduleKohana Twig 模块的缓存文件权限修复
【发布时间】:2016-04-20 10:07:25
【问题描述】:

如何配置Kohana + Twig 模块,以便Twig 对其所有缓存目录及其后代文件设置“所有人可写” 权限?

因此,例如,当我通过Apache 模块(mod_php)运行我的应用程序并且缓存文件所有者是apache(或httpd)用户时,我将能够删除缓存文件(到使用普通用户和ssh 访问权限清除缓存或完全删除整个应用程序。

我可以使用 Kohana 的缓存来做到这一点,但 Twig 的缓存创建方式有所不同。

【问题讨论】:

    标签: caching twig kohana file-permissions


    【解决方案1】:

    这不是很容易,但也不是太复杂。我通过试错法达到了下面的状态。

    1. 创建一个继承自Twig_Cache_Filesystem 的类,并将代替使用它。看看这个:

      <?php
      namespace Application\Twig;
      
      class Cache_Filesystem extends \Twig_Cache_Filesystem
      {
          public function write($key, $content)
          {
              $old = umask(0000);
              parent::write($key, $content);
              umask($old);
          }
      }
      

      注意,这个类的名字必须是唯一的,所以最好给它命名。此外,它必须可供其他代码访问,因此请考虑使用composer 的自动加载功能。

      这是修复本身,指南的其余部分只是将其实施到 Kohana+Twig 生态系统中的方式。

    2. Twig.phpmodules/kohana-twig/classes/Twig.php复制到您的应用程序目录,即application/classes/Twig.php(谢谢Kohana's Cascading Filesystem!)

    3. 修改一个新复制的文件,让Twig_CacheInterface 实例在配置文件(application/config/twig.php)中传递,而不仅仅是一个简单的字符串(指定到 Twig 的缓存目录)。看看我的例子:

      <?php defined('SYSPATH') or die('No direct script access.');
      
      class Twig extends Kohana_Twig
      {
          /**
           * Initialize the Twig module
           *
           * @throws Kohana_Exception
           * @return bool
           */
          public static function init()
          {
              $path = Kohana::$config->load('twig.environment.cache');
              if (is_string($path)) {
                  return parent::init();
              } else if ($path instanceof Twig_CacheInterface) {
                  return true;
              }
      
              throw new Kohana_Exception('Twig cache could not be initialized');
          }
      
      }
      
    4. kohana-twig 模块的配置文件中,即application/config/twig.php(如果尚未从模块复制到您的应用程序,请立即执行),定义environment.cache 键,如下所示:

      return array(
      
          'loader' => array(
              'extension' => 'twig',
              'path' => 'views',
          ),
          'environment' => array(
              'auto_reload' => (Kohana::$environment >= Kohana::TESTING),
              'autoescape' => true,
              'base_template_class' => 'Twig_Template',
              // Following line is related to this issue and fix:
              'cache' => new \Application\Twig\Cache_Filesystem(APPPATH . 'cache/twig'),
              'charset' => 'utf-8',
              'optimizations' => - 1,
              'strict_variables' => false,
          ),
          'functions' => array(),
          'filters' => array(),
          'tests' => array(),
      }
      

    这对我有用。希望它能帮助遇到类似问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 2013-04-28
      • 2012-03-06
      相关资源
      最近更新 更多