Skip to content

Commit

Permalink
Version 0.5.1 👾
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
brcontainer committed Dec 29, 2019
1 parent f5909b3 commit 44beb7e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 107 deletions.
143 changes: 76 additions & 67 deletions src/Experimental/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static function renderClasses()
*
* @param string $type
* @param string $view
*
* @throws \Inphinit\Experimental\Exception
* @return void
*/
public static function view($type, $view)
Expand Down Expand Up @@ -155,55 +155,6 @@ public static function view($type, $view)
}
}

private static function details($type, $message, $file, $line)
{
$match = array();
$oFile = $file;

if (preg_match('#called in ([\s\S]+?) on line (\d+)#', $message, $match)) {
$file = $match[1];
$line = (int) $match[2];
}

if (preg_match('#(.*?)\((\d+)\) : eval\(\)\'d code$#', trim($file), $match)) {
$oFile = $match[1] . ' : eval():' . $line;
$file = $match[1];
$line = (int) $match[2];
}
switch ($type) {
case E_PARSE:
$message = 'Parse error: ' . $message;
break;

case E_DEPRECATED:
$message = 'Deprecated: ' . $message;
break;

case E_ERROR:
case E_USER_ERROR:
$message = 'Fatal error: ' . $message;
break;

case E_WARNING:
case E_USER_WARNING:
$message = 'Warning: ' . $message;
break;

case E_NOTICE:
case E_USER_NOTICE:
$message = 'Notice: ' . $message;
break;
}

return array(
'message' => $message,
'file' => $oFile,
'line' => $line,
'source' => $line > -1 ? self::source($file, $line) : null
);
}

/**
* Get memory usage and you can also use it to calculate runtime.
*
Expand Down Expand Up @@ -253,7 +204,7 @@ public static function source($file, $line)
if ($line <= 0 || is_file($file) === false) {
return null;
} elseif ($line > 5) {
$init = $line - 5;
$init = $line - 6;
$end = $line + 5;
$breakpoint = 6;
} else {
Expand All @@ -262,17 +213,20 @@ public static function source($file, $line)
$breakpoint = $line;
}

$preview = preg_split('#\r\n|\n#', File::lines($file, $init, $end));

if (count($preview) !== $breakpoint && trim(end($preview)) === '') {
array_pop($preview);
}

return array(
'breakpoint' => $breakpoint,
'preview' => preg_split(
'#\r\n|\n#',
trim(File::portion($file, $init, $end, true), "\r\n")
)
'preview' => $preview
);
}

/**
* Get caller
* Get backtrace php scripts
*
* @param int $level
* @return array|null
Expand All @@ -281,23 +235,23 @@ public static function caller($level = 0)
{
$trace = debug_backtrace(0);

foreach ($trace as $key => &$value) {
if (isset($value['file']) === false) {
unset($trace[$key]);
} else {
self::evalFileLocation($value['file'], $value['line']);
}
}

$trace = array_values($trace);

if ($level < 0) {
return $trace;
} elseif (empty($trace[$level])) {
return null;
} elseif (empty($trace[$level]['file'])) {
$level = 1;
}

$file = $trace[$level]['file'];
$line = $trace[$level]['line'];

$trace = null;

return array(
'file' => $file,
'line' => $line
);
return $trace = $trace[$level];
}

/**
Expand Down Expand Up @@ -338,4 +292,59 @@ private static function render($view, $data)

View::render($view, $data);
}

private static function details($type, $message, $file, $line)
{
$match = array();
//$oFile = $file;

if (preg_match('#called in ([\s\S]+?) on line (\d+)#', $message, $match)) {
$file = $match[1];
$line = (int) $match[2];
}

self::evalFileLocation($file, $line);

switch ($type) {
case E_PARSE:
$message = 'Parse error: ' . $message;
break;

case E_DEPRECATED:
case E_USER_DEPRECATED:
$message = 'Deprecated: ' . $message;
break;

case E_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
$message = 'Fatal error: ' . $message;
break;

case E_WARNING:
case E_USER_WARNING:
$message = 'Warning: ' . $message;
break;

case E_NOTICE:
case E_USER_NOTICE:
$message = 'Notice: ' . $message;
break;
}

return array(
'message' => $message,
'file' => $file,
'line' => $line,
'source' => $line > -1 ? self::source($file, $line) : null
);
}

private static function evalFileLocation(&$file, &$line)
{
if (preg_match('#(.*?)\((\d+)\) : eval\(\)\'d code#', $file, $match)) {
$file = $match[1];
$line = (int) $match[2];
}
}
}
8 changes: 4 additions & 4 deletions src/Experimental/Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public function __construct($path)
$handle = opendir($path);

if ($handle) {
while (($name = readdir($handle)) !== false) {
if ($name !== '.' && $name !== '..') {
$current = $path . $name;
while ($file = readdir($handle)) {
if ($file !== '.' && $file !== '..') {
$current = $path . $file;

$data[] = (object) array(
'type' => filetype($current),
'path' => $current,
'name' => $name
'name' => $file
);
}
}
Expand Down
36 changes: 24 additions & 12 deletions src/Experimental/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function reporting()
* Convert a array in node elements
*
* @param array|\Traversable $data
* @throws \Inphinit\Experimental\Exception
* @throws \Inphinit\Experimental\Dom\DomException
* @return void
*/
public function fromArray(array $data)
Expand All @@ -92,8 +92,12 @@ public function fromArray(array $data)
throw new DomException('Array is empty', 2);
} elseif (count($data) > 1) {
throw new DomException('Root array accepts only a key', 2);
} elseif (Helper::seq($data)) {
throw new DomException('Document accpet only a node', 2);
}

$root = key($data);

if (self::validTag($root) === false) {
throw new DomException('Invalid root <' . $root . '> tag', 2);
}

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

$this->enableRestoreInternal(true);

$this->generate($this, $data);
$this->generate($this, $data, 2);

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

Expand Down Expand Up @@ -132,7 +136,7 @@ public function toJson($format = Document::MINIMAL, $options = 0)
* Convert DOM to Array
*
* @param int $type
* @throws \Inphinit\Experimental\Exception
* @throws \Inphinit\Experimental\Dom\DomException
* @return array
*/
public function toArray($type = Document::SIMPLE)
Expand Down Expand Up @@ -186,7 +190,7 @@ public function __toString()
*
* @param string $path
* @param int $format Support XML, HTML, and JSON
* @throws \Inphinit\Experimental\Exception
* @throws \Inphinit\Experimental\Dom\DomException
* @return void
*/
public function save($path, $format = Document::XML)
Expand Down Expand Up @@ -220,7 +224,6 @@ public function save($path, $format = Document::XML)
* Get namespace attributes from root element or specific element
*
* @param \DOMElement $element
* @throws \Inphinit\Experimental\Exception
* @return void
*/
public function getNamespaces(\DOMElement $element = null)
Expand Down Expand Up @@ -387,34 +390,43 @@ private function raise($debuglvl)
\libxml_clear_errors();
}

private function generate(\DOMNode $node, $data)
private function generate(\DOMNode $node, $data, $errorLevel)
{
if (is_array($data) === false) {
$node->textContent = $data;
return;
}

$nextLevel = $errorLevel + 1;

foreach ($data as $key => $value) {
if ($key === '@comments') {
continue;
} elseif ($key === '@contents') {
$this->generate($node, $value);
$this->generate($node, $value, $nextLevel);
} elseif ($key === '@attributes') {
$this->attrs($node, $value);
} elseif (preg_match('#^([a-z]|[a-z][\w:])+$#i', $key)) {
} elseif (self::validTag($key)) {
if (Helper::seq($value)) {
foreach ($value as $subvalue) {
$this->generate($node, array($key => $subvalue));
$this->generate($node, array($key => $subvalue), $nextLevel);
}
} elseif (is_array($value)) {
$this->generate($this->add($key, '', $node), $value);
$this->generate($this->add($key, '', $node), $value, $nextLevel);
} else {
$this->add($key, $value, $node);
}
} else {
throw new DomException('Invalid root <' . $key . '> tag', $nextLevel);
}
}
}

private function validTag($tagName)
{
return preg_match('#^([a-z_](\w+|)|[a-z_](\w+|):[a-z_](\w+|))$#i', $tagName) > 0;
}

private function add($name, $value, \DOMNode $node)
{
$newdom = $this->createElement($name, $value);
Expand Down
3 changes: 2 additions & 1 deletion src/Experimental/Dom/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class Selector extends \DOMXPath
array( '/\[(@\w+|lower-case\(@\w+\))\~=(.*?)\]/', '[contains(concat(" ",\\1," "),concat(" ",\\2," "))]' ),
array( '/\[(@\w+|lower-case\(@\w+\))\|=(.*?)\]/', '[starts-with(concat(\\1,"-"),concat(\\2,"-"))]' ),
array( '/\[(@\w+|lower-case\(@\w+\))\$=(.*?)\]/', '[substring(\\1,string-length(\\1)-2)=\\2]' ),
array( '/\:contains\((.*?)\)/i', '[contains(.,\\1)]' )
array( '/\:contains\((.*?)\)/i', '[contains(.,\\1)]' ),
array( '/\:contains-child\((.*?)\)/i', '[text()[contains(.,\\1)]]' )
);

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Experimental/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ public static function portion($path, $offset = 0, $max = 1024)
*
* @param string $path
* @param int $offset
* @param int $maxLines
* @param int $maxLine
* @throws \Inphinit\Experimental\Exception
* @return string
*/
public static function lines($path, $offset = 0, $maxLines = 1024)
public static function lines($path, $offset = 0, $maxLine = 32)
{
$i = 1;
$i = 0;
$output = '';

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

while (false === feof($handle) && $i <= $end) {
while (false === feof($handle) && $i < $maxLine) {
$data = fgets($handle);

if ($i >= $init) {
if ($i >= $offset) {
$output .= $data;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Experimental/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Request extends \Inphinit\Http\Request
* Get a value input handler
*
* @param bool $array
* @throws \Inphinit\Experimental\Exception
* @return array|stdClass|null
*/
public static function json($array = false)
Expand Down Expand Up @@ -55,6 +56,7 @@ public static function json($array = false)
/**
* Get a value input handler
*
* @throws \Inphinit\Experimental\Dom\DomException
* @return \Inphinit\Experimental\Dom\Document
*/
public static function xml()
Expand Down
2 changes: 2 additions & 0 deletions src/Experimental/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function regenerate($id = null, $trydeleteold = false)
/**
* Set cookie
*
* @throws \Inphinit\Experimental\Exception
* @return void
*/
private function cookie()
Expand Down Expand Up @@ -284,6 +285,7 @@ public function __get($name)
*
* @param string $name
* @param mixed $value
* @throws \Inphinit\Experimental\Exception
* @return void
*/
public function __set($name, $value)
Expand Down
Loading

0 comments on commit 44beb7e

Please sign in to comment.