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