Skip to content

Commit

Permalink
Fixed production mode and special chars in cli mode
Browse files Browse the repository at this point in the history
  • Loading branch information
solital committed Sep 28, 2024
1 parent 1c2b140 commit ce9970d
Show file tree
Hide file tree
Showing 10 changed files with 3,916 additions and 47 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Released Notes

## v3.3.6 - (2024-09-28)

### Added

- Added favicon in view

### Fixed

- Fixed special chars in CLI mode and JSON. Only in HTML
- Fixed `productionMode` method. Removed previously in 3.0.0
- Fixed code in production mode

### Changed

- Upgrade highlight js component to version 11.10

--------------------------------------------------------------------------

## v3.3.5 - (2024-09-12)

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions index-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
//$exc->ignoreErrors([E_USER_DEPRECATED, E_WARNING]);
$exc->start();

#http_response_code(404);
//http_response_code(404);
#echo '<pre>';

//throw new Exception("<script>alert('test from JS')</script>");

/* trigger_error("Test trigger", E_USER_DEPRECATED);
echo "After trigger function"; */

//UserTest::staticCall();
UserTest::staticCall();

/* function dividir($x, $y) {
if ($y == 0) {
Expand Down
10 changes: 4 additions & 6 deletions src/ModernPHPException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ModernPHPException
{
use HelpersTrait, HandlerAssetsTrait, RenderTrait;

public const VERSION = "3.3.5";
public const VERSION = "3.3.6";

/**
* @var Bench
Expand Down Expand Up @@ -316,9 +316,8 @@ public function errorHandler(int $code, string $message, string $file, int $line
'line' => ($line ?? '')
];

if ($this->getTitle() == "" || empty($this->getTitle())) {
if ($this->getTitle() == "" || empty($this->getTitle()))
$this->setTitle("ModernPHPException: " . $message);
}

$this->setError($code);
$this->type = "error";
Expand All @@ -341,7 +340,7 @@ public function errorHandler(int $code, string $message, string $file, int $line
*/
public function exceptionHandler(mixed $exception): void
{
$message = htmlspecialchars($exception->getMessage());
$message = $this->htmlSpecialCharsIgnoreCli($exception->getMessage());

$this->info_error_exception = [
'message' => $message,
Expand All @@ -352,9 +351,8 @@ public function exceptionHandler(mixed $exception): void
'namespace_exception' => get_class($exception)
];

if ($this->getTitle() == "" || empty($this->getTitle())) {
if ($this->getTitle() == "" || empty($this->getTitle()))
$this->setTitle("ModernPHPException: " . $message);
}

$reflection_class = new \ReflectionClass($this->info_error_exception['namespace_exception']);
$class_name = $reflection_class->newInstanceWithoutConstructor();
Expand Down
4 changes: 2 additions & 2 deletions src/Trait/HandlerAssetsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ private function loadAssets(array $info): string
private function loadJs(): string
{
if ($this->config['enable_cdn_assets'] == false) {
$asset = '<script>' . file_get_contents('assets/js/highlight.pack.js', FILE_USE_INCLUDE_PATH);
$asset = '<script>' . file_get_contents('assets/js/highlight.min.js', FILE_USE_INCLUDE_PATH);
$asset .= file_get_contents('assets/js/highlightjs-line-numbers.js', FILE_USE_INCLUDE_PATH) . '</script>';
$asset .= '<script>hljs.highlightAll(); hljs.initLineNumbersOnLoad();</script>';
$asset .= '<script>' . file_get_contents('assets/js/bootstrap.bundle.min.js', FILE_USE_INCLUDE_PATH) . '</script>';
$asset .= '<script>' . file_get_contents('assets/js/less.js', FILE_USE_INCLUDE_PATH) . '</script>';
} elseif ($this->config['enable_cdn_assets'] == true) {
$asset = '<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>';
$asset = '<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/highlight.min.js"></script>';
$asset .= '<script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js"></script>';
$asset .= '<script>hljs.highlightAll();hljs.initLineNumbersOnLoad();</script>';
$asset .= '<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>';
Expand Down
32 changes: 5 additions & 27 deletions src/Trait/HelpersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

trait HelpersTrait
{
/**
* @return bool
*/
private function isCli(): bool
{
if (defined('STDIN')) return true;
Expand All @@ -17,59 +14,36 @@ private function isCli(): bool
if (
empty($_SERVER['REMOTE_ADDR']) and
!isset($_SERVER['HTTP_USER_AGENT']) and
count($_SERVER['argv']) > 0
count((array)$_SERVER['argv']) > 0
) {
return true;
}

return false;
}

/**
* @param string $path
*
* @return string
*/
public function getPathInfo(string $path): string
{
return strtolower(pathinfo($path)['filename']);
}

/**
* @param string $value
*
* @return string
*/
private function replaceString(string $value): string
{
return str_replace(['#', '{', '}', '(', ')', '.'], '', $value);
}

/**
* @param string $value
*
* @return string
*/
private function replaceUrl(string $value): string
{
return str_replace(' ', '+', $value);
}

/**
* @param object $classname
*
* @return string
*/
private function getClassName(object $classname): string
{
$class = get_class($classname);
$class = explode("\\", $class);
return end($class);
}

/**
* @return string
*/
private static function getUri(): string
{
$http = "https://";
Expand All @@ -78,4 +52,8 @@ private static function getUri(): string
return $http . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

public function htmlSpecialCharsIgnoreCli(string $string): string
{
return (!$this->isCli()) ? htmlspecialchars($string) : $string;
}
}
22 changes: 17 additions & 5 deletions src/Trait/RenderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ private function render(): never
exit;
}

/**
* Enable production mode
*
* @return void
*/
private function productionMode(): void
{
include_once dirname(__DIR__) . '/View/templates/error-production.php';
exit;
}

/**
* Unset trace without 'file' and 'line' keys
*
Expand Down Expand Up @@ -151,7 +162,10 @@ private function loadResources(): array
private function renderJson(): never
{
if ($this->type == "error") {
echo json_encode([$this->getError() => $this->info_error_exception], JSON_UNESCAPED_UNICODE);
echo json_encode(
[$this->getError() => $this->info_error_exception],
JSON_UNESCAPED_UNICODE
);
}

if ($this->type == "exception") {
Expand All @@ -174,11 +188,9 @@ private function renderCli(): void
if ($this->isCli() === true) {
echo PHP_EOL;

if (isset($this->info_error_exception['type_exception'])) {
CliMessage::error($this->info_error_exception['type_exception'])->print();
} else {
(isset($this->info_error_exception['type_exception'])) ?
CliMessage::error($this->info_error_exception['type_exception'])->print() :
CliMessage::error($this->getError())->print();
}

CliMessage::line(" : " . $this->info_error_exception['message'])->print()->break(true);
$this->renderSolutionCli();
Expand Down
Loading

0 comments on commit ce9970d

Please sign in to comment.