Magento2 DI – Understanding Object Manager Argument Replacement Using di.xml

dimagento2object-manager

I'm following object manager argument replacement tutorial. One of the possible di.xml looks like

<type name="Pulsestorm\TutorialObjectManagerArguments\Model\Example">
    <arguments>
        <argument name="scaler1" xsi:type="string">bar</argument>
    </arguments>
</type>

Which means in Model Example's class's constructor, replace (whatever old) value of argument $scaler1 with bar.

Whereas for commands it looks like

<type name="Magento\Framework\Console\CommandList">
    <arguments>
        <argument name="commands" xsi:type="array">
            <item name="testbedCommand" xsi:type="object">Pulsestorm\TutorialObjectManagerArguments\Command\Testbed</item>
        </argument>
    </arguments>
</type>

Which says add 1 new item(command) in CommandList's constructor argument commands(which is of array type).

I don't see use of the name testbedCommand anywhere in the module.
What will be it's usage ? Should it be unique?

Best Answer

An item name is not used in Magento\Framework\Console\CommandList. If you look into this class, you can see, that only values are used, however array keys are also available.

If you define in your module the same type and with same argument item name:

<type name="Magento\Framework\Console\CommandList">
    <arguments>
        <argument name="commands" xsi:type="array">
            <item name="testbedCommand" xsi:type="object">Your\Module\Command\Testbed</item>
        </argument>
    </arguments>
</type>

and you module goes after Pulsestorm\TutorialObjectManagerArguments, then as the argument you will get your value:

  <argument name="commands" xsi:type="array">
                <item name="testbedCommand" xsi:type="object">Your\Module\Command\Testbed</item>
  </argument>
Related Topic