Skip to content

Commit 473fd3b

Browse files
Merge branch '2.8' into 3.1
* 2.8: 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 5c68c69 + 658a8a0 commit 473fd3b

File tree

16 files changed

+234
-45
lines changed

16 files changed

+234
-45
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 & 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));
@@ -149,13 +149,65 @@ public function load(array $configs, ContainerBuilder $container)
149149
));
150150
}
151151

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

161213
/**
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
@@ -165,8 +165,22 @@ public function testTwigLoaderPaths($format)
165165
array('namespaced_path1', 'namespace1'),
166166
array('namespaced_path2', 'namespace2'),
167167
array('namespaced_path3', 'namespace3'),
168+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildChildTwig'),
169+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
170+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildChildTwig'),
171+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'Twig'),
172+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'Twig'),
173+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'Twig'),
174+
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'Twig'),
168175
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
169176
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
177+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
178+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildTwig'),
179+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildTwig'),
180+
array(__DIR__.'/Fixtures/Bundle/ChildTwigBundle/Resources/views', 'ChildTwig'),
181+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
182+
array(__DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
183+
array(__DIR__.'/Fixtures/Bundle/ChildChildTwigBundle/Resources/views', 'ChildChildTwig'),
170184
array(__DIR__.'/Fixtures/Resources/views'),
171185
), $paths);
172186
}
@@ -218,8 +232,40 @@ private function createContainer()
218232
'kernel.root_dir' => __DIR__.'/Fixtures',
219233
'kernel.charset' => 'UTF-8',
220234
'kernel.debug' => false,
221-
'kernel.bundles' => array('TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle'),
222-
'kernel.bundles_metadata' => array('TwigBundle' => array('namespace' => 'Symfony\\Bundle\\TwigBundle', 'parent' => null, 'path' => realpath(__DIR__.'/../..'))),
235+
'kernel.bundles' => array(
236+
'TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle',
237+
'ChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
238+
'ChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
239+
'ChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
240+
'ChildChildChildChildTwigBundle' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
241+
),
242+
'kernel.bundles_metadata' => array(
243+
'ChildChildChildChildTwigBundle' => array(
244+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildChildTwigBundle\\ChildChildChildChildTwigBundle',
245+
'parent' => 'ChildChildChildTwigBundle',
246+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildChildTwigBundle',
247+
),
248+
'TwigBundle' => array(
249+
'namespace' => 'Symfony\\Bundle\\TwigBundle',
250+
'parent' => null,
251+
'path' => realpath(__DIR__.'/../..'),
252+
),
253+
'ChildTwigBundle' => array(
254+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildTwigBundle\\ChildTwigBundle',
255+
'parent' => 'TwigBundle',
256+
'path' => __DIR__.'/Fixtures/Bundle/ChildTwigBundle',
257+
),
258+
'ChildChildChildTwigBundle' => array(
259+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildChildTwigBundle\\ChildChildChildTwigBundle',
260+
'parent' => 'ChildChildTwigBundle',
261+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildChildTwigBundle',
262+
),
263+
'ChildChildTwigBundle' => array(
264+
'namespace' => 'Symfony\\Bundle\\TwigBundle\\Tests\\DependencyInjection\\Fixtures\\Bundle\\ChildChildTwigBundle\\ChildChildTwigBundle',
265+
'parent' => 'ChildTwigBundle',
266+
'path' => __DIR__.'/Fixtures/Bundle/ChildChildTwigBundle',
267+
),
268+
),
223269
)));
224270

225271
return $container;

0 commit comments

Comments
 (0)