Skip to content

Instantly share code, notes, and snippets.

@fcamp
Last active December 18, 2015 11:36
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 fcamp/c4cb828cfb22bbe845cb to your computer and use it in GitHub Desktop.
Save fcamp/c4cb828cfb22bbe845cb to your computer and use it in GitHub Desktop.
A simple buffer accumulator. Slightly inspired by Twig and Plates Section
<h1>Phonograph Usage Example</h1>
<?php Phonograph::start('a-block') ?>
<h1 style="color: red">This is the output</h1>
<p>Just another paragraph</p>
<?php Phonograph::stop();?>
<?= Phonograph::play('a-block');//print as is ?>
<?php Phonograph::start('another-block') ?>
<h1 style="color: red;">ALL THE TAGS WILL BE <strong>REMOVED</strong></h1>
<p>And The case will be changed to lower</p>
<?php Phonograph::stop();?>
<?= Phonograph::play('another-block',['effects' => 'strtolower|strip_tags']);//printing applying effects as pipe separated functions?>
<br>
<?= Phonograph::play('another-block',['effects' => function($output){
return str_replace('removed', 'ignored',strtolower($output));
}]);//effets is a callable ... in case we need more flexibility ?>
<br>
<h3>Output accumulated</h3>
<?php for($i=0; $i<5; ++$i)://printing after each line without cleaning the buffer (gets accumulated) ?>
<?php Phonograph::start('accumulated') ?>
Iteration: <?= $i + 1?>.
<?php Phonograph::stop();?>
<?= Phonograph::play('accumulated'); ?>
<br>
<?php endfor ?>
<h3>Output NOT accumulated</h3>
<?php for($i=0; $i<5; ++$i): ?>
<?php Phonograph::start('not-accumulated') ?>
Iteration: <?= $i + 1?>.
<?php Phonograph::stop();?>
<?= Phonograph::play('not-accumulated',['cleanBuffer' => true]); ?>
<br>
<?php endfor ?>
<?php
/**
* Just a tiny Wrapper of ob_start / ob_end
* @author https://github.com/fcamp
*/
class Phonograph
{
private static $data = [];
private static $currentName = null;
/**
* Start "recording" the buffer
* @param $regionName string the content block name
*/
static function start($regionName)
{
self::$currentName = $regionName;
ob_start();
}
/**
* Stop recordings
*/
static function stop()
{
self::$data[self::$currentName] .= ob_get_contents();
ob_end_clean();
}
/**
* @param string $regionName the name of the content block
* @param array $options <br>
* cleanBuffer: if true, cleans the buffer after printing. Default false
* effects: callable|pipe serparated functions that take the output as parameter and transform it.
* For example strtolower|ucwords. Default null
* @return string
*/
static function play($regionName, array $options = [])
{
$settings = array_merge([
'cleanBuffer' => false,
'effects' => null,
],$options);
$out = isset(self::$data[$regionName]) ? self::$data[$regionName] : null;
$settings['cleanBuffer'] && self::$data[$regionName] = null;//cleaning the buffer
if ($settings['effects']) {
if(is_callable($settings['effects'])){
$out = $settings['effects']($out);
} else {
foreach (explode('|', $settings['effects']) as $t) {
$out = is_callable($t) ? $t($out) : $out;
}
}
}
return $out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment