Skip to content

Instantly share code, notes, and snippets.

@leonrenkema
Created July 5, 2015 20:28
Show Gist options
  • Save leonrenkema/31460c796de0c44e3d5e to your computer and use it in GitHub Desktop.
Save leonrenkema/31460c796de0c44e3d5e to your computer and use it in GitHub Desktop.
Override of the default Filesystem loader for twig to be used on Google Appengine.
<?php
/**
* Override of the default Filesystem loader for twig to be used on Google Appengine.
* Appengine cannot write to the disk so all templates must be precompiled and the
* file id must stay the same.
*
* See https://www.leonrenkema.nl/2015/07/05/php-on-app-engine/
*/
class Twig_Loader_AppEngine extends Twig_Loader_Filesystem
{
/**
* Checks if the template can be found.
*
* @param string $name The template name
*
* @return bool true if the template exists, false otherwise
*/
protected function doFindTemplate($name)
{
$this->validateName($name);
if (isset($this->cache[$name])) {
return true;
}
if (isset($this->errorCache[$name])) {
return false;
}
list($namespace, $shortname) = $this->parseName($name);
if (!isset($this->paths[$namespace])) {
$this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace);
return false;
}
foreach ($this->paths[$namespace] as $path) {
if (is_file($path.'/'.$shortname)) {
$this->cache[$name] = $path.'/'.$shortname;
return true;
}
}
$this->errorCache[$name] = sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]));
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment