Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 44 additions & 29 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,17 @@ class Blueprint

private array $generators = [];

public static function relativeNamespace(string $fullyQualifiedClassName): string
{
$namespace = config('blueprint.namespace') . '\\';
$reference = ltrim($fullyQualifiedClassName, '\\');

if (Str::startsWith($reference, $namespace)) {
return Str::after($reference, $namespace);
}
private array $shorthands = [];

return $reference;
}

public static function appPath()
public function registerShorthand(string $shorthand, \Closure $callback): void
{
return str_replace('\\', '/', config('blueprint.app_path'));
$this->shorthands[$shorthand] = $callback;
}

public function parse($content, $strip_dashes = true)
private function expandShorthands(string $content): string
{
$content = str_replace(["\r\n", "\r"], "\n", $content);

if ($strip_dashes) {
$content = preg_replace('/^(\s*)-\s*/m', '\1', $content);
}

$content = $this->transformDuplicatePropertyKeys($content);

$content = preg_replace_callback(
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi',
fn ($matches) => $matches[1] . strtolower($matches[2]) . ': ' . $matches[2],
$content
);

$content = preg_replace_callback(
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?): true$/mi',
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)(: true)?$/mi',
fn ($matches) => $matches[1] . strtolower($matches[2]) . ': ' . $matches[2],
$content
);
Expand All @@ -70,6 +46,45 @@ public function parse($content, $strip_dashes = true)
$content
);

foreach ($this->shorthands as $shorthand => $callback) {
$content = preg_replace_callback(
'/^(\s+)' . preg_quote($shorthand, '/') . '$/mi',
$callback,
$content
);
}

return $content;
}

public static function relativeNamespace(string $fullyQualifiedClassName): string
{
$namespace = config('blueprint.namespace') . '\\';
$reference = ltrim($fullyQualifiedClassName, '\\');

if (Str::startsWith($reference, $namespace)) {
return Str::after($reference, $namespace);
}

return $reference;
}

public static function appPath()
{
return str_replace('\\', '/', config('blueprint.app_path'));
}

public function parse($content, $strip_dashes = true)
{
$content = str_replace(["\r\n", "\r"], "\n", $content);

if ($strip_dashes) {
$content = preg_replace('/^(\s*)-\s*/m', '\1', $content);
}

$content = $this->transformDuplicatePropertyKeys($content);
$content = $this->expandShorthands($content);

return Yaml::parse($content);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,28 @@ public function it_parses_uuid_shorthand(): void
], $this->subject->parse($blueprint));
}

#[Test]
public function it_replaces_custom_shorthands(): void
{
$this->subject->registerShorthand('custom', fn ($matches) => $matches[1] . 'custom: shorthand');

$blueprint = <<<'DRAFT'
models:
Person:
custom
another: custom
DRAFT;

$this->assertEquals([
'models' => [
'Person' => [
'custom' => 'shorthand',
'another' => 'custom',
],
],
], $this->subject->parse($blueprint));
}

#[Test]
public function it_parses_yaml_with_dashed_syntax(): void
{
Expand Down