Dynamically disable n98-magerun commands

In some cases you like to disable your commands i.e if you are running n98-magerun on a production machine or if it doesn’t support the entire operating system.
The symfony command component have a simple solution for that. All commands are extended from the symfony command base class Symfony\Component\Console\Command which defines the method
isEnabled for that. The method must return a boolean value.

If you want to disable a command for older n98-magerun versions (interesting for module developers)
you can do it like this:

/**
 * @return bool
 */
public function isEnabled()
{
    return version_compare($this->getApplication()->getVersion(), '1.74.1', '>=');
}    

If you want to disable a command for a operating system you can use a utility class which is available inside of n98-magerun (We will provide a additional post for the utility classes in the future).

use N98\Util\OperatingSystem

//...

/**
 * @return bool
 */
public function isEnabled()
{
    return !OperatingSystem::isWindows();
}

Or the hostname (here hardcoded):

/**
 * @return bool
 */
public function isEnabled()
{
    return \gethostname() == 'myproduction_hostname';
}

You can see it’s pretty simple to disable a command.

Happy coding!