diff --git a/.travis.yml b/.travis.yml index 7c16ac1..2340d33 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,8 @@ matrix: env: SYMFONY_VERSION=2.3.* - php: 5.6 env: SYMFONY_VERSION=2.8.* + - php: 5.6 + env: SYMFONY_VERSION=3.0.* fast_finish: true before_install: diff --git a/Form/Type/FileType.php b/Form/Type/FileType.php index dc428dd..c0654ce 100644 --- a/Form/Type/FileType.php +++ b/Form/Type/FileType.php @@ -13,6 +13,7 @@ use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelperInterface; use Symfony\Cmf\Bundle\MediaBundle\Form\DataTransformer\ModelToFileTransformer; +use Symfony\Cmf\Bundle\MediaBundle\Util\LegacyFormHelper; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -56,7 +57,7 @@ public function __construct($class, UploadFileHelperInterface $uploadFileHelper) */ public function getParent() { - return 'file'; + return LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\FileType'); } /** diff --git a/Tests/Resources/Controller/PhpcrFileTestController.php b/Tests/Resources/Controller/PhpcrFileTestController.php index b4d3480..bdc7abe 100644 --- a/Tests/Resources/Controller/PhpcrFileTestController.php +++ b/Tests/Resources/Controller/PhpcrFileTestController.php @@ -16,6 +16,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelperInterface; use Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content; +use Symfony\Cmf\Bundle\MediaBundle\Util\LegacyFormHelper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -23,10 +24,8 @@ class PhpcrFileTestController extends Controller { public function getUploadForm() { - $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Component\Form\Extension\Core\Type\FileType' : 'file'; - - return $this->container->get('form.factory')->createNamedBuilder(null, 'form') - ->add('file', $type) + return $this->container->get('form.factory')->createNamedBuilder(null, LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\FormType')) + ->add('file', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\FileType')) ->getForm() ; } @@ -38,12 +37,10 @@ protected function getContentForm(Content $contentObject = null) $contentObject = new Content(); } - $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Cmf\Bundle\MediaBundle\Form\Type\FileType' : 'cmf_media_file'; - return $this->createFormBuilder($contentObject) ->add('name') ->add('title') - ->add('file', $type, array('required' => $is_new)) + ->add('file', LegacyFormHelper::getType('Symfony\Cmf\Bundle\MediaBundle\Form\Type\FileType'), array('required' => $is_new)) ->getForm() ; } diff --git a/Tests/Resources/Controller/PhpcrImageTestController.php b/Tests/Resources/Controller/PhpcrImageTestController.php index 4bfe325..a5c2234 100644 --- a/Tests/Resources/Controller/PhpcrImageTestController.php +++ b/Tests/Resources/Controller/PhpcrImageTestController.php @@ -17,6 +17,7 @@ use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image; use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelperInterface; use Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content; +use Symfony\Cmf\Bundle\MediaBundle\Util\LegacyFormHelper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -24,10 +25,8 @@ class PhpcrImageTestController extends Controller { protected function getUploadForm() { - $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Component\Form\Extension\Core\Type\FileType' : 'file'; - - return $this->container->get('form.factory')->createNamedBuilder(null, 'form') - ->add('image', $type) + return $this->container->get('form.factory')->createNamedBuilder(null, LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\FormType')) + ->add('image', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\FileType')) ->getForm() ; } @@ -37,12 +36,11 @@ protected function getContentForm(Content $contentObject = null, array $imageOpt if (is_null($contentObject)) { $contentObject = new Content(); } - $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Cmf\Bundle\MediaBundle\Form\Type\ImageType' : 'cmf_media_image'; return $this->createFormBuilder($contentObject) ->add('name') ->add('title') - ->add('file', $type, array_merge(array('required' => false, 'label' => 'Image'), $imageOptions)) + ->add('file', LegacyFormHelper::getType('Symfony\Cmf\Bundle\MediaBundle\Form\Type\ImageType'), array_merge(array('required' => false, 'label' => 'Image'), $imageOptions)) ->getForm() ; } diff --git a/Tests/Resources/app/config/cmf_media.yml b/Tests/Resources/app/config/cmf_media.yml index 7f2cce7..2fc0c42 100644 --- a/Tests/Resources/app/config/cmf_media.yml +++ b/Tests/Resources/app/config/cmf_media.yml @@ -1,8 +1,3 @@ -twig: - form: - resources: - - 'CmfMediaBundle:Form:fields.html.twig' - cmf_media: persistence: phpcr: diff --git a/Tests/Resources/app/config/cmf_media_2.yml b/Tests/Resources/app/config/cmf_media_2.yml new file mode 100644 index 0000000..dda0705 --- /dev/null +++ b/Tests/Resources/app/config/cmf_media_2.yml @@ -0,0 +1,7 @@ +imports: + - { resource: cmf_media.yml } + +twig: + form: + resources: + - 'CmfMediaBundle:Form:fields.html.twig' \ No newline at end of file diff --git a/Tests/Resources/app/config/cmf_media_3.yml b/Tests/Resources/app/config/cmf_media_3.yml new file mode 100644 index 0000000..743e538 --- /dev/null +++ b/Tests/Resources/app/config/cmf_media_3.yml @@ -0,0 +1,6 @@ +imports: + - { resource: cmf_media.yml } + +twig: + form_themes: + - 'CmfMediaBundle:Form:fields.html.twig' \ No newline at end of file diff --git a/Tests/Resources/app/config/config.php b/Tests/Resources/app/config/config.php index d12d1f8..3658bf8 100644 --- a/Tests/Resources/app/config/config.php +++ b/Tests/Resources/app/config/config.php @@ -12,5 +12,9 @@ $container->setParameter('cmf_testing.bundle_fqn', 'Symfony\Cmf\Bundle\MediaBundle'); $loader->import(CMF_TEST_CONFIG_DIR.'/default.php'); $loader->import(CMF_TEST_CONFIG_DIR.'/phpcr_odm.php'); -$loader->import(__DIR__.'/cmf_media.yml'); +if (version_compare(strtolower(\Symfony\Component\HttpKernel\Kernel::VERSION), '3.0', '<')) { + $loader->import(__DIR__.'/cmf_media_2.yml'); +} else { + $loader->import(__DIR__.'/cmf_media_3.yml'); +} $loader->import(__DIR__.'/security.yml'); diff --git a/Util/LegacyFormHelper.php b/Util/LegacyFormHelper.php new file mode 100644 index 0000000..ec0a339 --- /dev/null +++ b/Util/LegacyFormHelper.php @@ -0,0 +1,48 @@ + 'cmf_media_file', + 'Symfony\Cmf\Bundle\MediaBundle\Form\Type\ImageType' => 'cmf_media_image', + 'Symfony\Component\Form\Extension\Core\Type\FileType' => 'file', + 'Symfony\Component\Form\Extension\Core\Type\FormType' => 'form', + ); + + public static function getType($class) + { + if (!self::isLegacy()) { + return $class; + } + + if (!isset(self::$map[$class])) { + throw new \InvalidArgumentException(sprintf('Form type with class "%s" can not be found. Please check for typos or add it to the map in LegacyFormHelper', $class)); + } + + return self::$map[$class]; + } + + public static function isLegacy() + { + return !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'); + } + + private function __construct() + { + } + + private function __clone() + { + } +} diff --git a/composer.json b/composer.json index d70f9c0..3b9f9cd 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": "^5.3.9|^7.0", - "symfony/framework-bundle": "~2.3" + "symfony/framework-bundle": "~2.3|~3.0" }, "require-dev": { "twig/twig": "^1.12|^2.0", @@ -21,7 +21,7 @@ "doctrine/phpcr-odm": "^1.3", "doctrine/annotations": "^1.1.2", "jms/serializer-bundle": "^0.12|^1.0", - "liip/imagine-bundle": "~1.0.4", + "liip/imagine-bundle": "^1.0.4", "mikey179/vfsStream": "^1.2" }, "suggest": {