Issue #2794249 by alexpott: Move file_directory_os_temp() to a class in Drupal\Component\FileSystem

8.3.x
Nathaniel Catchpole 2016-09-14 14:03:49 +01:00
parent 19387f80d5
commit b39b29ad14
3 changed files with 54 additions and 28 deletions

View File

@ -5,6 +5,7 @@
* API for handling file uploads and server file management.
*/
use Drupal\Component\FileSystem\FileSystem as ComponentFileSystem;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\PhpStorage\FileStorage;
@ -1189,7 +1190,7 @@ function file_directory_temp() {
if (empty($temporary_directory)) {
// Needs set up.
$config = \Drupal::configFactory()->getEditable('system.file');
$temporary_directory = file_directory_os_temp();
$temporary_directory = ComponentFileSystem::getOsTemporaryDirectory();
if (empty($temporary_directory)) {
// If no directory has been found default to 'files/tmp'.
@ -1214,34 +1215,12 @@ function file_directory_temp() {
*
* @return mixed
* A string containing the path to the temporary directory.
*
* @deprecated in Drupal 8.3.x-dev, will be removed before Drupal 9.0.0.
* Use \Drupal\Component\FileSystem\FileSystem::getOsTemporaryDirectory().
*/
function file_directory_os_temp() {
$directories = array();
// Has PHP been set with an upload_tmp_dir?
if (ini_get('upload_tmp_dir')) {
$directories[] = ini_get('upload_tmp_dir');
}
// Operating system specific dirs.
if (substr(PHP_OS, 0, 3) == 'WIN') {
$directories[] = 'c:\\windows\\temp';
$directories[] = 'c:\\winnt\\temp';
}
else {
$directories[] = '/tmp';
}
// PHP may be able to find an alternative tmp directory.
$directories[] = sys_get_temp_dir();
foreach ($directories as $directory) {
if (is_dir($directory) && is_writable($directory)) {
// Both sys_get_temp_dir() and ini_get('upload_tmp_dir') can return paths
// with a trailing directory separator.
return rtrim($directory, DIRECTORY_SEPARATOR);
}
}
return FALSE;
return ComponentFileSystem::getOsTemporaryDirectory();
}
/**

View File

@ -0,0 +1,46 @@
<?php
namespace Drupal\Component\FileSystem;
/**
* Provides file system functions.
*/
class FileSystem {
/**
* Discovers a writable system-appropriate temporary directory.
*
* @return string|false
* A string containing the path to the temporary directory, or FALSE if no
* suitable temporary directory can be found.
*/
public static function getOsTemporaryDirectory() {
$directories = array();
// Has PHP been set with an upload_tmp_dir?
if (ini_get('upload_tmp_dir')) {
$directories[] = ini_get('upload_tmp_dir');
}
// Operating system specific dirs.
if (substr(PHP_OS, 0, 3) == 'WIN') {
$directories[] = 'c:\\windows\\temp';
$directories[] = 'c:\\winnt\\temp';
}
else {
$directories[] = '/tmp';
}
// PHP may be able to find an alternative tmp directory.
$directories[] = sys_get_temp_dir();
foreach ($directories as $directory) {
if (is_dir($directory) && is_writable($directory)) {
// Both sys_get_temp_dir() and ini_get('upload_tmp_dir') can return paths
// with a trailing directory separator.
return rtrim($directory, DIRECTORY_SEPARATOR);
}
}
return FALSE;
}
}

View File

@ -7,6 +7,7 @@
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Environment;
use Drupal\Component\FileSystem\FileSystem;
use Drupal\Component\Utility\OpCodeCache;
use Drupal\Core\Path\AliasStorage;
use Drupal\Core\Url;
@ -541,7 +542,7 @@ function system_requirements($phase) {
else {
// If the temporary directory is not overridden use an appropriate
// temporary path for the system.
$directories[] = file_directory_os_temp();
$directories[] = FileSystem::getOsTemporaryDirectory();
}
}