Skip to content

Commit 5ca2737

Browse files
Merge branch '2.7' into 2.8
* 2.7: [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 a351c24 + 5cf600d commit 5ca2737

File tree

16 files changed

+234
-44
lines changed

16 files changed

+234
-44
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ServerCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ protected function isOtherServerProcessRunning($address)
5454
return true;
5555
}
5656

57-
list($hostname, $port) = explode(':', $address);
57+
$pos = strrpos($address, ':');
58+
$hostname = substr($address, 0, $pos);
59+
$port = substr($address, $pos + 1);
5860

5961
$fp = @fsockopen($hostname, $port, $errno, $errstr, 5);
6062

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
@@ -35,6 +35,17 @@ public function testLocateATemplate()
3535
$this->assertEquals('/path/to/template', $locator->locate($template));
3636
}
3737

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

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

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,24 @@ 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
}
107109
$container->addResource(new FileExistenceResource($dir));
108110
}
109111

110-
$dir = $container->getParameter('kernel.root_dir').'/Resources/views';
111-
if (is_dir($dir)) {
112+
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
112113
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
113114
}
114115
$container->addResource(new FileExistenceResource($dir));
@@ -149,13 +150,65 @@ public function load(array $configs, ContainerBuilder $container)
149150
));
150151
}
151152

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

161214
/**
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
@@ -201,8 +201,22 @@ public function testTwigLoaderPaths($format)
201201
array('namespaced_path1', 'namespace1'),
202202
array('namespaced_path2', 'namespace2'),
203203
array('namespaced_path3', 'namespace3'),
204+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildChildTwig'),
205+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
206+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
207+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'Twig'),
208+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'Twig'),
209+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
210+
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
204211
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
205212
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
213+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
214+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
215+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildTwig'),
216+
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'ChildTwig'),
217+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
218+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
219+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
206220
array(__DIR__.'/Fixtures/Resources/views'),
207221
), $paths);
208222
}
@@ -254,8 +268,40 @@ private function createContainer()
254268
'kernel.root_dir' => __DIR__.'/Fixtures',
255269
'kernel.charset' => 'UTF-8',
256270
'kernel.debug' => false,
257-
'kernel.bundles' => array('TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle'),
258-
'kernel.bundles_metadata' => array('TwigBundle' => array('namespace' => 'Symfony\\Bundle\\TwigBundle', 'parent' => null, 'path' => realpath(__DIR__.'/../..'))),
271+
'kernel.bundles' => array(
272+
'TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle',
273+
'ChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
274+
'ChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
275+
'ChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
276+
'ChildChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
277+
),
278+
'kernel.bundles_metadata' => array(
279+
'ChildChildChildChildTwigBundle' => array(
280+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
281+
'parent' => 'ChildChildChildTwigBundle',
282+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle',
283+
),
284+
'TwigBundle' => array(
285+
'namespace' => 'Symfony\\Bundle\\TwigBundle',
286+
'parent' => null,
287+
'path' => realpath(__DIR__.'/../..'),
288+
),
289+
'ChildTwigBundle' => array(
290+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
291+
'parent' => 'TwigBundle',
292+
'path' => __DIR__.'/Fixtures/Bundle/ChildTwigBundle',
293+
),
294+
'ChildChildChildTwigBundle' => array(
295+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
296+
'parent' => 'ChildChildTwigBundle',
297+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle',
298+
),
299+
'ChildChildTwigBundle' => array(
300+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
301+
'parent' => 'ChildTwigBundle',
302+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildTwigBundle',
303+
),
304+
),
259305
)));
260306

261307
return $container;

0 commit comments

Comments
 (0)