FeaturesGenerationMethodBase.php
3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* @file
* Contains \Drupal\features\FeaturesGenerationMethodBase.
*/
namespace Drupal\features;
use Drupal\Component\Serialization\Yaml;
use Drupal\features\FeaturesManagerInterface;
use Drupal\features\FeaturesAssignerInterface;
use Drupal\features\FeaturesBundleInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Base class for package assignment methods.
*/
abstract class FeaturesGenerationMethodBase implements FeaturesGenerationMethodInterface {
use StringTranslationTrait;
/**
* The features manager.
*
* @var \Drupal\features\FeaturesManagerInterface
*/
protected $featuresManager;
/**
* The features assigner.
*
* @var \Drupal\features\FeaturesAssignerInterface
*/
protected $assigner;
/**
* {@inheritdoc}
*/
public function setFeaturesManager(FeaturesManagerInterface $features_manager) {
$this->featuresManager = $features_manager;
}
/**
* {@inheritdoc}
*/
public function setAssigner(FeaturesAssignerInterface $assigner) {
$this->assigner = $assigner;
}
/**
* {@inheritdoc}
*/
public function exportFormSubmit(array &$form, FormStateInterface $form_state) {
}
/**
* Merges an info file into a package's info file.
*
* @param string $package_info
* The Yaml encoded package info.
* @param string $info_file_uri
* The info file's URI.
*/
protected function mergeInfoFile($package_info, $info_file_uri) {
$package_info = Yaml::decode($package_info);
$existing_info = \Drupal::service('info_parser')->parse($info_file_uri);
// Ensure the entire 'features' data is replaced by new data.
unset($existing_info['features']);
return Yaml::encode($this->featuresManager->arrayMergeUnique($existing_info, $package_info));
}
/**
* {@inheritdoc}
*/
public function prepare(array &$packages = array(), FeaturesBundleInterface $bundle = NULL) {
// If no packages were specified, get all packages.
if (empty($packages)) {
$packages = $this->featuresManager->getPackages();
}
// If any packages exist, read in their files.
$existing_packages = $this->featuresManager->listPackageDirectories(array_keys($packages), $bundle);
foreach ($packages as &$package) {
list($full_name, $path) = $this->featuresManager->getExportInfo($package, $bundle);
$package['directory'] = $path;
// If this is the profile, its directory is already assigned.
if (!isset($bundle) || !$bundle->isProfilePackage($package['machine_name'])) {
$package['directory'] .= '/' . $full_name;
}
$this->preparePackage($package, $existing_packages, $bundle);
}
// Clean up the $package pass by reference.
unset($package);
}
/**
* Performs any required changes on a package prior to generation.
*
* @param array $package
* The package to be prepared.
* @param array $existing_packages
* An array of existing packages with machine names as keys and paths as
* values.
* @param \Drupal\features\FeaturesBundleInterface $bundle
* Optional bundle used for export
*/
abstract protected function preparePackage(array &$package, array $existing_packages, FeaturesBundleInterface $bundle = NULL);
}