PHP code example of jaroslawzielinski / diagnostics-m2

1. Go to this page and download the library: Download jaroslawzielinski/diagnostics-m2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jaroslawzielinski / diagnostics-m2 example snippets




declare(strict_types=1);

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Diagnostics',
    __DIR__
);




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...
}




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';
    }
}




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);
    }
}




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...
}



    /** @var \Vendor\Diagnostics\Block\Adminhtml\Test $block */
    // TODO: override Your way...



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);
    }
}




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...
}



    /** @var \Vendor\Diagnostics\Block\Test $block */
    // TODO: override Your way...
shell
echo "app/code/Vendor/Diagnostics/" >> .git/info/exclude
shell
echo "app/code/Vendor/Diagnostics/" >> .git/info/exclude