View source
<?php
namespace Drupal\features\Plugin\FeaturesGeneration;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\features\FeaturesGenerationMethodBase;
use Drupal\features\FeaturesBundleInterface;
use Drupal\features\Package;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeaturesGenerationWrite extends FeaturesGenerationMethodBase implements ContainerFactoryPluginInterface {
const METHOD_ID = 'write';
protected $root;
protected $fileSystem;
public function __construct($root, FileSystemInterface $fileSystem) {
$this->root = $root;
$this->fileSystem = $fileSystem;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('app.root'), $container
->get('file_system'));
}
protected function preparePackage(Package $package, array $existing_packages, FeaturesBundleInterface $bundle = NULL) {
if (isset($existing_packages[$package
->getMachineName()])) {
$existing_directory = $existing_packages[$package
->getMachineName()];
$package
->setDirectory($existing_directory);
}
else {
$existing_directory = $package
->getDirectory();
}
$info_file_uri = $this->root . '/' . $existing_directory . '/' . $package
->getMachineName() . '.info.yml';
if (file_exists($info_file_uri)) {
$files = $package
->getFiles();
$files['info']['string'] = $this
->mergeInfoFile($package
->getFiles()['info']['string'], $info_file_uri);
$package
->setFiles($files);
foreach (array_keys($this->featuresManager
->getExtensionStorages()
->getExtensionStorages()) as $directory) {
$config_directory = $this->root . '/' . $existing_directory . '/' . $directory;
if (is_dir($config_directory)) {
$this->fileSystem
->deleteRecursive($config_directory);
}
}
}
}
public function generate(array $packages = [], FeaturesBundleInterface $bundle = NULL) {
if (empty($packages)) {
$packages = $this->featuresManager
->getPackages();
}
$return = [];
$files = \Drupal::state()
->get('system.module.files');
foreach ($packages as $package) {
$this
->generatePackage($return, $package);
if (!isset($files[$package
->getMachineName()]) && isset($package
->getFiles()['info'])) {
$files[$package
->getMachineName()] = $package
->getDirectory() . '/' . $package
->getFiles()['info']['filename'];
}
}
\Drupal::state()
->set('system.module.files', $files);
return $return;
}
protected function generatePackage(array &$return, Package $package) {
if (!$package
->getFiles()) {
$this
->failure($return, $package, NULL, $this
->t('No configuration was selected to be exported.'));
return;
}
$success = TRUE;
foreach ($package
->getFiles() as $file) {
try {
$this
->generateFile($package
->getDirectory(), $file);
} 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' => TRUE,
'message' => '@type @package written to @directory.',
'variables' => [
'@type' => $type,
'@package' => $package
->getName(),
'@directory' => $package
->getDirectory(),
],
];
}
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' => TRUE,
'message' => '@type @package not written to @directory. Error: @error.',
'variables' => [
'@type' => $type,
'@package' => $package
->getName(),
'@directory' => $package
->getDirectory(),
'@error' => isset($exception) ? $exception
->getMessage() : $message,
],
];
}
protected function generateFile($directory, array $file) {
if (!empty($file['subdirectory'])) {
$directory .= '/' . $file['subdirectory'];
}
$directory = $this->root . '/' . $directory;
if (!is_dir($directory)) {
if ($this->fileSystem
->mkdir($directory, NULL, TRUE) === FALSE) {
throw new \Exception($this
->t('Failed to create directory @directory.', [
'@directory' => $directory,
]));
}
}
if (file_put_contents($directory . '/' . $file['filename'], $file['string']) === FALSE) {
throw new \Exception($this
->t('Failed to write file @filename.', [
'@filename' => $file['filename'],
]));
}
}
}