Twig: BC break regarding relative paths in 1.25 due to realpath changes (#2127)
The 1.25 version does not allow relative loading paths ($this->paths) in the filesystem loader anymore.
The new normalizePath() method is more strict than realpath() as it does not allow relative paths (any extra ../ will be ignored). It means that if you have a FilesystemLoader configured with a relative directory in the paths list, the result of findTemplate() will be different than in the 1.24 version.
The result will also be wrong because the is_file() check (https://github.com/twigphp/Twig/blob/1.x/lib/Twig/Loader/Filesystem.php#L200) will succeed but the returned path won’t lead to an existing file, as a result a warning will be thrown by getSource().
Reproducer (expecting true with both 1.24 and 1.25):
<?php
include 'vendor/autoload.php';
$parts = explode('/', __FILE__);
$filename = array_pop($parts);
$dirname = array_pop($parts);
$loader = new Twig_Loader_Filesystem('../'.$dirname);
$r = new ReflectionMethod($loader, 'findTemplate');
$r->setAccessible(true);
var_dump(__FILE__ === $r->invoke($loader, $filename));
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 17 (10 by maintainers)
@omerta the workaround for now is to configure your loader with absolute paths (which is way more reliable than relying on the cwd when your code is called btw):
On a side note, you don’t need to call
loadTemplate()yourselves. You should useTwig_Environment::render()directly (and let it deal with the template loading internally)@fabpot the issue is that we always expected Twig_Loader_Filesystem to receive absolute paths when working on your refactoring and reviewing it, but the user base does not agree. I suggest making Twig_Loader_Filesystem turn paths to absolute ones when configuring it.
@stof I don’t see any mention of relative paths or absolute paths in the loader (see e.g. http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem). Only the cache seems to explicitly ask for absolute paths. I do understand your argument with possible issues caused by improperly using relative paths. This does however not change the fact that the documentation does not state the need for an absolute path and until now twig has supported relative paths for the filesystem loader. Therefore this is a clear BC break that I think should be addressed. If you want to enforce using absolute paths then I think this would fit better with a clear BC break in a major version (i.e. twig 2.x).