Migrate laminas-console to laminas-cli (detect console-request)

Hello

my steps:

  1. upgraded ZF3 to Laminas
  2. going to migrate from laminas-console/laminas-mvc-console to laminas-cli
  3. in ZF3 i used code to separate http and console request like:
  • $request instanceof \Laminas\Console\Request
  • \Laminas\Console::isConsole()

AFAIS now in laminas-sources i don’t see entity ConsoleRequest… it seems it deprecated?

what is suggested new way to detect the same using Laminas?

thanks!

Is not needed anymore. If you are in a command class, you are already in the console environment.
Controllers and actions are obsolete for CLI.

1 Like

thanks for the response!

@froschdesign
returning with the same question :).

i need to detect strictly what is the type of current mode (cli or web) in several places of laminas.

for example ServiceManager builds FooService using FooServiceFactory differently dependently to web or cli mode.

OR for example modules.config.php

$laminasModules = [
	'Laminas\Cache',
	'Laminas\Db',
        ...
];

# cli mode
if ( CONSOLE_REQUEST ) {
	$appModules = [
		'Application',
		'ConsoleModule',
                ...
	];
} # Default web mode
else {
	$appModules = [
		'Application',
                'WebModule',
               ...
	];
}

return array_merge( $laminasModules, $appModules );

history:

  1. on zend 2 i used things described on 1st post

  2. on laminas i migrated to own env-variable CONSOLE_REQUEST = PHP_SAPI == 'cli';, because didn’t find any better way

  3. then i added tests for web-mode (PHPUnit and Codeception later), but they underhood work as cli-mode, so i added additional env-variable like PHPUNIT_RUNNING:

if ( ! defined( 'PHPUNIT_RUNNING' ) ) {
	$executable = $_SERVER['argv'][0] ?? '';

	define( 'PHPUNIT_RUNNING',
		str_ends_with( $executable, 'phpunit' ) ||
		str_ends_with( $executable, 'codeception.php' ) ||
		str_ends_with( $executable, 'codecept' )
	);
}

so for now:
CONSOLE_REQUEST = PHP_SAPI == 'cli' && !PHPUNIT_RUNNING;

  1. BUT now i am realizing that i need to test also cli-things implemented using laminas-cli-commands. so it is cli-mode WITH PHPUnit/Codeception and therefore CONSOLE_REQUEST-variable should have some additional logic to achieve such additional detection. something like:

CONSOLE_REQUEST = PHP_SAPI == 'cli' && !PHPUNIT_RUNNING || PHPUNIT_RUNNING && PHPUNIT_CLI;
where PHPUNIT_CLI-variable should be enabled before each test for cli-logic and disabled after it.

looks very scary.

so we have next cases:
a) web-mode without PHPUnit/Codeception
b) cli-mode without PHPUnit/Codeception
c) web-mode with PHPUnit/Codeception
d) cli-mode with PHPUnit/Codeception

what is suggested way to to differentiate all these cases in simple and clear way?

any suggestion?
thanks