Last active
October 10, 2017 15:31
-
-
Save sergant210/4bb3bf3afafcd1794ebe2ca13992e35c to your computer and use it in GitHub Desktop.
Fenom Storage trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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