Skip to content

Commit 5500c46

Browse files
committed
Handle timeouts and other failures. Don't cache default responses
1 parent 4884eb0 commit 5500c46

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

config/torchlight.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<?php
22

33
return [
4+
// The Torchlight client caches highlighted code blocks. Here
5+
// you can define which cache driver you'd like to use.
46
'cache' => env('TORCHLIGHT_CACHE_DRIVER', 'default'),
57

6-
'bust' => 1,
7-
8+
// Which theme you want to use. You can find all of the themes at
9+
// https://torchlight.dev/themes, or you can provide your own.
810
'theme' => 'material-theme-palenight',
911

12+
// Your API token from torchlight.dev.
1013
'token' => env('TORCHLIGHT_TOKEN'),
1114

15+
// If you want to register the blade directives, set this to true.
1216
'blade_directives' => true,
1317
];

src/Client.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ public function highlight($blocks)
3737

3838
protected function request(Collection $blocks)
3939
{
40-
// @TODO handle timeout
41-
$response = Http::timeout(10)
40+
$response = Http::timeout(5)
4241
->withToken(config('torchlight.token'))
4342
->post('https://torchlight.dev/api/highlight', [
4443
'blocks' => $this->blocksAsRequestParam($blocks)->values(),
4544
])
4645
->json();
4746

48-
$response = collect($response['blocks'])->keyBy('id');
47+
$response = collect(Arr::get($response, 'blocks', []))->keyBy('id');
4948

5049
$blocks->each(function (Block $block) use ($response) {
5150
$block->setHtml(
5251
$block->html ?? $this->getHtmlFromResponse($response, $block)
5352
);
5453
});
5554

56-
$this->setCacheFromBlocks($blocks);
55+
// Only store the ones we got back from the API.
56+
$this->setCacheFromBlocks($blocks, $response->keys());
5757

5858
return $blocks;
5959
}
@@ -86,18 +86,16 @@ protected function blocksAsRequestParam(Collection $blocks)
8686
});
8787
}
8888

89-
protected function setCacheFromBlocks(Collection $blocks)
89+
protected function setCacheFromBlocks(Collection $blocks, Collection $ids)
9090
{
91-
return $blocks->each(function (Block $block) {
92-
if (!$block->html) {
93-
return;
91+
$blocks->only($ids)->each(function (Block $block) use ($ids) {
92+
if ($block->html) {
93+
$this->cache()->put(
94+
$this->cacheKey($block),
95+
$block->html,
96+
now()->addDays(7)
97+
);
9498
}
95-
96-
$this->cache()->put(
97-
$this->cacheKey($block),
98-
$block->html,
99-
now()->addDays(30)
100-
);
10199
});
102100
}
103101

@@ -132,6 +130,6 @@ protected function getHtmlFromResponse(Collection $response, Block $block)
132130
*/
133131
protected function defaultHtml(Block $block)
134132
{
135-
return "<pre><code>" . $block->code . "</code></pre>";
133+
return "<pre class='torchlight'><code>" . htmlentities($block->code) . "</code></pre>";
136134
}
137135
}

tests/BaseTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aaron@hammerstone.dev|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Hammerstone\Torchlight\Tests;
7+
8+
use Hammerstone\Torchlight\TorchlightServiceProvider;
9+
use Orchestra\Testbench\TestCase;
10+
11+
abstract class BaseTest extends TestCase
12+
{
13+
protected function getPackageProviders($app)
14+
{
15+
return [
16+
TorchlightServiceProvider::class
17+
];
18+
}
19+
}

0 commit comments

Comments
 (0)