private function makeCacheFilename($cachePath, $templatesRoot, $filename, $mkdir = false)
    {
        // If the template filename is relative (ie. not starting by / and not specifying a scheme)
        // then we first prefis it with the current working directory. And then we continue as if
        // absolute.
        if ( (substr($filename, 0, 1) != '/')  && ! parse_url($filename, PHP_URL_SCHEME)) {
            $filename = getcwd() . '/' . $filename;
        }

        $cacheFile = str_replace($templatesRoot, $cachePath . '/figs', $filename) . '.fig';
        if ($mkdir) {
            // Create the intermediary folders if not exist.
            $intermed = dirname($cacheFile);
            if (! file_exists($intermed)) {
                // In case of permission problems, or file already exists (and is not a directory),
                // the regular PHP warnings will be issued.
                mkdir($intermed, 0700, true);
            }
        }
        return $cacheFile;
    }