Skip to content

Commit 161d11f

Browse files
committed
Add Block::generateIdsUsing
1 parent 654e7bb commit 161d11f

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Changelog
2+
3+
## 0.2.1 - 2021-05-20
4+
5+
- Add `Block::generateIdsUsing`

src/Block.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class Block
1111
{
12+
/**
13+
* @var null|callable
14+
*/
15+
public static $generateIdsUsing;
16+
1217
/**
1318
* The language of the code that is being highlighted.
1419
*
@@ -80,7 +85,7 @@ public static function make($id = null)
8085
public function __construct($id = null)
8186
{
8287
// Generate a unique UUID.
83-
$this->id = $id ?? (string)Str::uuid();
88+
$this->id = $id ?? $this->generateId();
8489

8590
// Set a default theme.
8691
$this->theme = config('torchlight.theme');
@@ -94,6 +99,16 @@ public function id()
9499
return $this->id;
95100
}
96101

102+
/**
103+
* @return string
104+
*/
105+
protected function generateId()
106+
{
107+
$id = is_callable(static::$generateIdsUsing) ? call_user_func(static::$generateIdsUsing) : Str::uuid();
108+
109+
return (string)$id;
110+
}
111+
97112
/**
98113
* @return string
99114
*/

tests/BlockTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,20 @@ public function default_theme_is_used()
113113
$block = Block::make('id');
114114

115115
$this->assertEquals('a new default', $block->theme);
116+
}
117+
118+
/** @test */
119+
public function can_specify_an_id_generator()
120+
{
121+
Block::$generateIdsUsing = function () {
122+
return 'generated_via_test';
123+
};
124+
125+
$block = Block::make();
126+
127+
$this->assertEquals('generated_via_test', $block->id());
116128

129+
Block::$generateIdsUsing = null;
117130
}
118131

119132

0 commit comments

Comments
 (0)