Skip to content

Commit b471b35

Browse files
committed
Add post-processors
1 parent 9b8ea3c commit b471b35

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed

src/Contracts/PostProcessor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Torchlight\Contracts;
7+
8+
use Torchlight\Block;
9+
10+
interface PostProcessor
11+
{
12+
public function process(Block $block);
13+
}

src/Manager.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
namespace Torchlight;
77

8+
use Exception;
89
use Illuminate\Contracts\Cache\Repository;
910
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Facades\Cache;
1112
use Illuminate\Support\Str;
1213
use Illuminate\Support\Traits\Macroable;
14+
use Torchlight\Contracts\PostProcessor;
15+
use Torchlight\Exceptions\ConfigurationException;
1316

1417
class Manager
1518
{
@@ -35,6 +38,11 @@ class Manager
3538
*/
3639
protected $environment;
3740

41+
/**
42+
* @var array
43+
*/
44+
protected $postProcessors = [];
45+
3846
/**
3947
* @param Client $client
4048
* @return Manager
@@ -64,7 +72,15 @@ public function client()
6472
*/
6573
public function highlight($blocks)
6674
{
67-
return $this->client()->highlight($blocks);
75+
$blocks = $this->client()->highlight($blocks);
76+
77+
foreach ($blocks as $block) {
78+
foreach ($this->postProcessors as $processor) {
79+
app($processor)->process($block);
80+
}
81+
}
82+
83+
return $blocks;
6884
}
6985

7086
/**
@@ -83,6 +99,24 @@ public function overrideEnvironment($environment = null)
8399
$this->environment = $environment;
84100
}
85101

102+
/**
103+
* @param array|string $classes
104+
* @throws ConfigurationException
105+
*/
106+
public function addPostProcessors($classes)
107+
{
108+
$classes = Arr::wrap($classes);
109+
110+
foreach ($classes as $class) {
111+
if (!in_array(PostProcessor::class, class_implements($class))) {
112+
$class = is_string($class) ? $class : get_class($class);
113+
throw new ConfigurationException("Post-processor '$class' does not implement " . PostProcessor::class);
114+
}
115+
116+
$this->postProcessors[] = $class;
117+
}
118+
}
119+
86120
/**
87121
* Get an item out of the config using dot notation.
88122
*

tests/PostProcessorTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aaron@hammerstone.dev|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Torchlight\Tests;
7+
8+
use Torchlight\Block;
9+
use Torchlight\Contracts\PostProcessor;
10+
use Torchlight\Exceptions\ConfigurationException;
11+
use Torchlight\Exceptions\RequestException;
12+
use Torchlight\Torchlight;
13+
14+
class PostProcessorTest extends BaseTest
15+
{
16+
public function getEnvironmentSetUp($app)
17+
{
18+
config()->set('torchlight', [
19+
'theme' => 'material',
20+
'token' => 'token',
21+
]);
22+
}
23+
24+
/** @test */
25+
public function it_runs_post_processors()
26+
{
27+
$this->fakeSuccessfulResponse('id');
28+
29+
Torchlight::addPostProcessors([
30+
GoodbyePostProcessor::class
31+
]);
32+
33+
$blocks = Torchlight::highlight(
34+
Block::make('id')->language('php')->code('echo "hello world";')
35+
);
36+
37+
$this->assertEquals($blocks[0]->highlighted, '<div class=\'highlighted\'>echo "goodbye world";</div>');
38+
}
39+
40+
/** @test */
41+
public function null_processor_works()
42+
{
43+
$this->fakeSuccessfulResponse('id');
44+
45+
Torchlight::addPostProcessors([
46+
NullPostProcessor::class
47+
]);
48+
49+
$blocks = Torchlight::highlight(
50+
Block::make('id')->language('php')->code('echo "hello world";')
51+
);
52+
53+
$this->assertEquals($blocks[0]->highlighted, '<div class=\'highlighted\'>echo "hello world";</div>');
54+
}
55+
56+
/** @test */
57+
public function they_run_in_order()
58+
{
59+
$this->fakeSuccessfulResponse('id');
60+
61+
Torchlight::addPostProcessors([
62+
GoodbyePostProcessor::class,
63+
GoodbyeCruelPostProcessor::class
64+
]);
65+
66+
$blocks = Torchlight::highlight(
67+
Block::make('id')->language('php')->code('echo "hello world";')
68+
);
69+
70+
$this->assertEquals($blocks[0]->highlighted, '<div class=\'highlighted\'>echo "goodbye cruel world";</div>');
71+
}
72+
73+
/** @test */
74+
public function must_implement_interface()
75+
{
76+
$this->expectException(ConfigurationException::class);
77+
$this->expectExceptionMessage('Post-processor \'Torchlight\Block\' does not implement Torchlight\Contracts\PostProcessor');
78+
79+
Torchlight::addPostProcessors([
80+
Block::class
81+
]);
82+
}
83+
84+
}
85+
86+
class NullPostProcessor implements PostProcessor
87+
{
88+
public function process(Block $block)
89+
{
90+
91+
}
92+
}
93+
94+
class GoodbyePostProcessor implements PostProcessor
95+
{
96+
public function process(Block $block)
97+
{
98+
$block->highlighted = str_replace('hello', 'goodbye', $block->highlighted);
99+
}
100+
}
101+
102+
class GoodbyeCruelPostProcessor implements PostProcessor
103+
{
104+
public function process(Block $block)
105+
{
106+
$block->highlighted = str_replace('goodbye', 'goodbye cruel', $block->highlighted);
107+
}
108+
}

0 commit comments

Comments
 (0)