Skip to content

Commit 298ba65

Browse files
committed
add FileManager::manipulateYaml()
1 parent 01ce881 commit 298ba65

File tree

3 files changed

+57
-43
lines changed

3 files changed

+57
-43
lines changed

src/FileManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\MakerBundle\Util\AutoloaderUtil;
1515
use Symfony\Bundle\MakerBundle\Util\MakerFileLinkFormatter;
16+
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
1617
use Symfony\Component\Console\Style\SymfonyStyle;
1718
use Symfony\Component\Filesystem\Filesystem;
1819
use Symfony\Component\Finder\Finder;
@@ -86,6 +87,16 @@ public function dumpFile(string $filename, string $content): void
8687
}
8788
}
8889

90+
/**
91+
* @param callable(array):array $manipulator
92+
*/
93+
public function manipulateYaml(string $filename, callable $manipulator): void
94+
{
95+
$yaml = new YamlSourceManipulator($this->getFileContents($filename));
96+
$yaml->setData($manipulator($yaml->getData()));
97+
$this->dumpFile($filename, $yaml->getContents());
98+
}
99+
89100
public function fileExists($path): bool
90101
{
91102
return file_exists($this->absolutizePath($path));

src/Resources/scaffolds/6.0/auth.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use Symfony\Bundle\MakerBundle\FileManager;
4-
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
54

65
return [
76
'description' => 'Create login form and tests.',
@@ -14,43 +13,45 @@
1413
'symfony/stopwatch' => 'dev',
1514
],
1615
'configure' => function(FileManager $files) {
17-
$services = new YamlSourceManipulator($files->getFileContents('config/services.yaml'));
18-
$data = $services->getData();
19-
$data['services']['App\Security\LoginUser']['$authenticator'] ='@security.authenticator.form_login.main';
20-
$services->setData($data);
21-
$files->dumpFile('config/services.yaml', $services->getContents());
16+
// add LoginUser service
17+
$files->manipulateYaml('config/services.yaml', function(array $data) {
18+
$data['services']['App\Security\LoginUser']['$authenticator'] ='@security.authenticator.form_login.main';
2219

23-
$security = new YamlSourceManipulator($files->getFileContents('config/packages/security.yaml'));
24-
$data = $security->getData();
25-
$data['security']['providers'] = [
26-
'app_user_provider' => [
27-
'entity' => [
28-
'class' => 'App\Entity\User',
29-
'property' => 'email',
20+
return $data;
21+
});
22+
23+
// make security.yaml adjustments
24+
$files->manipulateYaml('config/packages/security.yaml', function(array $data) {
25+
$data['security']['providers'] = [
26+
'app_user_provider' => [
27+
'entity' => [
28+
'class' => 'App\Entity\User',
29+
'property' => 'email',
30+
],
31+
],
32+
];
33+
$data['security']['firewalls']['main'] = [
34+
'lazy' => true,
35+
'provider' => 'app_user_provider',
36+
'form_login' => [
37+
'login_path' => 'login',
38+
'check_path' => 'login',
39+
'username_parameter' => 'email',
40+
'password_parameter' => 'password',
41+
'enable_csrf' => true,
42+
],
43+
'logout' => [
44+
'path' => 'logout',
45+
'target' => 'homepage',
3046
],
31-
],
32-
];
33-
$data['security']['firewalls']['main'] = [
34-
'lazy' => true,
35-
'provider' => 'app_user_provider',
36-
'form_login' => [
37-
'login_path' => 'login',
38-
'check_path' => 'login',
39-
'username_parameter' => 'email',
40-
'password_parameter' => 'password',
41-
'enable_csrf' => true,
42-
],
43-
'logout' => [
44-
'path' => 'logout',
45-
'target' => 'homepage',
46-
],
47-
'remember_me' => [
48-
'secret' => '%kernel.secret%',
49-
'secure' => 'auto',
50-
'samesite' => 'lax',
51-
],
52-
];
53-
$security->setData($data);
54-
$files->dumpFile('config/packages/security.yaml', $security->getContents());
47+
'remember_me' => [
48+
'secret' => '%kernel.secret%',
49+
'secure' => 'auto',
50+
'samesite' => 'lax',
51+
],
52+
];
53+
54+
return $data;
55+
});
5556
},
5657
];

src/Resources/scaffolds/6.0/bootstrapcss.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use Symfony\Bundle\MakerBundle\FileManager;
4-
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
54

65
return [
76
'description' => 'Add bootstrap css/js.',
@@ -14,14 +13,17 @@
1413
'@popperjs/core' => '^2.0.0',
1514
],
1615
'configure' => function(FileManager $files) {
17-
$twig = new YamlSourceManipulator($files->getFileContents('config/packages/twig.yaml'));
18-
$data = $twig->getData();
19-
$data['twig']['form_themes'] = ['bootstrap_5_layout.html.twig'];
20-
$twig->setData($data);
21-
$files->dumpFile('config/packages/twig.yaml', $twig->getContents());
16+
// add bootstrap form theme
17+
$files->manipulateYaml('config/packages/twig.yaml', function(array $data) {
18+
$data['twig']['form_themes'] = ['bootstrap_5_layout.html.twig'];
2219

20+
return $data;
21+
});
22+
23+
// add bootstrap to app.css
2324
$files->dumpFile('assets/styles/app.css', "@import \"~bootstrap/dist/css/bootstrap.css\";\n");
2425

26+
// add bootstrap to app.js
2527
$appJs = $files->getFileContents('assets/app.js');
2628

2729
if (str_contains($appJs, "require('bootstrap');")) {

0 commit comments

Comments
 (0)