Skip to content

Commit 638644d

Browse files
committed
Add some tests, badges, and styleci
1 parent d3004fe commit 638644d

File tree

12 files changed

+474
-44
lines changed

12 files changed

+474
-44
lines changed

.github/workflows/tests.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Tests
2+
3+
on: [ push, pull_request ]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
if: "!contains(github.event.head_commit.message, 'ci skip')"
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
php: [ 7.2, 7.3, 7.4, 8.0 ]
13+
laravel: [ 7.*, 8.* ]
14+
dependency-version: [ prefer-lowest, prefer-stable ]
15+
include:
16+
- laravel: 7.*
17+
testbench: 5.*
18+
19+
- laravel: 7.*
20+
dependency-version: prefer-lowest
21+
composer-version: --1
22+
23+
- laravel: 8.*
24+
testbench: 6.*
25+
26+
exclude:
27+
# Laravel 8 requires PHP 7.3.
28+
- laravel: 8.*
29+
php: 7.2
30+
31+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
32+
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v2
36+
37+
- name: Cache dependencies
38+
uses: actions/cache@v2
39+
with:
40+
path: ~/.composer/cache/files
41+
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
42+
43+
- name: Setup PHP
44+
uses: shivammathur/setup-php@v2
45+
with:
46+
php-version: ${{ matrix.php }}
47+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
48+
coverage: none
49+
50+
- name: Install dependencies
51+
run: |
52+
composer self-update ${{ matrix.composer-version }}
53+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
54+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
55+
56+
- name: Execute tests
57+
run: vendor/bin/phpunit

.styleci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
preset: laravel
2+
3+
disabled:
4+
- concat_without_spaces
5+
- not_operator_with_successor_space
6+
- cast_spaces
7+
- trailing_comma_in_multiline_array
8+
- heredoc_to_nowdoc
9+
- phpdoc_summary
10+
11+
risky: false

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Laravel Torchlight Client
2+
3+
[![Tests](https://github.com/torchlight-api/torchlight-laravel/actions/workflows/tests.yml/badge.svg)](https://github.com/torchlight-api/torchlight-laravel/actions/workflows/tests.yml)
4+
5+
[![Latest Stable Version](https://poser.pugx.org/torchlight/laravel/v)](//packagist.org/packages/torchlight/laravel) [![Total Downloads](https://poser.pugx.org/torchlight/laravel/downloads)](//packagist.org/packages/torchlight/laravel) [![License](https://poser.pugx.org/torchlight/laravel/license)](//packagist.org/packages/torchlight/laravel)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"php": "^7.2|^8.0",
1414
"illuminate/support": "^7.0|^8.0",
1515
"illuminate/console": "^7.0|^8.0",
16-
"illuminate/http": "^7.0|^8.0"
16+
"illuminate/http": "^7.0|^8.0",
17+
"guzzlehttp/guzzle": "^7.2"
1718
},
1819
"require-dev": {
1920
"orchestra/testbench": "^5.0|^6.0",

config/torchlight.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
// Which theme you want to use. You can find all of the themes at
99
// https://torchlight.dev/themes, or you can provide your own.
10-
'theme' => 'material-theme-palenight',
10+
'theme' => env('TORCHLIGHT_THEME', 'material-theme-palenight'),
1111

1212
// Your API token from torchlight.dev.
1313
'token' => env('TORCHLIGHT_TOKEN'),
1414

1515
// If you want to register the blade directives, set this to true.
1616
'blade_components' => true,
17+
18+
// The Host of the API.
19+
'host' => env('TORCHLIGHT_HOST', 'https://api.torchlight.dev'),
1720
];

src/Blade/BladeManager.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@ public static function registerBlock(Block $block)
1919
static::$blocks[] = $block;
2020
}
2121

22-
public static function render(Response $response)
22+
public static function renderResponse(Response $response)
2323
{
24-
// Bail early if there are no blocks on this page.
24+
// Bail early if there are no blocks registered.
2525
if (!static::$blocks) {
2626
return $response;
2727
}
2828

29+
return $response->setContent(
30+
static::renderContent($response->content())
31+
);
32+
}
33+
34+
public static function renderContent($content)
35+
{
36+
// Bail early if there are no blocks registered.
37+
if (!static::$blocks) {
38+
return $content;
39+
}
40+
2941
$blocks = (new Client)->highlight(static::$blocks);
3042

3143
static::$blocks = [];
3244

33-
$content = $response->content();
34-
3545
foreach ($blocks as $block) {
3646
$swap = [
3747
$block->placeholder() => $block->highlighted,
@@ -45,6 +55,6 @@ public static function render(Response $response)
4555
}
4656
}
4757

48-
return $response->setContent($content);
58+
return $content;
4959
}
5060
}

src/Blade/CodeComponent.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct($language, $theme = null, $contents = null)
3232
$this->theme = $theme;
3333
$this->contents = $contents;
3434

35-
$this->block = Block::make()->setLanguage($this->language)->setTheme($this->theme);
35+
$this->block = Block::make()->language($this->language)->theme($this->theme);
3636
}
3737

3838
public function capture($contents)
@@ -43,7 +43,7 @@ public function capture($contents)
4343
$contents = file_get_contents(resource_path($contents));
4444
}
4545

46-
BladeManager::registerBlock($this->block->setCode($contents));
46+
BladeManager::registerBlock($this->block->code($contents));
4747
}
4848

4949
/**
@@ -54,10 +54,9 @@ public function capture($contents)
5454
public function render()
5555
{
5656
// Put all of the attributes on the code element, merging in our placeholder
57-
// classes and style string. Echo out the slot, but capture capture it
58-
// using output buffering. We then pass it through as the contents to
59-
// highlight, leaving the placeholder there so we can replace it
60-
// later with fully highlighted code.
57+
// classes and style string. Echo out the slot, but capture it using output
58+
// buffering. We then pass it through as the contents to highlight, leaving
59+
// the placeholder so we can replace it later with fully highlighted code.
6160
return <<<'EOT'
6261
<code {{
6362
$attributes->except('style')->merge([

src/Block.php

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,64 @@
55

66
namespace Hammerstone\Torchlight;
77

8-
use Illuminate\Support\Facades\Cache;
98
use Illuminate\Support\Str;
109

1110
class Block
1211
{
12+
/**
13+
* The language of the code that is being highlighted.
14+
*
15+
* @var string
16+
*/
1317
public $language;
1418

19+
/**
20+
* The theme of the code.
21+
*
22+
* @var string
23+
*/
1524
public $theme;
1625

26+
/**
27+
* The code itself.
28+
*
29+
* @var string
30+
*/
1731
public $code;
1832

19-
public $highlighted;
20-
33+
/**
34+
* The highlighted code, wrapped in pre+code tags.
35+
*
36+
* @var string
37+
*/
2138
public $wrapped;
2239

40+
/**
41+
* The highlighted code, not wrapped.
42+
*
43+
* @var string
44+
*/
45+
public $highlighted;
46+
47+
/**
48+
* Classes that should be applied to the code tag.
49+
*
50+
* @var string
51+
*/
2352
public $classes;
2453

54+
/**
55+
* Styles that should be applied to the code tag.
56+
*
57+
* @var string
58+
*/
2559
public $styles;
2660

27-
public $cacheBust = 0;
28-
61+
/**
62+
* The unique ID for the block.
63+
*
64+
* @var string
65+
*/
2966
protected $id;
3067

3168
/**
@@ -45,9 +82,6 @@ public function __construct($id = null)
4582
// Generate a unique UUID.
4683
$this->id = $id ?? (string)Str::uuid();
4784

48-
// An easy way to bust all caches.
49-
$this->cacheBust = config('torchlight.bust', 0);
50-
5185
// Set a default theme.
5286
$this->theme = config('torchlight.theme');
5387
}
@@ -65,10 +99,11 @@ public function id()
6599
*/
66100
public function hash()
67101
{
68-
return md5($this->language . $this->theme . $this->code . $this->cacheBust);
102+
return md5($this->language . $this->theme . $this->code . config('torchlight.bust'));
69103
}
70104

71105
/**
106+
* @param string $extra
72107
* @return string
73108
*/
74109
public function placeholder($extra = '')
@@ -84,7 +119,7 @@ public function placeholder($extra = '')
84119
* @param $language
85120
* @return $this
86121
*/
87-
public function setLanguage($language)
122+
public function language($language)
88123
{
89124
$this->language = $language;
90125

@@ -95,7 +130,7 @@ public function setLanguage($language)
95130
* @param $theme
96131
* @return $this
97132
*/
98-
public function setTheme($theme)
133+
public function theme($theme)
99134
{
100135
if ($theme) {
101136
$this->theme = $theme;
@@ -108,29 +143,18 @@ public function setTheme($theme)
108143
* @param $code
109144
* @return $this
110145
*/
111-
public function setCode($code)
146+
public function code($code)
112147
{
113148
$this->code = $this->clean($code);
114149

115150
return $this;
116151
}
117152

118-
/**
119-
* @param $number
120-
* @return $this
121-
*/
122-
public function setCacheBust($number)
123-
{
124-
$this->cacheBust = $number;
125-
126-
return $this;
127-
}
128-
129153
/**
130154
* @param $wrapped
131155
* @return $this
132156
*/
133-
public function setWrapped($wrapped)
157+
public function wrapped($wrapped)
134158
{
135159
$this->wrapped = $wrapped;
136160

@@ -148,7 +172,6 @@ public function toRequestParams()
148172
'language' => $this->language,
149173
'theme' => $this->theme,
150174
'code' => $this->code,
151-
'bust' => $this->cacheBust
152175
];
153176
}
154177

0 commit comments

Comments
 (0)