Skip to content

Commit

Permalink
Update PubSub to 3.0
Browse files Browse the repository at this point in the history
css: add a couple more resets
Discord, Slack, & Teams routes: assert necessary config
new `Utility/Php::getDebugType` method
  • Loading branch information
bkdotcom committed Jul 17, 2023
1 parent a656ff6 commit 037f98f
Show file tree
Hide file tree
Showing 42 changed files with 811 additions and 358 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
"replace": {
"bdk/backtrace": "2.1.1",
"bdk/curl-http-message": "1.0",
"bdk/errorhandler": "3.2",
"bdk/errorhandler": "3.3",
"bdk/http-message": "1.0",
"bdk/promise": "1.0",
"bdk/pubsub": "2.4",
"bdk/pubsub": "3.0",
"bdk/slack": "1.0",
"bdk/teams": "1.0",
"psr/http-message": "1.0.1"
Expand Down
6 changes: 4 additions & 2 deletions src/CurlHttpMessage/Handler/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ public function append($values)
|| $value instanceof Exception
|| \is_callable($value);
if ($isValid === false) {
$type = \is_object($value) ? \get_class($value) : \gettype($value);
throw new InvalidArgumentException('Expected a Response, Promise, Throwable or callable. ' . $type . ' provided');
throw new InvalidArgumentException(\sprintf(
'Expected a Response, Promise, Throwable or callable. %s provided',
\is_object($value) ? \get_class($value) : \gettype($value)
));
}
$this->queue[] = $value;
}
Expand Down
18 changes: 16 additions & 2 deletions src/CurlHttpMessage/HandlerStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function remove($remove)
if (\is_string($remove) === false && \is_callable($remove) === false) {
throw new InvalidArgumentException(\sprintf(
__METHOD__ . ' requires a string or callable. %s provided',
\gettype($remove)
self::getDebugType($remove)
));
}
$index = \is_callable($remove) ? 0 : 1;
Expand Down Expand Up @@ -172,7 +172,7 @@ private function assertName($name)
if (\is_string($name) === false) {
throw new InvalidArgumentException(\sprintf(
'Name should be a string. %s provided',
\gettype($name)
self::getDebugType($name)
));
}
$found = \array_filter($this->stack, static function ($callableAndName) use ($name) {
Expand Down Expand Up @@ -202,6 +202,20 @@ private function findByName($name)
throw new RuntimeException('Middleware not found: ' . $name);
}

/**
* Gets the type name of a variable in a way that is suitable for debugging
*
* @param mixed $value The value being type checked
*
* @return string
*/
protected static function getDebugType($value)
{
return \is_object($value)
? \get_class($value)
: \gettype($value);
}

/**
* Splices a function into the middleware list at a specific position.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Debug/Framework/Yii1_1/ErrorLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ private function logIgnoredErrors()
private function republishShutdown()
{
$eventManager = $this->debug->eventManager;
foreach ($eventManager->getSubscribers(EventManager::EVENT_PHP_SHUTDOWN) as $callable) {
foreach ($eventManager->getSubscribers(EventManager::EVENT_PHP_SHUTDOWN) as $subscriberInfo) {
$callable = $subscriberInfo['callable'];
$eventManager->unsubscribe(EventManager::EVENT_PHP_SHUTDOWN, $callable);
if (\is_array($callable) && $callable[0] === $this->debug->errorHandler) {
break;
Expand Down
3 changes: 2 additions & 1 deletion src/Debug/Framework/Yii2/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ public function onErrorLow(Error $error)
// exit within shutdown procedure (that's us) = immediate exit
// so... unsubscribe the callables that have already been called and
// re-publish the shutdown event before calling yii's error handler
foreach ($this->debug->rootInstance->eventManager->getSubscribers(EventManager::EVENT_PHP_SHUTDOWN) as $callable) {
foreach ($this->debug->rootInstance->eventManager->getSubscribers(EventManager::EVENT_PHP_SHUTDOWN) as $subscriberInfo) {
$callable = $subscriberInfo['callable'];
$this->debug->rootInstance->eventManager->unsubscribe(EventManager::EVENT_PHP_SHUTDOWN, $callable);
if (\is_array($callable) && $callable[0] === $this->debug->rootInstance->errorHandler) {
break;
Expand Down
4 changes: 1 addition & 3 deletions src/Debug/Method/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ public function doTrace(LogEntry $logEntry)
if (\is_string($caption) === false) {
$this->debug->warn(\sprintf(
'trace caption should be a string. %s provided',
\is_object($caption)
? \get_class($caption)
: \gettype($caption)
$this->debug->php->getDebugType($caption)
));
$logEntry->setMeta('caption', 'trace');
}
Expand Down
2 changes: 2 additions & 0 deletions src/Debug/Plugin/CustomMethod/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ public function setErrorCaller($caller = null)
/**
* Dump values to output
*
* Similar to php's `var_dump()`. Dump values immediately
*
* @param mixed $arg,... message / values
*
* @return void
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Plugin/CustomMethod/ReqRes.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function writeToResponse($response)
}
throw new InvalidArgumentException(\sprintf(
'writeToResponse expects ResponseInterface or HttpFoundationResponse, but %s provided',
\is_object($response) ? \get_class($response) : \gettype($response)
$this->debug->php->getDebugType($response)
));
}

Expand Down
10 changes: 6 additions & 4 deletions src/Debug/Plugin/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ private function assertPlugin($plugin)
return;
}
$backtrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$type = \is_object($plugin)
? \get_class($plugin)
: \gettype($plugin);
throw new InvalidArgumentException($backtrace[1]['function'] . ' expects \\bdk\\Debug\\AssetProviderInterface and/or \\bdk\\PubSub\\SubscriberInterface. ' . $type . ' provided');
throw new InvalidArgumentException(\sprintf(
'%s expects %s. %s provided',
$backtrace[1]['function'],
'\\bdk\\Debug\\AssetProviderInterface and/or \\bdk\\PubSub\\SubscriberInterface',
$this->debug->php->getDebugType($plugin)
));
}
}
76 changes: 49 additions & 27 deletions src/Debug/Route/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use bdk\Debug;
use bdk\ErrorHandler;
use bdk\ErrorHandler\Error;
use RuntimeException;

/**
* Send critical errors to Discord
Expand Down Expand Up @@ -80,39 +81,23 @@ public function onError(Error $error)
}

/**
* Send message to Discord
*
* @param array $message Discord message
* Validate configuration values
*
* @return void
*/
protected function sendMessage(array $message)
{
$client = $this->getClient();
$client->post(
$this->cfg['webhookUrl'],
array(
'Content-Type' => 'application/json; charset=utf-8',
),
$message
);
}

/**
* Return CurlHttpMessage
*
* @return CurlHttpMessageClient
* @throws RuntimeException
*/
protected function getClient()
private function assertCfg()
{
if ($this->client) {
return $this->client;
}
$this->client = new CurlHttpMessageClient();
if (\is_callable($this->cfg['onClientInit'])) {
\call_user_func($this->cfg['onClientInit'], $this->client);
if ($this->cfg['webhookUrl']) {
return;
}
return $this->client;
throw new RuntimeException(\sprintf(
'%s: missing config value: %s. Also tried env-var: %s',
__CLASS__,
'webhookUrl',
'DISCORD_WEBHOOK_URL'
));
}

/**
Expand All @@ -138,4 +123,41 @@ private function buildMessage(Error $error)
. $error['file'] . ' (line ' . $error['line'] . ')',
);
}

/**
* Return CurlHttpMessage
*
* @return CurlHttpMessageClient
*/
protected function getClient()
{
if ($this->client) {
return $this->client;
}
$this->assertCfg();
$this->client = new CurlHttpMessageClient();
if (\is_callable($this->cfg['onClientInit'])) {
\call_user_func($this->cfg['onClientInit'], $this->client);
}
return $this->client;
}

/**
* Send message to Discord
*
* @param array $message Discord message
*
* @return void
*/
protected function sendMessage(array $message)
{
$client = $this->getClient();
$client->post(
$this->cfg['webhookUrl'],
array(
'Content-Type' => 'application/json; charset=utf-8',
),
$message
);
}
}
83 changes: 57 additions & 26 deletions src/Debug/Route/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use bdk\Slack\SlackApi;
use bdk\Slack\SlackMessage;
use bdk\Slack\SlackWebhook;
use RuntimeException;

/**
* Send critical errors to Slack
Expand Down Expand Up @@ -85,49 +86,45 @@ public function onError(Error $error)
}

/**
* Send message(s) to Slack
*
* @param SlackMessage[] $messages Slack messages
* Validate configuration values
*
* @return void
*/
protected function sendMessages(array $messages)
{
$ts = null;
foreach ($messages as $message) {
$message = $message->withValue('thread_ts', $ts);
$response = $this->sendMessage($message);
$ts = $ts ?: $response['ts'];
}
}

/**
* Send message to Slack
*
* @param SlackMessage $message Slack messages
*
* @return array
* @throws RuntimeException
*/
protected function sendMessage(SlackMessage $message)
private function assertCfg()
{
$slackClient = $this->getSlackClient();
if ($slackClient instanceof SlackApi) {
$message = $message->withChannel($this->cfg['channel']);
return $slackClient->chatPostMessage($message);
if (\in_array($this->cfg['use'], array('auto', 'api', 'webhook'), true) === false) {
throw new RuntimeException(\sprintf(
'%s: Invalid cfg value. `use` must be one of "auto", "api", or "webhook"',
__CLASS__
));
}
return $slackClient->post($message);
if ($this->cfg['token'] && $this->cfg['channel']) {
return;
}
if ($this->cfg['webhookUrl']) {
return;
}
throw new RuntimeException(\sprintf(
'%s: missing config value(s). Must configure %s. Or define equivalent environment variable(s) (%s)',
__CLASS__,
'token+channel or webhookUrl',
'SLACK_TOKEN, SLACK_CHANNEL, SLACK_WEBHOOK_URL'
));
}

/**
* Return SlackApi or SlackWebhook client depending on what config provided
*
* @return SlackApi|SlackWebhook
*/
protected function getSlackClient()
protected function getClient()
{
if ($this->slackClient) {
return $this->slackClient;
}
$this->assertCfg();
$use = $this->cfg['use'];
if ($use === 'auto') {
$use = $this->cfg['token'] && $this->cfg['channel']
Expand Down Expand Up @@ -203,4 +200,38 @@ private function buildMessageBacktrace(Error $error)
->withHeader('Backtrace')
->withSection(\implode("\n", $frames));
}

/**
* Send message(s) to Slack
*
* @param SlackMessage[] $messages Slack messages
*
* @return void
*/
protected function sendMessages(array $messages)
{
$ts = null;
foreach ($messages as $message) {
$message = $message->withValue('thread_ts', $ts);
$response = $this->sendMessage($message);
$ts = $ts ?: $response['ts'];
}
}

/**
* Send message to Slack
*
* @param SlackMessage $message Slack messages
*
* @return array
*/
protected function sendMessage(SlackMessage $message)
{
$slackClient = $this->getClient();
if ($slackClient instanceof SlackApi) {
$message = $message->withChannel($this->cfg['channel']);
return $slackClient->chatPostMessage($message);
}
return $slackClient->post($message);
}
}
Loading

0 comments on commit 037f98f

Please sign in to comment.