Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FS.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function getInstance(): FileSystem
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (!isset(self::$filesystem)) {
self::$filesystem = new FileSystem();
self::$filesystem = new NativeFileSystem();
}

return self::$filesystem;
Expand Down
92 changes: 7 additions & 85 deletions src/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,8 @@
namespace Fidry\FileSystem;

use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Symfony\Component\Filesystem\Path;
use Webmozart\Assert\Assert;
use function error_get_last;
use function file_get_contents;
use function random_int;
use function realpath;
use function sprintf;
use function str_replace;
use function sys_get_temp_dir;
use const DIRECTORY_SEPARATOR;

class FileSystem extends SymfonyFilesystem
interface FileSystem extends SymfonyFileSystem
{
/**
* Returns whether a path is relative.
Expand All @@ -69,20 +58,11 @@ class FileSystem extends SymfonyFilesystem
* @return bool returns true if the path is relative or empty, false if
* it is absolute
*/
public function isRelativePath(string $path): bool
{
return !$this->isAbsolutePath($path);
}
public function isRelativePath(string $path): bool;

public function escapePath(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
public function escapePath(string $path): string;

public function dumpFile(string $filename, $content = ''): void
{
parent::dumpFile($filename, $content);
}
public function dumpFile(string $filename, $content = ''): void;

/**
* Gets the contents of a file.
Expand All @@ -93,26 +73,7 @@ public function dumpFile(string $filename, $content = ''): void
*
* @return string File contents
*/
public function getFileContents(string $file): string
{
Assert::file($file);
Assert::readable($file);

if (false === ($contents = @file_get_contents($file))) {
throw new IOException(
sprintf(
'Failed to read file "%s": %s.',
$file,
error_get_last()['message'],
),
0,
null,
$file,
);
}

return $contents;
}
public function getFileContents(string $file): string;

/**
* Creates a temporary directory.
Expand All @@ -122,51 +83,12 @@ public function getFileContents(string $file): string
*
* @return string the path to the created directory
*/
public function makeTmpDir(string $namespace, string $className): string
{
$shortClass = false !== ($pos = mb_strrpos($className, '\\'))
? mb_substr($className, $pos + 1)
: $className;

$basePath = $this->getNamespacedTmpDir($namespace).'/'.$shortClass;

$result = false;
$attempts = 0;

do {
$tmpDir = $this->escapePath($basePath.random_int(10000, 99999));

if ($this->exists($tmpDir)) {
++$attempts;

continue;
}

try {
$this->mkdir($tmpDir, 0o777);

$result = true;
} catch (IOException) {
++$attempts;
}
} while (false === $result && $attempts <= 10);

return $tmpDir;
}
public function makeTmpDir(string $namespace, string $className): string;

/**
* Gets a namespaced temporary directory.
*
* @param string $namespace the directory path in the system's temporary directory
*/
public function getNamespacedTmpDir(string $namespace): string
{
// Usage of realpath() is important if the temporary directory is a
// symlink to another directory (e.g. /var => /private/var on some Macs)
// We want to know the real path to avoid comparison failures with
// code that uses real paths only
$systemTempDir = str_replace('\\', '/', realpath(sys_get_temp_dir()));

return $systemTempDir.'/'.$namespace;
}
public function getNamespacedTmpDir(string $namespace): string;
}
141 changes: 141 additions & 0 deletions src/NativeFileSystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/*
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2022, Théo FIDRY <theo.fidry@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Fidry\FileSystem;

use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem as NativeSymfonyFilesystem;
use Webmozart\Assert\Assert;
use function error_get_last;
use function file_get_contents;
use function random_int;
use function realpath;
use function sprintf;
use function str_replace;
use function sys_get_temp_dir;
use const DIRECTORY_SEPARATOR;

class NativeFileSystem extends NativeSymfonyFilesystem implements FileSystem
{
public function isRelativePath(string $path): bool
{
return !$this->isAbsolutePath($path);
}

public function escapePath(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}

public function dumpFile(string $filename, $content = ''): void
{
parent::dumpFile($filename, $content);
}

public function getFileContents(string $file): string
{
Assert::file($file);
Assert::readable($file);

if (false === ($contents = @file_get_contents($file))) {
throw new IOException(
sprintf(
'Failed to read file "%s": %s.',
$file,
error_get_last()['message'],
),
0,
null,
$file,
);
}

return $contents;
}

public function makeTmpDir(string $namespace, string $className): string
{
$shortClass = false !== ($pos = mb_strrpos($className, '\\'))
? mb_substr($className, $pos + 1)
: $className;

$basePath = $this->getNamespacedTmpDir($namespace).'/'.$shortClass;

$result = false;
$attempts = 0;

do {
$tmpDir = $this->escapePath($basePath.random_int(10000, 99999));

if ($this->exists($tmpDir)) {
++$attempts;

continue;
}

try {
$this->mkdir($tmpDir, 0o777);

$result = true;
} catch (IOException) {
++$attempts;
}
} while (false === $result && $attempts <= 10);

return $tmpDir;
}

public function getNamespacedTmpDir(string $namespace): string
{
// Usage of realpath() is important if the temporary directory is a
// symlink to another directory (e.g. /var => /private/var on some Macs)
// We want to know the real path to avoid comparison failures with
// code that uses real paths only
$systemTempDir = str_replace('\\', '/', realpath(sys_get_temp_dir()));

return $systemTempDir.'/'.$namespace;
}
}
Loading