Permalink
Browse files

Initial code commit

  • Loading branch information...
csev committed Mar 19, 2017
1 parent 280661c commit 61dab338912eff6c781b7804960246b56c7fed34
Showing with 109 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +29 −0 composer.json
  3. +77 −0 src/Twig/Twig_Loader_Class.php
View
@@ -0,0 +1,3 @@
*.swp
vendor
composer.lock
View
@@ -0,0 +1,29 @@
{
"name": "koseu/lib",
"description": "A Next Generation Embeddable Learning Management System",
"homepage": "http://www.koseu.org/",
"type": "library",
"license": "Apache-2.0",
"authors": [
{
"name": "csev",
"email": "drchuck@gmail.com",
"homepage": "http://www.dr-chuck.com/",
"role": "Developer"
}
],
"autoload": {
"psr-4": {"Koseu\\": "src/"},
"exclude-from-classmap": [
"/tests/",
"phpdoc.dist.xml",
"phpunit.xml.dist"
]
},
"require": {
"php": ">=7.0.0",
"tsugi/lib": ">=0.3.1",
"silex/silex": "~2.0",
"twig/twig": "~2.0"
}
}
@@ -0,0 +1,77 @@
<?php
namespace Koseu\Twig;
/**
* Loads a template from a class path
*
* Sample usage:
*
* $loader = new Twig_Loader_Class();
* $app = new Silex\Application();
* $app->register(new Silex\Provider\TwigServiceProvider(), array(
* 'twig.loader' => $loader
* ));
* $app->get('/class/{name}', function ($name) use ($app) {
* echo("<pre>\n");
* return $app['twig']->render('\myview', array(
* 'name' => $name,
* ));
* });
*
* $app->run();
*
* This code looks as the first character of the template name and ignores
* all templates that do not start with '\\'.
*
* This loader should be last in a chain of Twig loaders because classes
* that do not exist are fatal, not exceptions so calling our exist()
* method with a class that does not exist or cannot be autoloaded,
* will be fatal, not simply return false :(
*
* We pretty much ignore working with Twig cache at all as the autoloader
* will function as a "within request" cache.
*/
final class Twig_Loader_Class implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface
{
public function getSourceContext($name)
{
$name = (string) $name;
$view = $this->getCacheKey($name);
return new Twig_Source($view, $name);
}
public function exists($name)
{
$name = (string) $name;
if ( strpos($name, '\\') !== 0 ) return false;
try {
$view = $this->getCacheKey($name);
return true;
} catch(Exception $e) {
return false;
}
}
// Understand that the loading of a class is a *Fatal* error, not an exception
// So we need to be last in the chain without a bit more autoloader magic here
public function getCacheKey($name)
{
$name = (string) $name;
try {
$class = new $name();
return $class->view;
} catch(Exception $e) {
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
}
}
public function isFresh($name, $time)
{
$name = (string) $name;
$this->getCacheKey();
return true;
}
}

0 comments on commit 61dab33

Please sign in to comment.