Skip to content

Commit ba8bd99

Browse files
Register custom shorthands (#762)
1 parent f66c4b2 commit ba8bd99

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

src/Blueprint.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,17 @@ class Blueprint
1313

1414
private array $generators = [];
1515

16-
public static function relativeNamespace(string $fullyQualifiedClassName): string
17-
{
18-
$namespace = config('blueprint.namespace') . '\\';
19-
$reference = ltrim($fullyQualifiedClassName, '\\');
20-
21-
if (Str::startsWith($reference, $namespace)) {
22-
return Str::after($reference, $namespace);
23-
}
16+
private array $shorthands = [];
2417

25-
return $reference;
26-
}
27-
28-
public static function appPath()
18+
public function registerShorthand(string $shorthand, \Closure $callback): void
2919
{
30-
return str_replace('\\', '/', config('blueprint.app_path'));
20+
$this->shorthands[$shorthand] = $callback;
3121
}
3222

33-
public function parse($content, $strip_dashes = true)
23+
private function expandShorthands(string $content): string
3424
{
35-
$content = str_replace(["\r\n", "\r"], "\n", $content);
36-
37-
if ($strip_dashes) {
38-
$content = preg_replace('/^(\s*)-\s*/m', '\1', $content);
39-
}
40-
41-
$content = $this->transformDuplicatePropertyKeys($content);
42-
4325
$content = preg_replace_callback(
44-
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi',
45-
fn ($matches) => $matches[1] . strtolower($matches[2]) . ': ' . $matches[2],
46-
$content
47-
);
48-
49-
$content = preg_replace_callback(
50-
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?): true$/mi',
26+
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)(: true)?$/mi',
5127
fn ($matches) => $matches[1] . strtolower($matches[2]) . ': ' . $matches[2],
5228
$content
5329
);
@@ -70,6 +46,45 @@ public function parse($content, $strip_dashes = true)
7046
$content
7147
);
7248

49+
foreach ($this->shorthands as $shorthand => $callback) {
50+
$content = preg_replace_callback(
51+
'/^(\s+)' . preg_quote($shorthand, '/') . '$/mi',
52+
$callback,
53+
$content
54+
);
55+
}
56+
57+
return $content;
58+
}
59+
60+
public static function relativeNamespace(string $fullyQualifiedClassName): string
61+
{
62+
$namespace = config('blueprint.namespace') . '\\';
63+
$reference = ltrim($fullyQualifiedClassName, '\\');
64+
65+
if (Str::startsWith($reference, $namespace)) {
66+
return Str::after($reference, $namespace);
67+
}
68+
69+
return $reference;
70+
}
71+
72+
public static function appPath()
73+
{
74+
return str_replace('\\', '/', config('blueprint.app_path'));
75+
}
76+
77+
public function parse($content, $strip_dashes = true)
78+
{
79+
$content = str_replace(["\r\n", "\r"], "\n", $content);
80+
81+
if ($strip_dashes) {
82+
$content = preg_replace('/^(\s*)-\s*/m', '\1', $content);
83+
}
84+
85+
$content = $this->transformDuplicatePropertyKeys($content);
86+
$content = $this->expandShorthands($content);
87+
7388
return Yaml::parse($content);
7489
}
7590

tests/Feature/BlueprintTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,28 @@ public function it_parses_uuid_shorthand(): void
624624
], $this->subject->parse($blueprint));
625625
}
626626

627+
#[Test]
628+
public function it_replaces_custom_shorthands(): void
629+
{
630+
$this->subject->registerShorthand('custom', fn ($matches) => $matches[1] . 'custom: shorthand');
631+
632+
$blueprint = <<<'DRAFT'
633+
models:
634+
Person:
635+
custom
636+
another: custom
637+
DRAFT;
638+
639+
$this->assertEquals([
640+
'models' => [
641+
'Person' => [
642+
'custom' => 'shorthand',
643+
'another' => 'custom',
644+
],
645+
],
646+
], $this->subject->parse($blueprint));
647+
}
648+
627649
#[Test]
628650
public function it_parses_yaml_with_dashed_syntax(): void
629651
{

0 commit comments

Comments
 (0)