b09f73ba4123eaac7f8f1da11fb450d75e5d9bee
[yaffs-website] / vendor / drupal / console / src / Command / Shared / FeatureTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Shared\FeatureTrait.
6  */
7
8 namespace Drupal\Console\Command\Shared;
9
10 use Drupal\features\FeaturesManagerInterface;
11 use Drupal\features\ConfigurationItem;
12 use Drupal\features\Plugin\FeaturesGeneration\FeaturesGenerationWrite;
13 use Drupal\config_update\ConfigRevertInterface;
14
15 /**
16  * Class FeatureTrait
17  *
18  * @package Drupal\Console\Command
19  */
20 trait FeatureTrait
21 {
22     public function packageQuestion($bundle)
23     {
24         $packages = $this->getPackagesByBundle($bundle);
25
26         if (empty($packages)) {
27             throw new \Exception(
28                 $this->trans('commands.features.message.no-packages')
29             );
30         }
31
32         $package = $this->getIo()->choiceNoList(
33             $this->trans('commands.features.import.questions.packages'),
34             $packages
35         );
36
37         return $package;
38     }
39
40
41     /**
42    * @param bool $bundle_name
43    *
44    * @return \Drupal\features\FeaturesAssignerInterface
45    */
46     protected function getAssigner($bundle_name)
47     {
48         /**
49          * @var \Drupal\features\FeaturesAssignerInterface $assigner
50          */
51         $assigner = \Drupal::service('features_assigner');
52         if (!empty($bundle_name)) {
53             $bundle = $assigner->applyBundle($bundle_name);
54
55             if ($bundle->getMachineName() != $bundle_name) {
56             }
57         }
58         // return configuration for default bundle
59         else {
60             $assigner->assignConfigPackages();
61         }
62         return $assigner;
63     }
64
65     /**
66      * Get a list of features.
67      *
68      * @param bundle
69      *
70      * @return array
71      */
72     protected function getFeatureList($bundle)
73     {
74         $features = [];
75         $manager =  $this->getFeatureManager();
76         $modules = $this->getPackagesByBundle($bundle);
77
78         foreach ($modules as $module_name) {
79             $feature = $manager->loadPackage($module_name, true);
80             $overrides = $manager->detectOverrides($feature);
81
82             $state = $feature->getState();
83
84             if (!empty($overrides) && ($feature->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT)) {
85                 $state = FeaturesManagerInterface::STATE_OVERRIDDEN;
86             }
87
88             if ($feature->getStatus() != FeaturesManagerInterface::STATUS_NO_EXPORT) {
89                 $features[$feature->getMachineName()] = [
90                     'name' => $feature->getName(),
91                     'machine_name' => $feature->getMachineName(),
92                     'bundle_name' => $feature->getBundle(),
93                     'status' => $manager->statusLabel($feature->getStatus()),
94                     'state' => ($state != FeaturesManagerInterface::STATE_DEFAULT) ? $manager->stateLabel($state) : '',
95                 ];
96             }
97         }
98
99         return $features;
100     }
101
102
103     protected function importFeature($packages)
104     {
105         $manager =  $this->getFeatureManager();
106
107         $modules = (is_array($packages)) ? $packages : [$packages];
108         $overridden = [] ;
109         foreach ($modules as $module_name) {
110             $package = $manager->loadPackage($module_name, true);
111
112             if (empty($package)) {
113                 $this->getIo()->warning(
114                     sprintf(
115                         $this->trans('commands.features.import.messages.not-available'),
116                         $module_name
117                     )
118                 );
119                 continue;
120             }
121
122             if ($package->getStatus() != FeaturesManagerInterface::STATUS_INSTALLED) {
123                 $this->getIo()->warning(
124                     sprintf(
125                         $this->trans('commands.features.import.messages.uninstall'),
126                         $module_name
127                     )
128                 );
129                 continue;
130             }
131
132             $overrides = $manager->detectOverrides($package);
133             $missing = $manager->reorderMissing($manager->detectMissing($package));
134
135             if (!empty($overrides) || !empty($missing) && ($package->getStatus() == FeaturesManagerInterface::STATUS_INSTALLED)) {
136                 $overridden[] = array_merge($missing, $overrides);
137             }
138         }
139
140         // Process only missing or overridden features
141         $components = $overridden;
142
143         if (empty($components)) {
144             $this->getIo()->warning(
145                 sprintf(
146                     $this->trans('commands.features.import.messages.nothing')
147                 )
148             );
149
150             return ;
151         } else {
152             $this->import($this->getIo(), $components);
153         }
154     }
155
156     public function import($components)
157     {
158         $manager = $this->getFeatureManager();
159         /**
160          * @var \Drupal\config_update\ConfigRevertInterface $config_revert
161          */
162         $config_revert = \Drupal::service('features.config_update');
163
164         $config = $manager->getConfigCollection();
165
166         foreach ($components as $component) {
167             foreach ($component as $feature) {
168                 if (!isset($config[$feature])) {
169                     //Import missing component.
170                     $item = $manager->getConfigType($feature);
171                     $type = ConfigurationItem::fromConfigStringToConfigType($item['type']);
172                     $config_revert->import($type, $item['name_short']);
173                     $this->getIo()->info(
174                         sprintf(
175                             $this->trans('commands.features.import.messages.importing'),
176                             $feature
177                         )
178                     );
179                 } else {
180                     // Revert existing component.
181                     $item = $config[$feature];
182                     $type = ConfigurationItem::fromConfigStringToConfigType($item->getType());
183                     $config_revert->revert($type, $item->getShortName());
184                     $this->getIo()->info(
185                         sprintf(
186                             $this->trans('commands.features.import.messages.reverting'),
187                             $feature
188                         )
189                     );
190                 }
191             }
192         }
193     }
194
195
196     public function getPackagesByBundle($bundle)
197     {
198         $manager =  $this->getFeatureManager();
199         $assigner = $this->getAssigner($bundle);
200         $current_bundle = $assigner->getBundle();
201
202         // List all packages availables
203         if ($current_bundle->getMachineName() == 'default') {
204             $current_bundle = null;
205         }
206
207         $packages = array_keys($manager->getFeaturesModules($current_bundle));
208
209         return $packages;
210     }
211
212     public function getFeatureManager()
213     {
214         return  \Drupal::service('features.manager');
215     }
216 }