Skip to content

Commit 8f49a46

Browse files
committed
Prevent doubling Sylius prefix in plugin name
1 parent ebaf9dc commit 8f49a46

File tree

5 files changed

+106
-25
lines changed

5 files changed

+106
-25
lines changed

bin/rename-plugin.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9696
$this->renamePluginFiles($io, $names, $dryRun);
9797
$this->updateFileContents($io, $names, $dryRun);
9898
$this->updateComposerJson($io, $names['package'], $description, $names, $dryRun);
99+
$this->createTestApplicationEnv($io, $names, $dryRun);
99100

100101
if (!$dryRun) {
101102
$this->runComposerDumpAutoload($io);
@@ -231,22 +232,26 @@ private function generateNameVariations(string $company, string $pluginName, boo
231232
];
232233
}
233234

234-
$syliusPlugin = 'Sylius' . $plugin;
235+
$hasSyliusPrefix = str_starts_with($pluginName, 'Sylius');
236+
$syliusPlugin = $hasSyliusPrefix ? $plugin : 'Sylius' . $plugin;
237+
$syliusPluginName = $hasSyliusPrefix ? $pluginName : 'Sylius' . $pluginName;
238+
235239
$fullClass = $company . $syliusPlugin;
236-
$extensionClass = $company . 'Sylius' . $pluginName . 'Extension';
240+
$extensionClass = $company . $syliusPluginName . 'Extension';
237241

238242
$companyKebab = $this->toKebabCase($company);
239243
$fullPluginKebab = $this->toKebabCase($syliusPlugin);
240244
$companySnake = $this->toSnakeCase($company);
245+
$pluginSnakeForDb = $hasSyliusPrefix ? $pluginSnake : 'sylius_' . $pluginSnake;
241246

242247
return [
243248
'company' => $company,
244249
'plugin' => $syliusPlugin,
245250
'fullClass' => $fullClass,
246251
'extensionClass' => $extensionClass,
247252
'package' => $companyKebab . '/' . $fullPluginKebab,
248-
'db' => $companySnake . '_sylius_' . $pluginSnake . '_plugin',
249-
'configKey' => $companySnake . '_sylius_' . $pluginSnake,
253+
'db' => $companySnake . '_' . $pluginSnakeForDb . '_plugin',
254+
'configKey' => $companySnake . '_' . $pluginSnakeForDb,
250255
];
251256
}
252257

@@ -382,6 +387,9 @@ private function updateComposerJson(SymfonyStyle $io, string $packageName, strin
382387
unset($composer['autoload-dev']['psr-4']['Tests\\Acme\\SyliusExamplePlugin\\']);
383388
$composer['autoload-dev']['psr-4']["Tests\\{$names['company']}\\{$names['plugin']}\\"] = ['tests/', 'tests/TestApplication/src/'];
384389

390+
unset($composer['scripts']['post-create-project-cmd']);
391+
unset($composer['scripts']['post-root-package-install']);
392+
385393
if ($dryRun) {
386394
$io->writeln('[DRY RUN] Would update composer.json with:');
387395
$io->writeln(' - name: ' . $packageName);
@@ -396,6 +404,30 @@ private function updateComposerJson(SymfonyStyle $io, string $packageName, strin
396404
}
397405
}
398406

407+
private function createTestApplicationEnv(SymfonyStyle $io, array $names, bool $dryRun): void
408+
{
409+
$io->section('Creating TestApplication .env');
410+
411+
$envDir = __DIR__ . '/../tests/TestApplication';
412+
$envFile = $envDir . '/.env';
413+
$envContent = "DATABASE_URL=mysql://root@127.0.0.1/{$names['db']}_%kernel.environment%\n";
414+
415+
if ($dryRun) {
416+
$io->writeln("[DRY RUN] Would create {$envFile} with DATABASE_URL for {$names['db']}");
417+
return;
418+
}
419+
420+
if (!is_dir($envDir)) {
421+
mkdir($envDir, 0755, true);
422+
}
423+
424+
if (file_put_contents($envFile, $envContent) === false) {
425+
throw new \RuntimeException("Failed to create {$envFile}");
426+
}
427+
428+
$io->success('TestApplication .env created');
429+
}
430+
399431
private function runComposerDumpAutoload(SymfonyStyle $io): void
400432
{
401433
$io->section('Refreshing autoload files');

bin/show-success.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
echo PHP_EOL;
7+
echo " \033[42;30m DONE \033[0m Your plugin is ready!" . PHP_EOL;
8+
echo PHP_EOL;
9+
echo " \033[1mNext steps:\033[0m" . PHP_EOL;
10+
echo " 1. Start the server: \033[36msymfony serve -d\033[0m" . PHP_EOL;
11+
echo " 2. Open in browser: \033[36mhttps://127.0.0.1:8000\033[0m" . PHP_EOL;
12+
echo PHP_EOL;
13+
14+
unlink(__FILE__);

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'",
86+
"@php bin/show-success.php"
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
}

tests/TestApplication/.env

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
11
DATABASE_URL=mysql://root@127.0.0.1/acme_sylius_example_plugin_%kernel.environment%
2-
3-
BEHAT_BASE_URL="http://nginx/"
4-
5-
SYLIUS_TEST_APP_BUNDLES_PATH="tests/TestApplication/config/bundles.php"
6-
SYLIUS_TEST_APP_CONFIGS_TO_IMPORT="@AcmeSyliusExamplePlugin/tests/TestApplication/config/config.yaml"
7-
SYLIUS_TEST_APP_ROUTES_TO_IMPORT="@AcmeSyliusExamplePlugin/tests/TestApplication/config/routes.yaml"

0 commit comments

Comments
 (0)