Basic Usage
Performing diagnostics with Zend Framework 2
- 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 Zend Framework 3; use the plain PHP diagnostics instructions below when using that framework version.
Using diagnostics with Symfony 2
- Install the LiipMonitorBundle.
- Enable diagnostic tests in your application configuration.
- In your console type
./app/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
ZendDiagnostics\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 ZendDiagnostics\Check;
use ZendDiagnostics\Runner\Runner;
use ZendDiagnostics\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 ZendDiagnostics\Result\Collection, even when no reporter is attached. This collection contains results for all tests and failure counters.
As an example:
<?php
use ZendDiagnostics\Check;
use ZendDiagnostics\Result;
use ZendDiagnostics\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";
}
Found a mistake or want to contribute to the documentation? Edit this page on GitHub!