Maslosoft Zamm API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/zamm
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/zamm/
*/
namespace Maslosoft\Zamm;
use Exception;
use Maslosoft\Zamm\Helpers\Tabs;
use Maslosoft\Zamm\Helpers\Wrapper;
/**
* Capture part of php code for later use.
* This is intended for parts of code which should be evaluated,
* for instance for HTML widgets.
*
* Just after evluation or later, executing code can be captured and
* displayed on HTML page.
*
* Example:
* ```php
* Capture::open();
* echo 'Hi World!';
* Capture::close();
* ```
* Above coge is executed, moreover php code is now available in `Capture` class,
* and can be salvaged by `get`:
* ```php
* echo Capute::get(); //echo 'Hi World!';
* ```
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Capture
{
/**
* Array of snippets
* @var string[]
*/
private static $snippets = [];
/**
* Id counter for auto id's
* @var int
*/
private static $idCounter = 0;
/**
* Whenever capture is opened
* @var bool
*/
private static $isOpen = false;
/**
* Currently capturing id
* @var int|string
*/
private static $currentId = 0;
/**
* Currently captured file
* @var string
*/
private static $currentFile = '';
/**
* Currentlty captured starting line
* @var int
*/
private static $currentLine = 0;
/**
* Open PHP capturing block
*
* @param int|string $id
*/
public static function open($id = null)
{
if (self::$isOpen)
{
throw new Exception('Capture is already open, close block before capturing another snippet');
}
if (null === $id)
{
$id = self::$idCounter++;
}
self::$isOpen = true;
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
self::$currentId = $id;
self::$currentFile = $trace['file'];
self::$currentLine = $trace['line'];
}
/**
* Close php capturing block and get it wrapped
* @return Wrapper
*/
public static function close()
{
if (!self::$isOpen)
{
throw new Exception('Capture is not open, open closing capturing block');
}
self::$isOpen = false;
$lines = file(self::$currentFile);
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
$fragment = array_slice($lines, self::$currentLine, $trace['line'] - self::$currentLine - 1);
Tabs::trim($fragment);
return new Wrapper(self::$snippets[self::$currentId] = implode('', $fragment));
}
/**
* Get last snipped or choosen by id.
* @param int|string $id
* @return Wrapper
*/
public static function get($id = null)
{
if (self::$isOpen)
{
throw new Exception('Capture is open, close block before getting snippet');
}
if (null === $id)
{
$id = self::$currentId;
}
return new Wrapper(self::$snippets[$id]);
}
/**
* Get captured PHP block, additionally wrapped by markdown
* fenced PHP code mark. This can be directly outputted to md file.
* @deprecated use Wrapper instead: append ->md
* @param int|string $id
*/
public static function getMd($id = null)
{
return sprintf("```php\n%s\n```", self::get($id));
}
/**
* Get captured PHP block, additionally wrapped by html pre and code tags.
* This can be directly outputted to HTML file.
* @deprecated use Wrapper instead: append ->html
* @param int|string $id
*/
public static function getHtml($id = null)
{
return sprintf('<pre class="php"><code>%s</code></pre>', self::get($id));
}
}
API documentation generated by ApiGen