Skip to content

Commit ae4728e

Browse files
committed
Merge pull request #326 from symfony-cmf/sf28
Symfony 2.8 support
2 parents 3c4b044 + adcc482 commit ae4728e

File tree

8 files changed

+81
-38
lines changed

8 files changed

+81
-38
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ matrix:
2424
- php: 5.6
2525
env: SYMFONY_VERSION=2.3.* SYMFONY_DEPRECATIONS_HELPER=weak
2626
- php: 5.6
27-
env: SYMFONY_VERSION=2.8.*
27+
env: SYMFONY_VERSION=2.8.* SYMFONY_DEPRECATIONS_HELPER=309
2828
- php: 5.6
2929
env: SYMFONY_VERSION=3.0.*
3030
allow_failures:
31-
- env: SYMFONY_VERSION=2.8.*
3231
- env: SYMFONY_VERSION=3.0.*
3332
fast_finish: true
3433

Admin/Extension/RouteReferrersExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Sonata\AdminBundle\Admin\AdminExtension;
1515
use Sonata\AdminBundle\Form\FormMapper;
16+
use Symfony\Cmf\Bundle\RoutingBundle\Util\Sf2CompatUtil;
1617

1718
/**
1819
* Admin extension to add routes tab to content implementing the
@@ -30,7 +31,7 @@ public function configureFormFields(FormMapper $formMapper)
3031
))
3132
->add(
3233
'routes',
33-
'sonata_type_collection',
34+
Sf2CompatUtil::getFormTypeName('sonata_type_collection'),
3435
array(),
3536
array(
3637
'edit' => 'inline',

Admin/RedirectRouteAdmin.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Sonata\AdminBundle\Form\FormMapper;
1717
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
1818
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route;
19+
use Symfony\Cmf\Bundle\RoutingBundle\Util\Sf2CompatUtil;
1920

2021
class RedirectRouteAdmin extends Admin
2122
{
@@ -37,17 +38,13 @@ protected function configureListFields(ListMapper $listMapper)
3738

3839
protected function configureFormFields(FormMapper $formMapper)
3940
{
40-
$isSf28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
41-
$textType = $isSf28 ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text';
42-
$doctrineTreeType = $isSf28 ? 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType' : 'doctrine_phpcr_odm_tree';
43-
4441
$formMapper
4542
->with('form.group_general')
46-
->add('parent', $doctrineTreeType, array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot))
47-
->add('name', $textType)
48-
->add('routeName', $textType, array('required' => false))
49-
->add('uri', $textType, array('required' => false))
50-
->add('routeTarget', $doctrineTreeType, array('choice_list' => array(), 'required' => false, 'root_node' => $this->routeRoot))
43+
->add('parent', Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'), array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot))
44+
->add('name', Sf2CompatUtil::getFormTypeName('text'))
45+
->add('routeName', Sf2CompatUtil::getFormTypeName('text'), array('required' => false))
46+
->add('uri', Sf2CompatUtil::getFormTypeName('text'), array('required' => false))
47+
->add('routeTarget', Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'), array('choice_list' => array(), 'required' => false, 'root_node' => $this->routeRoot))
5148
->end()
5249
;
5350
}

Admin/RouteAdmin.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
1818
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1919
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route;
20+
use Symfony\Cmf\Bundle\RoutingBundle\Util\Sf2CompatUtil;
2021
use PHPCR\Util\PathHelper;
2122

2223
class RouteAdmin extends Admin
@@ -60,42 +61,37 @@ protected function configureListFields(ListMapper $listMapper)
6061

6162
protected function configureFormFields(FormMapper $formMapper)
6263
{
63-
$isSf28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
64-
$textType = $isSf28 ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text';
65-
$doctrineTreeType = $isSf28 ? 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType' : 'doctrine_phpcr_odm_tree';
66-
$sonataImmutableType = $isSf28 ? 'Sonata\CoreBundle\Form\Type\ImmutableArrayType' : 'sonata_type_immutable_array';
67-
6864
$formMapper
6965
->with('form.group_general', array(
7066
'translation_domain' => 'CmfRoutingBundle',
7167
))
7268
->add(
7369
'parent',
74-
$doctrineTreeType,
70+
Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'),
7571
array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot)
7672
)
77-
->add('name', $textType)
73+
->add('name', Sf2CompatUtil::getFormTypeName('text'))
7874
->end();
7975

8076
if (null === $this->getParentFieldDescription()) {
8177
$formMapper
8278
->with('form.group_general', array(
8379
'translation_domain' => 'CmfRoutingBundle',
8480
))
85-
->add('content', $doctrineTreeType, array('choice_list' => array(), 'required' => false, 'root_node' => $this->contentRoot))
81+
->add('content', Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'), array('choice_list' => array(), 'required' => false, 'root_node' => $this->contentRoot))
8682
->end()
8783
->with('form.group_advanced', array(
8884
'translation_domain' => 'CmfRoutingBundle',
8985
))
90-
->add('variablePattern', $textType, array('required' => false), array('help' => 'form.help_variable_pattern'))
86+
->add('variablePattern', Sf2CompatUtil::getFormTypeName('text'), array('required' => false), array('help' => 'form.help_variable_pattern'))
9187
->add(
9288
'defaults',
93-
$sonataImmutableType,
89+
Sf2CompatUtil::getFormTypeName('sonata_type_immutable_array'),
9490
array('keys' => $this->configureFieldsForDefaults($this->getSubject()->getDefaults()))
9591
)
9692
->add(
9793
'options',
98-
$sonataImmutableType,
94+
Sf2CompatUtil::getFormTypeName('sonata_type_immutable_array'),
9995
array('keys' => $this->configureFieldsForOptions($this->getSubject()->getOptions())),
10096
array('help' => 'form.help_options')
10197
)
@@ -146,21 +142,18 @@ public function getExportFormats()
146142
*/
147143
protected function configureFieldsForDefaults($dynamicDefaults)
148144
{
149-
$isSf28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
150-
$textType = $isSf28 ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text';
151-
152145
$defaults = array(
153-
'_controller' => array('_controller', $textType, array('required' => false)),
154-
'_template' => array('_template', $textType, array('required' => false)),
155-
'type' => array('type', $isSf28 ? 'Symfony\Cmf\Bundle\RoutingBundle\Form\Type\RouteTypeType' : 'cmf_routing_route_type', array(
146+
'_controller' => array('_controller', Sf2CompatUtil::getFormTypeName('text'), array('required' => false)),
147+
'_template' => array('_template', Sf2CompatUtil::getFormTypeName('text'), array('required' => false)),
148+
'type' => array('type', Sf2CompatUtil::getFormTypeName('cmf_routing_route_type'), array(
156149
'empty_value' => '',
157150
'required' => false,
158151
)),
159152
);
160153

161154
foreach ($dynamicDefaults as $name => $value) {
162155
if (!isset($defaults[$name])) {
163-
$defaults[$name] = array($name, $textType, array('required' => false));
156+
$defaults[$name] = array($name, Sf2CompatUtil::getFormTypeName('text'), array('required' => false));
164157
}
165158
}
166159

@@ -172,16 +165,16 @@ protected function configureFieldsForDefaults($dynamicDefaults)
172165
foreach ($matches as $match) {
173166
$name = substr($match[0][0], 1, -1);
174167
if (!isset($defaults[$name])) {
175-
$defaults[$name] = array($name, $textType, array('required' => true));
168+
$defaults[$name] = array($name, Sf2CompatUtil::getFormTypeName('text'), array('required' => true));
176169
}
177170
}
178171
}
179172

180173
if ($route && $route->getOption('add_format_pattern')) {
181-
$defaults['_format'] = array('_format', $textType, array('required' => true));
174+
$defaults['_format'] = array('_format', Sf2CompatUtil::getFormTypeName('text'), array('required' => true));
182175
}
183176
if ($route && $route->getOption('add_locale_pattern')) {
184-
$defaults['_locale'] = array('_format', $textType, array('required' => false));
177+
$defaults['_locale'] = array('_format', Sf2CompatUtil::getFormTypeName('text'), array('required' => false));
185178
}
186179

187180
return $defaults;

Controller/RedirectController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Cmf\Bundle\RoutingBundle\Controller;
1313

1414
use Symfony\Component\Routing\RouterInterface;
15+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1516
use Symfony\Component\HttpFoundation\RedirectResponse;
1617
use Symfony\Cmf\Component\Routing\RedirectRouteInterface;
1718

@@ -53,10 +54,10 @@ public function redirectAction(RedirectRouteInterface $contentDocument)
5354
if (empty($url)) {
5455
$routeTarget = $contentDocument->getRouteTarget();
5556
if ($routeTarget) {
56-
$url = $this->router->generate($routeTarget, $contentDocument->getParameters(), true);
57+
$url = $this->router->generate($routeTarget, $contentDocument->getParameters(), UrlGeneratorInterface::ABSOLUTE_URL);
5758
} else {
5859
$routeName = $contentDocument->getRouteName();
59-
$url = $this->router->generate($routeName, $contentDocument->getParameters(), true);
60+
$url = $this->router->generate($routeName, $contentDocument->getParameters(), UrlGeneratorInterface::ABSOLUTE_URL);
6061
}
6162
}
6263

Tests/Functional/Routing/DynamicRouterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPCR\Util\NodeHelper;
1515
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1617
use Symfony\Cmf\Component\Routing\ChainRouter;
1718
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
1819
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
@@ -347,7 +348,7 @@ public function testGenerate()
347348
public function testGenerateAbsolute()
348349
{
349350
$route = $this->getDm()->find(null, self::ROUTE_ROOT.'/testroute/child');
350-
$url = $this->router->generate($route, array('test' => 'value'), true);
351+
$url = $this->router->generate($route, array('test' => 'value'), UrlGeneratorInterface::ABSOLUTE_URL);
351352
$this->assertEquals('http://localhost/testroute/child?test=value', $url);
352353
}
353354

Util/Sf2CompatUtil.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2015 Symfony CMF
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\Cmf\Bundle\RoutingBundle\Util;
13+
14+
final class Sf2CompatUtil
15+
{
16+
private static $isAtLeastSymfony28;
17+
private static $formTypes = array(
18+
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
19+
'doctrine_phpcr_odm_tree' => 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType',
20+
'sonata_type_immutable_array' => 'Sonata\CoreBundle\Form\Type\ImmutableArrayType',
21+
'sonata_type_collection' => 'Sonata\CoreBundle\Form\Type\CollectionType',
22+
'cmf_routing_route_type' => 'Symfony\Cmf\Bundle\RoutingBundle\Form\Type\RouteTypeType',
23+
);
24+
25+
public static function registerFormType($name, $fqcn)
26+
{
27+
self::$formTypes[$name] = $fqcn;
28+
}
29+
30+
public static function getFormTypeName($name)
31+
{
32+
if (!self::isAtLeastSymfony28()) {
33+
return $name;
34+
}
35+
36+
if (!isset(self::$formTypes[$name])) {
37+
throw new \InvalidArgumentException(sprintf('Unknown form type "%s", please register the FQCN of this form type to the Sf2CompatUtil first.', $name));
38+
}
39+
40+
return self::$formTypes[$name];
41+
}
42+
43+
public static function isAtLeastSymfony28()
44+
{
45+
if (null === self::$isAtLeastSymfony28) {
46+
self::$isAtLeastSymfony28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
47+
}
48+
49+
return self::$isAtLeastSymfony28;
50+
}
51+
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"symfony-cmf/testing": "~1.2,>=1.2.7",
2424
"doctrine/phpcr-odm": "~1.3",
2525
"matthiasnoback/symfony-dependency-injection-test": "~0.6",
26-
"matthiasnoback/symfony-config-test": "0.*",
26+
"matthiasnoback/symfony-config-test": "^1.3.1",
2727
"phpunit/php-code-coverage": "@stable",
28-
"sonata-project/doctrine-phpcr-admin-bundle": "~1.1",
28+
"sonata-project/doctrine-phpcr-admin-bundle": "^1.1",
2929
"symfony/monolog-bundle": "~2.3",
3030
"doctrine/orm": "~2.3",
3131
"doctrine/data-fixtures": "1.*,>=1.0.0-alpha3"

0 commit comments

Comments
 (0)