Unverified Commit 56be1a03 authored by alexpott's avatar alexpott

Issue #2244513 by kim.pepper, phenaproxima, 20th, andrei.dincu, beejeebus,...

Issue #2244513 by kim.pepper, phenaproxima, 20th, andrei.dincu, beejeebus, Berdir, jibran, alexpott, andypost, larowlan, Chadwick Wood, Wim Leers, acbramley, YesCT, sun, tim.plunkett, chx: Move the unmanaged file APIs to the file_system service (file.inc)
parent d4fa72e7
......@@ -1565,25 +1565,27 @@ services:
arguments: [ '@state' ]
asset.css.collection_optimizer:
class: Drupal\Core\Asset\CssCollectionOptimizer
arguments: [ '@asset.css.collection_grouper', '@asset.css.optimizer', '@asset.css.dumper', '@state' ]
arguments: [ '@asset.css.collection_grouper', '@asset.css.optimizer', '@asset.css.dumper', '@state', '@file_system']
asset.css.optimizer:
class: Drupal\Core\Asset\CssOptimizer
asset.css.collection_grouper:
class: Drupal\Core\Asset\CssCollectionGrouper
asset.css.dumper:
class: Drupal\Core\Asset\AssetDumper
arguments: ['@file_system']
asset.js.collection_renderer:
class: Drupal\Core\Asset\JsCollectionRenderer
arguments: [ '@state' ]
asset.js.collection_optimizer:
class: Drupal\Core\Asset\JsCollectionOptimizer
arguments: [ '@asset.js.collection_grouper', '@asset.js.optimizer', '@asset.js.dumper', '@state' ]
arguments: [ '@asset.js.collection_grouper', '@asset.js.optimizer', '@asset.js.dumper', '@state', '@file_system']
asset.js.optimizer:
class: Drupal\Core\Asset\JsOptimizer
asset.js.collection_grouper:
class: Drupal\Core\Asset\JsCollectionGrouper
asset.js.dumper:
class: Drupal\Core\Asset\AssetDumper
arguments: ['@file_system']
library.discovery:
class: Drupal\Core\Asset\LibraryDiscovery
arguments: ['@library.discovery.collector']
......
......@@ -6,13 +6,16 @@
*/
use Drupal\Component\FileSystem\FileSystem as ComponentFileSystem;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\PhpStorage\FileStorage;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\Exception\FileWriteException;
use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\StreamWrapper\PublicStream;
/**
* Default mode for new directories. See drupal_chmod().
......@@ -41,29 +44,44 @@
*/
/**
* Flag used by file_prepare_directory() -- create directory if not present.
* Flag used to create a directory if not present.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::CREATE_DIRECTORY.
*/
const FILE_CREATE_DIRECTORY = 1;
const FILE_CREATE_DIRECTORY = FileSystemInterface::CREATE_DIRECTORY;
/**
* Flag used by file_prepare_directory() -- file permissions may be changed.
* Flag used to indicate file permissions may be changed.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::MODIFY_PERMISSIONS.
*/
const FILE_MODIFY_PERMISSIONS = 2;
const FILE_MODIFY_PERMISSIONS = FileSystemInterface::MODIFY_PERMISSIONS;
/**
* Flag for dealing with existing files: Appends number until name is unique.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::EXISTS_RENAME.
*/
const FILE_EXISTS_RENAME = 0;
const FILE_EXISTS_RENAME = FileSystemInterface::EXISTS_RENAME;
/**
* Flag for dealing with existing files: Replace the existing file.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::EXISTS_REPLACE.
*/
const FILE_EXISTS_REPLACE = 1;
const FILE_EXISTS_REPLACE = FileSystemInterface::EXISTS_REPLACE;
/**
* Flag for dealing with existing files: Do nothing and return FALSE.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::EXISTS_ERROR.
*/
const FILE_EXISTS_ERROR = 2;
const FILE_EXISTS_ERROR = FileSystemInterface::EXISTS_ERROR;
/**
* Indicates that the file is permanent and should not be deleted.
......@@ -288,29 +306,13 @@ function file_url_transform_relative($file_url) {
* @return
* TRUE if the directory exists (or was created) and is writable. FALSE
* otherwise.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::prepareDirectory().
*/
function file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS) {
if (!file_stream_wrapper_valid_scheme(\Drupal::service('file_system')->uriScheme($directory))) {
// Only trim if we're not dealing with a stream.
$directory = rtrim($directory, '/\\');
}
// Check if directory exists.
if (!is_dir($directory)) {
// Let mkdir() recursively create directories and use the default directory
// permissions.
if ($options & FILE_CREATE_DIRECTORY) {
return @drupal_mkdir($directory, NULL, TRUE);
}
return FALSE;
}
// The directory exists, so check to see if it is writable.
$writable = is_writable($directory);
if (!$writable && ($options & FILE_MODIFY_PERMISSIONS)) {
return drupal_chmod($directory);
}
return $writable;
function file_prepare_directory(&$directory, $options = FileSystemInterface::MODIFY_PERMISSIONS) {
@trigger_error('file_prepare_directory() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::prepareDirectory(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
return \Drupal::service('file_system')->prepareDirectory($directory, $options);
}
/**
......@@ -452,25 +454,20 @@ function file_valid_uri($uri) {
* @return
* The path to the new file, or FALSE in the event of an error.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::copy().
*
* @see file_copy()
* @see https://www.drupal.org/node/3006851
*/
function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
if (!file_unmanaged_prepare($source, $destination, $replace)) {
return FALSE;
@trigger_error('file_unmanaged_copy() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::copy(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
try {
return \Drupal::service('file_system')->copy($source, $destination, $replace);
}
// Attempt to resolve the URIs. This is necessary in certain configurations
// (see above).
$file_system = \Drupal::service('file_system');
$real_source = $file_system->realpath($source) ?: $source;
$real_destination = $file_system->realpath($destination) ?: $destination;
// Perform the copy operation.
if (!@copy($real_source, $real_destination)) {
\Drupal::logger('file')->error('The specified file %file could not be copied to %destination.', ['%file' => $source, '%destination' => $destination]);
catch (FileException $e) {
return FALSE;
}
// Set the permissions on the new file.
drupal_chmod($destination);
return $destination;
}
/**
......@@ -500,12 +497,18 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
* @return
* TRUE, or FALSE in the event of an error.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::getDestinationFilename() instead.
*
* @see file_unmanaged_copy()
* @see file_unmanaged_move()
* @see https://www.drupal.org/node/3006851
*/
function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_EXISTS_RENAME) {
@trigger_error('file_unmanaged_prepare() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::getDestinationFilename() instead. See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
$original_source = $source;
$logger = \Drupal::logger('file');
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
// Assert that the source file actually exists.
......@@ -587,26 +590,15 @@ function file_build_uri($path) {
* @return
* The destination filepath, or FALSE if the file already exists
* and FILE_EXISTS_ERROR is specified.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::getDestinationFilename().
*
* @see https://www.drupal.org/node/3006851
*/
function file_destination($destination, $replace) {
if (file_exists($destination)) {
switch ($replace) {
case FILE_EXISTS_REPLACE:
// Do nothing here, we want to overwrite the existing file.
break;
case FILE_EXISTS_RENAME:
$basename = drupal_basename($destination);
$directory = drupal_dirname($destination);
$destination = file_create_filename($basename, $directory);
break;
case FILE_EXISTS_ERROR:
// Error reporting handled by calling function.
return FALSE;
}
}
return $destination;
@trigger_error('file_destination() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::getDestinationFilename(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
return \Drupal::service('file_system')->getDestinationFilename($destination, $replace);
}
/**
......@@ -640,36 +632,20 @@ function file_destination($destination, $replace) {
* @return
* The path to the new file, or FALSE in the event of an error.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::move().
*
* @see file_move()
* @see https://www.drupal.org/node/3006851
*/
function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
if (!file_unmanaged_prepare($source, $destination, $replace)) {
return FALSE;
}
// Ensure compatibility with Windows.
// @see drupal_unlink()
if ((substr(PHP_OS, 0, 3) == 'WIN') && (!file_stream_wrapper_valid_scheme(file_uri_scheme($source)))) {
chmod($source, 0600);
@trigger_error('file_unmanaged_move() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::move(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
try {
return \Drupal::service('file_system')->move($source, $destination, $replace);
}
// Attempt to resolve the URIs. This is necessary in certain configurations
// (see above) and can also permit fast moves across local schemes.
$file_system = \Drupal::service('file_system');
$real_source = $file_system->realpath($source) ?: $source;
$real_destination = $file_system->realpath($destination) ?: $destination;
// Perform the move operation.
if (!@rename($real_source, $real_destination)) {
// Fall back to slow copy and unlink procedure. This is necessary for
// renames across schemes that are not local, or where rename() has not been
// implemented. It's not necessary to use drupal_unlink() as the Windows
// issue has already been resolved above.
if (!@copy($real_source, $real_destination) || !@unlink($real_source)) {
\Drupal::logger('file')->error('The specified file %file could not be moved to %destination.', ['%file' => $source, '%destination' => $destination]);
return FALSE;
}
catch (FileException $e) {
return FALSE;
}
// Set the permissions on the new file.
drupal_chmod($destination);
return $destination;
}
/**
......@@ -768,45 +744,15 @@ function file_unmunge_filename($filename) {
* @return
* File path consisting of $directory and a unique filename based off
* of $basename.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::createFilename().
*
* @see https://www.drupal.org/node/3006851
*/
function file_create_filename($basename, $directory) {
// Strip control characters (ASCII value < 32). Though these are allowed in
// some filesystems, not many applications handle them well.
$basename = preg_replace('/[\x00-\x1F]/u', '_', $basename);
if (substr(PHP_OS, 0, 3) == 'WIN') {
// These characters are not allowed in Windows filenames
$basename = str_replace([':', '*', '?', '"', '<', '>', '|'], '_', $basename);
}
// A URI or path may already have a trailing slash or look like "public://".
if (substr($directory, -1) == '/') {
$separator = '';
}
else {
$separator = '/';
}
$destination = $directory . $separator . $basename;
if (file_exists($destination)) {
// Destination file already exists, generate an alternative.
$pos = strrpos($basename, '.');
if ($pos !== FALSE) {
$name = substr($basename, 0, $pos);
$ext = substr($basename, $pos);
}
else {
$name = $basename;
$ext = '';
}
$counter = 0;
do {
$destination = $directory . $separator . $name . '_' . $counter++ . $ext;
} while (file_exists($destination));
}
return $destination;
@trigger_error('file_create_filename() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::createFilename(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
return \Drupal::service('file_system')->createFilename($basename, $directory);
}
/**
......@@ -870,28 +816,21 @@ function file_delete_multiple(array $fids) {
* TRUE for success or path does not exist, or FALSE in the event of an
* error.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::delete().
*
* @see file_delete()
* @see file_unmanaged_delete_recursive()
* @see https://www.drupal.org/node/3006851
*/
function file_unmanaged_delete($path) {
if (is_file($path)) {
return drupal_unlink($path);
@trigger_error('file_unmanaged_delete() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::delete(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
try {
return \Drupal::service('file_system')->delete($path);
}
$logger = \Drupal::logger('file');
if (is_dir($path)) {
$logger->error('%path is a directory and cannot be removed using file_unmanaged_delete().', ['%path' => $path]);
catch (FileException $e) {
return FALSE;
}
// Return TRUE for non-existent file, but log that nothing was actually
// deleted, as the current state is the intended result.
if (!file_exists($path)) {
$logger->notice('The file %path was not deleted because it does not exist.', ['%path' => $path]);
return TRUE;
}
// We cannot handle anything other than files and directories. Log an error
// for everything else (sockets, symbolic links, etc).
$logger->error('The file %path is not of a recognized type so it was not deleted.', ['%path' => $path]);
return FALSE;
}
/**
......@@ -917,26 +856,21 @@ function file_unmanaged_delete($path) {
* TRUE for success or if path does not exist, FALSE in the event of an
* error.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::deleteRecursive().
*
* @see file_unmanaged_delete()
* @see https://www.drupal.org/node/3006851
*/
function file_unmanaged_delete_recursive($path, $callback = NULL) {
if (isset($callback)) {
call_user_func($callback, $path);
@trigger_error('file_unmanaged_delete_recursive() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::deleteRecursive(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
$callback = is_callable($callback) ? $callback : NULL;
try {
return \Drupal::service('file_system')->deleteRecursive($path, $callback);
}
if (is_dir($path)) {
$dir = dir($path);
while (($entry = $dir->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry_path = $path . '/' . $entry;
file_unmanaged_delete_recursive($entry_path, $callback);
}
$dir->close();
return drupal_rmdir($path);
catch (FileException $e) {
return FALSE;
}
return file_unmanaged_delete($path);
}
/**
......@@ -976,18 +910,24 @@ function drupal_move_uploaded_file($filename, $uri) {
* @return
* A string with the path of the resulting file, or FALSE on error.
*
* @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystemInterface::saveData().
*
* @see file_save_data()
* @see https://www.drupal.org/node/3006851
*/
function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
// Write the data to a temporary file.
$temp_name = drupal_tempnam('temporary://', 'file');
if (file_put_contents($temp_name, $data) === FALSE) {
@trigger_error('file_unmanaged_save_data() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::saveData(). See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
try {
return \Drupal::service('file_system')->saveData($data, $destination, $replace);
}
catch (FileWriteException $e) {
\Drupal::messenger()->addError(t('The file could not be created.'));
return FALSE;
}
// Move the file to its final destination.
return file_unmanaged_move($temp_name, $destination, $replace);
catch (FileException $e) {
return FALSE;
}
}
/**
......
......@@ -15,6 +15,7 @@
use Drupal\Core\DrupalKernel;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Installer\Exception\AlreadyInstalledException;
use Drupal\Core\Installer\Exception\InstallerException;
......@@ -1106,7 +1107,7 @@ function install_base_system(&$install_state) {
// configurable. The temporary directory needs to match what is set in each
// test types ::prepareEnvironment() step.
$temporary_directory = dirname(PublicStream::basePath()) . '/temp';
file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
\Drupal::service('file_system')->prepareDirectory($temporary_directory, FileSystemInterface::MODIFY_PERMISSIONS | FileSystemInterface::CREATE_DIRECTORY);
\Drupal::configFactory()->getEditable('system.file')
->set('path.temporary', $temporary_directory)
->save();
......@@ -1915,9 +1916,12 @@ function install_check_translations($langcode, $server_pattern) {
$online = FALSE;
// First attempt to create or make writable the files directory.
file_prepare_directory($files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$file_system->prepareDirectory($files_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
// Then, attempt to create or make writable the translations directory.
file_prepare_directory($translations_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$file_system->prepareDirectory($translations_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
// Get values so the requirements errors can be specific.
if (drupal_verify_install_file($translations_directory, FILE_EXIST, 'dir')) {
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Extension\Dependency;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\OpCodeCache;
......@@ -511,7 +512,7 @@ function drupal_install_config_directories() {
// public files directory, which has already been verified to be writable
// itself. But if it somehow fails anyway, the installation cannot proceed.
// Bail out using a similar error message as in system_requirements().
if (!file_prepare_directory($config_directories[CONFIG_SYNC_DIRECTORY], FILE_CREATE_DIRECTORY)
if (!\Drupal::service('file_system')->prepareDirectory($config_directories[CONFIG_SYNC_DIRECTORY], FileSystemInterface::CREATE_DIRECTORY)
&& !file_exists($config_directories[CONFIG_SYNC_DIRECTORY])) {
throw new Exception(t('The directory %directory could not be created. To proceed with the installation, either create the directory or ensure that the installer has the permissions to create it automatically. For more information, see the <a href=":handbook_url">online handbook</a>.', [
'%directory' => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
......@@ -538,12 +539,13 @@ function drupal_install_config_directories() {
* TRUE if the config directory exists and is writable.
*
* @deprecated in Drupal 8.1.x, will be removed before Drupal 9.0.x. Use
* config_get_config_directory() and file_prepare_directory() instead.
* config_get_config_directory() and
* \Drupal\Core\File\FileSystemInterface::prepareDirectory() instead.
*
* @see https://www.drupal.org/node/2501187
*/
function install_ensure_config_directory($type) {
@trigger_error('install_ensure_config_directory() is deprecated in Drupal 8.1.0 and will be removed before Drupal 9.0.0. Use config_get_config_directory() and file_prepare_directory() instead. See https://www.drupal.org/node/2501187.', E_USER_DEPRECATED);
@trigger_error('install_ensure_config_directory() is deprecated in Drupal 8.1.0 and will be removed before Drupal 9.0.0. Use config_get_config_directory() and \Drupal\Core\File\FileSystemInterface::prepareDirectory() instead. See https://www.drupal.org/node/2501187.', E_USER_DEPRECATED);
// The config directory must be defined in settings.php.
global $config_directories;
if (!isset($config_directories[$type])) {
......@@ -553,7 +555,7 @@ function install_ensure_config_directory($type) {
// directories that the installer creates.
else {
$config_directory = config_get_config_directory($type);
return file_prepare_directory($config_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
return \Drupal::service('file_system')->prepareDirectory($config_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
}
}
......
......@@ -3,12 +3,31 @@
namespace Drupal\Core\Asset;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
/**
* Dumps a CSS or JavaScript asset.
*/
class AssetDumper implements AssetDumperInterface {
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* AssetDumper constructor.
*
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file handler.
*/
public function __construct(FileSystemInterface $file_system = NULL) {
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*
......@@ -24,8 +43,13 @@ public function dump($data, $file_extension) {
$path = 'public://' . $file_extension;
$uri = $path . '/' . $filename;
// Create the CSS or JS file.
file_prepare_directory($path, FILE_CREATE_DIRECTORY);
if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
$this->getFileSystem()->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
try {
if (!file_exists($uri) && !$this->getFileSystem()->saveData($data, $uri, FileSystemInterface::EXISTS_REPLACE)) {
return FALSE;
}
}
catch (FileException $e) {
return FALSE;
}
// If CSS/JS gzip compression is enabled and the zlib extension is available
......@@ -37,11 +61,30 @@ public function dump($data, $file_extension) {
// aren't working can set css.gzip to FALSE in order to skip
// generating a file that won't be used.
if (extension_loaded('zlib') && \Drupal::config('system.performance')->get($file_extension . '.gzip')) {
if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
try {
if (!file_exists($uri . '.gz') && !$this->getFileSystem()->saveData(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
return FALSE;
}
}
catch (FileException $e) {
return FALSE;
}
}
return $uri;
}
/**
* Helper method for returning the file system service.
*
* @return \Drupal\Core\File\FileSystemInterface
* The file system service.
*/
private function getFileSystem() {
if (!$this->fileSystem) {
@trigger_error('\Drupal\Core\File\FileSystemInterface is a dependency of this class in Drupal 8.7.0 and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
$this->fileSystem = \Drupal::service('file_system');
}
return $this->fileSystem;
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Core\Asset;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\State\StateInterface;
/**
......@@ -37,6 +38,13 @@ class CssCollectionOptimizer implements AssetCollectionOptimizerInterface {
*/
protected $state;
/**