comparison core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
2 2
3 namespace Drupal\system\Plugin\ImageToolkit; 3 namespace Drupal\system\Plugin\ImageToolkit;
4 4
5 use Drupal\Component\Utility\Color; 5 use Drupal\Component\Utility\Color;
6 use Drupal\Core\Config\ConfigFactoryInterface; 6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\File\Exception\FileException;
8 use Drupal\Core\File\FileSystemInterface;
7 use Drupal\Core\Form\FormStateInterface; 9 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\ImageToolkit\ImageToolkitBase; 10 use Drupal\Core\ImageToolkit\ImageToolkitBase;
9 use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface; 11 use Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface;
10 use Drupal\Core\StreamWrapper\StreamWrapperInterface; 12 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
11 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; 13 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
55 * The StreamWrapper manager. 57 * The StreamWrapper manager.
56 * 58 *
57 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface 59 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
58 */ 60 */
59 protected $streamWrapperManager; 61 protected $streamWrapperManager;
62
63 /**
64 * The file system.
65 *
66 * @var \Drupal\Core\File\FileSystemInterface
67 */
68 protected $fileSystem;
60 69
61 /** 70 /**
62 * Constructs a GDToolkit object. 71 * Constructs a GDToolkit object.
63 * 72 *
64 * @param array $configuration 73 * @param array $configuration
73 * A logger instance. 82 * A logger instance.
74 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory 83 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
75 * The config factory. 84 * The config factory.
76 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager 85 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
77 * The StreamWrapper manager. 86 * The StreamWrapper manager.
78 */ 87 * @param \Drupal\Core\File\FileSystemInterface $file_system
79 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, StreamWrapperManagerInterface $stream_wrapper_manager) { 88 * The file system.
89 */
90 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitOperationManagerInterface $operation_manager, LoggerInterface $logger, ConfigFactoryInterface $config_factory, StreamWrapperManagerInterface $stream_wrapper_manager, FileSystemInterface $file_system = NULL) {
80 parent::__construct($configuration, $plugin_id, $plugin_definition, $operation_manager, $logger, $config_factory); 91 parent::__construct($configuration, $plugin_id, $plugin_definition, $operation_manager, $logger, $config_factory);
81 $this->streamWrapperManager = $stream_wrapper_manager; 92 $this->streamWrapperManager = $stream_wrapper_manager;
93 if (!$file_system) {
94 @trigger_error('The file_system service must be passed to GDToolkit::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED);
95 $file_system = \Drupal::service('file_system');
96 }
97 $this->fileSystem = $file_system;
82 } 98 }
83 99
84 /** 100 /**
85 * Destructs a GDToolkit object. 101 * Destructs a GDToolkit object.
86 * 102 *
101 $plugin_id, 117 $plugin_id,
102 $plugin_definition, 118 $plugin_definition,
103 $container->get('image.toolkit.operation.manager'), 119 $container->get('image.toolkit.operation.manager'),
104 $container->get('logger.channel.image'), 120 $container->get('logger.channel.image'),
105 $container->get('config.factory'), 121 $container->get('config.factory'),
106 $container->get('stream_wrapper_manager') 122 $container->get('stream_wrapper_manager'),
123 $container->get('file_system')
107 ); 124 );
108 } 125 }
109 126
110 /** 127 /**
111 * Sets the GD image resource. 128 * Sets the GD image resource.
218 if ($scheme && file_stream_wrapper_valid_scheme($scheme)) { 235 if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
219 // If destination is not local, save image to temporary local file. 236 // If destination is not local, save image to temporary local file.
220 $local_wrappers = $this->streamWrapperManager->getWrappers(StreamWrapperInterface::LOCAL); 237 $local_wrappers = $this->streamWrapperManager->getWrappers(StreamWrapperInterface::LOCAL);
221 if (!isset($local_wrappers[$scheme])) { 238 if (!isset($local_wrappers[$scheme])) {
222 $permanent_destination = $destination; 239 $permanent_destination = $destination;
223 $destination = drupal_tempnam('temporary://', 'gd_'); 240 $destination = $this->fileSystem->tempnam('temporary://', 'gd_');
224 } 241 }
225 // Convert stream wrapper URI to normal path. 242 // Convert stream wrapper URI to normal path.
226 $destination = \Drupal::service('file_system')->realpath($destination); 243 $destination = $this->fileSystem->realpath($destination);
227 } 244 }
228 245
229 $function = 'image' . image_type_to_extension($this->getType(), FALSE); 246 $function = 'image' . image_type_to_extension($this->getType(), FALSE);
230 if (!function_exists($function)) { 247 if (!function_exists($function)) {
231 return FALSE; 248 return FALSE;
241 } 258 }
242 $success = $function($this->getResource(), $destination); 259 $success = $function($this->getResource(), $destination);
243 } 260 }
244 // Move temporary local file to remote destination. 261 // Move temporary local file to remote destination.
245 if (isset($permanent_destination) && $success) { 262 if (isset($permanent_destination) && $success) {
246 return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE); 263 try {
264 $this->fileSystem->move($destination, $permanent_destination, FileSystemInterface::EXISTS_REPLACE);
265 return TRUE;
266 }
267 catch (FileException $e) {
268 return FALSE;
269 }
247 } 270 }
248 return $success; 271 return $success;
249 } 272 }
250 273
251 /** 274 /**