Fixes #15335: Added FileHelper::unlink() that works well under all OSes

This commit is contained in:
Alexander Makarov
2017-12-11 22:29:31 +03:00
committed by GitHub
parent adc441de83
commit efac23dde7
4 changed files with 38 additions and 31 deletions

View File

@ -250,13 +250,13 @@ class DevController extends Controller
{
if (is_link($link = "$dir/vendor/yiisoft/yii2")) {
$this->stdout("Removing symlink $link.\n");
$this->unlink($link);
FileHelper::unlink($link);
}
$extensions = $this->findDirs("$dir/vendor/yiisoft");
foreach ($extensions as $ext) {
if (is_link($link = "$dir/vendor/yiisoft/yii2-$ext")) {
$this->stdout("Removing symlink $link.\n");
$this->unlink($link);
FileHelper::unlink($link);
}
}
}
@ -293,20 +293,6 @@ class DevController extends Controller
}
}
/**
* Properly removes symlinked directory under Windows, MacOS and Linux.
*
* @param string $file path to symlink
*/
protected function unlink($file)
{
if (is_dir($file) && DIRECTORY_SEPARATOR === '\\') {
rmdir($file);
} else {
unlink($file);
}
}
/**
* Get a list of subdirectories for directory specified.
* @param string $dir directory to read

View File

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.14 under development
------------------------
- Enh #15335: Added `FileHelper::unlink()` that works well under all OSes (samdark)
- Bug #15142: Fixed array params replacing in `yii\helpers\BaseUrl::current()` (IceJOKER)
- Bug #15249: Controllers in subdirectories were not visible in commands list (IceJOKER)
- Enh #5515: Added default value for `yii\behaviors\BlameableBehavior` for cases when the user is guest (dmirogin)

View File

@ -376,28 +376,48 @@ class BaseFileHelper
if (is_dir($path)) {
static::removeDirectory($path, $options);
} else {
try {
unlink($path);
} catch (ErrorException $e) {
if (DIRECTORY_SEPARATOR === '\\') {
// last resort measure for Windows
$lines = [];
exec("DEL /F/Q \"$path\"", $lines, $deleteError);
} else {
throw $e;
}
}
static::unlink($path);
}
}
closedir($handle);
}
if (is_link($dir)) {
unlink($dir);
static::unlink($dir);
} else {
rmdir($dir);
}
}
/**
* Removes a file or symlink in a cross-platform way
*
* @param string $path
* @return bool
*
* @since 2.0.14
*/
public static function unlink($path)
{
$isWindows = DIRECTORY_SEPARATOR === '\\';
if (!$isWindows) {
return unlink($path);
}
if (is_link($path) && is_dir($path)) {
return rmdir($path);
}
try {
return unlink($path);
} catch (ErrorException $e) {
// last resort measure for Windows
$lines = [];
exec("DEL /F/Q \"$path\"", $lines, $deleteError);
return $deleteError !== 0;
}
}
/**
* Returns the files found under the specified directory and subdirectories.
* @param string $dir the directory under which the files will be looked for.

View File

@ -42,7 +42,7 @@ class AssetBundleTest extends \yiiunit\TestCase
if (is_dir($path)) {
FileHelper::removeDirectory($path);
} else {
unlink($path);
FileHelper::unlink($path);
}
}
closedir($handle);
@ -194,7 +194,7 @@ class AssetBundleTest extends \yiiunit\TestCase
$this->assertFileEquals($publishedFile, $sourceFile);
}
$this->assertTrue(unlink($bundle->basePath));
$this->assertTrue(FileHelper::unlink($bundle->basePath));
return $bundle;
}
@ -383,7 +383,7 @@ EOF;
<link href="/screen_and_print.css" rel="stylesheet" media="screen, print" hreflang="en">23<script src="/normal.js" charset="utf-8"></script>
<script src="/defered.js" charset="utf-8" defer></script>4
EOF;
$this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
$this->assertEqualsWithoutLE($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
}
public function registerFileDataProvider()