Plain CLI scripts with eZScript and ezexec
This page is for PHP developers who maintain command line scripts written as plain files, or who need to run a piece
of PHP with the kernel booted. New commands are written as classes (Write a console command);
the plain form still runs exactly as before, and every class-based command uses the same eZScript underneath, so
knowing it helps either way.
Contents
- The life cycle
- A plain script
- The settings of eZScript::instance()
- eZCLI output
- Running code with the kernel: ezexec.php
- Moving a plain script to a class
The life cycle
| Step | Call | Does |
|---|---|---|
| 1 | eZScript::instance( $settings ) | creates the script object |
| 2 | $script->startup() | checks that it runs on the command line, sets error reporting and the debug handling |
| 3 | $script->getOptions( $config, $argumentConfig, $help ) | parses the command line; prints --help and exits; applies -s, -l, -d, -v, -q |
| 4 | $script->initialize() | loads the siteaccess, the extensions, the session and the database |
| 5 | your work | |
| 6 | $script->shutdown( $exitCode, $exitText ) | writes the debug report if asked, and exits |
Do the option parsing before initialize(), so --help answers without a database.
A plain script
#!/usr/bin/env php
<?php
/**
* @description Counts the items of acmereport
*/
require_once 'autoload.php';
$cli = eZCLI::instance();
$script = eZScript::instance( array( 'description' => "Counts the items of acmereport",
'use-session' => false, 'use-modules' => false, 'use-extensions' => true ) );
$script->startup();
$options = $script->getOptions( '[status:]', '', array( 'status' => 'Only items with this status' ) );
$script->initialize();
$conds = $options['status'] ? array( 'status' => (string)$options['status'] ) : null;
$cli->output( 'Items: ' . eZPersistentObject::count( acmeReportItem::definition(), $conds ) );
$script->shutdown( 0 );Run it from the installation root: php extension/acmereport/bin/php/count.php --status=open, or through the console
as ext:acmereport:count.
The settings of eZScript::instance()
| Key | Meaning |
|---|---|
description | the text of --help |
use-session | start a session (most scripts do not need one) |
use-modules | load the module system (needed to run views or check policies of modules) |
use-extensions | load the active extensions and their settings (almost always true) |
site-access | a fixed siteaccess instead of the default |
eZCLI output
| Call | Writes |
|---|---|
$cli->output( $text ) | a line to standard output; nothing with -q |
$cli->notice( $text ), warning( $text ), error( $text ) | marked lines |
$cli->stylize( 'emphasize', $text ) | text with a terminal style; plain when colors are off |
Running code with the kernel: ezexec.php
To run a PHP file once with the kernel booted (settings, extensions, database), for a diagnosis or a one-time data fix,
pass it to bin/php/ezexec.php:
php bin/php/ezexec.php path/to/your-script.php
php bin/php/ezexec.php tests/bin/bootsmoke.php # how CI boots the kernel on PHP 8.0The file runs with the kernel's classes and settings available; eZDB::instance() connects to the site's database.
Keep such files out of the web root's reach and out of your extension's releases, and give them a name that says what
they do. Anything you run twice belongs in a proper command.
Moving a plain script to a class
- Create
extension/<ext>/classes/runnable/commands/<name>.phpwith a class extendingExponential\Runnable\CommandinExponential\Command\Extension\<Ext>. - Move the code into
run(); replaceeZScript::instance()with$this->script(), the three start steps with$this->startup(),$cli->output()with$this->output(),$script->shutdown()with$this->shutdown(). - Write class names fully qualified (
\eZCLI), because the class is in a namespace. - Reduce the old file to
require_once 'autoload.php';and onemain( __FILE__ )call. - Compare
--helpand the exit codes before and after; the kernel proved its own moves this way (Commands, cronjob parts and module views as classes).
Related pages
- eZScript and eZCLI reference
- CLI scripts with eZScript (application creators)
- CLI scripts and console commands
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.