FontawesomeIconPickerCommands.php in Font Awesome Icons 8.2
File
modules/fontawesome_iconpicker_widget/src/Commands/FontawesomeIconPickerCommands.php
View source
<?php
namespace Drupal\fontawesome_iconpicker_widget\Commands;
use Drupal\Core\File\FileSystemInterface;
use Drush\Commands\DrushCommands;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Archiver\ArchiverManager;
class FontawesomeIconPickerCommands extends DrushCommands {
protected $libraryDiscovery;
protected $fileSystem;
protected $archiverManager;
public function __construct(LibraryDiscoveryInterface $library_discovery, FileSystemInterface $file_system, ArchiverManager $archiver_manager) {
parent::__construct();
$this->libraryDiscovery = $library_discovery;
$this->fileSystem = $file_system;
$this->archiverManager = $archiver_manager;
}
public function download($path = '') {
if (empty($path)) {
$path = DRUPAL_ROOT . '/libraries/fonticonpicker--fonticonpicker';
}
if (substr($path, -30) == 'fonticonpicker--fonticonpicker' && !is_dir($path)) {
$this->fileSystem
->mkdir($path);
}
if (is_dir($path . '/dist')) {
$this
->logger()
->notice(dt('IconPicker already present at @path. No download required.', [
'@path' => $path,
]));
return;
}
if ($iconpicker_library = $this->libraryDiscovery
->getLibraryByName('fontawesome_iconpicker_widget', 'fonticonpicker')) {
$destination = tempnam(sys_get_temp_dir(), 'file.') . "tar.gz";
system_retrieve_file($iconpicker_library['remote'], $destination);
if (!file_exists($destination)) {
$this->fileSystem
->rmdir($path);
$this
->logger()
->error(dt('Drush was unable to download the fontIconPicker library from @remote.', [
'@remote' => $iconpicker_library['remote'],
]));
return;
}
$this->fileSystem
->move($destination, $path . '/fontIconPicker.zip');
if (!file_exists($path . '/fontIconPicker.zip')) {
$this->fileSystem
->rmdir($path);
$this
->logger()
->error(dt('Error: unable to download fontIconPicker library from @remote', [
'@remote' => $iconpicker_library['remote'],
]));
return;
}
$zipFile = $this->archiverManager
->getInstance([
'filepath' => $path . '/fontIconPicker.zip',
]);
$zipFile
->extract($path);
$this->fileSystem
->unlink($path . '/fontIconPicker.zip');
$this
->logger()
->notice(dt('fontIconPicker library has been successfully downloaded to @path.', [
'@path' => $path,
]));
}
else {
$this
->logger()
->error(dt('Drush was unable to load the fontIconPicker) library'));
}
}
}