Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit dd927c0

Browse files
author
Biuhub Studio
committed
updated
1 parent a7025e3 commit dd927c0

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

src/AbstractFileHandler.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ public function has_string(string $str): bool
293293
}
294294
}
295295

296+
public function match(string $pattern)
297+
{
298+
return $this->silencer->call('glob', $pattern, GLOB_ERR);
299+
}
300+
296301
/**
297302
* Match path against an extended wildcard pattern.
298303
*
@@ -389,8 +394,6 @@ public function put($contents, $lock = false)
389394
*
390395
* @param string $path
391396
* @param string $content
392-
*
393-
* @return void
394397
*/
395398
public function replace($content = null)
396399
{
@@ -546,7 +549,7 @@ protected function _decode($var, $options = null): string
546549
public function mkdir($dir = null)
547550
{
548551
if ($dir !== null) {
549-
$this->path = $dir;
552+
$this->path = $this->silencer->call('dirname', $dir);
550553
}
551554

552555
// Silence error for open_basedir; should fail in mkdir instead.
@@ -628,7 +631,7 @@ public function remove_dir($dir = null, $traverseSymlinks = false): bool
628631
/**
629632
* See unlink() or unset().
630633
*
631-
* @return boolean
634+
* @return bool
632635
*/
633636
public function delete(): bool
634637
{
@@ -650,7 +653,7 @@ public function delete(): bool
650653
/**
651654
* Checks whether the file is a directory.
652655
*
653-
* @return boolean
656+
* @return bool
654657
*/
655658
public function is_dir(): bool
656659
{
@@ -693,7 +696,7 @@ public function is_empty($exclude = []): bool
693696
/**
694697
* Checks whether the file is a regular file.
695698
*
696-
* @return boolean
699+
* @return bool
697700
*/
698701
public function is_file(): bool
699702
{
@@ -847,7 +850,7 @@ public function free()
847850
* @param string $filename The filename to set the writable bit on
848851
* @param bool $writable Whether to make the file writable or not
849852
*
850-
* @return boolean
853+
* @return bool
851854
*/
852855
public function writable(bool $writable = true): bool
853856
{
@@ -864,7 +867,7 @@ public function writable(bool $writable = true): bool
864867
* @param string $filename The filename to set the readable bit on
865868
* @param bool $readable Whether to make the file readable or not
866869
*
867-
* @return boolean
870+
* @return bool
868871
*/
869872
public function readable(bool $readable = true): bool
870873
{
@@ -880,7 +883,7 @@ public function readable(bool $readable = true): bool
880883
*
881884
* @param bool $executable Whether to make the file executable or not
882885
*
883-
* @return boolean
886+
* @return bool
884887
*/
885888
public function executable($executable = true): bool
886889
{

src/FileHandler.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919
* funtion usuage when it comes to php filesystem.
2020
*
2121
* @author Divine Niiquaye <[email protected]>
22+
* @license MIT
2223
*/
2324
class FileHandler extends AbstractFileHandler
2425
{
26+
public function __construct()
27+
{
28+
parent::__construct();
29+
}
2530
/**
2631
* Get relative path between target and base path. If path isn't relative, return full path.
2732
*
@@ -156,7 +161,7 @@ public function extension(): string
156161
/**
157162
* @return string
158163
*/
159-
public function real(): string
164+
public function realpath(): string
160165
{
161166
if (isset($this->path)) {
162167
return $this->silencer->call('realpath', $this->path);
@@ -229,6 +234,47 @@ public function mime_type()
229234
}
230235
}
231236

237+
/**
238+
* Creates a temporary directory.
239+
*
240+
* @param string $namespace the directory path in the system's temporary directory
241+
* @param string $className the name of the test class
242+
*
243+
* @return string the path to the created directory
244+
*/
245+
public function makeTmpDir(string $namespace, string $className): string
246+
{
247+
if (false !== ($pos = strrpos($className, '\\'))) {
248+
$shortClass = substr($className, $pos + 1);
249+
} else {
250+
$shortClass = $className;
251+
}
252+
253+
// Usage of realpath() is important if the temporary directory is a
254+
// symlink to another directory (e.g. /var => /private/var on some Macs)
255+
// We want to know the real path to avoid comparison failures with
256+
// code that uses real paths only
257+
$systemTempDir = str_replace('\\', '/', $this->silencer->call('realpath', sys_get_temp_dir()));
258+
$basePath = $systemTempDir.'/'.$namespace.'/'.$shortClass;
259+
260+
$result = false;
261+
$attempts = 0;
262+
263+
do {
264+
$tmpDir = str_replace('/', DIRECTORY_SEPARATOR, $basePath.random_int(10000, 99999));
265+
266+
try {
267+
$this->silencer->call('mkdir', $tmpDir, 0777);
268+
269+
$result = true;
270+
} catch (FileException $exception) {
271+
++$attempts;
272+
}
273+
} while (false === $result && $attempts <= 10);
274+
275+
return $tmpDir;
276+
}
277+
232278
/**
233279
* Changes the file group.
234280
*

0 commit comments

Comments
 (0)