Skip to content

Commit 220a82f

Browse files
committed
Prevent doubling Sylius prefix in plugin name
1 parent ebaf9dc commit 220a82f

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL=mysql://root@127.0.0.1/acme_sylius_example_plugin_%kernel.environment%

bin/rename-plugin.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,26 @@ private function generateNameVariations(string $company, string $pluginName, boo
231231
];
232232
}
233233

234-
$syliusPlugin = 'Sylius' . $plugin;
234+
$hasSyliusPrefix = str_starts_with($pluginName, 'Sylius');
235+
$syliusPlugin = $hasSyliusPrefix ? $plugin : 'Sylius' . $plugin;
236+
$syliusPluginName = $hasSyliusPrefix ? $pluginName : 'Sylius' . $pluginName;
237+
235238
$fullClass = $company . $syliusPlugin;
236-
$extensionClass = $company . 'Sylius' . $pluginName . 'Extension';
239+
$extensionClass = $company . $syliusPluginName . 'Extension';
237240

238241
$companyKebab = $this->toKebabCase($company);
239242
$fullPluginKebab = $this->toKebabCase($syliusPlugin);
240243
$companySnake = $this->toSnakeCase($company);
244+
$pluginSnakeForDb = $hasSyliusPrefix ? $pluginSnake : 'sylius_' . $pluginSnake;
241245

242246
return [
243247
'company' => $company,
244248
'plugin' => $syliusPlugin,
245249
'fullClass' => $fullClass,
246250
'extensionClass' => $extensionClass,
247251
'package' => $companyKebab . '/' . $fullPluginKebab,
248-
'db' => $companySnake . '_sylius_' . $pluginSnake . '_plugin',
249-
'configKey' => $companySnake . '_sylius_' . $pluginSnake,
252+
'db' => $companySnake . '_' . $pluginSnakeForDb . '_plugin',
253+
'configKey' => $companySnake . '_' . $pluginSnakeForDb,
250254
];
251255
}
252256

bin/validate-directory.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,57 @@
44
declare(strict_types=1);
55

66
$dirName = basename(dirname(__DIR__));
7+
$isSyliusOfficial = getenv('SYLIUS') === '1';
78

8-
if (!preg_match('/^[A-Z][a-zA-Z0-9]*Plugin$/', $dirName)) {
9-
fwrite(STDERR, "\n");
10-
fwrite(STDERR, " \033[41;37m ERROR \033[0m Directory name must end with 'Plugin' suffix\n");
11-
fwrite(STDERR, "\n");
12-
fwrite(STDERR, " Current: {$dirName}\n");
13-
fwrite(STDERR, " Expected: {$dirName}Plugin (or similar PascalCase name ending with Plugin)\n");
14-
fwrite(STDERR, "\n");
15-
fwrite(STDERR, " Example:\n");
16-
fwrite(STDERR, " composer create-project sylius/plugin-skeleton WishlistPlugin\n");
17-
fwrite(STDERR, "\n");
18-
19-
exit(1);
9+
if ($isSyliusOfficial) {
10+
if (!preg_match('/^[A-Z][a-zA-Z0-9]*Plugin$/', $dirName) || str_starts_with($dirName, 'Sylius')) {
11+
$hasPluginSuffix = str_ends_with($dirName, 'Plugin');
12+
13+
$suggestion = $dirName;
14+
if (str_starts_with($suggestion, 'Sylius')) {
15+
$suggestion = substr($suggestion, 6);
16+
}
17+
if (!$hasPluginSuffix) {
18+
$suggestion .= 'Plugin';
19+
}
20+
21+
fwrite(STDERR, "\n");
22+
fwrite(STDERR, " \033[41;37m ERROR \033[0m Official Sylius plugin name must match pattern: {Feature}Plugin\n");
23+
fwrite(STDERR, "\n");
24+
fwrite(STDERR, " Current: {$dirName}\n");
25+
fwrite(STDERR, " Expected: {$suggestion}\n");
26+
fwrite(STDERR, "\n");
27+
fwrite(STDERR, " Example:\n");
28+
fwrite(STDERR, " SYLIUS=1 composer create-project sylius/plugin-skeleton MailerLitePlugin\n");
29+
fwrite(STDERR, "\n");
30+
31+
exit(1);
32+
}
33+
} else {
34+
if (!preg_match('/^Sylius[A-Z][a-zA-Z0-9]*Plugin$/', $dirName)) {
35+
$hasSyliusPrefix = str_starts_with($dirName, 'Sylius');
36+
$hasPluginSuffix = str_ends_with($dirName, 'Plugin');
37+
38+
$suggestion = $dirName;
39+
if (!$hasSyliusPrefix) {
40+
$suggestion = 'Sylius' . $suggestion;
41+
}
42+
if (!$hasPluginSuffix) {
43+
$suggestion .= 'Plugin';
44+
}
45+
46+
fwrite(STDERR, "\n");
47+
fwrite(STDERR, " \033[41;37m ERROR \033[0m Community plugin name must match pattern: Sylius{Feature}Plugin\n");
48+
fwrite(STDERR, "\n");
49+
fwrite(STDERR, " Current: {$dirName}\n");
50+
fwrite(STDERR, " Expected: {$suggestion}\n");
51+
fwrite(STDERR, "\n");
52+
fwrite(STDERR, " Example:\n");
53+
fwrite(STDERR, " composer create-project sylius/plugin-skeleton SyliusWishlistPlugin\n");
54+
fwrite(STDERR, "\n");
55+
56+
exit(1);
57+
}
2058
}
2159

2260
unlink(__FILE__);

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@
7979
],
8080
"post-create-project-cmd": [
8181
"@php bin/rename-plugin.php",
82-
"@test-app-init"
82+
"@test-app-init",
83+
"git init -q",
84+
"git add -A",
85+
"git commit -q -m 'Initial commit from PluginSkeleton'",
86+
"@php -r \"echo PHP_EOL.' \\033[42;30m DONE \\033[0m Your plugin is ready!'.PHP_EOL.PHP_EOL.' Run: symfony serve -d'.PHP_EOL.PHP_EOL;\""
8387
],
8488
"database-reset": [
8589
"vendor/bin/console doctrine:database:drop --force --if-exists",
@@ -93,8 +97,7 @@
9397
],
9498
"test-app-init": [
9599
"@database-reset",
96-
"@frontend-clear",
97-
"echo '\nNow run:\n\n symfony serve -d\n'"
100+
"@frontend-clear"
98101
]
99102
}
100103
}

0 commit comments

Comments
 (0)