Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Convert sub-namespaces to underscore as well #508

Merged
merged 1 commit into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Templating/TemplateGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function guessTemplateName($controller, Request $request)
$matchController = null;
foreach ($this->controllerPatterns as $pattern) {
if (preg_match($pattern, $className, $tempMatch)) {
$matchController = $tempMatch;
$matchController = str_replace('\\', '/', strtolower(preg_replace('/([a-z\d])([A-Z])/', '\\1_\\2', $tempMatch[1])));
break;
}
}
Expand All @@ -78,14 +78,14 @@ public function guessTemplateName($controller, Request $request)
if ('__invoke' === $controller[1]) {
$matchAction = $matchController;
$matchController = null;
} elseif (!preg_match('/^(.+)Action$/', $controller[1], $matchAction)) {
$matchAction = array(null, $controller[1]);
} else {
$matchAction = preg_replace('/Action$/', '', $controller[1]);
}

$matchAction[1] = strtolower(preg_replace('/([a-z\d])([A-Z])/', '\\1_\\2', $matchAction[1]));
$matchAction = strtolower(preg_replace('/([a-z\d])([A-Z])/', '\\1_\\2', $matchAction));
$bundleName = $this->getBundleForClass($className);

return sprintf(($bundleName ? '@'.$bundleName.'/' : '').$matchController[1].($matchController[1] ? '/' : '').$matchAction[1].'.'.$request->getRequestFormat().'.twig');
return sprintf(($bundleName ? '@'.$bundleName.'/' : '').$matchController.($matchController ? '/' : '').$matchAction.'.'.$request->getRequestFormat().'.twig');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/**
* @Route(service="test.invokable_class_level.predefined")
* @Template("@Foo/Invokable/predefined.html.twig")
* @Template("@Foo/invokable/predefined.html.twig")
*/
class InvokableClassLevelController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function variableAction($variable)

/**
* @Route("/invokable/another-variable/container/{variable}/")
* @Template("@Foo/InvokableContainer/variable.html.twig")
* @Template("@Foo/invokable_container/variable.html.twig")
*/
public function anotherVariableAction($variable)
{
Expand All @@ -38,7 +38,7 @@ public function anotherVariableAction($variable)

/**
* @Route("/invokable/variable/container/{variable}/{another_variable}/")
* @Template("@Foo/InvokableContainer/another_variable.html.twig")
* @Template("@Foo/invokable_container/another_variable.html.twig")
*/
public function doubleVariableAction($variable, $another_variable)
{
Expand All @@ -50,7 +50,7 @@ public function doubleVariableAction($variable, $another_variable)

/**
* @Route("/invokable/predefined/container/")
* @Template("@Foo/Invokable/predefined.html.twig")
* @Template("@Foo/invokable/predefined.html.twig")
*/
public function __invoke()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InvokableController
{
/**
* @Route("/invokable/predefined/service/")
* @Template("@Foo/Invokable/predefined.html.twig")
* @Template("@Foo/invokable/predefined.html.twig")
*/
public function __invoke()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
* @Template("@Foo/Invokable/predefined.html.twig")
* @Template("@Foo/invokable/predefined.html.twig")
*/
class MultipleActionsClassLevelTemplateController extends Controller
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/FooBundle/Controller/SimpleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function someAction($a, $b, $c = 'c')

/**
* @Route("/simple/multiple/{a}/{b}/")
* @Template("@Foo/Simple/some.html.twig")
* @Template("@Foo/simple/some.html.twig")
*/
public function someMoreAction($a, $b, $c = 'c')
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sensio\Bundle\FrameworkExtraBundle\Tests\Templating\Fixture\Controller;
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\Templating\Fixture\Controller\MyAdmin;

class OutOfBundleController
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Sensio\Bundle\FrameworkExtraBundle\Tests\Templating\Fixture\FooBundle\Controller\SubController;

class FooBarController
{
}
21 changes: 16 additions & 5 deletions Tests/Templating/TemplateGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,29 @@ public function testGuessTemplateName()
'indexAction',
), new Request());

$this->assertEquals('@Foo/Foo/index.html.twig', (string) $templateReference);
$this->assertEquals('@Foo/foo/index.html.twig', (string) $templateReference);
}

public function testGuessTemplateWithoutBundle()
{
$templateGuesser = new TemplateGuesser($this->kernel);
$templateReference = $templateGuesser->guessTemplateName(array(
new Fixture\Controller\OutOfBundleController(),
new Fixture\Controller\MyAdmin\OutOfBundleController(),
'indexAction',
), new Request());

$this->assertEquals('OutOfBundle/index.html.twig', (string) $templateReference);
$this->assertEquals('my_admin/out_of_bundle/index.html.twig', (string) $templateReference);
}

public function testGuessTemplateWithSubNamespace()
{
$templateGuesser = new TemplateGuesser($this->kernel);
$templateReference = $templateGuesser->guessTemplateName(array(
new Fixture\FooBundle\Controller\SubController\FooBarController(),
'fooBaz',
), new Request());

$this->assertEquals('@Foo/sub_controller/foo_bar/foo_baz.html.twig', (string) $templateReference);
}

/**
Expand Down Expand Up @@ -88,7 +99,7 @@ public function testGuessTemplateWithACustomPattern($controller, $patterns)
'indexAction',
), new Request());

$this->assertEquals('@Foo/Foo/index.html.twig', (string) $templateReference);
$this->assertEquals('@Foo/foo/index.html.twig', (string) $templateReference);
}

/**
Expand All @@ -103,7 +114,7 @@ public function testGuessTemplateWithNotStandardMethodName($controller, $pattern
'fooBar',
), new Request());

$this->assertEquals('@Foo/Foo/foo_bar.html.twig', (string) $templateReference);
$this->assertEquals('@Foo/foo/foo_bar.html.twig', (string) $templateReference);
}

public function controllerProvider()
Expand Down