Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeloses committed Nov 21, 2021
1 parent 67c78d6 commit f1a1c35
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/docs export-ignore
/tests export-ignore
/.git export-ignore
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/Procfile export-ignore
/*.jpg export-ignore
/*.png export-ignore
/*.png export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor/
.env
.cmd
.log
.tmp
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"autoload": {
"psr-4": {
"Xeloses\\XLog\\": "lib"
"Xeloses\\XLog\\": "src"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 6 additions & 2 deletions lib/Providers/LogFile.php → src/Providers/LogFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

class LogFile implements ILogOutputProvider
{
use \Xeloses\XLog\Traits\PhpDocLinkHelper;
use \Xeloses\XLog\Traits\ErrorCodeHelper;
use \Xeloses\XLog\Traits\VarTypeHelper;

Expand All @@ -42,6 +43,8 @@ class LogFile implements ILogOutputProvider
'error' => '[{timestamp}] <{error}> {description} (in {file}:{line})',
'message' => '[{timestamp}] {message}',
'dump' => '[{timestamp}] DUMP <{type}>: {comment}'.PHP_EOL.'{value}',

'link' => '{text}', // remove links; to print links use template like '{text} ({url})'
];

/**
Expand Down Expand Up @@ -92,21 +95,22 @@ public function logError(int $error_code, string $error_description, string $fil
$error_type = $this->getErrorType($error_code);

$type = strtolower($error_category);
$error_description = $this->formatLinks($error_description, $this->templates['link']);

$log = str_replace(
[
'{error}',
'{description}',
'{file}',
'{line}',
'{timestamp}',
'{timestamp}'
],
[
$error_category.': '.$error_type,
$error_description,
$file,
$line,
date($this->options['timestamp_format']),
date($this->options['timestamp_format'])
],
$this->templates['error']
);
Expand Down
6 changes: 5 additions & 1 deletion lib/Providers/LogScreen.php → src/Providers/LogScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

class LogScreen implements ILogOutputProvider
{
use \Xeloses\XLog\Traits\PhpDocLinkHelper;
use \Xeloses\XLog\Traits\ErrorCodeHelper;
use \Xeloses\XLog\Traits\VarTypeHelper;

Expand Down Expand Up @@ -56,6 +57,8 @@ class LogScreen implements ILogOutputProvider
'<pre style="display:block;margin:10px 10px 5px;padding:7px;border:1px inset #aaa;border-radius:10px;background:#222;color:#ddd;font-size:.85em;">{value}</pre>'.
'</div>'.
'</div>'.PHP_EOL,

'link' => '<a href="{url}" title="Open PHP documentation in new tab" target="_blank" rel="noopener noreferer">{text}</a>'.PHP_EOL
];

/**
Expand Down Expand Up @@ -131,6 +134,7 @@ public function logError(int $error_code, string $error_description, string $fil
$error_type = $this->getErrorType($error_code);

$type = strtolower($error_category);
$error_description = $this->formatLinks($error_description, $this->html['link']);

echo str_replace(
[
Expand All @@ -144,7 +148,7 @@ public function logError(int $error_code, string $error_description, string $fil
],
[
'<span style="text-decoration:underline">'.$error_category.'</span>: '.$error_type,
$this->safeStr($error_description),
$error_description,
$file,
$line,
date($this->options['timestamp_format']),
Expand Down
File renamed without changes.
91 changes: 91 additions & 0 deletions src/Traits/PhpDocLinkHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* Intercept and log PHP exceptions/errors/warnings.
*
* @author Xeloses (https://github.com/Xeloses)
* @package XLog (https://github.com/Xeloses/xlog)
* @version 1.0
* @copyright Xeloses 2018-2020
* @license GNU GPL v3 (https://www.gnu.org/licenses/gpl-3.0.html)
*/

namespace Xeloses\XLog\Traits;

/**
* PhpDocLinkHelper trait.
*
* Helper functions working with PHP documentation links.
*
* @package XLog
* @subpackage Traits
*
* @method string formatLinks(string $text, string $template)
*/
trait PhpDocLinkHelper
{
/**
* Default template for links
*
* @var string
*/
protected string $__html_link_template = '<a href="{url}" title="Open PHP documentation in new tab" target="_blank" rel="noopener noreferer">{text}</a>'.PHP_EOL;

/**
* PHP documentation
*
* @var string
*/
protected string $__php_docs_url = 'https://www.php.net/manual/';

/**
* Apply format to all PHP documentation links.
*
* @internal
*
* @param string $text
* @param string $template
*
* @return string
*/
protected function formatLinks(string $text, string $template): string
{
$result = trim($text);

if(!strlen($result) || ini_get('html_errors') != '1' || !ini_get('docref_root')) return $result;

if(!$template) $template = $this->__html_link_template;

if(preg_match_all('/\<a[\s]+href\=[\'\"]{1}(?<url>[\w\-\.\:\/]+)[\'\"]{1}\>(?<text>[^\<]+)\<\/a\>/i', $result, $links, PREG_SET_ORDER|PREG_UNMATCHED_AS_NULL))
{
$php_docref_root = ini_get('docref_root');

foreach($links as $link)
{
//if(str_starts_with($link['url'], $php_docref_root))
if(substr($link['url'], 0, strlen($php_docref_root)) == $php_docref_root)
{
$url = str_replace($php_docref_root, $this->__php_docs_url, $link['url']);
$lnk = str_replace(
[
'{text}',
'{url}'
],
[
$link['text'],
$this->safeStr($url)
],
$template
);

$result = str_replace($link[0], $lnk, $result);
}
}

$result = preg_replace('/[\s]+\]\: /', ']: ', $result);
}

return $result;
}
}
?>
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/XLog.php → src/XLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ protected static function handleException(\Throwable $e): void
protected static function handleShutdown(): void
{
$error = error_get_last();
if($error["type"])
if($error && $error["type"])
{
self::handleError($error["type"],$error["message"],$error["file"],$error["line"]);
}
Expand Down

0 comments on commit f1a1c35

Please sign in to comment.