| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WordPress\Plugin_Check\Checker\Checks |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Checker; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class to run checks on a plugin. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
final class Checks { |
| 18 |
|
| 19 |
/** |
| 20 |
* Array of all available Checks. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $checks; |
| 26 |
|
| 27 |
/** |
| 28 |
* Runs checks against the plugin. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* |
| 32 |
* @param Check_Context $context The check context for the plugin to be checked. |
| 33 |
* @param array $checks An array of Check objects to run. |
| 34 |
* @return Check_Result Object containing all check results. |
| 35 |
* |
| 36 |
* @throws Exception Thrown when check fails with critical error. |
| 37 |
*/ |
| 38 |
public function run_checks( Check_Context $context, array $checks ) { |
| 39 |
$result = new Check_Result( $context ); |
| 40 |
|
| 41 |
// Run the checks. |
| 42 |
array_walk( |
| 43 |
$checks, |
| 44 |
function ( Check $check ) use ( $result ) { |
| 45 |
$this->run_check_with_result( $check, $result ); |
| 46 |
} |
| 47 |
); |
| 48 |
|
| 49 |
return $result; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Runs a given check with the given result object to amend. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* |
| 57 |
* @param Check $check The check to run. |
| 58 |
* @param Check_Result $result The result object to amend. |
| 59 |
* |
| 60 |
* @throws Exception Thrown when check fails with critical error. |
| 61 |
*/ |
| 62 |
private function run_check_with_result( Check $check, Check_Result $result ) { |
| 63 |
// If $check implements Preparation interface, ensure the preparation and clean up is run. |
| 64 |
if ( $check instanceof Preparation ) { |
| 65 |
$cleanup = $check->prepare(); |
| 66 |
|
| 67 |
try { |
| 68 |
$check->run( $result ); |
| 69 |
} catch ( Exception $e ) { |
| 70 |
// Run clean up in case of any exception thrown from check. |
| 71 |
$cleanup(); |
| 72 |
throw $e; |
| 73 |
} |
| 74 |
|
| 75 |
$cleanup(); |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Otherwise, just run the check. |
| 80 |
$check->run( $result ); |
| 81 |
} |
| 82 |
} |
| 83 |
|