|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Eav\Attribute; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
|
7
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class ViewCommand extends AbstractMagentoCommand |
|
14
|
|
|
{ |
|
15
|
|
|
protected function configure() |
|
16
|
|
|
{ |
|
17
|
|
|
$this |
|
18
|
|
|
->setName('eav:attribute:view') |
|
19
|
|
|
->addArgument('entityType', InputArgument::REQUIRED, 'Entity Type Code like catalog_product') |
|
20
|
|
|
->addArgument('attributeCode', InputArgument::REQUIRED, 'Attribute Code') |
|
21
|
|
|
->setDescription('View informations about an EAV attribute') |
|
22
|
|
|
->addOption( |
|
23
|
|
|
'format', |
|
24
|
|
|
null, |
|
25
|
|
|
InputOption::VALUE_OPTIONAL, |
|
26
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
|
27
|
|
|
); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param InputInterface $input |
|
32
|
|
|
* @param OutputInterface $output |
|
33
|
|
|
* |
|
34
|
|
|
* @return int|void |
|
35
|
|
|
* @throws InvalidArgumentException |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->detectMagento($output); |
|
40
|
|
|
if (!$this->initMagento()) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$entityType = $input->getArgument('entityType'); |
|
45
|
|
|
$attributeCode = $input->getArgument('attributeCode'); |
|
46
|
|
|
|
|
47
|
|
|
$attribute = $this->getAttribute($entityType, $attributeCode); |
|
48
|
|
|
if (!$attribute) { |
|
49
|
|
|
throw new InvalidArgumentException('Attribute was not found.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$table = array( |
|
53
|
|
|
array('ID', $attribute->getId()), |
|
54
|
|
|
array('Code', $attribute->getName()), |
|
55
|
|
|
array('Attribute-Set-ID', $attribute->getAttributeSetId()), |
|
56
|
|
|
array('Visible-On-Front', $attribute->getIsVisibleOnFront() ? 'yes' : 'no'), |
|
57
|
|
|
array('Attribute-Model', $attribute->getAttributeModel() ? $attribute->getAttributeModel() : ''), |
|
58
|
|
|
array('Backend-Model', $attribute->getBackendModel() ? $attribute->getBackendModel() : ''), |
|
59
|
|
|
array('Backend-Table', $attribute->getBackendTable() ? $attribute->getBackendTable() : ''), |
|
60
|
|
|
array('Backend-Type', $attribute->getBackendType() ? $attribute->getBackendType() : ''), |
|
61
|
|
|
array('Source-Model', $attribute->getSourceModel() ? $attribute->getSourceModel() : ''), |
|
62
|
|
|
array('Cache-ID-Tags', $attribute->getCacheIdTags() ? implode(',', $attribute->getCacheIdTags()) : ''), |
|
63
|
|
|
array('Cache-Tags', $attribute->getCacheTags() ? implode(',', $attribute->getCacheTags()) : ''), |
|
64
|
|
|
array('Default-Value', $attribute->getDefaultValue() ? $attribute->getDefaultValue() : ''), |
|
65
|
|
|
array( |
|
66
|
|
|
'Flat-Columns', |
|
67
|
|
|
$attribute->getFlatColumns() ? implode(',', array_keys($attribute->getFlatColumns())) : '', |
|
68
|
|
|
), |
|
69
|
|
|
array('Flat-Indexes', $attribute->getFlatIndexes() ? implode(',', $attribute->getFlatIndexes()) : ''), |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
if ($attribute->getFrontend()) { |
|
73
|
|
|
$table[] = array('Frontend-Label', $attribute->getFrontend()->getLabel()); |
|
74
|
|
|
$table[] = array('Frontend-Class', trim($attribute->getFrontend()->getClass())); |
|
75
|
|
|
$table[] = array('Frontend-Input', trim($attribute->getFrontend()->getInputType())); |
|
76
|
|
|
$table[] = array( |
|
77
|
|
|
'Frontend-Input-Renderer-Class', |
|
78
|
|
|
trim($attribute->getFrontend()->getInputRendererClass()), |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this |
|
|
|
|
|
|
83
|
|
|
->getHelper('table') |
|
84
|
|
|
->setHeaders(array('Type', 'Value')) |
|
85
|
|
|
->renderByFormat($output, $table, $input->getOption('format')); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $entityType |
|
90
|
|
|
* @param string $attributeCode |
|
91
|
|
|
* |
|
92
|
|
|
* @return \Mage_Eav_Model_Entity_Attribute_Abstract|false |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getAttribute($entityType, $attributeCode) |
|
95
|
|
|
{ |
|
96
|
|
|
return \Mage::getModel('eav/config')->getAttribute($entityType, $attributeCode); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: