1. Go to this page and download the library: Download bottledcode/durable-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bottledcode / durable-php example snippets
# SendEmailActivity.php
class SendEmailActivity {
public function __invoke(string $to, string $subject, string $body) {
// Send email
}
}
# UploadEntityInterface.php
interface UploadEntityInterface {
public function getFileUrl(): string;
public function setProcessState(string $state): void;
public function getProcessState(): string;
}
# UploadEntity.php
class UploadEntity extends \Bottledcode\DurablePhp\State\EntityState implements UploadEntityInterface {
public function __construct(private string $url, private string $state = 'pending') {}
public function setProcessState(string $state): void {
$this->state = $state;
}
public function getProcessState(): string {
return $this->state;
}
}
# UploadOrchestration.php
class UploadOrchestration {
public function __construct(private \Bottledcode\DurablePhp\OrchestrationContextInterface $context) {}
public function __invoke(string $url) {
$ctx = $this->context();
$uploadId = $ctx->newUuid();
// get a future that will be resolved when the upload is processed
$signal = $context->waitForExternalEvent('upload-processed');
// get a future that will be resolved when the timer expires (one hour from now)
$timeout = $context->createTimer($context->getCurrentTime()->add(new DateInterval('PT1H')))
// wait for the upload to be processed or the timer to expire
$winner = $context->waitAny($signal, $timeout);
if($winner === $signal) {
// upload was processed
$ctx->signal($uploadId, fn(UploadEntity $entity) => $entity->setProcessState('processed'));
$context->callActivity(SendEmailActivity::class, ['to' => 'user', 'subject' => 'Upload processed', 'body' => 'Your upload was processed']);
} else {
// upload was not processed
$ctx->signal($uploadId, fn(UploadEntity $entity) => $entity->setProcessState('timed-out'));
$context->callActivity(SendEmailActivity::class, ['to' => 'admin', 'subject' => 'Upload failed', 'body' => 'The upload timed out']);
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.