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