CliCommandWrapper.php in CKEditor Media Embed Plugin 8
File
src/Command/CliCommandWrapper.php
View source
<?php
namespace Drupal\ckeditor_media_embed\Command;
use Drupal\ckeditor_media_embed\AssetManager;
use Drupal\ckeditor\CKEditorPluginManager;
use Drupal\Core\Archiver\Zip;
use Drupal\Core\Asset\LibraryDiscovery;
use Drupal\Core\Config\ConfigFactory;
use GuzzleHttp\Client;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
class CliCommandWrapper {
protected $ckeditorPluginManager;
protected $httpClient;
protected $libraryDiscovery;
protected $configFactory;
protected $fileSystem;
protected $packageVersion;
public function __construct(CKEditorPluginManager $ckeditorPluginManager, LibraryDiscovery $libraryDiscovery, Client $httpClient, ConfigFactory $configFactory) {
$this->ckeditorPluginManager = $ckeditorPluginManager;
$this->libraryDiscovery = $libraryDiscovery;
$this->httpClient = $httpClient;
$this->configFactory = $configFactory;
$this->fileSystem = new FileSystem();
$this
->setPackageVersion();
}
protected function setPackageVersion() {
$this->packageVersion = AssetManager::getCKEditorVersion($this->libraryDiscovery, $this->configFactory);
return $this;
}
public function askToOverwritePluginFiles(CKEditorCliCommandInterface $command) {
$yes = $command
->getInput()
->hasOption('yes') ? $command
->getInput()
->getOption('yes') : FALSE;
if (!$yes) {
$libraries_path = AssetManager::getCKEditorLibraryPluginDirectory();
if (file_exists($libraries_path)) {
$question = sprintf($command
->getMessage('question-overwrite-files'), $libraries_path);
return $command
->confirmation($question);
}
}
return TRUE;
}
public function overwritePluginFiles(CKEditorCliCommandInterface $command, $overwrite) {
$package_directory = $this
->downloadCKEditorFull($command);
foreach (AssetManager::getPlugins($this->packageVersion) as $plugin) {
$this
->installCKEditorPlugin($command, $package_directory, $plugin, $overwrite);
}
$this->configFactory
->getEditable('ckeditor_media_embed.settings')
->set('plugins_version_installed', $this->packageVersion)
->save();
$this->ckeditorPluginManager
->clearCachedDefinitions();
}
protected function downloadCKEditorFull(CKEditorCliCommandInterface $command) {
$command
->comment(sprintf($command
->getMessage('comment-downloading-package'), $this->packageVersion));
$package_name = AssetManager::getCKEditorDevFullPackageName($this->packageVersion);
$package_url = AssetManager::getCKEditorDevFullPackageUrl($this->packageVersion);
$package_directory = sys_get_temp_dir() . '/' . $package_name;
$package_archive = sys_get_temp_dir() . "/{$package_name}.zip";
try {
$this
->downloadFile($package_url, $package_archive);
if (is_file($package_archive)) {
$archive = new Zip($package_archive);
$archive
->extract(sys_get_temp_dir());
$command
->getIo()
->success(sprintf($command
->getMessage('success-downloading-package'), $this->packageVersion));
}
} catch (\Exception $e) {
$command
->getIo()
->error($e
->getMessage());
}
return $package_directory;
}
protected function installCKEditorPlugin(CKEditorCliCommandInterface $command, $package_directory, $plugin_name, $overwrite = FALSE) {
$libraries_path = AssetManager::getCKEditorLibraryPluginDirectory() . $plugin_name;
$package_plugin_path = $package_directory . '/plugins/' . $plugin_name;
try {
$this->fileSystem
->mkdir($libraries_path);
$this->fileSystem
->mirror($package_plugin_path, $libraries_path, NULL, [
'override' => $overwrite,
]);
$command
->getIo()
->success(sprintf($command
->getMessage('success-installed-plugin'), $plugin_name));
} catch (IOExceptionInterface $e) {
$command
->getIo()
->error($e
->getMessage());
}
return $this;
}
protected function downloadFile($url, $destination) {
$this->httpClient
->get($url, [
'sink' => $destination,
]);
return file_exists($destination);
}
}