Commit b39b29ad authored by catch's avatar catch
Browse files

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

parent 19387f80
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
* API for handling file uploads and server file management. * 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\Unicode;
use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\PhpStorage\FileStorage; use Drupal\Component\PhpStorage\FileStorage;
...@@ -1189,7 +1190,7 @@ function file_directory_temp() { ...@@ -1189,7 +1190,7 @@ function file_directory_temp() {
if (empty($temporary_directory)) { if (empty($temporary_directory)) {
// Needs set up. // Needs set up.
$config = \Drupal::configFactory()->getEditable('system.file'); $config = \Drupal::configFactory()->getEditable('system.file');
$temporary_directory = file_directory_os_temp(); $temporary_directory = ComponentFileSystem::getOsTemporaryDirectory();
if (empty($temporary_directory)) { if (empty($temporary_directory)) {
// If no directory has been found default to 'files/tmp'. // If no directory has been found default to 'files/tmp'.
...@@ -1214,34 +1215,12 @@ function file_directory_temp() { ...@@ -1214,34 +1215,12 @@ function file_directory_temp() {
* *
* @return mixed * @return mixed
* A string containing the path to the temporary directory. * 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() { function file_directory_os_temp() {
$directories = array(); return ComponentFileSystem::getOsTemporaryDirectory();
// 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;
} }
/** /**
......
<?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;
}
}
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\Environment;
use Drupal\Component\FileSystem\FileSystem;
use Drupal\Component\Utility\OpCodeCache; use Drupal\Component\Utility\OpCodeCache;
use Drupal\Core\Path\AliasStorage; use Drupal\Core\Path\AliasStorage;
use Drupal\Core\Url; use Drupal\Core\Url;
...@@ -541,7 +542,7 @@ function system_requirements($phase) { ...@@ -541,7 +542,7 @@ function system_requirements($phase) {
else { else {
// If the temporary directory is not overridden use an appropriate // If the temporary directory is not overridden use an appropriate
// temporary path for the system. // temporary path for the system.
$directories[] = file_directory_os_temp(); $directories[] = FileSystem::getOsTemporaryDirectory();
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment