@vituum/vite-plugin-latte 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/vendor/autoload.php +1 -1
- package/vendor/composer/autoload_classmap.php +49 -0
- package/vendor/composer/autoload_real.php +4 -4
- package/vendor/composer/autoload_static.php +51 -2
- package/vendor/composer/installed.json +102 -12
- package/vendor/composer/installed.php +14 -5
- package/vendor/latte/latte/composer.json +6 -5
- package/vendor/latte/latte/readme.md +27 -9
- package/vendor/latte/latte/src/Bridges/Tracy/templates/LattePanel.panel.phtml +4 -1
- package/vendor/latte/latte/src/Latte/Compiler/TagLexer.php +1 -1
- package/vendor/latte/latte/src/Latte/Compiler/TagParserData.php +129 -129
- package/vendor/latte/latte/src/Latte/Compiler/TemplateGenerator.php +1 -1
- package/vendor/latte/latte/src/Latte/Compiler/TemplateParser.php +1 -1
- package/vendor/latte/latte/src/Latte/Engine.php +22 -2
- package/vendor/latte/latte/src/Latte/Essential/CachingIterator.php +2 -3
- package/vendor/latte/latte/src/Latte/Essential/CoreExtension.php +9 -1
- package/vendor/latte/latte/src/Latte/Essential/Filters.php +110 -10
- package/vendor/latte/latte/src/Latte/Essential/Nodes/ImportNode.php +8 -2
- package/vendor/latte/latte/src/Latte/Essential/Nodes/VarNode.php +14 -18
- package/vendor/latte/latte/src/Latte/Essential/TranslatorExtension.php +1 -1
- package/vendor/latte/latte/src/Latte/Loaders/FileLoader.php +5 -4
- package/vendor/latte/latte/src/Latte/Runtime/Template.php +1 -1
- package/vendor/latte/latte/src/Latte/Sandbox/Nodes/FunctionCallNode.php +0 -1
- package/vendor/nette/utils/.phpstorm.meta.php +13 -0
- package/vendor/nette/utils/composer.json +51 -0
- package/vendor/nette/utils/license.md +60 -0
- package/vendor/nette/utils/readme.md +55 -0
- package/vendor/nette/utils/src/HtmlStringable.php +22 -0
- package/vendor/nette/utils/src/Iterators/CachingIterator.php +150 -0
- package/vendor/nette/utils/src/Iterators/Mapper.php +33 -0
- package/vendor/nette/utils/src/SmartObject.php +140 -0
- package/vendor/nette/utils/src/StaticClass.php +34 -0
- package/vendor/nette/utils/src/Translator.php +25 -0
- package/vendor/nette/utils/src/Utils/ArrayHash.php +106 -0
- package/vendor/nette/utils/src/Utils/ArrayList.php +136 -0
- package/vendor/nette/utils/src/Utils/Arrays.php +553 -0
- package/vendor/nette/utils/src/Utils/Callback.php +137 -0
- package/vendor/nette/utils/src/Utils/DateTime.php +140 -0
- package/vendor/nette/utils/src/Utils/FileInfo.php +69 -0
- package/vendor/nette/utils/src/Utils/FileSystem.php +326 -0
- package/vendor/nette/utils/src/Utils/Finder.php +510 -0
- package/vendor/nette/utils/src/Utils/Floats.php +107 -0
- package/vendor/nette/utils/src/Utils/Helpers.php +104 -0
- package/vendor/nette/utils/src/Utils/Html.php +839 -0
- package/vendor/nette/utils/src/Utils/Image.php +831 -0
- package/vendor/nette/utils/src/Utils/ImageColor.php +75 -0
- package/vendor/nette/utils/src/Utils/ImageType.php +25 -0
- package/vendor/nette/utils/src/Utils/Iterables.php +238 -0
- package/vendor/nette/utils/src/Utils/Json.php +84 -0
- package/vendor/nette/utils/src/Utils/ObjectHelpers.php +229 -0
- package/vendor/nette/utils/src/Utils/Paginator.php +245 -0
- package/vendor/nette/utils/src/Utils/Random.php +52 -0
- package/vendor/nette/utils/src/Utils/Reflection.php +322 -0
- package/vendor/nette/utils/src/Utils/ReflectionMethod.php +36 -0
- package/vendor/nette/utils/src/Utils/Strings.php +728 -0
- package/vendor/nette/utils/src/Utils/Type.php +267 -0
- package/vendor/nette/utils/src/Utils/Validators.php +416 -0
- package/vendor/nette/utils/src/Utils/exceptions.php +50 -0
- package/vendor/nette/utils/src/compatibility.php +32 -0
- package/vendor/nette/utils/src/exceptions.php +109 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This file is part of the Nette Framework (https://nette.org)
|
|
5
|
+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
namespace Nette\Utils;
|
|
11
|
+
|
|
12
|
+
use Nette;
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* DateTime.
|
|
17
|
+
*/
|
|
18
|
+
class DateTime extends \DateTime implements \JsonSerializable
|
|
19
|
+
{
|
|
20
|
+
use Nette\SmartObject;
|
|
21
|
+
|
|
22
|
+
/** minute in seconds */
|
|
23
|
+
public const MINUTE = 60;
|
|
24
|
+
|
|
25
|
+
/** hour in seconds */
|
|
26
|
+
public const HOUR = 60 * self::MINUTE;
|
|
27
|
+
|
|
28
|
+
/** day in seconds */
|
|
29
|
+
public const DAY = 24 * self::HOUR;
|
|
30
|
+
|
|
31
|
+
/** week in seconds */
|
|
32
|
+
public const WEEK = 7 * self::DAY;
|
|
33
|
+
|
|
34
|
+
/** average month in seconds */
|
|
35
|
+
public const MONTH = 2_629_800;
|
|
36
|
+
|
|
37
|
+
/** average year in seconds */
|
|
38
|
+
public const YEAR = 31_557_600;
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a DateTime object from a string, UNIX timestamp, or other DateTimeInterface object.
|
|
43
|
+
* @throws \Exception if the date and time are not valid.
|
|
44
|
+
*/
|
|
45
|
+
public static function from(string|int|\DateTimeInterface|null $time): static
|
|
46
|
+
{
|
|
47
|
+
if ($time instanceof \DateTimeInterface) {
|
|
48
|
+
return new static($time->format('Y-m-d H:i:s.u'), $time->getTimezone());
|
|
49
|
+
|
|
50
|
+
} elseif (is_numeric($time)) {
|
|
51
|
+
if ($time <= self::YEAR) {
|
|
52
|
+
$time += time();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return (new static)->setTimestamp((int) $time);
|
|
56
|
+
|
|
57
|
+
} else { // textual or null
|
|
58
|
+
return new static((string) $time);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Creates DateTime object.
|
|
65
|
+
* @throws Nette\InvalidArgumentException if the date and time are not valid.
|
|
66
|
+
*/
|
|
67
|
+
public static function fromParts(
|
|
68
|
+
int $year,
|
|
69
|
+
int $month,
|
|
70
|
+
int $day,
|
|
71
|
+
int $hour = 0,
|
|
72
|
+
int $minute = 0,
|
|
73
|
+
float $second = 0.0,
|
|
74
|
+
): static
|
|
75
|
+
{
|
|
76
|
+
$s = sprintf('%04d-%02d-%02d %02d:%02d:%02.5F', $year, $month, $day, $hour, $minute, $second);
|
|
77
|
+
if (
|
|
78
|
+
!checkdate($month, $day, $year)
|
|
79
|
+
|| $hour < 0
|
|
80
|
+
|| $hour > 23
|
|
81
|
+
|| $minute < 0
|
|
82
|
+
|| $minute > 59
|
|
83
|
+
|| $second < 0
|
|
84
|
+
|| $second >= 60
|
|
85
|
+
) {
|
|
86
|
+
throw new Nette\InvalidArgumentException("Invalid date '$s'");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return new static($s);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Returns new DateTime object formatted according to the specified format.
|
|
95
|
+
*/
|
|
96
|
+
public static function createFromFormat(
|
|
97
|
+
string $format,
|
|
98
|
+
string $time,
|
|
99
|
+
string|\DateTimeZone|null $timezone = null,
|
|
100
|
+
): static|false
|
|
101
|
+
{
|
|
102
|
+
if ($timezone === null) {
|
|
103
|
+
$timezone = new \DateTimeZone(date_default_timezone_get());
|
|
104
|
+
|
|
105
|
+
} elseif (is_string($timezone)) {
|
|
106
|
+
$timezone = new \DateTimeZone($timezone);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
$date = parent::createFromFormat($format, $time, $timezone);
|
|
110
|
+
return $date ? static::from($date) : false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Returns JSON representation in ISO 8601 (used by JavaScript).
|
|
116
|
+
*/
|
|
117
|
+
public function jsonSerialize(): string
|
|
118
|
+
{
|
|
119
|
+
return $this->format('c');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns the date and time in the format 'Y-m-d H:i:s'.
|
|
125
|
+
*/
|
|
126
|
+
public function __toString(): string
|
|
127
|
+
{
|
|
128
|
+
return $this->format('Y-m-d H:i:s');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* You'd better use: (clone $dt)->modify(...)
|
|
134
|
+
*/
|
|
135
|
+
public function modifyClone(string $modify = ''): static
|
|
136
|
+
{
|
|
137
|
+
$dolly = clone $this;
|
|
138
|
+
return $modify ? $dolly->modify($modify) : $dolly;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This file is part of the Nette Framework (https://nette.org)
|
|
5
|
+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
namespace Nette\Utils;
|
|
11
|
+
|
|
12
|
+
use Nette;
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represents the file or directory returned by the Finder.
|
|
17
|
+
* @internal do not create instances directly
|
|
18
|
+
*/
|
|
19
|
+
final class FileInfo extends \SplFileInfo
|
|
20
|
+
{
|
|
21
|
+
private string $relativePath;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
public function __construct(string $file, string $relativePath = '')
|
|
25
|
+
{
|
|
26
|
+
parent::__construct($file);
|
|
27
|
+
$this->setInfoClass(static::class);
|
|
28
|
+
$this->relativePath = $relativePath;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns the relative directory path.
|
|
34
|
+
*/
|
|
35
|
+
public function getRelativePath(): string
|
|
36
|
+
{
|
|
37
|
+
return $this->relativePath;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns the relative path including file name.
|
|
43
|
+
*/
|
|
44
|
+
public function getRelativePathname(): string
|
|
45
|
+
{
|
|
46
|
+
return ($this->relativePath === '' ? '' : $this->relativePath . DIRECTORY_SEPARATOR)
|
|
47
|
+
. $this->getBasename();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns the contents of the file.
|
|
53
|
+
* @throws Nette\IOException
|
|
54
|
+
*/
|
|
55
|
+
public function read(): string
|
|
56
|
+
{
|
|
57
|
+
return FileSystem::read($this->getPathname());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Writes the contents to the file.
|
|
63
|
+
* @throws Nette\IOException
|
|
64
|
+
*/
|
|
65
|
+
public function write(string $content): void
|
|
66
|
+
{
|
|
67
|
+
FileSystem::write($this->getPathname(), $content);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This file is part of the Nette Framework (https://nette.org)
|
|
5
|
+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
namespace Nette\Utils;
|
|
11
|
+
|
|
12
|
+
use Nette;
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* File system tool.
|
|
17
|
+
*/
|
|
18
|
+
final class FileSystem
|
|
19
|
+
{
|
|
20
|
+
/**
|
|
21
|
+
* Creates a directory if it does not exist, including parent directories.
|
|
22
|
+
* @throws Nette\IOException on error occurred
|
|
23
|
+
*/
|
|
24
|
+
public static function createDir(string $dir, int $mode = 0777): void
|
|
25
|
+
{
|
|
26
|
+
if (!is_dir($dir) && !@mkdir($dir, $mode, recursive: true) && !is_dir($dir)) { // @ - dir may already exist
|
|
27
|
+
throw new Nette\IOException(sprintf(
|
|
28
|
+
"Unable to create directory '%s' with mode %s. %s",
|
|
29
|
+
self::normalizePath($dir),
|
|
30
|
+
decoct($mode),
|
|
31
|
+
Helpers::getLastError(),
|
|
32
|
+
));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Copies a file or an entire directory. Overwrites existing files and directories by default.
|
|
39
|
+
* @throws Nette\IOException on error occurred
|
|
40
|
+
* @throws Nette\InvalidStateException if $overwrite is set to false and destination already exists
|
|
41
|
+
*/
|
|
42
|
+
public static function copy(string $origin, string $target, bool $overwrite = true): void
|
|
43
|
+
{
|
|
44
|
+
if (stream_is_local($origin) && !file_exists($origin)) {
|
|
45
|
+
throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($origin)));
|
|
46
|
+
|
|
47
|
+
} elseif (!$overwrite && file_exists($target)) {
|
|
48
|
+
throw new Nette\InvalidStateException(sprintf("File or directory '%s' already exists.", self::normalizePath($target)));
|
|
49
|
+
|
|
50
|
+
} elseif (is_dir($origin)) {
|
|
51
|
+
static::createDir($target);
|
|
52
|
+
foreach (new \FilesystemIterator($target) as $item) {
|
|
53
|
+
static::delete($item->getPathname());
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($origin, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
|
|
57
|
+
if ($item->isDir()) {
|
|
58
|
+
static::createDir($target . '/' . $iterator->getSubPathName());
|
|
59
|
+
} else {
|
|
60
|
+
static::copy($item->getPathname(), $target . '/' . $iterator->getSubPathName());
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
static::createDir(dirname($target));
|
|
65
|
+
if (@stream_copy_to_stream(static::open($origin, 'rb'), static::open($target, 'wb')) === false) { // @ is escalated to exception
|
|
66
|
+
throw new Nette\IOException(sprintf(
|
|
67
|
+
"Unable to copy file '%s' to '%s'. %s",
|
|
68
|
+
self::normalizePath($origin),
|
|
69
|
+
self::normalizePath($target),
|
|
70
|
+
Helpers::getLastError(),
|
|
71
|
+
));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Opens file and returns resource.
|
|
79
|
+
* @return resource
|
|
80
|
+
* @throws Nette\IOException on error occurred
|
|
81
|
+
*/
|
|
82
|
+
public static function open(string $path, string $mode)
|
|
83
|
+
{
|
|
84
|
+
$f = @fopen($path, $mode); // @ is escalated to exception
|
|
85
|
+
if (!$f) {
|
|
86
|
+
throw new Nette\IOException(sprintf(
|
|
87
|
+
"Unable to open file '%s'. %s",
|
|
88
|
+
self::normalizePath($path),
|
|
89
|
+
Helpers::getLastError(),
|
|
90
|
+
));
|
|
91
|
+
}
|
|
92
|
+
return $f;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first.
|
|
98
|
+
* @throws Nette\IOException on error occurred
|
|
99
|
+
*/
|
|
100
|
+
public static function delete(string $path): void
|
|
101
|
+
{
|
|
102
|
+
if (is_file($path) || is_link($path)) {
|
|
103
|
+
$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
|
|
104
|
+
if (!@$func($path)) { // @ is escalated to exception
|
|
105
|
+
throw new Nette\IOException(sprintf(
|
|
106
|
+
"Unable to delete '%s'. %s",
|
|
107
|
+
self::normalizePath($path),
|
|
108
|
+
Helpers::getLastError(),
|
|
109
|
+
));
|
|
110
|
+
}
|
|
111
|
+
} elseif (is_dir($path)) {
|
|
112
|
+
foreach (new \FilesystemIterator($path) as $item) {
|
|
113
|
+
static::delete($item->getPathname());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!@rmdir($path)) { // @ is escalated to exception
|
|
117
|
+
throw new Nette\IOException(sprintf(
|
|
118
|
+
"Unable to delete directory '%s'. %s",
|
|
119
|
+
self::normalizePath($path),
|
|
120
|
+
Helpers::getLastError(),
|
|
121
|
+
));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Renames or moves a file or a directory. Overwrites existing files and directories by default.
|
|
129
|
+
* @throws Nette\IOException on error occurred
|
|
130
|
+
* @throws Nette\InvalidStateException if $overwrite is set to false and destination already exists
|
|
131
|
+
*/
|
|
132
|
+
public static function rename(string $origin, string $target, bool $overwrite = true): void
|
|
133
|
+
{
|
|
134
|
+
if (!$overwrite && file_exists($target)) {
|
|
135
|
+
throw new Nette\InvalidStateException(sprintf("File or directory '%s' already exists.", self::normalizePath($target)));
|
|
136
|
+
|
|
137
|
+
} elseif (!file_exists($origin)) {
|
|
138
|
+
throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($origin)));
|
|
139
|
+
|
|
140
|
+
} else {
|
|
141
|
+
static::createDir(dirname($target));
|
|
142
|
+
if (realpath($origin) !== realpath($target)) {
|
|
143
|
+
static::delete($target);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!@rename($origin, $target)) { // @ is escalated to exception
|
|
147
|
+
throw new Nette\IOException(sprintf(
|
|
148
|
+
"Unable to rename file or directory '%s' to '%s'. %s",
|
|
149
|
+
self::normalizePath($origin),
|
|
150
|
+
self::normalizePath($target),
|
|
151
|
+
Helpers::getLastError(),
|
|
152
|
+
));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Reads the content of a file.
|
|
160
|
+
* @throws Nette\IOException on error occurred
|
|
161
|
+
*/
|
|
162
|
+
public static function read(string $file): string
|
|
163
|
+
{
|
|
164
|
+
$content = @file_get_contents($file); // @ is escalated to exception
|
|
165
|
+
if ($content === false) {
|
|
166
|
+
throw new Nette\IOException(sprintf(
|
|
167
|
+
"Unable to read file '%s'. %s",
|
|
168
|
+
self::normalizePath($file),
|
|
169
|
+
Helpers::getLastError(),
|
|
170
|
+
));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return $content;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Reads the file content line by line. Because it reads continuously as we iterate over the lines,
|
|
179
|
+
* it is possible to read files larger than the available memory.
|
|
180
|
+
* @return \Generator<int, string>
|
|
181
|
+
* @throws Nette\IOException on error occurred
|
|
182
|
+
*/
|
|
183
|
+
public static function readLines(string $file, bool $stripNewLines = true): \Generator
|
|
184
|
+
{
|
|
185
|
+
return (function ($f) use ($file, $stripNewLines) {
|
|
186
|
+
$counter = 0;
|
|
187
|
+
do {
|
|
188
|
+
$line = Callback::invokeSafe('fgets', [$f], fn($error) => throw new Nette\IOException(sprintf(
|
|
189
|
+
"Unable to read file '%s'. %s",
|
|
190
|
+
self::normalizePath($file),
|
|
191
|
+
$error,
|
|
192
|
+
)));
|
|
193
|
+
if ($line === false) {
|
|
194
|
+
fclose($f);
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
if ($stripNewLines) {
|
|
198
|
+
$line = rtrim($line, "\r\n");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
yield $counter++ => $line;
|
|
202
|
+
|
|
203
|
+
} while (true);
|
|
204
|
+
})(static::open($file, 'r'));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Writes the string to a file.
|
|
210
|
+
* @throws Nette\IOException on error occurred
|
|
211
|
+
*/
|
|
212
|
+
public static function write(string $file, string $content, ?int $mode = 0666): void
|
|
213
|
+
{
|
|
214
|
+
static::createDir(dirname($file));
|
|
215
|
+
if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
|
|
216
|
+
throw new Nette\IOException(sprintf(
|
|
217
|
+
"Unable to write file '%s'. %s",
|
|
218
|
+
self::normalizePath($file),
|
|
219
|
+
Helpers::getLastError(),
|
|
220
|
+
));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
|
|
224
|
+
throw new Nette\IOException(sprintf(
|
|
225
|
+
"Unable to chmod file '%s' to mode %s. %s",
|
|
226
|
+
self::normalizePath($file),
|
|
227
|
+
decoct($mode),
|
|
228
|
+
Helpers::getLastError(),
|
|
229
|
+
));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Sets file permissions to `$fileMode` or directory permissions to `$dirMode`.
|
|
236
|
+
* Recursively traverses and sets permissions on the entire contents of the directory as well.
|
|
237
|
+
* @throws Nette\IOException on error occurred
|
|
238
|
+
*/
|
|
239
|
+
public static function makeWritable(string $path, int $dirMode = 0777, int $fileMode = 0666): void
|
|
240
|
+
{
|
|
241
|
+
if (is_file($path)) {
|
|
242
|
+
if (!@chmod($path, $fileMode)) { // @ is escalated to exception
|
|
243
|
+
throw new Nette\IOException(sprintf(
|
|
244
|
+
"Unable to chmod file '%s' to mode %s. %s",
|
|
245
|
+
self::normalizePath($path),
|
|
246
|
+
decoct($fileMode),
|
|
247
|
+
Helpers::getLastError(),
|
|
248
|
+
));
|
|
249
|
+
}
|
|
250
|
+
} elseif (is_dir($path)) {
|
|
251
|
+
foreach (new \FilesystemIterator($path) as $item) {
|
|
252
|
+
static::makeWritable($item->getPathname(), $dirMode, $fileMode);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (!@chmod($path, $dirMode)) { // @ is escalated to exception
|
|
256
|
+
throw new Nette\IOException(sprintf(
|
|
257
|
+
"Unable to chmod directory '%s' to mode %s. %s",
|
|
258
|
+
self::normalizePath($path),
|
|
259
|
+
decoct($dirMode),
|
|
260
|
+
Helpers::getLastError(),
|
|
261
|
+
));
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($path)));
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Determines if the path is absolute.
|
|
271
|
+
*/
|
|
272
|
+
public static function isAbsolute(string $path): bool
|
|
273
|
+
{
|
|
274
|
+
return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Normalizes `..` and `.` and directory separators in path.
|
|
280
|
+
*/
|
|
281
|
+
public static function normalizePath(string $path): string
|
|
282
|
+
{
|
|
283
|
+
$parts = $path === '' ? [] : preg_split('~[/\\\\]+~', $path);
|
|
284
|
+
$res = [];
|
|
285
|
+
foreach ($parts as $part) {
|
|
286
|
+
if ($part === '..' && $res && end($res) !== '..' && end($res) !== '') {
|
|
287
|
+
array_pop($res);
|
|
288
|
+
} elseif ($part !== '.') {
|
|
289
|
+
$res[] = $part;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return $res === ['']
|
|
294
|
+
? DIRECTORY_SEPARATOR
|
|
295
|
+
: implode(DIRECTORY_SEPARATOR, $res);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Joins all segments of the path and normalizes the result.
|
|
301
|
+
*/
|
|
302
|
+
public static function joinPaths(string ...$paths): string
|
|
303
|
+
{
|
|
304
|
+
return self::normalizePath(implode('/', $paths));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Converts backslashes to slashes.
|
|
310
|
+
*/
|
|
311
|
+
public static function unixSlashes(string $path): string
|
|
312
|
+
{
|
|
313
|
+
return strtr($path, '\\', '/');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Converts slashes to platform-specific directory separators.
|
|
319
|
+
*/
|
|
320
|
+
public static function platformSlashes(string $path): string
|
|
321
|
+
{
|
|
322
|
+
return DIRECTORY_SEPARATOR === '/'
|
|
323
|
+
? strtr($path, '\\', '/')
|
|
324
|
+
: str_replace(':\\\\', '://', strtr($path, '/', '\\')); // protocol://
|
|
325
|
+
}
|
|
326
|
+
}
|