Skip to content

Commit 44beb7e

Browse files
committed
Version 0.5.1 👾
- Fixed preview in Debug::source() - Debug::source() returns only files backtrace (ignore closures and etc) - Debug::source() returns more details - Improved offset and max in File::portion() and File::lines() - Removed unnecessary LOCK and improved performance in Storage::temp() - Improved Dom\Document::fromArray() (`Helper::seq` is unnecessary now) - New CSS selector (non-standard) to Document::query() (and `new Dom\Selector` class) - Searches for file that "EVAL" fails (`Debug::evalFileLocation()`) - Search for the file where the `eval()'d` failed (`Debug::evalFileLocation()`) - Improved PHPDoc
1 parent f5909b3 commit 44beb7e

File tree

9 files changed

+134
-107
lines changed

9 files changed

+134
-107
lines changed

src/Experimental/Debug.php

Lines changed: 76 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static function renderClasses()
115115
*
116116
* @param string $type
117117
* @param string $view
118-
*
118+
* @throws \Inphinit\Experimental\Exception
119119
* @return void
120120
*/
121121
public static function view($type, $view)
@@ -155,55 +155,6 @@ public static function view($type, $view)
155155
}
156156
}
157157

158-
private static function details($type, $message, $file, $line)
159-
{
160-
$match = array();
161-
$oFile = $file;
162-
163-
if (preg_match('#called in ([\s\S]+?) on line (\d+)#', $message, $match)) {
164-
$file = $match[1];
165-
$line = (int) $match[2];
166-
}
167-
168-
if (preg_match('#(.*?)\((\d+)\) : eval\(\)\'d code$#', trim($file), $match)) {
169-
$oFile = $match[1] . ' : eval():' . $line;
170-
$file = $match[1];
171-
$line = (int) $match[2];
172-
}
173-
174-
switch ($type) {
175-
case E_PARSE:
176-
$message = 'Parse error: ' . $message;
177-
break;
178-
179-
case E_DEPRECATED:
180-
$message = 'Deprecated: ' . $message;
181-
break;
182-
183-
case E_ERROR:
184-
case E_USER_ERROR:
185-
$message = 'Fatal error: ' . $message;
186-
break;
187-
188-
case E_WARNING:
189-
case E_USER_WARNING:
190-
$message = 'Warning: ' . $message;
191-
break;
192-
193-
case E_NOTICE:
194-
case E_USER_NOTICE:
195-
$message = 'Notice: ' . $message;
196-
break;
197-
}
198-
199-
return array(
200-
'message' => $message,
201-
'file' => $oFile,
202-
'line' => $line,
203-
'source' => $line > -1 ? self::source($file, $line) : null
204-
);
205-
}
206-
207158
/**
208159
* Get memory usage and you can also use it to calculate runtime.
209160
*
@@ -253,7 +204,7 @@ public static function source($file, $line)
253204
if ($line <= 0 || is_file($file) === false) {
254205
return null;
255206
} elseif ($line > 5) {
256-
$init = $line - 5;
207+
$init = $line - 6;
257208
$end = $line + 5;
258209
$breakpoint = 6;
259210
} else {
@@ -262,17 +213,20 @@ public static function source($file, $line)
262213
$breakpoint = $line;
263214
}
264215

216+
$preview = preg_split('#\r\n|\n#', File::lines($file, $init, $end));
217+
218+
if (count($preview) !== $breakpoint && trim(end($preview)) === '') {
219+
array_pop($preview);
220+
}
221+
265222
return array(
266223
'breakpoint' => $breakpoint,
267-
'preview' => preg_split(
268-
'#\r\n|\n#',
269-
trim(File::portion($file, $init, $end, true), "\r\n")
270-
)
224+
'preview' => $preview
271225
);
272226
}
273227

274228
/**
275-
* Get caller
229+
* Get backtrace php scripts
276230
*
277231
* @param int $level
278232
* @return array|null
@@ -281,23 +235,23 @@ public static function caller($level = 0)
281235
{
282236
$trace = debug_backtrace(0);
283237

238+
foreach ($trace as $key => &$value) {
239+
if (isset($value['file']) === false) {
240+
unset($trace[$key]);
241+
} else {
242+
self::evalFileLocation($value['file'], $value['line']);
243+
}
244+
}
245+
246+
$trace = array_values($trace);
247+
284248
if ($level < 0) {
285249
return $trace;
286250
} elseif (empty($trace[$level])) {
287251
return null;
288-
} elseif (empty($trace[$level]['file'])) {
289-
$level = 1;
290252
}
291253

292-
$file = $trace[$level]['file'];
293-
$line = $trace[$level]['line'];
294-
295-
$trace = null;
296-
297-
return array(
298-
'file' => $file,
299-
'line' => $line
300-
);
254+
return $trace = $trace[$level];
301255
}
302256

303257
/**
@@ -338,4 +292,59 @@ private static function render($view, $data)
338292

339293
View::render($view, $data);
340294
}
295+
296+
private static function details($type, $message, $file, $line)
297+
{
298+
$match = array();
299+
//$oFile = $file;
300+
301+
if (preg_match('#called in ([\s\S]+?) on line (\d+)#', $message, $match)) {
302+
$file = $match[1];
303+
$line = (int) $match[2];
304+
}
305+
306+
self::evalFileLocation($file, $line);
307+
308+
switch ($type) {
309+
case E_PARSE:
310+
$message = 'Parse error: ' . $message;
311+
break;
312+
313+
case E_DEPRECATED:
314+
case E_USER_DEPRECATED:
315+
$message = 'Deprecated: ' . $message;
316+
break;
317+
318+
case E_ERROR:
319+
case E_USER_ERROR:
320+
case E_RECOVERABLE_ERROR:
321+
$message = 'Fatal error: ' . $message;
322+
break;
323+
324+
case E_WARNING:
325+
case E_USER_WARNING:
326+
$message = 'Warning: ' . $message;
327+
break;
328+
329+
case E_NOTICE:
330+
case E_USER_NOTICE:
331+
$message = 'Notice: ' . $message;
332+
break;
333+
}
334+
335+
return array(
336+
'message' => $message,
337+
'file' => $file,
338+
'line' => $line,
339+
'source' => $line > -1 ? self::source($file, $line) : null
340+
);
341+
}
342+
343+
private static function evalFileLocation(&$file, &$line)
344+
{
345+
if (preg_match('#(.*?)\((\d+)\) : eval\(\)\'d code#', $file, $match)) {
346+
$file = $match[1];
347+
$line = (int) $match[2];
348+
}
349+
}
341350
}

src/Experimental/Dir.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public function __construct($path)
3434
$handle = opendir($path);
3535

3636
if ($handle) {
37-
while (($name = readdir($handle)) !== false) {
38-
if ($name !== '.' && $name !== '..') {
39-
$current = $path . $name;
37+
while ($file = readdir($handle)) {
38+
if ($file !== '.' && $file !== '..') {
39+
$current = $path . $file;
4040

4141
$data[] = (object) array(
4242
'type' => filetype($current),
4343
'path' => $current,
44-
'name' => $name
44+
'name' => $file
4545
);
4646
}
4747
}

src/Experimental/Dom/Document.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function reporting()
8383
* Convert a array in node elements
8484
*
8585
* @param array|\Traversable $data
86-
* @throws \Inphinit\Experimental\Exception
86+
* @throws \Inphinit\Experimental\Dom\DomException
8787
* @return void
8888
*/
8989
public function fromArray(array $data)
@@ -92,8 +92,12 @@ public function fromArray(array $data)
9292
throw new DomException('Array is empty', 2);
9393
} elseif (count($data) > 1) {
9494
throw new DomException('Root array accepts only a key', 2);
95-
} elseif (Helper::seq($data)) {
96-
throw new DomException('Document accpet only a node', 2);
95+
}
96+
97+
$root = key($data);
98+
99+
if (self::validTag($root) === false) {
100+
throw new DomException('Invalid root <' . $root . '> tag', 2);
97101
}
98102

99103
if ($this->documentElement) {
@@ -102,7 +106,7 @@ public function fromArray(array $data)
102106

103107
$this->enableRestoreInternal(true);
104108

105-
$this->generate($this, $data);
109+
$this->generate($this, $data, 2);
106110

107111
$this->raise($this->exceptionlevel);
108112

@@ -132,7 +136,7 @@ public function toJson($format = Document::MINIMAL, $options = 0)
132136
* Convert DOM to Array
133137
*
134138
* @param int $type
135-
* @throws \Inphinit\Experimental\Exception
139+
* @throws \Inphinit\Experimental\Dom\DomException
136140
* @return array
137141
*/
138142
public function toArray($type = Document::SIMPLE)
@@ -186,7 +190,7 @@ public function __toString()
186190
*
187191
* @param string $path
188192
* @param int $format Support XML, HTML, and JSON
189-
* @throws \Inphinit\Experimental\Exception
193+
* @throws \Inphinit\Experimental\Dom\DomException
190194
* @return void
191195
*/
192196
public function save($path, $format = Document::XML)
@@ -220,7 +224,6 @@ public function save($path, $format = Document::XML)
220224
* Get namespace attributes from root element or specific element
221225
*
222226
* @param \DOMElement $element
223-
* @throws \Inphinit\Experimental\Exception
224227
* @return void
225228
*/
226229
public function getNamespaces(\DOMElement $element = null)
@@ -387,34 +390,43 @@ private function raise($debuglvl)
387390
\libxml_clear_errors();
388391
}
389392

390-
private function generate(\DOMNode $node, $data)
393+
private function generate(\DOMNode $node, $data, $errorLevel)
391394
{
392395
if (is_array($data) === false) {
393396
$node->textContent = $data;
394397
return;
395398
}
396399

400+
$nextLevel = $errorLevel + 1;
401+
397402
foreach ($data as $key => $value) {
398403
if ($key === '@comments') {
399404
continue;
400405
} elseif ($key === '@contents') {
401-
$this->generate($node, $value);
406+
$this->generate($node, $value, $nextLevel);
402407
} elseif ($key === '@attributes') {
403408
$this->attrs($node, $value);
404-
} elseif (preg_match('#^([a-z]|[a-z][\w:])+$#i', $key)) {
409+
} elseif (self::validTag($key)) {
405410
if (Helper::seq($value)) {
406411
foreach ($value as $subvalue) {
407-
$this->generate($node, array($key => $subvalue));
412+
$this->generate($node, array($key => $subvalue), $nextLevel);
408413
}
409414
} elseif (is_array($value)) {
410-
$this->generate($this->add($key, '', $node), $value);
415+
$this->generate($this->add($key, '', $node), $value, $nextLevel);
411416
} else {
412417
$this->add($key, $value, $node);
413418
}
419+
} else {
420+
throw new DomException('Invalid root <' . $key . '> tag', $nextLevel);
414421
}
415422
}
416423
}
417424

425+
private function validTag($tagName)
426+
{
427+
return preg_match('#^([a-z_](\w+|)|[a-z_](\w+|):[a-z_](\w+|))$#i', $tagName) > 0;
428+
}
429+
418430
private function add($name, $value, \DOMNode $node)
419431
{
420432
$newdom = $this->createElement($name, $value);

src/Experimental/Dom/Selector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class Selector extends \DOMXPath
4444
array( '/\[(@\w+|lower-case\(@\w+\))\~=(.*?)\]/', '[contains(concat(" ",\\1," "),concat(" ",\\2," "))]' ),
4545
array( '/\[(@\w+|lower-case\(@\w+\))\|=(.*?)\]/', '[starts-with(concat(\\1,"-"),concat(\\2,"-"))]' ),
4646
array( '/\[(@\w+|lower-case\(@\w+\))\$=(.*?)\]/', '[substring(\\1,string-length(\\1)-2)=\\2]' ),
47-
array( '/\:contains\((.*?)\)/i', '[contains(.,\\1)]' )
47+
array( '/\:contains\((.*?)\)/i', '[contains(.,\\1)]' ),
48+
array( '/\:contains-child\((.*?)\)/i', '[text()[contains(.,\\1)]]' )
4849
);
4950

5051
/**

src/Experimental/File.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ public static function portion($path, $offset = 0, $max = 1024)
3232
*
3333
* @param string $path
3434
* @param int $offset
35-
* @param int $maxLines
35+
* @param int $maxLine
3636
* @throws \Inphinit\Experimental\Exception
3737
* @return string
3838
*/
39-
public static function lines($path, $offset = 0, $maxLines = 1024)
39+
public static function lines($path, $offset = 0, $maxLine = 32)
4040
{
41-
$i = 1;
41+
$i = 0;
4242
$output = '';
4343

4444
$handle = fopen($path, 'rb');
4545

46-
while (false === feof($handle) && $i <= $end) {
46+
while (false === feof($handle) && $i < $maxLine) {
4747
$data = fgets($handle);
4848

49-
if ($i >= $init) {
49+
if ($i >= $offset) {
5050
$output .= $data;
5151
}
5252

src/Experimental/Http/Request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Request extends \Inphinit\Http\Request
2222
* Get a value input handler
2323
*
2424
* @param bool $array
25+
* @throws \Inphinit\Experimental\Exception
2526
* @return array|stdClass|null
2627
*/
2728
public static function json($array = false)
@@ -55,6 +56,7 @@ public static function json($array = false)
5556
/**
5657
* Get a value input handler
5758
*
59+
* @throws \Inphinit\Experimental\Dom\DomException
5860
* @return \Inphinit\Experimental\Dom\Document
5961
*/
6062
public static function xml()

src/Experimental/Session.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public function regenerate($id = null, $trydeleteold = false)
169169
/**
170170
* Set cookie
171171
*
172+
* @throws \Inphinit\Experimental\Exception
172173
* @return void
173174
*/
174175
private function cookie()
@@ -284,6 +285,7 @@ public function __get($name)
284285
*
285286
* @param string $name
286287
* @param mixed $value
288+
* @throws \Inphinit\Experimental\Exception
287289
* @return void
288290
*/
289291
public function __set($name, $value)

0 commit comments

Comments
 (0)