View source
<?php
namespace Drupal\features\Plugin\FeaturesGeneration;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\features\FeaturesGenerationMethodBase;
use Drupal\Core\Archiver\ArchiveTar;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\features\FeaturesBundleInterface;
use Drupal\features\Package;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeaturesGenerationArchive extends FeaturesGenerationMethodBase implements ContainerFactoryPluginInterface {
protected $root;
protected $csrfToken;
protected $fileSystem;
public function __construct($root, CsrfTokenGenerator $csrf_token, FileSystem $file_system) {
$this->root = $root;
$this->csrfToken = $csrf_token;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('app.root'), $container
->get('csrf_token'), $container
->get('file_system'));
}
const METHOD_ID = 'archive';
protected $archiveName;
protected function preparePackage(Package $package, array $existing_packages, FeaturesBundleInterface $bundle = NULL) {
if (isset($existing_packages[$package
->getMachineName()])) {
$existing_directory = $existing_packages[$package
->getMachineName()];
}
else {
$existing_directory = $package
->getDirectory();
}
$existing_directory = $this->root . '/' . $existing_directory;
if (is_dir($existing_directory)) {
$files = $this->fileSystem
->scanDirectory($existing_directory, '/.*/');
$exclude_files = [
$package
->getMachineName() . '.features',
];
foreach ($files as $file) {
foreach (array_keys($this->featuresManager
->getExtensionStorages()
->getExtensionStorages()) as $directory) {
if (strpos($file->uri, $directory) !== FALSE) {
continue 2;
}
}
if ($file->name == $package
->getMachineName() . '.info') {
$files = $package
->getFiles();
$files['info']['string'] = $this
->mergeInfoFile($package
->getFiles()['info']['string'], $file->uri);
$package
->setFiles($files);
}
elseif (!in_array($file->name, $exclude_files)) {
$file_directory = dirname($file->uri);
if ($file_directory !== $existing_directory) {
$subdirectory = substr($file_directory, strlen($existing_directory) + 1);
}
else {
$subdirectory = NULL;
}
$package
->appendFile([
'filename' => $file->filename,
'subdirectory' => $subdirectory,
'string' => file_get_contents($file->uri),
]);
}
}
}
}
public function generate(array $packages = [], FeaturesBundleInterface $bundle = NULL) {
if (empty($packages)) {
$packages = $this->featuresManager
->getPackages();
}
if (count($packages) == 1) {
$filename = current($packages)
->getMachineName();
}
elseif (isset($bundle) && $bundle
->isProfile()) {
$filename = $bundle
->getProfileName();
}
elseif (isset($bundle) && !$bundle
->isDefault()) {
$filename = $bundle
->getMachineName();
}
else {
$filename = 'generated_features';
}
$return = [];
$this->archiveName = $filename . '.tar.gz';
$archive_name = $this->fileSystem
->getTempDirectory() . '/' . $this->archiveName;
if (file_exists($archive_name)) {
$this->fileSystem
->delete($archive_name);
}
$archiver = new ArchiveTar($archive_name);
foreach ($packages as $package) {
if (count($packages) == 1) {
$package
->setDirectory($package
->getMachineName());
}
$this
->generatePackage($return, $package, $archiver);
}
return $return;
}
protected function generatePackage(array &$return, Package $package, ArchiveTar $archiver) {
$success = TRUE;
foreach ($package
->getFiles() as $file) {
try {
$this
->generateFile($package
->getDirectory(), $file, $archiver);
} catch (\Exception $exception) {
$this
->failure($return, $package, $exception);
$success = FALSE;
break;
}
}
if ($success) {
$this
->success($return, $package);
}
}
protected function success(array &$return, Package $package) {
$type = $package
->getType() == 'module' ? $this
->t('Package') : $this
->t('Profile');
$return[] = [
'success' => TRUE,
'display' => FALSE,
'message' => '@type @package written to archive.',
'variables' => [
'@type' => $type,
'@package' => $package
->getName(),
],
];
}
protected function failure(array &$return, Package $package, \Exception $exception = NULL, $message = '') {
$type = $package
->getType() == 'module' ? $this
->t('Package') : $this
->t('Profile');
$return[] = [
'success' => FALSE,
'display' => FALSE,
'message' => '@type @package not written to archive. Error: @error.',
'variables' => [
'@type' => $type,
'@package' => $package
->getName(),
'@error' => isset($exception) ? $exception
->getMessage() : $message,
],
];
}
protected function generateFile($directory, array $file, ArchiveTar $archiver) {
$filename = $directory;
if (!empty($file['subdirectory'])) {
$filename .= '/' . $file['subdirectory'];
}
$filename .= '/' . $file['filename'];
if ($archiver
->addString($filename, $file['string'], FALSE, [
'mode' => 0644,
]) === FALSE) {
throw new \Exception($this
->t('Failed to archive file @filename.', [
'@filename' => $file['filename'],
]));
}
}
public function exportFormSubmit(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirect('features.export_download', [
'uri' => $this->archiveName,
'token' => $this->csrfToken
->get($this->archiveName),
]);
}
}