Skip to content

Symfony 2.8 support #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ matrix:
- php: 5.6
env: SYMFONY_VERSION=3.0.*
allow_failures:
- env: SYMFONY_VERSION=2.8.*
- env: SYMFONY_VERSION=3.0.*
fast_finish: true

Expand Down
3 changes: 2 additions & 1 deletion Admin/Extension/RouteReferrersExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Sonata\AdminBundle\Admin\AdminExtension;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Cmf\Bundle\RoutingBundle\Util\Sf2CompatUtil;

/**
* Admin extension to add routes tab to content implementing the
Expand All @@ -30,7 +31,7 @@ public function configureFormFields(FormMapper $formMapper)
))
->add(
'routes',
'sonata_type_collection',
Sf2CompatUtil::getFormTypeName('sonata_type_collection'),
array(),
array(
'edit' => 'inline',
Expand Down
15 changes: 6 additions & 9 deletions Admin/RedirectRouteAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route;
use Symfony\Cmf\Bundle\RoutingBundle\Util\Sf2CompatUtil;

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

protected function configureFormFields(FormMapper $formMapper)
{
$isSf28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
$textType = $isSf28 ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text';
$doctrineTreeType = $isSf28 ? 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType' : 'doctrine_phpcr_odm_tree';

$formMapper
->with('form.group_general')
->add('parent', $doctrineTreeType, array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot))
->add('name', $textType)
->add('routeName', $textType, array('required' => false))
->add('uri', $textType, array('required' => false))
->add('routeTarget', $doctrineTreeType, array('choice_list' => array(), 'required' => false, 'root_node' => $this->routeRoot))
->add('parent', Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'), array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot))
->add('name', Sf2CompatUtil::getFormTypeName('text'))
->add('routeName', Sf2CompatUtil::getFormTypeName('text'), array('required' => false))
->add('uri', Sf2CompatUtil::getFormTypeName('text'), array('required' => false))
->add('routeTarget', Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'), array('choice_list' => array(), 'required' => false, 'root_node' => $this->routeRoot))
->end()
;
}
Expand Down
35 changes: 14 additions & 21 deletions Admin/RouteAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route;
use Symfony\Cmf\Bundle\RoutingBundle\Util\Sf2CompatUtil;
use PHPCR\Util\PathHelper;

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

protected function configureFormFields(FormMapper $formMapper)
{
$isSf28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
$textType = $isSf28 ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text';
$doctrineTreeType = $isSf28 ? 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType' : 'doctrine_phpcr_odm_tree';
$sonataImmutableType = $isSf28 ? 'Sonata\CoreBundle\Form\Type\ImmutableArrayType' : 'sonata_type_immutable_array';

$formMapper
->with('form.group_general', array(
'translation_domain' => 'CmfRoutingBundle',
))
->add(
'parent',
$doctrineTreeType,
Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'),
array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot)
)
->add('name', $textType)
->add('name', Sf2CompatUtil::getFormTypeName('text'))
->end();

if (null === $this->getParentFieldDescription()) {
$formMapper
->with('form.group_general', array(
'translation_domain' => 'CmfRoutingBundle',
))
->add('content', $doctrineTreeType, array('choice_list' => array(), 'required' => false, 'root_node' => $this->contentRoot))
->add('content', Sf2CompatUtil::getFormTypeName('doctrine_phpcr_odm_tree'), array('choice_list' => array(), 'required' => false, 'root_node' => $this->contentRoot))
->end()
->with('form.group_advanced', array(
'translation_domain' => 'CmfRoutingBundle',
))
->add('variablePattern', $textType, array('required' => false), array('help' => 'form.help_variable_pattern'))
->add('variablePattern', Sf2CompatUtil::getFormTypeName('text'), array('required' => false), array('help' => 'form.help_variable_pattern'))
->add(
'defaults',
$sonataImmutableType,
Sf2CompatUtil::getFormTypeName('sonata_type_immutable_array'),
array('keys' => $this->configureFieldsForDefaults($this->getSubject()->getDefaults()))
)
->add(
'options',
$sonataImmutableType,
Sf2CompatUtil::getFormTypeName('sonata_type_immutable_array'),
array('keys' => $this->configureFieldsForOptions($this->getSubject()->getOptions())),
array('help' => 'form.help_options')
)
Expand Down Expand Up @@ -146,21 +142,18 @@ public function getExportFormats()
*/
protected function configureFieldsForDefaults($dynamicDefaults)
{
$isSf28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
$textType = $isSf28 ? 'Symfony\Component\Form\Extension\Core\Type\TextType' : 'text';

$defaults = array(
'_controller' => array('_controller', $textType, array('required' => false)),
'_template' => array('_template', $textType, array('required' => false)),
'type' => array('type', $isSf28 ? 'Symfony\Cmf\Bundle\RoutingBundle\Form\Type\RouteTypeType' : 'cmf_routing_route_type', array(
'_controller' => array('_controller', Sf2CompatUtil::getFormTypeName('text'), array('required' => false)),
'_template' => array('_template', Sf2CompatUtil::getFormTypeName('text'), array('required' => false)),
'type' => array('type', Sf2CompatUtil::getFormTypeName('cmf_routing_route_type'), array(
'empty_value' => '',
'required' => false,
)),
);

foreach ($dynamicDefaults as $name => $value) {
if (!isset($defaults[$name])) {
$defaults[$name] = array($name, $textType, array('required' => false));
$defaults[$name] = array($name, Sf2CompatUtil::getFormTypeName('text'), array('required' => false));
}
}

Expand All @@ -172,16 +165,16 @@ protected function configureFieldsForDefaults($dynamicDefaults)
foreach ($matches as $match) {
$name = substr($match[0][0], 1, -1);
if (!isset($defaults[$name])) {
$defaults[$name] = array($name, $textType, array('required' => true));
$defaults[$name] = array($name, Sf2CompatUtil::getFormTypeName('text'), array('required' => true));
}
}
}

if ($route && $route->getOption('add_format_pattern')) {
$defaults['_format'] = array('_format', $textType, array('required' => true));
$defaults['_format'] = array('_format', Sf2CompatUtil::getFormTypeName('text'), array('required' => true));
}
if ($route && $route->getOption('add_locale_pattern')) {
$defaults['_locale'] = array('_format', $textType, array('required' => false));
$defaults['_locale'] = array('_format', Sf2CompatUtil::getFormTypeName('text'), array('required' => false));
}

return $defaults;
Expand Down
5 changes: 3 additions & 2 deletions Controller/RedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Cmf\Bundle\RoutingBundle\Controller;

use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Cmf\Component\Routing\RedirectRouteInterface;

Expand Down Expand Up @@ -53,10 +54,10 @@ public function redirectAction(RedirectRouteInterface $contentDocument)
if (empty($url)) {
$routeTarget = $contentDocument->getRouteTarget();
if ($routeTarget) {
$url = $this->router->generate($routeTarget, $contentDocument->getParameters(), true);
$url = $this->router->generate($routeTarget, $contentDocument->getParameters(), UrlGeneratorInterface::ABSOLUTE_URL);
} else {
$routeName = $contentDocument->getRouteName();
$url = $this->router->generate($routeName, $contentDocument->getParameters(), true);
$url = $this->router->generate($routeName, $contentDocument->getParameters(), UrlGeneratorInterface::ABSOLUTE_URL);
}
}

Expand Down
1 change: 1 addition & 0 deletions Form/Type/RouteTypeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Util\Sf2CompatUtil;

class RouteTypeType extends AbstractType
{
Expand Down
3 changes: 2 additions & 1 deletion Tests/Functional/Routing/DynamicRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\ChainRouter;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute;
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
Expand Down Expand Up @@ -347,7 +348,7 @@ public function testGenerate()
public function testGenerateAbsolute()
{
$route = $this->getDm()->find(null, self::ROUTE_ROOT.'/testroute/child');
$url = $this->router->generate($route, array('test' => 'value'), true);
$url = $this->router->generate($route, array('test' => 'value'), UrlGeneratorInterface::ABSOLUTE_URL);
$this->assertEquals('http://localhost/testroute/child?test=value', $url);
}

Expand Down
51 changes: 51 additions & 0 deletions Util/Sf2CompatUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Bundle\RoutingBundle\Util;

final class Sf2CompatUtil
{
private static $isAtLeastSymfony28;
private static $formTypes = array(
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'doctrine_phpcr_odm_tree' => 'Sonata\DoctrinePHPCRAdminBundle\Form\Type\TreeModelType',
'sonata_type_immutable_array' => 'Sonata\CoreBundle\Form\Type\ImmutableArrayType',
'sonata_type_collection' => 'Sonata\CoreBundle\Form\Type\CollectionType',
'cmf_routing_route_type' => 'Symfony\Cmf\Bundle\RoutingBundle\Form\Type\RouteTypeType',
);

public static function registerFormType($name, $fqcn)
{
self::$formTypes[$name] = $fqcn;
}

public static function getFormTypeName($name)
{
if (!self::isAtLeastSymfony28()) {
return $name;
}

if (!isset(self::$formTypes[$name])) {
throw new \InvalidArgumentException(sprintf('Unknown form type "%s", please register the FQCN of this form type to the Sf2CompatUtil first.', $name));
}

return self::$formTypes[$name];
}

public static function isAtLeastSymfony28()
{
if (null === self::$isAtLeastSymfony28) {
self::$isAtLeastSymfony28 = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
}

return self::$isAtLeastSymfony28;
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"symfony-cmf/testing": "~1.2,>=1.2.7",
"doctrine/phpcr-odm": "~1.3",
"matthiasnoback/symfony-dependency-injection-test": "~0.6",
"matthiasnoback/symfony-config-test": "0.*",
"matthiasnoback/symfony-config-test": "^1.3.1",
"phpunit/php-code-coverage": "@stable",
"sonata-project/doctrine-phpcr-admin-bundle": "~1.1",
"sonata-project/doctrine-phpcr-admin-bundle": "^1.1|^2.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not allow sonata admin 2.0 as per #325 (comment)

"symfony/monolog-bundle": "~2.3",
"doctrine/orm": "~2.3",
"doctrine/data-fixtures": "1.*,>=1.0.0-alpha3"
Expand Down