On this page
Basic Usage
Performing diagnostics with Laminas
- Install the ZFTool module.
- Enable diagnostic tests in your application config.php.
- In your console type
php public/index.php diag
to run diagnostics.
Note: this does not work with Laminas MVC version 3; use the plain PHP diagnostics instructions below when using that framework version.
Using diagnostics with Symfony
- Install the LiipMonitorBundle.
- Enable diagnostic tests in your application configuration.
- In your console type
./bin/console monitor:health
to run diagnostics.
Using diagnostics with PSR-7 middleware
Install the rstgroup/diagnostics-middleware.
Using diagnostics in plain PHP
- Create an instance of
Laminas\Diagnostics\Runner
. - Add tests using
Runner::addTest()
. - Optionally add a reporter to display progress using
Runner::addReporter()
. - Run diagnostics
Runner::run()
.
For example:
<?php
// run_diagnostics.php
use Laminas\Diagnostics\Check;
use Laminas\Diagnostics\Runner\Runner;
use Laminas\Diagnostics\Runner\Reporter\BasicConsole;
include 'vendor/autoload.php';
// Create Runner instance
$runner = new Runner();
// Add checks
$runner->addCheck(new Check\DirWritable('/tmp'));
$runner->addCheck(new Check\DiskFree(100000000, '/tmp'));
// Add console reporter
$runner->addReporter(new BasicConsole(80, true));
// Run all checks
$results = $runner->run();
// Emit an appropriate exit code
$status = ($results->getFailureCount() + $results->getWarningCount()) > 0 ? 1 : 0;
exit($status);
You can now run the file in your console (command line):
$ php run_diagnostics.php
Starting diagnostics:
..
OK (2 diagnostic tests)
Using a result collection
The diagnostics runner will always return a Laminas\Diagnostics\Result\Collection, even when no reporter is attached. This collection contains results for all tests and failure counters.
As an example:
<?php
use Laminas\Diagnostics\Check;
use Laminas\Diagnostics\Result;
use Laminas\Diagnostics\Runner\Runner;
$runner = new Runner();
$checkSpace = new Check\DiskFree(100000000, '/tmp');
$checkTemp = new Check\DirWritable('/tmp');
$runner->addCheck($checkSpace);
$runner->addCheck($checkTemp);
// Run all checks
$results = $runner->run();
echo "Number of successful tests: " . $results->getSuccessCount() . "\n";
echo "Number of failed tests: " . $results->getFailureCount() . "\n";
if ($results[$checkSpace] instanceof Result\FailureInterface) {
echo "Oooops! We're running out of space on temp.\n";
}
if ($results[$checkTemp] instanceof Result\FailureInterface) {
echo "It seems that /tmp is not writable - this is a serious problem!\n";
}