Skip to content

Commit af631fd

Browse files
committed
fd command.
1 parent 82fe0b0 commit af631fd

File tree

1 file changed

+73
-51
lines changed

1 file changed

+73
-51
lines changed

src/Commands/FeaturesCommands.php

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Drupal\features\Commands;
44

5-
use Consolidation\OutputFormatters\StructuredData\ListDataFromKeys;
6-
use Consolidation\OutputFormatters\StructuredData\PropertyList;
75
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
86
use Drupal\Component\Diff\DiffFormatter;
97
use Drupal\config_update\ConfigDiffInterface;
@@ -23,6 +21,39 @@
2321
*/
2422
class FeaturesCommands extends DrushCommands {
2523

24+
const OPTIONS =[
25+
'bundle' => NULL,
26+
];
27+
28+
const OPTIONS_ADD = self::OPTIONS;
29+
30+
const OPTIONS_COMPONENTS = self::OPTIONS + [
31+
'exported' => NULL,
32+
'format' => 'table',
33+
'not-exported' => NULL,
34+
];
35+
36+
const OPTIONS_DIFF = self::OPTIONS + [
37+
'ctypes' => NULL,
38+
'lines' => NULL,
39+
];
40+
41+
const OPTIONS_EXPORT = self::OPTIONS + [
42+
'add-profile' => NULL,
43+
];
44+
45+
const OPTIONS_IMPORT = self::OPTIONS + [
46+
'force' => NULL,
47+
];
48+
49+
const OPTIONS_IMPORT_ALL = self::OPTIONS;
50+
51+
const OPTIONS_LIST = self::OPTIONS + [
52+
'format' => 'table',
53+
];
54+
55+
const OPTIONS_STATUS = self::OPTIONS;
56+
2657
/**
2758
* The features_assigner service.
2859
*
@@ -142,7 +173,7 @@ protected function getOption(array $options, $name, $default = NULL) {
142173
*
143174
* @aliases fs,features-status
144175
*/
145-
public function status($keys = NULL, array $options = ['bundle' => NULL]) {
176+
public function status($keys = NULL, array $options = self::OPTIONS_STATUS) {
146177
$this->featuresOptions($options);
147178

148179
$currentBundle = $this->assigner->getBundle();
@@ -212,10 +243,7 @@ public function status($keys = NULL, array $options = ['bundle' => NULL]) {
212243
*
213244
* @aliases fl,features-list-packages
214245
*/
215-
public function listPackages(
216-
$package_name = NULL,
217-
$options = ['format' => 'table', 'bundle' => NULL]
218-
) {
246+
public function listPackages($package_name = NULL, $options = self::OPTIONS_LIST) {
219247
$assigner = $this->featuresOptions($options);
220248
$current_bundle = $assigner->getBundle();
221249
$namespace = $current_bundle->isDefault() ? '' : $current_bundle->getMachineName();
@@ -281,7 +309,7 @@ public function listPackages(
281309
*
282310
* @aliases fra,fia,fim-all,features-import-all
283311
*/
284-
public function importAll($options = ['bundle' => NULL]) {
312+
public function importAll($options = self::OPTIONS_IMPORT_ALL) {
285313
$assigner = $this->featuresOptions($options);
286314
$currentBundle = $assigner->getBundle();
287315
$namespace = $currentBundle->isDefault() ? '' : $currentBundle->getMachineName();
@@ -332,10 +360,7 @@ public function importAll($options = ['bundle' => NULL]) {
332360
* @throws \Drush\Exceptions\UserAbortException
333361
* @throws \Exception
334362
*/
335-
public function export(
336-
array $packages,
337-
$options = ['add-profile' => NULL, 'bundle' => NULL]
338-
) {
363+
public function export(array $packages, $options = self::OPTIONS_EXPORT) {
339364
$assigner = $this->featuresOptions($options);
340365
$manager = $this->manager;
341366
$generator = $this->generator;
@@ -407,6 +432,7 @@ public function export(
407432
* Patterns of config to add, see features:components for the format to use.
408433
*
409434
* @command features:add
435+
*
410436
* @todo @param $feature Feature package to export and add config to.
411437
*
412438
* @option bundle Use a specific bundle namespace.
@@ -416,7 +442,7 @@ public function export(
416442
* @throws \Drush\Exceptions\UserAbortException
417443
* @throws \Exception
418444
*/
419-
public function add($components = NULL, $options = ['bundle' => NULL]) {
445+
public function add($components = NULL, $options = self::OPTIONS_ADD) {
420446
if ($components) {
421447
$assigner = $this->featuresOptions($options);
422448
$manager = $this->manager;
@@ -511,15 +537,7 @@ public function add($components = NULL, $options = ['bundle' => NULL]) {
511537
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields|null
512538
* The command output. May be empty.
513539
*/
514-
public function components(
515-
array $patterns,
516-
$options = [
517-
'format' => 'table',
518-
'exported' => NULL,
519-
'not-exported' => NULL,
520-
'bundle' => NULL,
521-
]
522-
) {
540+
public function components(array $patterns, $options = self::OPTIONS_COMPONENTS) {
523541
$args = $patterns;
524542
$this->featuresOptions($options);
525543

@@ -561,7 +579,7 @@ public function components(
561579
*
562580
* @command features:diff
563581
*
564-
* @option ctypes Comma separated list of component types to limit the output
582+
* @option ctypes Comma-separated list of component types to limit the output
565583
* to. Defaults to all types.
566584
* @option lines Generate diffs with <n> lines of context instead of the
567585
* usual two.
@@ -571,24 +589,24 @@ public function components(
571589
*
572590
* @throws \Exception
573591
*/
574-
public function diff(
575-
$feature,
576-
$options = ['ctypes' => NULL, 'lines' => NULL, 'bundle' => NULL]
577-
) {
592+
public function diff($feature, $options = self::OPTIONS_DIFF) {
578593
$manager = $this->manager;
579594
$assigner = $this->featuresOptions($options);
580595
$assigner->assignConfigPackages();
581596

582597
$module = $feature;
583-
$filter_ctypes = $options["ctypes"];
598+
599+
// @FIXME Actually do something with the "ctypes" option.
600+
$filter_ctypes = $options['ctypes'];
584601
if ($filter_ctypes) {
585602
$filter_ctypes = explode(',', $filter_ctypes);
586603
}
587604

588605
$feature = $manager->loadPackage($module, TRUE);
589606
if (empty($feature)) {
590-
throw new \Exception(dt('No such feature is available: @module',
591-
['@module' => $module]));
607+
throw new DomainException(dt('No such feature is available: @module', [
608+
'@module' => $module,
609+
]));
592610
}
593611

594612
$lines = $options['lines'];
@@ -611,25 +629,28 @@ public function diff(
611629
$missing = $manager->reorderMissing($manager->detectMissing($feature));
612630
$overrides = array_merge($overrides, $missing);
613631

632+
$output = $this->output();
633+
614634
if (empty($overrides)) {
615-
drush_print(dt('Active config matches stored config for @module.',
616-
['@module' => $module]));
635+
$output->writeln(dt('Active config matches stored config for @module.', [
636+
'@module' => $module,
637+
]));
617638
}
618639
else {
619640
$config_diff = $this->configDiff;
620641

621642
// Print key for colors.
622-
drush_print(dt('Legend: '));
623-
drush_print(sprintf($red,
624-
dt('Code: drush features-import will replace the active config with the displayed code.')));
625-
drush_print(sprintf($green,
626-
dt('Active: drush features-export will update the exported feature with the displayed active config')));
643+
$output->writeln(dt('Legend: '));
644+
$output->writeln(sprintf($red,
645+
dt('Code: drush features-import will replace the active config with the displayed code.')));
646+
$output->writeln(sprintf($green,
647+
dt('Active: drush features-export will update the exported feature with the displayed active config')));
627648

628649
foreach ($overrides as $name) {
629650
$message = '';
630651
if (in_array($name, $missing)) {
631-
$message = sprintf($red, dt('(missing from active)'));
632652
$extension = [];
653+
$message = sprintf($red, dt('(missing from active)'));
633654
}
634655
else {
635656
$active = $manager->getActiveStorage()->read($name);
@@ -641,19 +662,23 @@ public function diff(
641662
$diff = $config_diff->diff($extension, $active);
642663
$rows = explode("\n", $formatter->format($diff));
643664
}
644-
drush_print();
645-
drush_print(dt("Config @name @message",
646-
['@name' => $name, '@message' => $message]));
665+
666+
$output->writeln('');
667+
$output->writeln(dt("Config @name @message", [
668+
'@name' => $name,
669+
'@message' => $message,
670+
]));
671+
647672
if (!empty($extension)) {
648673
foreach ($rows as $row) {
649674
if (strpos($row, '>') === 0) {
650-
drush_print(sprintf($green, $row));
675+
$output->writeln(sprintf($green, $row));
651676
}
652677
elseif (strpos($row, '<') === 0) {
653-
drush_print(sprintf($red, $row));
678+
$output->writeln(sprintf($red, $row));
654679
}
655680
else {
656-
drush_print($row);
681+
$output->writeln($row);
657682
}
658683
}
659684
}
@@ -680,13 +705,7 @@ public function diff(
680705
*
681706
* @throws \Exception
682707
*/
683-
public function import(
684-
$feature,
685-
$options = [
686-
'force' => NULL,
687-
'bundle' => NULL,
688-
]
689-
) {
708+
public function import($feature, $options = self::OPTIONS_IMPORT) {
690709
$this->featuresOptions($options);
691710

692711
$features = StringUtils::csvToArray($feature);
@@ -802,6 +821,9 @@ public function import(
802821
*
803822
* @param array $items
804823
* The items to return data for.
824+
*
825+
* @return array
826+
* An array of config items.
805827
*/
806828
protected function buildConfig(array $items) {
807829
$result = [];

0 commit comments

Comments
 (0)