0

If I configure Twig's cache to myapp/storage/cache and manually set the correct permissions it works, but configuring it to sys_get_temp_dir() (which returns /tmp) doesn't seem to work. File structure in /tmp remains the same, altough no error is triggered.

My block of code is like this:

// [...]
$app->register(new \Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/templates',
    'twig.options' => array(
        'cache' => sys_get_temp_dir(), // If changed to myapp/storage/cache, it works.
    ),
));
llanfair
  • 1,845
  • 4
  • 27
  • 43
  • I would like to avoid the need to set proper permissions to cache folder manually because I'm doing a boilerplate. Less manual configuration would be better. – llanfair Mar 08 '17 at 17:17
  • Why do you want to place the files inside `/tmp`? – DarkBee Mar 08 '17 at 21:28
  • have you tried to manually create a file inside this directory? – mTorres Mar 09 '17 at 17:25
  • @DarkBee mainly because it's a boilerplate and making user change folder permissions is not good nor comfortable. /tmp is already there with the right permissions. – llanfair Mar 09 '17 at 21:01
  • @mTorres yes. Creating a file there worked, but Twig didn't do anything even manually creating the folders. – llanfair Mar 09 '17 at 22:40
  • [Here](http://stackoverflow.com/questions/28082846/mod-perl-cant-see-files-in-tmp) is why I was not able to see the temp files. – llanfair Mar 10 '17 at 01:42

1 Answers1

1

I don't know if this can help you, but it is possible to override the default writeCacheFile method of the Twig_Environment. By doing so you can create the temporary folder yourself and apply the wanted permissions, so your user don't have to do this themself.

Custom Twig_Env

class Environment extends \Twig_Environment {
    protected function writeCacheFile($file, $content){
        createDirectoryTree(dirname($file));
        parent::writeCacheFile($file, $content);
        chmod($file,0664);
        chgrp($file, 'psacln');
        chown($file, 'www-data');
    }
}

Functions.php

function createDirectoryTree($folder) {
    if (is_dir($folder)) return;
    $folder = str_replace('/', DIRECTORY_SEPARATOR, $folder);
    $branches = explode(DIRECTORY_SEPARATOR, $folder);
    $tree = '';

    $old_mask = umask(0);
    while(!empty($branches)) {
        $tree .= array_shift($branches).DIRECTORY_SEPARATOR;
        if (!@file_exists($tree)) {

            if (@mkdir($tree, 0774)){
                chown($tree, 'www-data');
                chgrp($tree, 'psacln');
            }
        }
    }
    umask($old_mask);
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • Thank you @DarkBee. The "problem" is because a security feature implemented in RHEL7. Files were being creating normally, however in a private context that could not be seen for external processes. That's why I assumed that they were not being created: I was using terminal and / or file manager to check if the cache existed. [See here](http://stackoverflow.com/questions/28082846/mod-perl-cant-see-files-in-tmp) for more information. – llanfair Mar 10 '17 at 01:46
  • Good thing you got it sorted out in the end – DarkBee Mar 10 '17 at 06:34