Skip to content

Commit 69c5192

Browse files
committed
added Helpers::getLastError()
1 parent 0cebb14 commit 69c5192

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

src/Utils/FileSystem.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class FileSystem
2626
public static function createDir(string $dir, int $mode = 0777): void
2727
{
2828
if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist
29-
throw new Nette\IOException("Unable to create directory '$dir'. " . self::getLastError());
29+
throw new Nette\IOException("Unable to create directory '$dir'. " . Helpers::getLastError());
3030
}
3131
}
3232

@@ -59,7 +59,7 @@ public static function copy(string $source, string $dest, bool $overwrite = true
5959
} else {
6060
static::createDir(dirname($dest));
6161
if (($s = @fopen($source, 'rb')) && ($d = @fopen($dest, 'wb')) && @stream_copy_to_stream($s, $d) === false) { // @ is escalated to exception
62-
throw new Nette\IOException("Unable to copy file '$source' to '$dest'. " . self::getLastError());
62+
throw new Nette\IOException("Unable to copy file '$source' to '$dest'. " . Helpers::getLastError());
6363
}
6464
}
6565
}
@@ -74,15 +74,15 @@ public static function delete(string $path): void
7474
if (is_file($path) || is_link($path)) {
7575
$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
7676
if (!@$func($path)) { // @ is escalated to exception
77-
throw new Nette\IOException("Unable to delete '$path'. " . self::getLastError());
77+
throw new Nette\IOException("Unable to delete '$path'. " . Helpers::getLastError());
7878
}
7979

8080
} elseif (is_dir($path)) {
8181
foreach (new \FilesystemIterator($path) as $item) {
8282
static::delete($item->getPathname());
8383
}
8484
if (!@rmdir($path)) { // @ is escalated to exception
85-
throw new Nette\IOException("Unable to delete directory '$path'. " . self::getLastError());
85+
throw new Nette\IOException("Unable to delete directory '$path'. " . Helpers::getLastError());
8686
}
8787
}
8888
}
@@ -107,7 +107,7 @@ public static function rename(string $name, string $newName, bool $overwrite = t
107107
static::delete($newName);
108108
}
109109
if (!@rename($name, $newName)) { // @ is escalated to exception
110-
throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'. " . self::getLastError());
110+
throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'. " . Helpers::getLastError());
111111
}
112112
}
113113
}
@@ -121,7 +121,7 @@ public static function read(string $file): string
121121
{
122122
$content = @file_get_contents($file); // @ is escalated to exception
123123
if ($content === false) {
124-
throw new Nette\IOException("Unable to read file '$file'. " . self::getLastError());
124+
throw new Nette\IOException("Unable to read file '$file'. " . Helpers::getLastError());
125125
}
126126
return $content;
127127
}
@@ -135,10 +135,10 @@ public static function write(string $file, string $content, ?int $mode = 0666):
135135
{
136136
static::createDir(dirname($file));
137137
if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
138-
throw new Nette\IOException("Unable to write file '$file'. " . self::getLastError());
138+
throw new Nette\IOException("Unable to write file '$file'. " . Helpers::getLastError());
139139
}
140140
if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
141-
throw new Nette\IOException("Unable to chmod file '$file'. " . self::getLastError());
141+
throw new Nette\IOException("Unable to chmod file '$file'. " . Helpers::getLastError());
142142
}
143143
}
144144

@@ -179,10 +179,4 @@ public static function joinPaths(string ...$paths): string
179179
{
180180
return self::normalizePath(implode('/', $paths));
181181
}
182-
183-
184-
private static function getLastError(): string
185-
{
186-
return preg_replace('#^\w+\(.*?\): #', '', error_get_last()['message']);
187-
}
188182
}

src/Utils/Helpers.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public static function capture(callable $func): string
2828
}
2929

3030

31+
public static function getLastError(): string
32+
{
33+
$message = error_get_last()['message'] ?? '';
34+
if (ini_get('html_errors')) {
35+
$message = html_entity_decode(strip_tags($message));
36+
}
37+
$message = preg_replace('#^\w+\(.*?\): #', '', $message);
38+
return $message;
39+
}
40+
41+
3142
public static function falseToNull($val)
3243
{
3344
return $val === false ? null : $val;

src/Utils/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ private function output(int $type, ?int $quality, string $file = null): void
578578
throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
579579
}
580580
if (!$success) {
581-
throw new ImageException(error_get_last()['message'] ?: 'Unknown error');
581+
throw new ImageException(Helpers::getLastError() ?: 'Unknown error');
582582
}
583583
}
584584

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Utils\Helpers;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::same('', Helpers::getLastError());
13+
14+
ini_set('html_errors', '1');
15+
@constant('"');
16+
Assert::same('Couldn\'t find constant "', Helpers::getLastError());
17+
18+
ini_set('html_errors', '0');
19+
@constant('"');
20+
Assert::same('Couldn\'t find constant "', Helpers::getLastError());
21+
22+
ini_set('html_errors', '1');
23+
@fopen('nonexist', 'r');
24+
Assert::same('failed to open stream: No such file or directory', Helpers::getLastError());
25+
26+
ini_set('html_errors', '0');
27+
@fopen('nonexist', 'r');
28+
Assert::same('failed to open stream: No such file or directory', Helpers::getLastError());

0 commit comments

Comments
 (0)