Skip to content
Snippets Groups Projects
Commit 30360f11 authored by Kevin Abel's avatar Kevin Abel
Browse files

Update Savvy lib from Undergrad bulletin

parent b05bbff1
Branches
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@
* @copyright 2010 Brett Bieber
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version SVN: $Id$
* @link http://svn.php.net/repository/pear2/Savvy
* @link https://github.com/saltybeagle/savvy
*/
/**
......@@ -21,7 +21,7 @@
* @author Brett Bieber <saltybeagle@php.net>
* @copyright 2010 Brett Bieber
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://svn.php.net/repository/pear2/Savvy
* @link https://github.com/saltybeagle/savvy
*/
class Savvy
{
......@@ -39,6 +39,7 @@ class Savvy
'compiler' => null,
'filters' => array(),
'escape' => null,
'iterate_traversable' => false,
);
/**
......@@ -106,7 +107,6 @@ class Savvy
//
// -----------------------------------------------------------------
/**
*
* Constructor.
......@@ -141,6 +141,11 @@ class Savvy
if (isset($config['filters'])) {
$this->addFilters($config['filters']);
}
// set whether to iterate over Traversable objects
if (isset($config['iterate_traversable'])) {
$this->setIterateTraversable($config['iterate_traversable']);
}
}
/**
......@@ -161,6 +166,7 @@ class Savvy
unset($__name, $__value);
ob_start();
include $file;
return ob_get_clean();
}
......@@ -182,6 +188,7 @@ class Savvy
unset($__name, $__value);
ob_start();
include $file;
return $savvy->applyFilters(ob_get_clean());
}
......@@ -203,6 +210,7 @@ class Savvy
unset($__name, $__value);
ob_start();
include $savvy->template($file);
return ob_get_clean();
}
......@@ -224,6 +232,7 @@ class Savvy
unset($__name, $__value);
ob_start();
include $savvy->template($file);
return $savvy->applyFilters(ob_get_clean());
}
......@@ -260,13 +269,24 @@ class Savvy
/**
* Add a global variable which will be available inside every template
*
* Inside templates, reference the global using the name passed
* <code>
* $savvy->addGlobal('formHelper', new FormHelper());
* </code>
*
* Sample template, Form.tpl.php
* <code>
* echo $formHelper->renderInput('name');
* </code>
*
* @param string $var The global variable name
* @param mixed $value The value
* @param mixed $value The value or variable to expose globally
*
* @return void
*/
function addGlobal($name, $value)
public function addGlobal($name, $value)
{
// disallow specific variable names, these are reserved variables
switch ($name) {
case 'context':
case 'parent':
......@@ -276,25 +296,45 @@ class Savvy
throw new Savvy_BadMethodCallException('Invalid global variable name');
}
// if output is currently escaped, make sure the global is escaped
if ($this->__config['escape']) {
switch (gettype($value)) {
$value = $this->filterVar($value);
}
$this->globals[$name] = $value;
}
/**
* Filter a variable of unknown type
*
* @param mixed $var The variable to filter
*
* @return string|Savvy_ObjectProxy
*/
public function filterVar($var)
{
switch (gettype($var)) {
case 'object':
if (!$value instanceof Savvy_ObjectProxy) {
$value = Savvy_ObjectProxy::factory($value, $this);
if ($var instanceof ArrayIterator) {
return new Savvy_ObjectProxy_ArrayIterator($var, $this);
}
break;
if ($var instanceof ArrayAccess) {
return new Savvy_ObjectProxy_ArrayAccess($var, $this);
}
return Savvy_ObjectProxy::factory($var, $this);
case 'string':
case 'int':
case 'integer':
case 'double':
$value = $this->escape($value);
break;
return $this->escape($var);
case 'array':
$value = new Savvy_ObjectProxy_ArrayIterator($value, $this);
break;
}
return new Savvy_ObjectProxy_ArrayObject(
new \ArrayObject($var),
$this
);
}
$this->globals[$name] = $value;
return $var;
}
/**
......@@ -302,7 +342,7 @@ class Savvy
*
* @return array
*/
function getGlobals()
public function getGlobals()
{
return $this->globals;
}
......@@ -312,19 +352,17 @@ class Savvy
*
* @return string
*/
function getTemplate()
public function getTemplate()
{
return $this->template;
}
// -----------------------------------------------------------------
//
// Public configuration management (getters and setters).
//
// -----------------------------------------------------------------
/**
*
* Returns a copy of the Savvy configuration parameters.
......@@ -352,7 +390,6 @@ class Savvy
}
}
/**
*
* Sets a custom compiler/pre-processor callback for template sources.
......@@ -384,6 +421,7 @@ class Savvy
$this->selected_controller = 'filterfastcompiled';
break;
}
return;
}
if (!strpos($this->selected_controller, 'compiled')) {
......@@ -400,9 +438,10 @@ class Savvy
*
* @return Main
*/
function setClassToTemplateMapper(Savvy_MapperInterface $mapper)
public function setClassToTemplateMapper(Savvy_MapperInterface $mapper)
{
$this->class_to_template = $mapper;
return $this;
}
......@@ -411,14 +450,26 @@ class Savvy
*
* @return MapperInterface
*/
function getClassToTemplateMapper()
public function getClassToTemplateMapper()
{
if (!isset($this->class_to_template)) {
$this->setClassToTemplateMapper(new Savvy_ClassToTemplateMapper());
}
return $this->class_to_template;
}
public function setIterateTraversable($iterate)
{
$this->__config['iterate_traversable'] = (bool)$iterate;
return $this;
}
public function getIterateTraversable()
{
return $this->__config['iterate_traversable'];
}
// -----------------------------------------------------------------
//
......@@ -426,7 +477,6 @@ class Savvy
//
// -----------------------------------------------------------------
/**
*
* Clears then sets the callbacks to use when calling $this->escape().
......@@ -452,10 +502,10 @@ class Savvy
public function setEscape()
{
$this->__config['escape'] = @func_get_args();
return $this;
}
/**
*
* Gets the array of output-escaping callbacks.
......@@ -471,7 +521,6 @@ class Savvy
return $this->__config['escape'];
}
/**
* Escapes a value for output in a view script.
*
......@@ -495,10 +544,10 @@ class Savvy
$var = call_user_func($escape, $var);
}
}
return $var;
}
// -----------------------------------------------------------------
//
// File management
......@@ -510,7 +559,7 @@ class Savvy
*
* @return array
*/
function getTemplatePath()
public function getTemplatePath()
{
return $this->template_path;
}
......@@ -535,10 +584,10 @@ class Savvy
// actually add the user-specified directories
$this->addTemplatePath($path);
return $this;
}
/**
*
* Adds to the search path for templates and resources.
......@@ -671,9 +720,10 @@ class Savvy
*
* @return string The template output
*/
function render($mixed = null, $template = null)
public function render($mixed = null, $template = null)
{
$method = 'render'.gettype($mixed);
return $this->$method($mixed, $template);
}
......@@ -728,6 +778,7 @@ class Savvy
if (!$this->__config['filters']) {
return $string;
}
return $this->applyFilters($string);
}
......@@ -745,6 +796,7 @@ class Savvy
foreach ($array as $mixed) {
$output .= $this->render($mixed, $template);
}
return $output;
}
......@@ -766,15 +818,17 @@ class Savvy
foreach ($array as $key => $element) {
$ret .= $template($key, $element, $selected);
}
return $ret;
}
protected function renderArrayAccess(ArrayAccess $array, $template = null)
protected function renderTraversable(Traversable $array, $template = null)
{
$ret = '';
foreach ($array as $key => $element) {
$ret .= $this->render($element, $template);
}
return $ret;
}
......@@ -814,10 +868,13 @@ class Savvy
$object = Savvy_ObjectProxy::factory($object, $this);
}
if ($object instanceof Savvy_ObjectProxy_ArrayIterator) {
return $this->renderArrayAccess($object);
if ($object instanceof Traversable
&& $this->__config['iterate_traversable']
) {
return $this->renderTraversable($object->getRawObject(), $template);
}
}
return $this->fetch($object, $template);
}
......@@ -836,7 +893,7 @@ class Savvy
}
}
public function fetch($mixed, $template = null)
protected function fetch($mixed, $template = null)
{
if ($template) {
$this->template = $template;
......@@ -858,6 +915,7 @@ class Savvy
$this->templateStack[] = $current;
$ret = call_user_func(array($this, $this->selected_controller.'OutputController'), $current->context, $current->parent, $current->file, $this);
array_pop($this->templateStack);
return $ret;
}
......@@ -980,6 +1038,7 @@ class Savvy
foreach ($this->__config['filters'] as $callback) {
$buffer = call_user_func($callback, $buffer);
}
return $buffer;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment