Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added formatter for compiler messages
- Loading branch information
1 parent
86ccc38
commit 9c667de
Showing
5 changed files
with
139 additions
and
133 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Zephir. | ||
* | ||
* (c) Zephir Team <team@zephir-lang.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zephir\Logger\Formatter; | ||
|
||
use Monolog\Formatter\LineFormatter; | ||
use Zephir\Config; | ||
|
||
/** | ||
* Formatter for warnings/notices/errors generated in compilation. | ||
*/ | ||
final class CompilerFormatter extends LineFormatter | ||
{ | ||
const SIMPLE_FORMAT = "%level_name%: %message% in %file% on line %line% %type%\n"; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* The contents of the files that are involved in the log message. | ||
* | ||
* @var array | ||
*/ | ||
private $filesContent = []; | ||
|
||
public function __construct(Config $config) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format(array $record) | ||
{ | ||
if ($this->config->get('silent')) { | ||
return ''; | ||
} | ||
|
||
$vars = parent::normalize($record); | ||
|
||
$output = $this->format; | ||
|
||
// unused | ||
unset($vars['extra']); | ||
$output = str_replace('%extra%', '', $output); | ||
$output = str_replace('%context%', '', $output); | ||
|
||
// ignore empty context or invalid format | ||
if (!empty($vars['context']) && | ||
\is_array($vars['context']) && | ||
2 == \count($vars['context']) | ||
) { | ||
$type = $vars['context'][0]; | ||
$node = $vars['context'][1]; | ||
|
||
if (!$this->config->get($type, 'warnings')) { | ||
return ''; | ||
} | ||
|
||
$vars['type'] = "[{$type}]"; | ||
|
||
if (!isset($node['file'])) { | ||
$vars['file'] = 'unknown'; | ||
$vars['line'] = '0'; | ||
} else { | ||
$vars['file'] = $node['file']; | ||
$vars['line'] = $node['line']; | ||
$output .= PHP_EOL; | ||
|
||
$lines = $this->getFileContents($node['file']); | ||
if (isset($lines[$node['line'] - 1])) { | ||
$line = $lines[$node['line'] - 1]; | ||
$output .= "\t".str_replace("\t", ' ', $line); | ||
if (($node['char'] - 1) > 0) { | ||
$output .= PHP_EOL."\t".str_repeat('-', $node['char'] - 1).'^'.PHP_EOL; | ||
} | ||
} | ||
} | ||
|
||
$output = str_replace('%file%', $this->stringify($vars['file']), $output); | ||
$output = str_replace('%line%', $this->stringify($vars['line']), $output); | ||
$output = str_replace('%type%', $this->stringify($vars['type']), $output); | ||
} | ||
|
||
unset($vars['context']); | ||
|
||
// WARNING -> Warning | ||
$vars['level_name'] = ucfirst(strtolower($vars['level_name'])); | ||
|
||
foreach ($vars as $var => $val) { | ||
if (false !== strpos($output, '%'.$var.'%')) { | ||
$output = str_replace('%'.$var.'%', $this->stringify($val), $output); | ||
} | ||
} | ||
|
||
// remove leftover %extra.xxx% and %context.xxx% if any | ||
if (false !== strpos($output, '%')) { | ||
$output = preg_replace('/%(?:extra|context)\..+?%/', '', $output); | ||
$output = preg_replace('/ %type%\n/', "\n", $output); | ||
$output = preg_replace('/on line %line%/', '', $output); | ||
$output = preg_replace('/ in %file% /', '', $output); | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* Gets the contents of the files that are involved in the log message. | ||
* | ||
* @param string $file File path | ||
* | ||
* @return array | ||
*/ | ||
private function getFileContents($file) | ||
{ | ||
if (!isset($this->filesContent[$file])) { | ||
$this->filesContent[$file] = file_exists($file) ? file($file) : []; | ||
} | ||
|
||
return $this->filesContent[$file]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters