0

Seems that the twig getTimestamp() function is not fully compatible with PHP 5.5.12 (windows/linux) and throws an exception when the file is not available.

public function getTimestamp($key)
{
    return (int) @filemtime($key);
}

We are using Twig 1.22.2 without the rest of symfony. I was checking for the requested key but this file does not exists. Shouldnt twig check if the file exists before doing a filemtime() call? Otherwise the cache never gets populated. We were configuring twig the following:

$this->loader = new \Twig_Loader_Filesystem($options['TEMPLATE_DIR']);
    $this->twig = new \Twig_Environment($this->loader, array(
        "auto_reload" =>  true
        , "cache"     =>  'cache/twig'
    ));

Like this the cache files are never created. We have to disable the caching to make it working.

"cache" => false

For a production environment this is not working

Another Solution was the following:

public function getTimestamp($key)
{
    **return false;**
    return (int) @filemtime($key);
}

and then reverting back to the original source. Then the cache files were written to the disk and everything worked like a charm.

I guess something changed in php internals and the twig team needs to adapt to that change. By default twig always expects the file to be there which is for every application on the first hit not the case!

Robert
  • 176
  • 1
  • 19
  • _and throws an exception_ - Which exception? `filemtime` should return false in case of failure. If something change in `filemtime`, almost all existing applications would break – Federkun Sep 28 '15 at 14:07
  • Have you the permission to read `cache/twig` dir? Also, use absolute path in `cache` parameter. – Federkun Sep 28 '15 at 14:14
  • Hi @Leggendario and thanks for your comment. We catch all of the errors and "exceptions" in our system. The following line we get: filemtime(): stat failed for .cache/twig/0/8/089721f50b4ba7d49f297b857394801be24fd79ee3830d069b437e50cc2ddf19.php E:\Web\...\vendor\twig\twig\lib\Twig\Cache\Filesystem.php 90 – Robert Sep 28 '15 at 14:20
  • Tested now absolute path which is not working. As i said. If the getTimestamp function returns false everything works and the cache files are written. I dont know why in my environment the filemtime function fails and not just write a warning. – Robert Sep 28 '15 at 14:41
  • Ok, tested a bit more and it seems that our custom error handler was reacting wrong on warnings and throwing an exception. Only on notices he was logging it correctly. It seems this case is solved now. No bug in twig. – Robert Sep 28 '15 at 14:48

1 Answers1

0

Ok, tested a bit more and it seems that our custom error handler was reacting wrong on warnings and throwing an exception. Only on notices he was logging it correctly. It seems this case is solved now. No bug in twig

Robert
  • 176
  • 1
  • 19