Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Last active December 10, 2024 21:39
Show Gist options
  • Save bhubbard/5f68f9d7dca7c8e6eaa3ac537414ea39 to your computer and use it in GitHub Desktop.
Save bhubbard/5f68f9d7dca7c8e6eaa3ac537414ea39 to your computer and use it in GitHub Desktop.
Concept The “Impulse Programming Pattern” is designed for tasks that require a reactive, single-trigger response to changes in application state or environment variables. Unlike traditional observer patterns that continuously monitor state changes, the impulse pattern is event-specific, running a predefined set of actions the moment an impulse (…
<?php
/*
The Cascade Isolation Pattern is designed to manage a chain of dependent operations where the execution of each step in the chain is isolated from the others unless explicitly passed a “success state.” This pattern ensures that downstream operations in a chain won’t execute unless the preceding steps complete successfully.
This is particularly useful in applications requiring tightly controlled workflows, like multi-step transaction processing, data pipelines, or sequential validations.
*/
class CascadeStep {
private $callback;
private $isolation = true; // Default to isolation unless overridden
public function __construct(callable $callback, bool $isolation = true) {
$this->callback = $callback;
$this->isolation = $isolation;
}
public function execute($input) {
if (!$this->isolation) {
return ($this->callback)($input); // Execute without checking input validity
}
// For isolated steps, execute only if input is valid
if ($input['status'] === 'success') {
return ($this->callback)($input);
} else {
return [
'status' => 'failure',
'message' => 'Previous step failed. Cascade halted.'
];
}
}
}
class Cascade {
private $steps = [];
public function addStep(CascadeStep $step) {
$this->steps[] = $step;
}
public function execute($initialInput) {
$result = $initialInput;
foreach ($this->steps as $step) {
$result = $step->execute($result);
if ($result['status'] !== 'success') {
break; // Stop the cascade if a step fails
}
}
return $result;
}
}
// Example Usage:
// Define a multi-step process
$step1 = new CascadeStep(function($input) {
echo "Step 1: Validating data...\n";
if ($input['data'] === 'valid') {
return ['status' => 'success', 'message' => 'Step 1 successful'];
}
return ['status' => 'failure', 'message' => 'Invalid data in Step 1'];
});
$step2 = new CascadeStep(function($input) {
echo "Step 2: Processing data...\n";
return ['status' => 'success', 'message' => 'Step 2 successful'];
});
$step3 = new CascadeStep(function($input) {
echo "Step 3: Finalizing...\n";
return ['status' => 'success', 'message' => 'All steps completed'];
});
// Create a cascade
$cascade = new Cascade();
$cascade->addStep($step1);
$cascade->addStep($step2);
$cascade->addStep($step3);
// Run the cascade
$input = ['status' => 'success', 'data' => 'valid'];
$result = $cascade->execute($input);
print_r($result); // Should output the success message of Step 3
// Run with invalid input
$invalidInput = ['status' => 'success', 'data' => 'invalid'];
$result = $cascade->execute($invalidInput);
print_r($result); // Should halt at Step 1 with failure message
<?php
class Impulse {
private $triggered = false;
private $callback;
public function __construct(callable $callback) {
$this->callback = $callback;
}
public function trigger() {
if (!$this->triggered) {
($this->callback)();
$this->triggered = true;
} else {
echo "Impulse already triggered and cannot be reactivated. Reset it if needed.\n";
}
}
public function reset() {
$this->triggered = false;
}
public function isTriggered(): bool {
return $this->triggered;
}
}
// Example use case
// Create an impulse that initializes database connection settings on first use
$dbImpulse = new Impulse(function() {
echo "Setting up database connection...\n";
// Actual database setup code would go here
});
// Triggering the impulse
$dbImpulse->trigger(); // Output: Setting up database connection...
$dbImpulse->trigger(); // Output: Impulse already triggered and cannot be reactivated. Reset it if needed.
// Resetting the impulse for reuse
$dbImpulse->reset();
$dbImpulse->trigger(); // Output: Setting up database connection...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment