1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Eav\Attribute; |
4
|
|
|
|
5
|
|
|
use Magento\Eav\Model\Attribute; |
6
|
|
|
use Magento\Eav\Model\Entity\Type as EntityType; |
7
|
|
|
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection as AttributeCollection; |
8
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
9
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class ListCommand extends AbstractMagentoCommand |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AttributeCollection |
18
|
|
|
*/ |
19
|
|
|
private $attributeCollection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param AttributeCollection $attributeCollection |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function inject( |
26
|
|
|
AttributeCollection $attributeCollection |
27
|
|
|
) { |
28
|
|
|
$this->attributeCollection = $attributeCollection; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName('eav:attribute:list') |
38
|
|
|
->addOption( |
39
|
|
|
'add-source', |
40
|
|
|
null, |
41
|
|
|
InputOption::VALUE_NONE, |
42
|
|
|
'Add source models to list' |
43
|
|
|
) |
44
|
|
|
->addOption( |
45
|
|
|
'add-backend', |
46
|
|
|
null, |
47
|
|
|
InputOption::VALUE_NONE, |
48
|
|
|
'Add backend type to list' |
49
|
|
|
) |
50
|
|
|
->addOption( |
51
|
|
|
'filter-type', |
52
|
|
|
null, |
53
|
|
|
InputOption::VALUE_OPTIONAL, |
54
|
|
|
'Filter attributes by entity type' |
55
|
|
|
) |
56
|
|
|
->addOption( |
57
|
|
|
'format', |
58
|
|
|
null, |
59
|
|
|
InputOption::VALUE_OPTIONAL, |
60
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
61
|
|
|
) |
62
|
|
|
->setDescription('List EAV attributes'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param InputInterface $input |
67
|
|
|
* @param OutputInterface $output |
68
|
|
|
* @return int|void |
69
|
|
|
*/ |
70
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
$this->detectMagento($output); |
73
|
|
|
if (!$this->initMagento()) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$table = []; |
78
|
|
|
$addSource = $input->getOption('add-source'); |
79
|
|
|
$addBackend = $input->getOption('add-backend'); |
80
|
|
|
$filterType = $input->getOption('filter-type'); |
81
|
|
|
$this->attributeCollection->setOrder('attribute_code', 'asc'); |
82
|
|
|
|
83
|
|
|
/** @var Attribute $attribute */ |
84
|
|
|
foreach ($this->attributeCollection as $attribute) { |
85
|
|
|
/** @var EntityType $entityType */ |
86
|
|
|
$entityType = $attribute->getEntityType(); |
87
|
|
|
if ($filterType && |
88
|
|
|
$entityType->getEntityTypeCode() !== $filterType) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$row = [ |
93
|
|
|
$attribute->getAttributeCode(), |
94
|
|
|
$attribute->getId(), |
95
|
|
|
$entityType->getEntityTypeCode() . ' (#' . $entityType->getEntityTypeId() . ')', |
96
|
|
|
$attribute->getFrontendLabel(), |
97
|
|
|
]; |
98
|
|
|
if ($addBackend) { |
99
|
|
|
$row[] = $attribute->getBackendType(); |
100
|
|
|
} |
101
|
|
|
if ($addSource) { |
102
|
|
|
$row[] = $attribute->getSourceModel() ? $attribute->getSourceModel() : ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$table[] = $row; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$headers = [ |
109
|
|
|
'code', |
110
|
|
|
'id', |
111
|
|
|
'entity_type', |
112
|
|
|
'label', |
113
|
|
|
]; |
114
|
|
|
if ($addBackend) { |
115
|
|
|
$headers[] = 'backend_type'; |
116
|
|
|
} |
117
|
|
|
if ($addSource) { |
118
|
|
|
$headers[] = 'source'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->getHelper('table') |
|
|
|
|
122
|
|
|
->setHeaders($headers) |
123
|
|
|
->renderByFormat($output, $table, $input->getOption('format')); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.