Add your own check to sys:check command

Published by Christian Münch on

For two days we published the new n98-magerun version 1.90.0 with a refactored sys:check command.
The command is now modular and all checks are organized in sub-classes.
It’s now possible to add own checks to the command. The blog post shows you how easy it is.

Registration of Check Classes

If you investigate the distribution config of n98-magerun you can see a bunch of new configuration for the command.
The new config node is “checks” which contains the class names of all checks. All classes are organized in check groups.

Check Class Config

If you look to the output of the sys:check command you can see who every check group is rendered.

check_group_output

If you need a new checkgroup you can define a new one in your config. This can be done per project or as n98-magerun module.

If you need to check all store or websites you can use the StoreCheck or WebsiteCheck interfaces.
The sys:check command will look a websites,stores and create an instance of your check command for every store/website.
The objects are injected to your object.

Example config for a dummy module:

autoloaders:
  Acme\MyModule: %module%/src

commands:
  N98\Magento\Command\System\CheckCommand::
    checks:
      settings:
         - Acme\MyModule\MyPseudoCheck

Your first check class

A check class is very simple. We offer three PHP interfaces for different check.

  • N98\Magento\Command\System\Check\SimpleCheck
  • N98\Magento\Command\System\Check\StoreCheck
  • N98\Magento\Command\System\Check\WebsiteCheck

SimpleCheck Interface

storecheck_interface

websitecheck_interface

As you can see you must only implement a method with the name check.
First we investigate the SimpleCheck interface. There is a single parameter $result of type N98\Magento\Command\System\Check\ResultCollection. We work with dependency injection and inject the result collection (with all check results of other command) into every check class.
It’s now the job of every check class to add one or more check results to the result collection.
Every check must contain a output message.
Please set the message with setMessage to every result.

How can i add a result?

It’s pretty simple…

/**
 * @param ResultCollection $results
 */
public function check(ResultCollection $results)
{        
    $result = $results->createResult();
    // my check ...            
    $result->setStatus(Result::STATUS_ERROR);
}

A check result can have three states:

  • OK
  • Error
  • Warning

The result class has three constants for that:

  • Result::STATUS_OK
  • Result::STATUS_ERROR
  • Result::STATUS_WARNING

That’s all to create our first check class.

I need the command config

In some cases we need the command config to configure a check. A good example is the folder check which can be extended by adding new folder names by configuration.
What can we do in this case? The check class is not the command class. We have no direct access to the n98-magerun config system.
The simple solution in this case is again: Dependency Injection.

If you need the config you must only add a second interface “CommandConfigAware” to your check class and we give your check class the config array of the check command.
We have another interface (CommandAware) if you need the command itself.

Please look at the FileCheck if you need an example for both interfaces:

<?php

namespace N98\Magento\Command\System\Check\Filesystem;

use N98\Magento\Command\CommandAware;
use N98\Magento\Command\CommandConfigAware;
use N98\Magento\Command\System\Check\Result;
use N98\Magento\Command\System\Check\ResultCollection;
use N98\Magento\Command\System\Check\SimpleCheck;
use N98\Magento\Command\System\CheckCommand;
use Symfony\Component\Console\Command\Command;

class FilesCheck implements SimpleCheck, CommandAware, CommandConfigAware
{
    /**
    * @var array
    */
    protected $_commandConfig;

    /**
    * @var CheckCommand
    */
    protected $_checkCommand;

    /**
    * @param ResultCollection $results
    */
    public function check(ResultCollection $results)
    {
        $files = $this->_commandConfig['filesystem']['files'];
        $magentoRoot = $this->_checkCommand->getApplication()->getMagentoRootFolder();

        foreach ($files as $file => $comment) {
            $result = $results->createResult();

            if (file_exists($magentoRoot . DIRECTORY_SEPARATOR . $file)) {
                $result->setStatus(Result::STATUS_OK);
                $result->setMessage("<info>File <comment>" . $file . "</comment> found.</info>");
            } else {

                $result->setStatus(Result::STATUS_ERROR);
                $result->setMessage("<error>File " . $file . " not found!</error><comment> Usage: " . $comment . "</comment>");
            }
        }
    }

    /**
    * @param array $commandConfig
    */
    public function setCommandConfig(array $commandConfig)
    {
        $this->_commandConfig = $commandConfig;
    }

    /**
    * @param Command $command
    */
    public function setCommand(Command $command)
    {
        $this->_checkCommand = $command;
    }
}

You need more examples?

We deployed an example n98-magerun module for you with some dummy checks…

https://github.com/netz98/n98-magerun-syscheck-example

Conclusion / What can i do?

The new system gives you the chance to create new checks for your system.
You have a special environment? Write a check for that! So every developer
must not investigate what’s wrong on the local system.

You work in a Magento agency and you audit foreign Magento stores?
Automate your work and write a check in your own n98-magerun module. So you can resuse
the check everytime you need it.

You do many QA before every store launch? Automate the checks and put them in
your QA n98-magerun module.

I hope this feature can help you to optimize the quality in your company and saves
you many time to enjoy your life 🙂

Categories: MagerunTips

1 Comment

Magento-Neuigkeiten der Wochen 27/28 2014 - Matthias Zeis · July 9, 2018 at 21:22

[…] sowie unterschiedliche Ausgabearten des Ergebnisses (z.B. als CSV) mit. Im Blogpost wird erklärt, wie man eigene Checks schreibt. Außerdem gibt es einen Beispiel-Check. Außerdem validiert ein neuer Befehl […]

Leave a Reply to Magento-Neuigkeiten der Wochen 27/28 2014 - Matthias Zeis Cancel reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.