jaroslawzielinski / diagnostics-m2
此模块用于项目的诊断
1.0.14
2024-09-11 05:48 UTC
Requires
- symfony/console: ^v4 || ^v5
README
这是一个用于magento2项目的诊断模块,可以作为新代码的脚手架使用。
为什么你的项目需要这个模块?
简介
你有没有...
- ...在后台区域使用单独的控制器测试/调试你的代码的一部分?
- ...在前端区域使用单独的控制器测试/调试你的代码的一部分?
- ...使用shell命令快速测试/更改数据库中的某些内容?
- ...在一些单独的项目位置测试/更改你的前端工作(如js/css/html),并保留一段时间?
- ...由于某些原因仅在本地修复项目中的某些内容,但你不知道在哪里放置它?
- ...做上述任何问题,并且还有专门配置?
- ...做上述任何问题,并且更改仅限于本地环境,无需修改仓库?
如果你的答案是肯定的,并且你还记得这发生不止一次,或者经常发生,请确认这个诊断模块是为你准备的,而且是免费的。
它将帮助你的工作,你将在任何类型的magento2项目中找到它很有用。
模块的整体理念
您将通过composer require --dev调用安装核心模块。这样,在生产模式下,此模块将不会在仓库中。
您将通过自己的Vendor_Diagnostics模块覆盖核心模块(核心模块只是您添加到Vendor_Diagnostics模块的定制代码的基础)。
如果您是git用户,请将所有更改添加到.git/info/exclude中
echo "app/code/Vendor/Diagnostics/" >> .git/info/exclude
使用诊断模块你将获得什么?
您将有一个简单的方法来测试/调试后端控制器(1.),在新的标签页中测试/调试前端控制器(2.),并在管理面板中从菜单中查看/编辑您专门的配置(3.)。您可以从一个地方完成所有这些,而不会影响仓库(您可以更改您的分支,模块将始终在那里,始终不受影响...)。请参阅下面的截图
您还可以使用CLI命令,您可以根据需要使用/修改它
bin/magento jaroslawzielinski:diagnostics:test --help
或者在覆盖getName和getDescription方法后,您可以这样使用,例如
bin/magento vendor:diagnostics:customer --help
如何通过shell安装诊断模块(3步中的第一步)
cd YourProject
composer require --dev jaroslawzielinski/diagnostics-m2
如何通过自定义代码覆盖核心模块(3步中的第二步)
创建一个模块
Vendor/Diagnostics/registration.php
<?php declare(strict_types=1); \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Diagnostics', __DIR__ );
Vendor/Diagnostics/etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Diagnostics"> <sequence> <module name="JaroslawZielinski_Diagnostics"/> </sequence> </module> </config>
覆盖管理面板配置
Vendor/Diagnostics/etc/adminhtml/system.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="jaroslawzielinski_diagnostics"> <!-- TODO: add/modify Your way... --> <group id="settings"> <field id="my_option" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1"> <label>My option</label> </field> </group> </section> </system> </config>
Vendor/Diagnostics/etc/config.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <jaroslawzielinski_diagnostics> <!-- TODO: add/modify Your way... --> <settings> <my_option><![CDATA[Test]]></my_option> </settings> </jaroslawzielinski_diagnostics> </default> </config>
Vendor/Diagnostics/Model/Config.php
<?php declare(strict_types=1); namespace Vendor\Diagnostics\Model; use Magento\Store\Model\ScopeInterface; class Config extends \JaroslawZielinski\Diagnostics\Model\Config { public const PATH_SETTINGS_MY_OPTION = 'jaroslawzielinski_diagnostics/settings/my_option'; //TODO: add Your constants here... public function getMyOption(): ?string { $myOption = $this->scopeConfig->getValue(self::PATH_SETTINGS_MY_OPTION, ScopeInterface::SCOPE_STORE); return empty($myOption) ? null : (string)$myOption; } //TODO: add Your Code here... }
覆盖控制台命令
Vendor/Diagnostics/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Vendor\Diagnostics\Model\Test\Customer"> <arguments> <argument name="logger" xsi:type="object">JaroslawZielinski\Diagnostics\Logger</argument> </arguments> </type> <preference for="JaroslawZielinski\Diagnostics\Model\Test\Test" type="Vendor\Diagnostics\Model\Test\Customer"/> </config>
Vendor/Diagnostics/Model/Test/Customer.php
<?php declare(strict_types=1); namespace Vendor\Diagnostics\Model\Test; use JaroslawZielinski\Diagnostics\Model\Test\Test; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; class Customer extends Test { /** * @var CustomerRepositoryInterface */ private $customerRepository; /** * {@inheritDoc} * * TODO: add Your DI */ public function __construct( CustomerRepositoryInterface $customerRepository, LoggerInterface $logger ) { parent::__construct($logger); $this->customerRepository = $customerRepository; } /** * {@inheritDoc} * * TODO: add Your code here... (this is only an example) */ public function execute(array $input, OutputInterface $output): array { $customerId = $input['customer_id'] ?? null; try { $customer = $this->customerRepository->getById($customerId); $email = $customer->getEmail(); } catch (NoSuchEntityException|LocalizedException $e) { $this->logger->error($e->getMessage(), $e->getTrace()); return [ __('Something went wrong [message: %1]', $e->getMessage()) ]; } $output->writeln('OK'); $this->logger->info('Customer Input', [ 'customer_id' => $customerId, 'email' => $email ]); return [ __('Done. [customer_id: %1, email: %2]', $customerId, $email) ]; } /** * @inheritDoc */ public function getArgumentsDefinition(): array { return [ [ 'name' => 'customer_id', 'mode' => InputArgument::REQUIRED, 'description' => (string)__('Customer ID'), 'default' => null ] ]; } /** * @inheritDoc */ public function getName(): string { return 'vendor:diagnostics:customer'; } /** * @inheritDoc */ public function getDescription(): string { return 'Vendor Diagnostics customer'; } }
覆盖控制器Adminhtml Test
Vendor/Diagnostics/etc/adminhtml/events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="adminhtml_diagnostics_controller_test"> <observer name="vendor_adminhtml_diagnostics_controller_test" instance="Vendor\Diagnostics\Observer\Adminhtml\Test"/> </event> </config>
Vendor/Diagnostics/Observer/Adminhtml/Test.php
<?php declare(strict_types=1); namespace Vendor\Diagnostics\Observer\Adminhtml; use JaroslawZielinski\Diagnostics\Model\Console\Output as ConsoleOutput; use JaroslawZielinski\Diagnostics\Console\Command\Test as TestCommand; use Magento\Framework\App\Request\Http; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Symfony\Component\Console\Exception\ExceptionInterface; use Symfony\Component\Console\Input\ArrayInput; class Test implements ObserverInterface { /** * @var ConsoleOutput */ private $output; /** * @var TestCommand */ private $testCommand; /** * TODO: Your DI here... */ public function __construct( ConsoleOutput $output, TestCommand $testCommand ) { $this->output = $output; $this->testCommand = $testCommand; } /** * @inheritDoc */ public function execute(Observer $observer) { $event = $observer->getEvent(); /** @var Http $request */ $request = $event->getRequest(); //TODO: Your Code here to test in backend controller $customerId = 1; $input = new ArrayInput(['customer_id' => $customerId]); $this->testCommand->run($input, $this->output); /** @var array $result */ $result = $event->getResult(); //TODO: Log the results here $result['test'] = 123; $event->setResult($result); } }
Vendor/Diagnostics/Block/Adminhtml/Test.php
<?php declare(strict_types=1); namespace Vendor\Diagnostics\Block\Adminhtml; use Vendor\Diagnostics\Model\Config; use Magento\Backend\Block\Template\Context; use Magento\Directory\Helper\Data as DirectoryHelper; use Magento\Framework\Json\Helper\Data as JsonHelper; class Test extends \JaroslawZielinski\Diagnostics\Block\Adminhtml\Test { /** * {@inheritDoc} * * TODO: add Your DI */ public function __construct( Config $config, Context $context, array $data = [], ?JsonHelper $jsonHelper = null, ?DirectoryHelper $directoryHelper = null ) { parent::__construct($config, $context, $data, $jsonHelper, $directoryHelper); } public function getMyOption(): ?string { return $this->config->getMyOption(); } //TODO: add Your Code here... }
Vendor/Diagnostics/view/adminhtml/layout/JaroslawZielinski_diagnostics_diagnose_test.xml
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <!-- TODO: override Your way... --> <body> <referenceContainer name="content"> <block class="Vendor\Diagnostics\Block\Adminhtml\Test" name="admin_test_block" template="Vendor_Diagnostics::diagnose/test.phtml"/> </referenceContainer> </body> </page>
Vendor/Diagnostics/view/adminhtml/templates/diagnose/test.phtml
<?php /** @var \Vendor\Diagnostics\Block\Adminhtml\Test $block */ // TODO: override Your way... ?> <span><?= __('This is Vendor test [my_option: %1].', $block->getMyOption()); ?></span> <br /> <?= $this->getChildHtml(); ?>
覆盖控制器Test
Vendor/Diagnostics/etc/frontend/events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="diagnostics_controller_test"> <observer name="vendor_diagnostics_controller_test" instance="Vendor\Diagnostics\Observer\Test"/> </event> </config>
Vendor/Diagnostics/Observer/Test.php
<?php declare(strict_types=1); namespace Vendor\Diagnostics\Observer; use Magento\Framework\App\Request\Http; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class Test implements ObserverInterface { /** * TODO: Your DI here... */ public function __construct() { } /** * @inheritDoc */ public function execute(Observer $observer) { $event = $observer->getEvent(); /** @var Http $request */ $request = $event->getRequest(); //TODO: Your Code here to test in frontend controller /** @var array $result */ $result = $event->getResult(); //TODO: Log the results here $result['test'] = 234; $event->setResult($result); } }
Vendor/Diagnostics/Block/Test.php
<?php declare(strict_types=1); namespace Vendor\Diagnostics\Block; use Vendor\Diagnostics\Model\Config; use Magento\Framework\View\Element\Template\Context; class Test extends \JaroslawZielinski\Diagnostics\Block\Test { /** * {@inheritDoc} * * TODO: add Your DI */ public function __construct( Config $config, Context $context, array $data = [] ) { parent::__construct($config, $context, $data); } public function getMyOption(): ?string { return $this->config->getMyOption(); } //TODO: add Your Code here... }
Vendor/Diagnostics/view/frontend/layout/JaroslawZielinski_diagnostics_diagnose_test.xml
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="empty" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <!-- TODO: override Your way... --> <body> <referenceContainer name="content"> <block class="Vendor\Diagnostics\Block\Test" name="test_block" template="Vendor_Diagnostics::diagnose/test.phtml" /> </referenceContainer> </body> </page>
Vendor/Diagnostics/view/frontend/templates/diagnose/test.phtml
<?php /** @var \Vendor\Diagnostics\Block\Test $block */ // TODO: override Your way... ?> <span><?= __('This is Vendor test [my_option: %1].', $block->getMyOption()); ?></span> <br /> <?= $this->getChildHtml(); ?>
从git隐藏你的自定义代码(3步中的第三步)
echo "app/code/Vendor/Diagnostics/" >> .git/info/exclude
修改您的供应商/诊断模块,在那里添加您的代码,使用 X 调试器...