From 637c5c5086eb1b4a089880da9d3f2a8071f4ed60 Mon Sep 17 00:00:00 2001 From: Theo D Date: Fri, 1 Mar 2024 22:11:50 +0100 Subject: [PATCH] ix: Update `fingerprint` function to return execution status --- CHANGELOG.md | 1 + examples/fingerprint.php | 6 +++++- src/functions.php | 16 ++++++++-------- ...ngerprintAndForceTest.php.output_runnable.txt | 1 + 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6649c7d..5111371e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Add a `yaml_dump()` function to dump any PHP value to a YAML string * Add a `yaml_parse()` function to parse a YAML string to a PHP value * Remove the default timeout of 60 seconds from the Context +* Add `bool` return type to `fingerprint()` function to indicate if the callable was run ## 0.13.1 (2024-02-27) diff --git a/examples/fingerprint.php b/examples/fingerprint.php index 929e33b2..cacf843c 100644 --- a/examples/fingerprint.php +++ b/examples/fingerprint.php @@ -47,7 +47,7 @@ function task_with_a_fingerprint_and_force( ): void { run('echo "Hello Task with Fingerprint!"'); - fingerprint( + $hasRun = fingerprint( callback: function () { run('echo "Cool, no fingerprint! Executing..."'); }, @@ -55,6 +55,10 @@ function task_with_a_fingerprint_and_force( force: $force // This option will force the task to run even if the fingerprint has not changed ); + if ($hasRun) { + run('echo "Fingerprint has been executed!"'); + } + run('echo "Cool! I finished!"'); } diff --git a/src/functions.php b/src/functions.php index 29e1cf67..19436ce2 100644 --- a/src/functions.php +++ b/src/functions.php @@ -885,16 +885,16 @@ function fingerprint_save(string $fingerprint): void GlobalHelper::getApplication()->fingerprintHelper->postProcessFingerprintForHash($fingerprint); } -function fingerprint(callable $callback, string $fingerprint, bool $force = false): void +function fingerprint(callable $callback, string $fingerprint, bool $force = false): bool { - if (!fingerprint_exists($fingerprint) || $force) { - try { - $callback(); - fingerprint_save($fingerprint); - } catch (\Throwable $e) { - throw $e; - } + if ($force || !fingerprint_exists($fingerprint)) { + $callback(); + fingerprint_save($fingerprint); + + return true; } + + return false; } /** diff --git a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt index 00f0ed61..4eb89b59 100644 --- a/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt +++ b/tests/Examples/Fingerprint/FingerprintTaskWithAFingerprintAndForceTest.php.output_runnable.txt @@ -1,3 +1,4 @@ Hello Task with Fingerprint! Cool, no fingerprint! Executing... +Fingerprint has been executed! Cool! I finished!