Skip to content

Commit 8226e18

Browse files
committed
add scaffold configuration system to manipulate files
1 parent 898902e commit 8226e18

File tree

8 files changed

+90
-208
lines changed

8 files changed

+90
-208
lines changed

src/Maker/MakeScaffold.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Composer\InstalledVersions;
1515
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1616
use Symfony\Bundle\MakerBundle\DependencyBuilder;
17+
use Symfony\Bundle\MakerBundle\FileManager;
1718
use Symfony\Bundle\MakerBundle\Generator;
1819
use Symfony\Bundle\MakerBundle\InputConfiguration;
1920
use Symfony\Component\Console\Command\Command;
@@ -29,14 +30,14 @@
2930
*/
3031
final class MakeScaffold extends AbstractMaker
3132
{
32-
private $projectDir;
33+
private $files;
3334
private $availableScaffolds;
3435
private $installedScaffolds = [];
3536
private $installedPackages = [];
3637

37-
public function __construct($projectDir)
38+
public function __construct(FileManager $files)
3839
{
39-
$this->projectDir = $projectDir;
40+
$this->files = $files;
4041
}
4142

4243
public static function getCommandName(): string
@@ -116,7 +117,7 @@ private function generateScaffold(string $name, ConsoleStyle $io): void
116117

117118
// todo composer bin detection
118119
$command = ['composer', 'require', '--no-scripts', 'dev' === $env ? '--dev' : null, $package];
119-
$process = new Process(array_filter($command), $this->projectDir);
120+
$process = new Process(array_filter($command), $this->files->getRootDirectory());
120121

121122
$process->run();
122123

@@ -130,7 +131,13 @@ private function generateScaffold(string $name, ConsoleStyle $io): void
130131

131132
$io->text('Copying scaffold files...');
132133

133-
(new Filesystem())->mirror($scaffold['dir'], $this->projectDir, null, ['override' => true]);
134+
(new Filesystem())->mirror($scaffold['dir'], $this->files->getRootDirectory());
135+
136+
if (isset($scaffold['configure'])) {
137+
$io->text('Executing configuration...');
138+
139+
$scaffold['configure']($this->files);
140+
}
134141

135142
$io->text("Successfully installed scaffold <info>{$name}</info>.");
136143
$io->newLine();

src/Resources/config/makers.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</service>
2727

2828
<service id="maker.maker.make_scaffold" class="Symfony\Bundle\MakerBundle\Maker\MakeScaffold">
29-
<argument>%kernel.project_dir%</argument>
29+
<argument type="service" id="maker.file_manager" />
3030
<tag name="maker.command" />
3131
</service>
3232

src/Resources/scaffolds/6.0/auth.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use Symfony\Bundle\MakerBundle\FileManager;
4+
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
5+
36
return [
47
'description' => 'Create login form and tests.',
58
'dependents' => [
@@ -8,5 +11,45 @@
811
],
912
'packages' => [
1013
'profiler' => 'dev',
11-
]
14+
],
15+
'configure' => function(FileManager $files) {
16+
$services = new YamlSourceManipulator($files->getFileContents('config/services.yaml'));
17+
$data = $services->getData();
18+
$data['services']['App\Security\LoginUser']['$authenticator'] ='@security.authenticator.form_login.main';
19+
$services->setData($data);
20+
$files->dumpFile('config/services.yaml', $services->getContents());
21+
22+
$security = new YamlSourceManipulator($files->getFileContents('config/packages/security.yaml'));
23+
$data = $security->getData();
24+
$data['security']['providers'] = [
25+
'app_user_provider' => [
26+
'entity' => [
27+
'class' => 'App\Entity\User',
28+
'property' => 'email',
29+
],
30+
],
31+
];
32+
$data['security']['firewalls']['main'] = [
33+
'lazy' => true,
34+
'provider' => 'app_user_provider',
35+
'form_login' => [
36+
'login_path' => 'login',
37+
'check_path' => 'login',
38+
'username_parameter' => 'email',
39+
'password_parameter' => 'password',
40+
'enable_csrf' => true,
41+
],
42+
'logout' => [
43+
'path' => 'logout',
44+
'target' => 'homepage',
45+
],
46+
'remember_me' => [
47+
'secret' => '%kernel.secret%',
48+
'secure' => 'auto',
49+
'samesite' => 'lax',
50+
],
51+
];
52+
$security->setData($data);
53+
$files->dumpFile('config/packages/security.yaml', $security->getContents());
54+
},
1255
];

src/Resources/scaffolds/6.0/auth/config/packages/security.yaml

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/Resources/scaffolds/6.0/auth/config/services.yaml

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Symfony\Bundle\MakerBundle\FileManager;
4+
35
return [
46
'description' => 'Create registration form and tests.',
57
'dependents' => [
@@ -8,5 +10,26 @@
810
'packages' => [
911
'form' => 'all',
1012
'validator' => 'all',
11-
]
13+
],
14+
'configure' => function(FileManager $files) {
15+
$userEntity = $files->getFileContents('src/Entity/User.php');
16+
17+
if (str_contains($userEntity, $attribute = "#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]")) {
18+
// unique constraint already there
19+
return;
20+
}
21+
22+
$userEntity = str_replace(
23+
[
24+
'#[ORM\Entity(repositoryClass: UserRepository::class)]',
25+
'use Doctrine\ORM\Mapping as ORM;'
26+
],
27+
[
28+
"#[ORM\Entity(repositoryClass: UserRepository::class)]\n{$attribute}",
29+
"use Doctrine\ORM\Mapping as ORM;\nuse Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;",
30+
],
31+
$userEntity
32+
);
33+
$files->dumpFile('src/Entity/User.php', $userEntity);
34+
},
1235
];

src/Resources/scaffolds/6.0/register/src/Entity/User.php

Lines changed: 0 additions & 113 deletions
This file was deleted.

src/Resources/scaffolds/6.0/reset-password.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Symfony\Bundle\MakerBundle\FileManager;
4+
35
return [
46
'description' => 'Reset password system using symfonycasts/reset-password-bundle with tests.',
57
'dependents' => [
@@ -11,5 +13,11 @@
1113
'mailer' => 'all',
1214
'symfonycasts/reset-password-bundle' => 'all',
1315
'zenstruck/mailer-test' => 'dev',
14-
]
16+
],
17+
'configure' => function(FileManager $files) {
18+
$files->dumpFile(
19+
'config/packages/reset_password.yaml',
20+
file_get_contents(__DIR__.'/reset-password/config/packages/reset_password.yaml')
21+
);
22+
},
1523
];

0 commit comments

Comments
 (0)