Skip to content

Commit

Permalink
Bug SAFeSEA#31, move TWIG configuration into Utils PHP class [iet:102…
Browse files Browse the repository at this point in the history
…99536]
  • Loading branch information
nfreear committed Feb 2, 2018
1 parent 8f46a3d commit c6ad725
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 62 deletions.
103 changes: 103 additions & 0 deletions app/utils/TwigApp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php namespace IET_OU\OpenEssayist\Utils;

/**
* Setup the TWIG template environment.
*
* @package OpenEssayist-slim
* @copyright © 2013-2018 The Open University. (Institute of Educational Technology)
* @author Nick Freear, 02-February-2018.
*/

// Was in: `public_html/index.php`

class TwigApp
{
/**
* Configure extensions, template directory, setup custom filters.
* @param object $view
*/
public static function setup( $view )
{
// Asset Management
\TwigView::$twigExtensions = array(
'Twig_Extensions_Slim',
'Twig_Extension_Debug'
);

\TwigView::$twigTemplateDirs = array(
__DIR__ . '/../../templates', // Was: '../templates',
);

\TwigView::$twigOptions = array(
'cache' => __DIR__ . '/../../.cache', // Was: '../.cache',
'debug'=> true,
);

if ($view instanceof \TwigView)
{
/* @var $twig Twig_Environment */
$twig = $view->getEnvironment();

$filters = self::createFilterFunctions();

$twig->addFilter($filters->boolean);
$twig->addFilter($filter->config);

$twig->addTest(self::createTestFunctions());
}
}

protected function createFilterFunctions()
{
/**
* Create a TWIG filter for Boolean values
* @param $var The variable to render
* @return A String containing "True" or "False"
* Usage: {{ item|boolean}}
*/
$boolean_filter = new \Twig_SimpleFilter('boolean', function ($var) {
if (is_bool($var)) {
return ($var) ? 'True' : 'False';
} else {
return $var;
}
});

/**
* A TWIG filter for configuration values. USAGE: {{ 'key' | config }}
* @param string $key
* @return string String (or object) value.
*/
$config_filter = new \Twig_SimpleFilter('config', function ($key) {
return \Application::config($key);
});

return (object) [
'boolean' => $boolean_filter,
'config' => $config_filter,
];
}

protected static function createTestFunctions()
{
/**
* Create a TWIG test for checking the existence of a value in an array
* @param $val The value to search for
* @param $arr The array
* @param $def The default value if the array does not exist
* @return True if the value is in the array, False if not, $def if the array is not set
* Usage: {{ val is inOption(arr,def) }}
*/
$test = new \Twig_SimpleTest('inOption', function ($val, $arr, $def = true) {
if (! isset($arr)) {
return $def;
}
if (isset($arr) && in_array($val, $arr) ) {
return true;
}
return false;
});

return $test;
}
}
1 change: 1 addition & 0 deletions app/utils/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require_once __DIR__ . '/../utils/LoggerMiddleware.php';
require_once __DIR__ . '/../utils/PDOAdmin.php';
require_once __DIR__ . '/../utils/StrongAuthAdmin.php';
require_once __DIR__ . '/../utils/TwigApp.php';
require_once __DIR__ . '/../utils/UASparser.php';

// Controllers.
Expand Down
70 changes: 8 additions & 62 deletions public_html/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,68 +37,12 @@
))
));

// Asset Management
TwigView::$twigExtensions = array(
'Twig_Extensions_Slim',
'Twig_Extension_Debug'
);

TwigView::$twigTemplateDirs = array(
'../templates',
);
TwigView::$twigOptions = array(
'cache' => '../.cache',
'debug'=> true,
);
// Twig Asset Management
// Set custom Twig filters -- code moved to `app/utils/TwigApp.php`.

IET_OU\OpenEssayist\Utils\TwigApp::setup( $app->view() ); // Was: $view = $app->view();

// Set custom Twig filters
$view = $app->view();

if ($view instanceof TwigView)
{
/* @var $twig Twig_Environment */
$twig = $view->getEnvironment();

/**
* Create a TWIG filter for Boolean values
* @param $var The variable to render
* @return A String containing "True" or "False"
* Usage: {{ item|boolean}}
*/
$filter = new Twig_SimpleFilter('boolean', function ($var) {
if (is_bool($var))
return ($var) ? "True":"False";
else
return $var;
});

/** A TWIG filter for configuration values. USAGE: {{ 'key' | config }}
* @param string $key
* @return string String (or object) value.
*/
$config_filter = new Twig_SimpleFilter('config', function ($key) {
return Application::config($key);
});

/**
* Create a TWIG test for checking the existence of a value in an array
* @param $val The value to search for
* @param $arr The array
* @param $def The default value if the array does not exist
* @return True if the value is in the array, False if not, $def if the array is not set
* Usage: {{ val is inOption(arr,def) }}
*/
$test = new Twig_SimpleTest('inOption', function ($val,$arr,$def=true) {
if (!isset($arr)) return $def;
if (isset($arr) && in_array($val, $arr) )
return true;
return false;
});

$twig->addFilter($filter);
$twig->addFilter($config_filter);
$twig->addTest($test);
}

// Create a hook to add the root URI to all views
$app->hook('slim.before.dispatch', function() use ($app) {
Expand Down Expand Up @@ -223,7 +167,7 @@
$c->app->get('/help/on/:topic', array($tutorCtrl, 'getHelpOnTopic'))->name('ajax.help.topic');
$c->app->get('/help/on/:topic/hints', array($tutorCtrl, 'getHelpOnTopic'))->name('ajax.help.hint');


// ADMIN routes.

$c->app->get('/admin/', array($adminCtrl, 'index'))->name('admin.home');
$c->app->get('/admin/reset', array($adminCtrl, 'reset'))->name('admin.reset');
Expand Down Expand Up @@ -259,5 +203,7 @@
$c->app->error(array($appController, 'error'));
$c->app->notFound(array($appController, 'NotFound'));

// Run the application
// Run the application.
$c->run();

// End.

0 comments on commit c6ad725

Please sign in to comment.