Const argument in virtual type breaks magento2 [closed]
-
26-12-2020 - |
Question
So, I'm using the code from here sample-payment and I have in di.xml
the following code:
<!-- Payment Method Facade configuration -->
<virtualType name="SamplePaymentGatewayFacade" type="Magento\Payment\Model\Method\Adapter">
<arguments>
<argument name="code" xsi:type="const">Magento\SamplePaymentGateway\Model\Ui\ConfigProvider::CODE</argument>
<argument name="formBlockType" xsi:type="string">Magento\Payment\Block\Form</argument>
<argument name="infoBlockType" xsi:type="string">Magento\SamplePaymentGateway\Block\Info</argument>
<argument name="valueHandlerPool" xsi:type="object">SamplePaymentGatewayValueHandlerPool</argument>
<argument name="commandPool" xsi:type="object">SamplePaymentGatewayCommandPool</argument>
</arguments>
</virtualType>
<!-- Configuration reader -->
<virtualType name="SamplePaymentGatewayConfig" type="Magento\Payment\Gateway\Config\Config">
<arguments>
<argument name="methodCode" xsi:type="const">Magento\SamplePaymentGateway\Model\Ui\ConfigProvider::CODE</argument>
</arguments>
</virtualType>
that when running it in this way it breaks the magento2. Note: the code is under root/app/code
stored.
With breaks I mean the following:
when I run a simple php bin/magento list
I receive:
root@ea40a2340686:/var/www/magento2# php bin/magento list
Magento CLI version 2.1.10
Usage:
command [options] [arguments]
Options:
--help (-h) Display this help message
--quiet (-q) Do not output any message
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
--no-interaction (-n) Do not ask any interactive question
Available commands:
help Displays help for a command
list Lists commands
We're sorry, an error occurred. Try clearing the cache and code generation directories. By default, they are: var/cache, var/di, var/generation, and var/page_cache.
[InvalidArgumentException]
Constant name is expected.
And if I try now to do something in the browser I get:
Fatal error: Cannot declare class Magento\SamplePaymentGateway\Model\Ui\ConfigProvider, because the name is already in use in /var/www/magento2/app/code/Magento/SamplePaymentGateway/Model/Ui/ConfigProvider.php on line 22
The class:
/**
* Class ConfigProvider
*/
final class ConfigProvider implements ConfigProviderInterface
{
const CODE = 'sample_gateway';
/**
* Retrieve assoc array of checkout configuration
*
* @return array
*/
public function getConfig()
{
return [
'payment' => [
self::CODE => [
'transactionResults' => [
ClientMock::SUCCESS => __('Success'),
ClientMock::FAILURE => __('Fraud'),
],
],
],
];
}
}
If I remove this line (and the other one in the other virtual type) in the di.xml
:
<argument name="code" xsi:type="const">Magento\SamplePaymentGateway\Model\Ui\ConfigProvider::CODE</argument>
everything works again.
So, my question: What is wrong by this declaration of the const?
As from the config.xsd
and types.xsd
everything should be ok.
<!-- config.xsd -->
<xs:complexType name="const">
<xs:complexContent>
<xs:extension base="argumentType" />
</xs:complexContent>
</xs:complexType>
and
<!-- types.xsd -->
<xs:complexType name="argumentType" abstract="true" mixed="true">
<xs:attribute name="name" use="required"/>
</xs:complexType>
PS: I'm using Magento2 V2.1.10 with php 7.0.12 in the magento2devbox.
Solution
Apparently for me was just a type in the namespace of the ConfigProvider
class. Instead of namespace Magento\SamplePaymentGateway\Model\Ui;
was only namespace Magento\MyTestPaymentGateway\Model\Ui;
.
I've copied this from another test project and that's the reason didn't find the const.
PS: If you have this error -> be careful with typos!!