View source
<?php
namespace Drupal\audiofield\Commands;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\audiofield\AudioFieldPlayerManager;
use Drupal\Core\Archiver\ArchiverManager;
class AudiofieldCommands extends DrushCommands {
protected $playerManager;
protected $fileSystem;
protected $archiverManager;
public function __construct(AudioFieldPlayerManager $player_manager, FileSystemInterface $file_system, ArchiverManager $archiver_manager) {
parent::__construct();
$this->playerManager = $player_manager;
$this->fileSystem = $file_system;
$this->archiverManager = $archiver_manager;
}
public function download($installLibrary = '', $print_messages = TRUE) {
$pluginList = $this->playerManager
->getDefinitions();
if (!empty($installLibrary)) {
if (!isset($pluginList[$installLibrary . '_audio_player'])) {
$this
->logger()
->error(dt('Error: @library is not a valid Audiofield library.', [
'@library' => $installLibrary,
]));
return;
}
$pluginList = [
$installLibrary . '_audio_player' => $pluginList[$installLibrary . '_audio_player'],
];
}
foreach ($pluginList as $pluginName => $plugin) {
$pluginInstance = $this->playerManager
->createInstance($pluginName);
if (!$pluginInstance
->getPluginLibrary()) {
continue;
}
if ($pluginInstance
->checkInstalled()) {
if ($print_messages) {
$this
->logger()
->notice(dt('Audiofield library for @library is already installed at @location', [
'@library' => $pluginInstance
->getPluginTitle(),
'@location' => $pluginInstance
->getPluginLibraryPath(),
]));
}
continue;
}
$path = DRUPAL_ROOT . $pluginInstance
->getPluginLibraryPath();
if (!is_dir($path)) {
$this->fileSystem
->mkdir($path);
}
$destination = $this->fileSystem
->tempnam($this->fileSystem
->getTempDirectory(), 'file.') . "tar.gz";
system_retrieve_file($pluginInstance
->getPluginRemoteSource(), $destination, FALSE);
if (!file_exists($destination)) {
$this->fileSystem
->rmdir($path);
$this
->logger()
->error(dt('Error: unable to download @library.', [
'@library' => $pluginInstance
->getPluginTitle(),
]));
continue;
}
$this->fileSystem
->move($destination, $path . '/audiofield-dl.zip');
if (!file_exists($path . '/audiofield-dl.zip')) {
$this->fileSystem
->rmdir($path);
if ($print_messages) {
$this
->logger()
->error(dt('Error: unable to download Audiofield library @library', [
'@library' => $pluginInstance
->getPluginTitle(),
]));
continue;
}
}
$zipFile = $this->archiverManager
->getInstance([
'filepath' => $path . '/audiofield-dl.zip',
]);
$zipFile
->extract($path);
$this->fileSystem
->unlink($path . '/audiofield-dl.zip');
if (!$pluginInstance
->checkInstalled()) {
$directories = $this->fileSystem
->scanDirectory($path, '/.*?/', [
'recurse' => FALSE,
]);
foreach ($directories as $dirName) {
$this->fileSystem
->move($path . '/' . $dirName->filename, $this->fileSystem
->getTempDirectory() . '/temp_audiofield', FileSystemInterface::EXISTS_REPLACE);
$this->fileSystem
->rmdir($path);
$this->fileSystem
->move($this->fileSystem
->getTempDirectory() . '/temp_audiofield', $path, FileSystemInterface::EXISTS_REPLACE);
}
if ($pluginInstance
->getPluginId() == 'projekktor_audio_player') {
$process = Drush::process('npm install', $path);
$process
->run();
$process = Drush::process('grunt --force', $path);
$process
->run();
}
if ($pluginInstance
->getPluginId() == 'wavesurfer_audio_player') {
$this
->logger()
->notice(dt('Installing @library', [
'@library' => $pluginInstance
->getPluginTitle(),
]));
$process = Drush::process('npm install', $path);
$process
->run();
$this
->logger()
->notice(dt('Building @library', [
'@library' => $pluginInstance
->getPluginTitle(),
]));
$process = Drush::process('npm run build', $path);
$process
->run();
}
}
if ($pluginInstance
->checkInstalled()) {
if ($print_messages) {
$this
->logger()
->notice(dt('Audiofield library for @library has been successfully installed at @location', [
'@library' => $pluginInstance
->getPluginTitle(),
'@location' => $pluginInstance
->getPluginLibraryPath(),
]));
}
}
else {
$this->fileSystem
->rmdir($path);
if ($print_messages) {
$this
->logger()
->error(dt('Error: unable to install Audiofield library @library', [
'@library' => $pluginInstance
->getPluginTitle(),
]));
}
}
}
}
public function update($updateLibrary = '', $print_messages = TRUE) {
$pluginList = $this->playerManager
->getDefinitions();
if (!empty($updateLibrary)) {
if (!isset($pluginList[$updateLibrary . '_audio_player'])) {
$this
->logger()
->error(dt('Error: @library is not a valid Audiofield library.', [
'@library' => $updateLibrary,
]));
return;
}
$pluginList = [
$updateLibrary . '_audio_player' => $pluginList[$updateLibrary . '_audio_player'],
];
}
foreach ($pluginList as $pluginName => $plugin) {
$pluginInstance = $this->playerManager
->createInstance($pluginName);
if (!$pluginInstance
->getPluginLibrary()) {
continue;
}
$path = DRUPAL_ROOT . $pluginInstance
->getPluginLibraryPath();
if (!$pluginInstance
->checkInstalled(FALSE)) {
$this
->download($pluginInstance
->getPluginLibraryName());
continue;
}
if ($pluginInstance
->checkVersion(FALSE)) {
$this
->logger()
->notice(dt('Audiofield library for @library is already up to date', [
'@library' => $pluginInstance
->getPluginTitle(),
]));
continue;
}
$this->fileSystem
->move($path, $this->fileSystem
->getTempDirectory() . '/temp_audiofield', TRUE);
if (is_dir($path)) {
$this->fileSystem
->rmdir($path);
}
$this
->download($updateLibrary, FALSE);
if ($pluginInstance
->checkInstalled()) {
$this->fileSystem
->rmdir($this->fileSystem
->getTempDirectory() . '/temp_audiofield');
$this
->logger()
->notice(dt('Audiofield library for @library has been successfully updated at @location', [
'@library' => $pluginInstance
->getPluginTitle(),
'@location' => $pluginInstance
->getPluginLibraryPath(),
]));
}
else {
$this->fileSystem
->rmdir($path);
$this
->logger()
->error(dt('Error: unable to update Audiofield library @library', [
'@library' => $pluginInstance
->getPluginTitle(),
]));
$this->fileSystem
->move($this->fileSystem
->getTempDirectory() . '/temp_audiofield', $path, TRUE);
}
}
}
}