Commit fc301d85 authored by larowlan's avatar larowlan

Issue #2908176 by andypost, claudiu.cristea, vacho, YurkinPark, naveenvalecha,...

Issue #2908176 by andypost, claudiu.cristea, vacho, YurkinPark, naveenvalecha, alexpott, fenstrat, Berdir, Mile23: Deprecate archiver procedural functions
parent 87d4369e
......@@ -1245,37 +1245,43 @@ function drupal_check_incompatibility($v, $current_version) {
/**
* Returns a string of supported archive extensions.
*
* @return
* @return string
* A space-separated string of extensions suitable for use by the file
* validation system.
*
* @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use
* \Drupal\Core\Archiver\ArchiverManager::getExtensions() instead.
*
* @see https://www.drupal.org/node/2999951
*/
function archiver_get_extensions() {
$valid_extensions = [];
foreach (\Drupal::service('plugin.manager.archiver')->getDefinitions() as $archive) {
foreach ($archive['extensions'] as $extension) {
foreach (explode('.', $extension) as $part) {
if (!in_array($part, $valid_extensions)) {
$valid_extensions[] = $part;
}
}
}
}
return implode(' ', $valid_extensions);
@trigger_error('archiver_get_extensions() is deprecated in Drupal 8.8.0 and will be removed in Drupal 9.0.0. Use \Drupal\Core\Archiver\ArchiverManager::getExtensions() instead. See https://www.drupal.org/node/2999951', E_USER_DEPRECATED);
return \Drupal::service('plugin.manager.archiver')->getExtensions();
}
/**
* Creates the appropriate archiver for the specified file.
*
* @param $file
* @param string $file
* The full path of the archive file. Note that stream wrapper paths are
* supported, but not remote ones.
*
* @return
* A newly created instance of the archiver class appropriate
* for the specified file, already bound to that file.
* If no appropriate archiver class was found, will return FALSE.
* @return \Drupal\Core\Archiver\ArchiverInterface|false
* A newly created instance of the archiver class appropriate for the
* specified file, already bound to that file. If no appropriate archiver
* class was found, will return FALSE.
*
* @throws \Exception
* If a remote stream wrapper path was passed.
*
* @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Instead,
* get plugin.manager.archiver service from container and call getInstance()
* method on it. For example $archiver->getInstance(['filepath' => $file]);
*
* @see https://www.drupal.org/node/2999951
*/
function archiver_get_archiver($file) {
@trigger_error('archiver_get_archiver() is deprecated in Drupal 8.8.0 and will be removed in Drupal 9.0.x. Instead, get plugin.manager.archiver service from container and call getInstance() method on it. For example $archiver->getInstance(["filepath" => $file]); See https://www.drupal.org/node/2999951', E_USER_DEPRECATED);
// Archivers can only work on local paths
$filepath = \Drupal::service('file_system')->realpath($file);
if (!is_file($filepath)) {
......
......@@ -61,4 +61,25 @@ public function getInstance(array $options) {
}
}
/**
* Returns a string of supported archive extensions.
*
* @return string
* A space-separated string of extensions suitable for use by the file
* validation system.
*/
public function getExtensions() {
$valid_extensions = [];
foreach ($this->getDefinitions() as $archive) {
foreach ($archive['extensions'] as $extension) {
foreach (explode('.', $extension) as $part) {
if (!in_array($part, $valid_extensions)) {
$valid_extensions[] = $part;
}
}
}
}
return implode(' ', $valid_extensions);
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\update\Form;
use Drupal\Core\Archiver\ArchiverManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\FileTransfer\Local;
use Drupal\Core\Form\FormBase;
......@@ -38,6 +39,13 @@ class UpdateManagerInstall extends FormBase {
*/
protected $sitePath;
/**
* The archiver plugin manager service.
*
* @var \Drupal\Core\Archiver\ArchiverManager
*/
protected $archiverManager;
/**
* Constructs a new UpdateManagerInstall.
*
......@@ -47,11 +55,14 @@ class UpdateManagerInstall extends FormBase {
* The module handler.
* @param string $site_path
* The site path.
* @param \Drupal\Core\Archiver\ArchiverManager $archiver_manager
* The archiver plugin manager service.
*/
public function __construct($root, ModuleHandlerInterface $module_handler, $site_path) {
public function __construct($root, ModuleHandlerInterface $module_handler, $site_path, ArchiverManager $archiver_manager) {
$this->root = $root;
$this->moduleHandler = $module_handler;
$this->sitePath = $site_path;
$this->archiverManager = $archiver_manager;
}
/**
......@@ -68,7 +79,8 @@ public static function create(ContainerInterface $container) {
return new static(
$container->get('update.root'),
$container->get('module_handler'),
$container->get('site.path')
$container->get('site.path'),
$container->get('plugin.manager.archiver')
);
}
......@@ -87,7 +99,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
':module_url' => 'https://www.drupal.org/project/modules',
':theme_url' => 'https://www.drupal.org/project/themes',
':drupal_org_url' => 'https://www.drupal.org',
'%extensions' => archiver_get_extensions(),
'%extensions' => $this->archiverManager->getExtensions(),
]),
'#suffix' => '</p>',
];
......@@ -144,7 +156,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
elseif (!empty($all_files['project_upload'])) {
$validators = ['file_validate_extensions' => [archiver_get_extensions()]];
$validators = ['file_validate_extensions' => [$this->archiverManager->getExtensions()]];
if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FILE_EXISTS_REPLACE))) {
// Failed to upload the file. file_save_upload() calls
// \Drupal\Core\Messenger\MessengerInterface::addError() on failure.
......
......@@ -49,7 +49,8 @@ public function testUploadModule() {
];
// This also checks that the correct archive extensions are allowed.
$this->drupalPostForm('admin/modules/install', $edit, t('Install'));
$this->assertText(t('Only files with the following extensions are allowed: @archive_extensions.', ['@archive_extensions' => archiver_get_extensions()]), 'Only valid archives can be uploaded.');
$extensions = \Drupal::service('plugin.manager.archiver')->getExtensions();
$this->assertSession()->pageTextContains(t('Only files with the following extensions are allowed: @archive_extensions.', ['@archive_extensions' => $extensions]));
$this->assertUrl('admin/modules/install');
// Check to ensure an existing module can't be reinstalled. Also checks that
......
......@@ -144,13 +144,16 @@ function _update_manager_check_backends(&$form, $operation) {
* @param string $directory
* The directory you wish to extract the archive into.
*
* @return Archiver
* @return \Drupal\Core\Archiver\ArchiverInterface
* The Archiver object used to extract the archive.
*
* @throws Exception
*/
function update_manager_archive_extract($file, $directory) {
$archiver = archiver_get_archiver($file);
/** @var \Drupal\Core\Archiver\ArchiverInterface $archiver */
$archiver = \Drupal::service('plugin.manager.archiver')->getInstance([
'filepath' => $file,
]);
if (!$archiver) {
throw new Exception(t('Cannot extract %file, not a valid archive.', ['%file' => $file]));
}
......
......@@ -30,4 +30,24 @@ public function testDrupalSetTimeLimit() {
drupal_set_time_limit(1000);
}
/**
* @expectedDeprecation archiver_get_extensions() is deprecated in Drupal 8.8.0 and will be removed in Drupal 9.0.0. Use \Drupal\Core\Archiver\ArchiverManager::getExtensions() instead. See https://www.drupal.org/node/2999951
*/
public function testArchiverGetExtensions() {
$expected = \Drupal::service('plugin.manager.archiver')->getExtensions();
$this->assertEquals($expected, archiver_get_extensions());
}
/**
* @expectedDeprecation archiver_get_archiver() is deprecated in Drupal 8.8.0 and will be removed in Drupal 9.0.x. Instead, get plugin.manager.archiver service from container and call getInstance() method on it. For example $archiver->getInstance(["filepath" => $file]); See https://www.drupal.org/node/2999951
*/
public function testArchiverGetArchiver() {
$file = $this->root . '/core/modules/update/tests/aaa_update_test.tar.gz';
$expected = \Drupal::service('plugin.manager.archiver')->getInstance([
'filepath' => $file,
]);
$actual = archiver_get_archiver($file);
$this->assertEquals($expected, $actual);
}
}
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