Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ba28ae

Browse filesBrowse files
committed
Add path provider for bundle paths configuration
1 parent b5f592e commit 3ba28ae
Copy full SHA for 3ba28ae

File tree

Expand file treeCollapse file tree

22 files changed

+299
-34
lines changed
Filter options
Expand file treeCollapse file tree

22 files changed

+299
-34
lines changed

‎UPGRADE-4.4.md

Copy file name to clipboardExpand all lines: UPGRADE-4.4.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ HttpFoundation
100100
HttpKernel
101101
----------
102102

103-
* Implementing the `BundleInterface` without implementing the `getPublicDir()` method is deprecated.
103+
* Implementing the `BundleInterface` without implementing the `getPaths()` method is deprecated.
104104
This method will be added to the interface in 5.0.
105105
* The `DebugHandlersListener` class has been marked as `final`
106106

‎UPGRADE-5.0.md

Copy file name to clipboardExpand all lines: UPGRADE-5.0.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ HttpFoundation
283283
HttpKernel
284284
----------
285285

286-
* The `getPublicDir()` method has been added to the `BundleInterface`.
286+
* The `getPaths()` method has been added to the `BundleInterface`.
287287
* Removed `Client`, use `HttpKernelBrowser` instead
288288
* The `Kernel::getRootDir()` and the `kernel.root_dir` parameter have been removed
289289
* The `KernelInterface::getName()` and the `kernel.name` parameter have been removed

‎src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
137137
$validAssetDirs = [];
138138
/** @var BundleInterface $bundle */
139139
foreach ($kernel->getBundles() as $bundle) {
140-
if (!method_exists($bundle, 'getPublicDir')) {
141-
@trigger_error(sprintf('Not defining "getPublicDir()" method in the "%s" class is deprecated since Symfony 4.4 and will not be supported in 5.0.', \get_class($bundle)), E_USER_DEPRECATED);
142-
$publicDir = 'Resources/public';
140+
if (method_exists($bundle, 'getPaths')) {
141+
$bundlePaths = $bundle->getPaths();
142+
$originDir = $bundlePaths['public_dir'] ?? $bundle->getPath().'/Resources/public';
143143
} else {
144-
$publicDir = ltrim($bundle->getPublicDir(), '\\/');
144+
$originDir = $bundle->getPath().'/Resources/public';
145145
}
146-
if (!is_dir($originDir = $bundle->getPath().\DIRECTORY_SEPARATOR.$publicDir)) {
146+
if (!is_dir($originDir)) {
147147
continue;
148148
}
149149

‎src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php
+26-4Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
161161
// Override with provided Bundle info
162162
if (null !== $input->getArgument('bundle')) {
163163
try {
164+
$transPaths = [];
165+
$viewsPaths = [];
164166
$bundle = $kernel->getBundle($input->getArgument('bundle'));
165-
$transPaths = [$bundle->getPath().'/Resources/translations'];
167+
if (method_exists($bundle, 'getPaths')) {
168+
$bundlePaths = $bundle->getPaths();
169+
if (isset($bundlePaths['translations_dir'])) {
170+
$transPaths = [$bundlePaths['translations_dir']];
171+
}
172+
if (isset($bundlePaths['templates_dir'])) {
173+
$viewsPaths = [$bundlePaths['templates_dir']];
174+
}
175+
} else {
176+
$transPaths = [$bundle->getPath().'/Resources/translations'];
177+
$viewsPaths = [$bundle->getPath().'/Resources/views'];
178+
}
166179
if ($this->defaultTransPath) {
167180
$transPaths[] = $this->defaultTransPath;
168181
}
@@ -171,7 +184,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
171184
$notice = sprintf('Storing translations files for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $dir, $bundle->getName());
172185
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
173186
}
174-
$viewsPaths = [$bundle->getPath().'/Resources/views'];
175187
if ($this->defaultViewsPath) {
176188
$viewsPaths[] = $this->defaultViewsPath;
177189
}
@@ -206,13 +218,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
206218
}
207219
} elseif ($input->getOption('all')) {
208220
foreach ($kernel->getBundles() as $bundle) {
209-
$transPaths[] = $bundle->getPath().'/Resources/translations';
221+
if (method_exists($bundle, 'getPaths')) {
222+
$bundlePaths = $bundle->getPaths();
223+
if (isset($bundlePaths['translations_dir'])) {
224+
$transPaths[] = $bundlePaths['translations_dir'];
225+
}
226+
if (isset($bundlePaths['templates_dir'])) {
227+
$viewsPaths[] = $bundlePaths['templates_dir'];
228+
}
229+
} else {
230+
$transPaths[] = $bundle->getPath().'/Resources/translations';
231+
$viewsPaths[] = $bundle->getPath().'/Resources/views';
232+
}
210233
if (is_dir($deprecatedPath = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName()))) {
211234
$transPaths[] = $deprecatedPath;
212235
$notice = sprintf('Storing translations files for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $bundle->getName(), $deprecatedPath);
213236
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
214237
}
215-
$viewsPaths[] = $bundle->getPath().'/Resources/views';
216238
if (is_dir($deprecatedPath = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName()))) {
217239
$viewsPaths[] = $deprecatedPath;
218240
$notice = sprintf('Loading Twig templates for "%s" from the "%s" directory is deprecated since Symfony 4.2, ', $bundle->getName(), $deprecatedPath);

‎src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
153153
// Override with provided Bundle info
154154
if (null !== $input->getArgument('bundle')) {
155155
try {
156+
$transPaths = [];
157+
$viewsPaths = [];
156158
$foundBundle = $kernel->getBundle($input->getArgument('bundle'));
157-
$transPaths = [$foundBundle->getPath().'/Resources/translations'];
159+
if (method_exists($foundBundle, 'getPaths')) {
160+
$bundlePaths = $foundBundle->getPaths();
161+
if (isset($bundlePaths['translations_dir'])) {
162+
$transPaths = [$bundlePaths['translations_dir']];
163+
}
164+
if (isset($bundlePaths['templates_dir'])) {
165+
$viewsPaths = [$bundlePaths['templates_dir']];
166+
}
167+
} else {
168+
$transPaths = [$foundBundle->getPath().'/Resources/translations'];
169+
$viewsPaths = [$foundBundle->getPath().'/Resources/views'];
170+
}
158171
if ($this->defaultTransPath) {
159172
$transPaths[] = $this->defaultTransPath;
160173
}
@@ -163,7 +176,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
163176
$notice = sprintf('Storing translations files for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $foundBundle->getName(), $dir);
164177
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
165178
}
166-
$viewsPaths = [$foundBundle->getPath().'/Resources/views'];
167179
if ($this->defaultViewsPath) {
168180
$viewsPaths[] = $this->defaultViewsPath;
169181
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+11-11Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
11431143
$defaultDir = $container->getParameterBag()->resolveValue($config['default_path']);
11441144
$rootDir = $container->getParameter('kernel.root_dir');
11451145
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
1146-
if ($container->fileExists($dir = $bundle['path'].'/Resources/translations')) {
1146+
if ($container->fileExists($dir = $bundle['paths']['translations_dir'] ?? $bundle['path'].'/Resources/translations')) {
11471147
$dirs[] = $dir;
11481148
} else {
11491149
$nonExistingDirs[] = $dir;
@@ -1305,20 +1305,20 @@ private function registerValidatorMapping(ContainerBuilder $container, array $co
13051305
}
13061306

13071307
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
1308-
$dirname = $bundle['path'];
1308+
$configDir = $bundle['paths']['config_dir'] ?? $bundle['path'].'/Resources/config';
13091309

13101310
if (
1311-
$container->fileExists($file = $dirname.'/Resources/config/validation.yaml', false) ||
1312-
$container->fileExists($file = $dirname.'/Resources/config/validation.yml', false)
1311+
$container->fileExists($file = $configDir.'/validation.yaml', false) ||
1312+
$container->fileExists($file = $configDir.'/validation.yml', false)
13131313
) {
13141314
$fileRecorder('yml', $file);
13151315
}
13161316

1317-
if ($container->fileExists($file = $dirname.'/Resources/config/validation.xml', false)) {
1317+
if ($container->fileExists($file = $configDir.'/validation.xml', false)) {
13181318
$fileRecorder('xml', $file);
13191319
}
13201320

1321-
if ($container->fileExists($dir = $dirname.'/Resources/config/validation', '/^$/')) {
1321+
if ($container->fileExists($dir = $configDir.'/validation', '/^$/')) {
13221322
$this->registerMappingFilesFromDir($dir, $fileRecorder);
13231323
}
13241324
}
@@ -1499,20 +1499,20 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
14991499
};
15001500

15011501
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
1502-
$dirname = $bundle['path'];
1502+
$configDir = $bundle['paths']['config_dir'] ?? $bundle['path'].'/Resources/config';
15031503

1504-
if ($container->fileExists($file = $dirname.'/Resources/config/serialization.xml', false)) {
1504+
if ($container->fileExists($file = $configDir.'/serialization.xml', false)) {
15051505
$fileRecorder('xml', $file);
15061506
}
15071507

15081508
if (
1509-
$container->fileExists($file = $dirname.'/Resources/config/serialization.yaml', false) ||
1510-
$container->fileExists($file = $dirname.'/Resources/config/serialization.yml', false)
1509+
$container->fileExists($file = $configDir.'/serialization.yaml', false) ||
1510+
$container->fileExists($file = $configDir.'/serialization.yml', false)
15111511
) {
15121512
$fileRecorder('yml', $file);
15131513
}
15141514

1515-
if ($container->fileExists($dir = $dirname.'/Resources/config/serialization', '/^$/')) {
1515+
if ($container->fileExists($dir = $configDir.'/serialization', '/^$/')) {
15161516
$this->registerMappingFilesFromDir($dir, $fileRecorder);
15171517
}
15181518
}
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle;
13+
14+
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
16+
class ModernBundle extends Bundle
17+
{
18+
public function getPaths(): array
19+
{
20+
$bundlePath = $this->getPath();
21+
22+
return [
23+
'config_dir' => $bundlePath.'/config',
24+
'public_dir' => $bundlePath.'/public',
25+
'templates_dir' => $bundlePath.'/templates',
26+
'translations_dir' => $bundlePath.'/translations',
27+
];
28+
}
29+
}
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle\src\Entity\Person:
2+
attributes:
3+
name:
4+
serialized_name: 'full_name'
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle\src\Entity\Person:
2+
properties:
3+
age:
4+
- GreaterThan:
5+
value: 18

‎src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/ModernBundle/public/modern.css

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/ModernBundle/public/modern.css
Whitespace-only changes.
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle\src\Entity;
13+
14+
class Person
15+
{
16+
public $name;
17+
public $age;
18+
19+
public function __construct(string $name, string $age)
20+
{
21+
$this->name = $name;
22+
$this->age = $age;
23+
}
24+
}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OK
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ok_label: OK
+108Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Command\AssetsInstallCommand;
15+
use Symfony\Bundle\FrameworkBundle\Console\Application;
16+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle\src\Entity\Person;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Component\Filesystem\Filesystem;
19+
20+
class BundlePathsTest extends AbstractWebTestCase
21+
{
22+
public function testDefaultBundlePaths()
23+
{
24+
static::bootKernel(['test_case' => 'BundlePaths']);
25+
$bundlesMetadata = static::$container->getParameter('kernel.bundles_metadata');
26+
$bundlePath = $bundlesMetadata['FrameworkBundle']['path'];
27+
$expectedSubset = [
28+
'paths' => [
29+
'config_dir' => $bundlePath.'/Resources/config',
30+
'public_dir' => $bundlePath.'/Resources/public',
31+
'templates_dir' => $bundlePath.'/Resources/views',
32+
'translations_dir' => $bundlePath.'/Resources/translations',
33+
],
34+
];
35+
36+
$this->assertArraySubset($expectedSubset, $bundlesMetadata['FrameworkBundle']);
37+
}
38+
39+
public function testCustomBundlePaths()
40+
{
41+
static::bootKernel(['test_case' => 'BundlePaths']);
42+
$bundlesMetadata = static::$container->getParameter('kernel.bundles_metadata');
43+
$bundlePath = $bundlesMetadata['ModernBundle']['path'];
44+
$expectedSubset = [
45+
'paths' => [
46+
'config_dir' => $bundlePath.'/config',
47+
'public_dir' => $bundlePath.'/public',
48+
'templates_dir' => $bundlePath.'/templates',
49+
'translations_dir' => $bundlePath.'/translations',
50+
],
51+
];
52+
53+
$this->assertArraySubset($expectedSubset, $bundlesMetadata['ModernBundle']);
54+
}
55+
56+
public function testCustomBundlePublicPath()
57+
{
58+
$kernel = static::bootKernel(['test_case' => 'BundlePaths']);
59+
$projectDir = sys_get_temp_dir().'/'.uniqid('sf_modern', true);
60+
61+
$fs = new Filesystem();
62+
$fs->mkdir($projectDir.'/public');
63+
$command = (new Application($kernel))->add(new AssetsInstallCommand($fs, $projectDir));
64+
$commandTester = new CommandTester($command);
65+
66+
$this->assertSame(0, $commandTester->execute(['target' => $projectDir.'/public']));
67+
$this->assertFileExists($projectDir.'/public/bundles/modern/modern.css');
68+
69+
$fs->remove($projectDir);
70+
}
71+
72+
public function testCustomBundleTwigTemplatesPath()
73+
{
74+
static::bootKernel(['test_case' => 'BundlePaths']);
75+
$bundlesMetadata = static::$container->getParameter('kernel.bundles_metadata');
76+
$bundlePath = $bundlesMetadata['ModernBundle']['path'];
77+
78+
$twig = static::$container->get('twig');
79+
$this->assertSame([$bundlePath.'/templates'], $twig->getLoader()->getPaths('Modern'));
80+
$this->assertSame("OK\n", $twig->render('@Modern/index.html.twig'));
81+
}
82+
83+
public function testCustomBundleTranslationsPath()
84+
{
85+
static::bootKernel(['test_case' => 'BundlePaths']);
86+
87+
$translator = static::$container->get('translator');
88+
$this->assertSame('OK', $translator->trans('ok_label', [], 'modern'));
89+
}
90+
91+
public function testCustomBundleValidationConfigPath()
92+
{
93+
static::bootKernel(['test_case' => 'BundlePaths']);
94+
95+
$validator = static::$container->get('validator');
96+
$this->assertTrue($validator->hasMetadataFor(Person::class));
97+
$this->assertCount(1, $constraintViolationList = $validator->validate(new Person('john', 5)));
98+
$this->assertSame('This value should be greater than 18.', $constraintViolationList->get(0)->getMessage());
99+
}
100+
101+
public function testCustomBundleSerializationConfigPath()
102+
{
103+
static::bootKernel(['test_case' => 'BundlePaths']);
104+
105+
$serializer = static::$container->get('serializer');
106+
$this->assertEquals(['full_name' => 'john', 'age' => 5], $serializer->normalize(new Person('john', 5), 'json'));
107+
}
108+
}
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle\ModernBundle;
14+
use Symfony\Bundle\TwigBundle\TwigBundle;
15+
16+
return [
17+
new FrameworkBundle(),
18+
new TwigBundle(),
19+
new ModernBundle(),
20+
];
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
translator: true
6+
validation: true
7+
serializer: true
8+
9+
twig:
10+
strict_variables: '%kernel.debug%'
11+
exception_controller: ~

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.