-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements and additions to AppServers and Deferrers
- Improved `AppServer` implementations: - Added overrideable `init()` method to assist in initialization since constructor is final. - Added support for setting AppCredentials on the base AppServer. - Improved `Deferrer` implementations: - Moved `PreAckDeferrer` to new `Deferral` namespace. - Added `ShellExecDeferrer` to allow deferring via a background `shell_exec` call. - Added `DeferredContextCliServer` for processing deferred `Context`s via CLI. - Fixed initialization bug in `Ack` listener. - Improved `MultiTenantHttpServer` implementation: - Added support for more lenient/intuitive app registrations. - Added `Coerce::application()` helper. - Improved `Auth` components: - Fixed bug in `AuthMiddleware` that validated credentials too early. - Updated `AppCredentials::supports*()` methods. - Added missing `App::withBotToken()` method.
- Loading branch information
1 parent
6310342
commit a650d04
Showing
19 changed files
with
473 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SlackPhp\Framework\Deferral; | ||
|
||
use Closure; | ||
use SlackPhp\Framework\{AppServer, Context, Exception}; | ||
use Throwable; | ||
|
||
/** | ||
* Server implementation meant to be run from the CLI to process a deferred context. | ||
*/ | ||
class DeferredContextCliServer extends AppServer | ||
{ | ||
/** @var string[] */ | ||
private array $args; | ||
private ?Closure $deserializeCallback; | ||
private int $exitCode = 0; | ||
|
||
/** | ||
* @param string[] $args | ||
* @return $this | ||
*/ | ||
public function withArgs(array $args): self | ||
{ | ||
$this->args = $args; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param callable(string): Context $deserializeCallback | ||
* @return $this | ||
*/ | ||
public function withDeserializeCallback(callable $deserializeCallback): self | ||
{ | ||
$this->deserializeCallback = Closure::fromCallable($deserializeCallback); | ||
|
||
return $this; | ||
} | ||
|
||
protected function init(): void | ||
{ | ||
global $argv; | ||
$this->args = $argv ?? []; | ||
} | ||
|
||
public function start(): void | ||
{ | ||
try { | ||
$this->getLogger()->debug('Started processing of deferred context'); | ||
$context = $this->deserializeContext($this->args[1] ?? ''); | ||
$this->getApp()->handle($context); | ||
$this->getLogger()->debug('Completed processing of deferred context'); | ||
} catch (Throwable $exception) { | ||
$this->getLogger()->error('Error occurred during processing of deferred context', compact('exception')); | ||
$this->exitCode = 1; | ||
} | ||
|
||
$this->stop(); | ||
} | ||
|
||
public function stop(): void | ||
{ | ||
if (isset($this->args[2]) && $this->args[2] === '--soft-exit') { | ||
return; | ||
} | ||
|
||
exit($this->exitCode); | ||
} | ||
|
||
private function deserializeContext(string $serializedContext): Context | ||
{ | ||
$fn = $this->deserializeCallback ?? function (string $serializedContext): Context { | ||
if (strlen($serializedContext) === 0) { | ||
throw new Exception('No context provided'); | ||
} | ||
|
||
$data = json_decode(base64_decode($serializedContext), true); | ||
if (empty($data)) { | ||
throw new Exception('Invalid context data'); | ||
} | ||
|
||
$context = Context::fromArray($data); | ||
if (!($context->isAcknowledged() && $context->isDeferred())) { | ||
throw new Exception('Context was not deferred'); | ||
} | ||
|
||
return $context; | ||
}; | ||
|
||
return $fn($serializedContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.