Skip to content

Instantly share code, notes, and snippets.

@sergant210
Last active October 10, 2017 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergant210/4bb3bf3afafcd1794ebe2ca13992e35c to your computer and use it in GitHub Desktop.
Save sergant210/4bb3bf3afafcd1794ebe2ca13992e35c to your computer and use it in GitHub Desktop.
Fenom Storage trait
<?php
namespace Fenom;
trait StorageTrait
{
/**
* @var array storage
*/
protected $_vars = array();
/**
* @param string $name
* @param mixed $variable
* @return $this
*/
public function assign($name, $variable)
{
$this->_vars[$name] = $variable;
return $this;
}
/**
* @param string $name
* @param mixed $key
* @param mixed $variable
* @return $this
*/
public function assignKey($name, $key, $variable)
{
if (!is_array($this->_vars[$name])) {
$this->_vars[$name] = (array)$this->_vars[$name];
}
$this->_vars[$name][$key] = $variable;
return $this;
}
/**
* @param string $name
* @param mixed $variable
* @return $this
*/
public function assignByRef($name, &$variable)
{
$this->_vars[$name] = & $variable;
return $this;
}
/**
* Set all variables
* @param array $vars
* @param bool $merge merge new variables array with current or rewrite
* @return $this
*/
public function assignAll(array $vars, $merge = true) {
if($merge) {
$this->_vars = $vars + $this->_vars;
} else {
$this->_vars = $vars;
}
return $this;
}
/**
* @param string $name
* @param mixed $variable
* @return $this
*/
public function prepend($name, $variable)
{
if (!isset($this->_vars[$name])) {
$this->_vars[$name] = array();
}
if (!is_array($this->_vars[$name])) {
$this->_vars[$name] = (array)$this->_vars[$name];
}
array_unshift($this->_vars[$name], $variable);
return $this;
}
/**
* @param string $name
* @param mixed $variable
* @return $this
*/
public function append($name, $variable)
{
if (!isset($this->_vars[$name])) {
$this->_vars[$name] = array();
}
if (!is_array($this->_vars[$name])) {
$this->_vars[$name] = (array)$this->_vars[$name];
}
$this->_vars[$name][] = $variable;
return $this;
}
/**
* Get collected variables
* @return array
*/
public function getVars()
{
return $this->_vars;
}
/**
* Reset collected variables
* @return $this
*/
public function resetVars()
{
$this->_vars = array();
return $this;
}
/**
* {@inheritdoc}
*/
public function display($template, array $vars = array())
{
/* @var \Fenom|StorageTrait $this */
if (!$template instanceof Template) $template = $this->getTemplate($template);
return $this->_vars = $template->display($vars ? $vars + $this->_vars : $this->_vars);
}
/**
* {@inheritdoc}
*/
public function fetch($template, array $vars = array())
{
/* @var \Fenom|StorageTrait $this */
if (!$template instanceof Template) $template = $this->getTemplate($template);
ob_start();
try {
$this->_vars = $template->display($vars ? $vars + $this->_vars : $this->_vars);
return ob_get_clean();
} catch(\Exception $e) {
ob_end_clean();
throw $e;
}
}
/**
* {@inheritdoc}
*/
public function pipe($template, $callback, array $vars = array(), $chunk = 1e6)
{
/* @var \Fenom|StorageTrait $this */
if (!$template instanceof Template) $template = $this->getTemplate($template);
ob_start($callback, $chunk, PHP_OUTPUT_HANDLER_STDFLAGS);
$data = $template->display($vars ? $vars + $this->_vars : $this->_vars);
ob_end_flush();
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment