Skip to content

Commit 641bb1a

Browse files
Merge branch '3.2'
* 3.2: Fix getMock usage Remove dead code [Form] DateTimeToLocalizedStringTransformer does not use TZ when using only date [Validator] Fix caching of constraints derived from non-serializable parents [TwigBundle] Fix bug where namespaced paths don't take parent bundles in account [FrameworkBundle] Fix relative paths used as cache keys respect groups when merging constraints fix IPv6 address handling in server commands
2 parents 566732d + ec858ce commit 641bb1a

File tree

15 files changed

+231
-44
lines changed

15 files changed

+231
-44
lines changed

src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class TemplateLocator implements FileLocatorInterface
2424
protected $locator;
2525
protected $cache;
2626

27+
private $cacheHits = array();
28+
2729
/**
2830
* Constructor.
2931
*
@@ -71,12 +73,15 @@ public function locate($template, $currentPath = null, $first = true)
7173

7274
$key = $this->getCacheKey($template);
7375

76+
if (isset($this->cacheHits[$key])) {
77+
return $this->cacheHits[$key];
78+
}
7479
if (isset($this->cache[$key])) {
75-
return $this->cache[$key];
80+
return $this->cacheHits[$key] = realpath($this->cache[$key]) ?: $this->cache[$key];
7681
}
7782

7883
try {
79-
return $this->cache[$key] = $this->locator->locate($template->getPath(), $currentPath);
84+
return $this->cacheHits[$key] = $this->locator->locate($template->getPath(), $currentPath);
8085
} catch (\InvalidArgumentException $e) {
8186
throw new \InvalidArgumentException(sprintf('Unable to find template "%s" : "%s".', $template, $e->getMessage()), 0, $e);
8287
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return array(
4+
'bundle:controller:name.format.engine' => __DIR__.'/../Fixtures/Resources/views/this.is.a.template.format.engine',
5+
);

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/TemplateLocatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ public function testLocateATemplate()
3838
$this->assertEquals('/path/to/template', $locator->locate($template));
3939
}
4040

41+
public function testLocateATemplateFromCacheDir()
42+
{
43+
$template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
44+
45+
$fileLocator = $this->getFileLocator();
46+
47+
$locator = new TemplateLocator($fileLocator, __DIR__.'/../../Fixtures');
48+
49+
$this->assertEquals(realpath(__DIR__.'/../../Fixtures/Resources/views/this.is.a.template.format.engine'), $locator->locate($template));
50+
}
51+
4152
public function testThrowsExceptionWhenTemplateNotFound()
4253
{
4354
$template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,23 @@ public function load(array $configs, ContainerBuilder $container)
9292
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
9393
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);
9494

95-
// register bundles as Twig namespaces
96-
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
97-
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views';
98-
if (is_dir($dir)) {
99-
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $name);
95+
$bundleHierarchy = $this->getBundleHierarchy($container);
96+
97+
foreach ($bundleHierarchy as $name => $bundle) {
98+
$namespace = $this->normalizeBundleName($name);
99+
100+
foreach ($bundle['children'] as $child) {
101+
foreach ($bundleHierarchy[$child]['paths'] as $path) {
102+
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
103+
}
100104
}
101-
$container->addResource(new FileExistenceResource($dir));
102105

103-
$dir = $bundle['path'].'/Resources/views';
104-
if (is_dir($dir)) {
105-
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $name);
106+
foreach ($bundle['paths'] as $path) {
107+
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
106108
}
107-
$container->addResource(new FileExistenceResource($dir));
108109
}
109110

110-
$dir = $container->getParameter('kernel.root_dir').'/Resources/views';
111-
if (is_dir($dir)) {
111+
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
112112
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
113113
}
114114
$container->addResource(new FileExistenceResource($dir));
@@ -151,13 +151,65 @@ public function load(array $configs, ContainerBuilder $container)
151151
}
152152
}
153153

154-
private function addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
154+
private function getBundleHierarchy(ContainerBuilder $container)
155+
{
156+
$bundleHierarchy = array();
157+
158+
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
159+
if (!array_key_exists($name, $bundleHierarchy)) {
160+
$bundleHierarchy[$name] = array(
161+
'paths' => array(),
162+
'parents' => array(),
163+
'children' => array(),
164+
);
165+
}
166+
167+
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
168+
$bundleHierarchy[$name]['paths'][] = $dir;
169+
}
170+
$container->addResource(new FileExistenceResource($dir));
171+
172+
if (is_dir($dir = $bundle['path'].'/Resources/views')) {
173+
$bundleHierarchy[$name]['paths'][] = $dir;
174+
}
175+
$container->addResource(new FileExistenceResource($dir));
176+
177+
if (null === $bundle['parent']) {
178+
continue;
179+
}
180+
181+
$bundleHierarchy[$name]['parents'][] = $bundle['parent'];
182+
183+
if (!array_key_exists($bundle['parent'], $bundleHierarchy)) {
184+
$bundleHierarchy[$bundle['parent']] = array(
185+
'paths' => array(),
186+
'parents' => array(),
187+
'children' => array(),
188+
);
189+
}
190+
191+
$bundleHierarchy[$bundle['parent']]['children'] = array_merge($bundleHierarchy[$name]['children'], array($name), $bundleHierarchy[$bundle['parent']]['children']);
192+
193+
foreach ($bundleHierarchy[$bundle['parent']]['parents'] as $parent) {
194+
$bundleHierarchy[$name]['parents'][] = $parent;
195+
$bundleHierarchy[$parent]['children'] = array_merge($bundleHierarchy[$name]['children'], array($name), $bundleHierarchy[$parent]['children']);
196+
}
197+
198+
foreach ($bundleHierarchy[$name]['children'] as $child) {
199+
$bundleHierarchy[$child]['parents'] = array_merge($bundleHierarchy[$child]['parents'], $bundleHierarchy[$name]['parents']);
200+
}
201+
}
202+
203+
return $bundleHierarchy;
204+
}
205+
206+
private function normalizeBundleName($name)
155207
{
156-
$name = $bundle;
157208
if ('Bundle' === substr($name, -6)) {
158209
$name = substr($name, 0, -6);
159210
}
160-
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
211+
212+
return $name;
161213
}
162214

163215
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a layout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a layout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a layout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a layout

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,22 @@ public function testTwigLoaderPaths($format)
167167
array('namespaced_path1', 'namespace1'),
168168
array('namespaced_path2', 'namespace2'),
169169
array('namespaced_path3', 'namespace3'),
170+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildChildTwig'),
171+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
172+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
173+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'Twig'),
174+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'Twig'),
175+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
176+
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
170177
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
171178
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
179+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
180+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
181+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildTwig'),
182+
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'ChildTwig'),
183+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
184+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
185+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
172186
array(__DIR__.'/Fixtures/Resources/views'),
173187
), $paths);
174188
}
@@ -244,8 +258,40 @@ private function createContainer()
244258
'kernel.root_dir' => __DIR__.'/Fixtures',
245259
'kernel.charset' => 'UTF-8',
246260
'kernel.debug' => false,
247-
'kernel.bundles' => array('TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle'),
248-
'kernel.bundles_metadata' => array('TwigBundle' => array('namespace' => 'Symfony\\Bundle\\TwigBundle', 'parent' => null, 'path' => realpath(__DIR__.'/../..'))),
261+
'kernel.bundles' => array(
262+
'TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle',
263+
'ChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
264+
'ChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
265+
'ChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
266+
'ChildChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
267+
),
268+
'kernel.bundles_metadata' => array(
269+
'ChildChildChildChildTwigBundle' => array(
270+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
271+
'parent' => 'ChildChildChildTwigBundle',
272+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle',
273+
),
274+
'TwigBundle' => array(
275+
'namespace' => 'Symfony\\Bundle\\TwigBundle',
276+
'parent' => null,
277+
'path' => realpath(__DIR__.'/../..'),
278+
),
279+
'ChildTwigBundle' => array(
280+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
281+
'parent' => 'TwigBundle',
282+
'path' => __DIR__.'/Fixtures/Bundle/ChildTwigBundle',
283+
),
284+
'ChildChildChildTwigBundle' => array(
285+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
286+
'parent' => 'ChildChildTwigBundle',
287+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle',
288+
),
289+
'ChildChildTwigBundle' => array(
290+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
291+
'parent' => 'ChildTwigBundle',
292+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildTwigBundle',
293+
),
294+
),
249295
)));
250296

251297
return $container;

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ public function reverseTransform($value)
129129
try {
130130
if ($dateOnly) {
131131
// we only care about year-month-date, which has been delivered as a timestamp pointing to UTC midnight
132-
return new \DateTime(gmdate('Y-m-d', $timestamp), new \DateTimeZone($this->inputTimezone));
132+
$dateTime = new \DateTime(gmdate('Y-m-d', $timestamp), new \DateTimeZone($this->outputTimezone));
133+
} else {
134+
// read timestamp into DateTime object - the formatter delivers a timestamp
135+
$dateTime = new \DateTime(sprintf('@%s', $timestamp));
133136
}
134-
135-
// read timestamp into DateTime object - the formatter delivers a timestamp
136-
$dateTime = new \DateTime(sprintf('@%s', $timestamp));
137137
// set timezone separately, as it would be ignored if set via the constructor,
138138
// see http://php.net/manual/en/datetime.construct.php
139139
$dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));

0 commit comments

Comments
 (0)