1 | <?php |
||
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) |
||
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) |
||
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) |
||
159 | |||
160 | } |
||
161 |