Skip to content

Commit

Permalink
Implemented Symfony Compatibility layer/hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi committed Dec 3, 2015
1 parent 855da92 commit 6b03968
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 34 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ phpunit.xml
### NodeJS ###
##############
node_modules/

### JQueryValidation ###
########################
/.tmp/
27 changes: 23 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,42 @@ env:

matrix:
include:
- php: 5.6
env: |
- SYMFONY_VERSION=2.7.*
# [PHPUnit] Test symfony versions
- php: 5.3.3
env: |
COMPOSER_FLAGS='--prefer-stable --prefer-lowest'
PHPUNIT_COVERAGE='--coverage-text --coverage-clover=coverage.clover'
- php: 5.6
env: |
SYMFONY_VERSION=2.7.*
- php: 5.6
env: |
SYMFONY_VERSION=2.8.*
- php: 5.6
env: |
SYMFONY_VERSION=3.0.*
# [BEHAT] Test 2.8 without additionals in FF
- php: 5.4
env: |
SYMFONY_VERSION=2.8.*
TEST_SCOPE='javascript'
SYMFONY_ENVIRONMENT='test'
SYMFONY__ENABLE_ADDITIONALS='false'
addons:
firefox: "42.0"
# [BEHAT] Test 2.3 with additionals in FF
- php: 5.4
env: |
SYMFONY_VERSION=2.3.*
TEST_SCOPE='javascript'
SYMFONY_ENVIRONMENT='test'
SYMFONY__ENABLE_ADDITIONALS='true'
addons:
firefox: "42.0"
# [BEHAT] Test 3.0 with additionals in FF
- php: 5.6
env: |
COMPOSER_FLAGS='--prefer-stable --prefer-lowest'
SYMFONY_VERSION=3.0.*
TEST_SCOPE='javascript'
SYMFONY_ENVIRONMENT='test'
SYMFONY__ENABLE_ADDITIONALS='true'
Expand Down
8 changes: 4 additions & 4 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ default:
- FeatureContext
- Behat\MinkExtension\Context\MinkContext
filters:
tags: ~@serverSide&&~@additionals&&~@basic
tags: "~@serverSide&&~@additionals&&~@basic"
javascript_basic:
paths: [ %paths.base%/tests/Functional/features ]
contexts:
- FeatureContext
- Behat\MinkExtension\Context\MinkContext
filters:
tags: @basic
tags: "@basic"
javascript_additionals:
paths: [ %paths.base%/tests/Functional/features ]
contexts:
- FeatureContext
- Behat\MinkExtension\Context\MinkContext
filters:
tags: @additionals
tags: "@additionals"
server_side:
paths: [ %paths.base%/tests/Functional/features ]
contexts:
- FeatureContext:
- serverSide: true
- Behat\MinkExtension\Context\MinkContext
filters:
tags: ~@clientSide
tags: "~@clientSide"

extensions:
Behat\MinkExtension:
Expand Down
22 changes: 12 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
"description": "Jquery form validation bundle for symfony 2",
"require": {
"php": ">=5.3.3",
"symfony/framework-bundle": "~2.3",
"symfony/dependency-injection": "~2.3",
"symfony/form": "~2.3, >=2.3.4",
"symfony/validator": "~2.3",
"symfony/twig-bundle": "~2.3",
"symfony/twig-bridge": "~2.3",
"symfony/framework-bundle": "~2.3|~3.0",
"symfony/dependency-injection": "~2.3|~3.0",
"symfony/form": "~2.3, >=2.3.5|~3.0",
"symfony/validator": "~2.3|~3.0",
"symfony/twig-bundle": "~2.3|~3.0",
"symfony/twig-bridge": "~2.3|~3.0",
"twig/twig": "~1.14, >=1.14.2",
"doctrine/collections": "~1.0"
},
"require-dev": {
"symfony/browser-kit": "~2.3",
"symfony/dom-crawler": "~2.3",
"symfony/finder": "~2.3",
"symfony/yaml": "~2.3",
"symfony/browser-kit": "~2.3|~3.0",
"symfony/dom-crawler": "~2.3|~3.0",
"symfony/finder": "~2.3|~3.0",
"symfony/yaml": "~2.3|~3.0",
"symfony/css-selector": "~2.3|~3.0",

"phpunit/phpunit": "~4.7@stable",
"symfony/phpunit-bridge": "~2.7@stable",
Expand Down Expand Up @@ -46,5 +47,6 @@
"Tests\\Boekkooi\\Bundle\\JqueryValidationBundle\\": "tests/"
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<phpunit colors="true" bootstrap="tests/bootstrap.php">
<php>
<ini name="error_reporting" value="-1"/>
<server name="KERNEL_DIR" value="tests/Functional/app" />
</php>

Expand Down
3 changes: 2 additions & 1 deletion src/Form/Extension/ButtonTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\ClickableInterface;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

Expand Down Expand Up @@ -35,7 +36,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
*/
public function getExtendedType()
{
return 'button';
return FormHelper::isSymfony3Compatible() ? ButtonType::class : 'button';
}

protected function hasRuleBuilderContext(FormView $view)
Expand Down
3 changes: 2 additions & 1 deletion src/Form/Extension/FormTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
use Boekkooi\Bundle\JqueryValidationBundle\Validator\ConstraintCollection;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -150,7 +151,7 @@ public function configureOptions(OptionsResolver $resolver)
*/
public function getExtendedType()
{
return 'form';
return FormHelper::isSymfony3Compatible() ? 'Symfony\Component\Form\Extension\Core\Type\FormType' : 'form';
}

/**
Expand Down
19 changes: 15 additions & 4 deletions src/Form/FormDataConstraintFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\Validator\Mapping\CascadingStrategy;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\MemberMetadata;
use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;

/**
* @author Warnar Boekkooi <[email protected]>
Expand All @@ -21,8 +21,19 @@ class FormDataConstraintFinder
*/
private $metadataFactory;

public function __construct(MetadataFactoryInterface $metadataFactory)
/**
* Constructor.
* @param MetadataFactoryInterface $metadataFactory
*/
public function __construct($metadataFactory)
{
if (
!$metadataFactory instanceof MetadataFactoryInterface &&
!$metadataFactory instanceof \Symfony\Component\Validator\MetadataFactoryInterface
) {
throw new \InvalidArgumentException('metadataFactory must be a instanceof MetadataFactoryInterface');
}

$this->metadataFactory = $metadataFactory;
}

Expand Down Expand Up @@ -177,7 +188,7 @@ private function resolveDataClass(FormInterface $form)

// Now locate the closest data class
// TODO what is the length really for?
for ($i = $propertyPath->getLength(); $i != 0; $i--) {
for ($i = $propertyPath->getLength(); $i !== 0; $i--) {
$dataForm = $dataForm->getParent();

# When a data class is found then use that form
Expand Down Expand Up @@ -296,7 +307,7 @@ protected function findPropertyDataTypeInfo(MemberMetadata $propertyMetadata, $d
}

$type = strtolower($constraint->type);
$type = $type == 'boolean' ? 'bool' : $constraint->type;
$type = $type === 'boolean' ? 'bool' : $constraint->type;
$isFunction = 'is_' . $type;
$ctypeFunction = 'ctype_' . $type;
if (function_exists($isFunction) || function_exists($ctypeFunction)) {
Expand Down
8 changes: 7 additions & 1 deletion src/Form/Rule/Mapping/MaxLengthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public function supports(Constraint $constraint, FormInterface $form)
return false;
}

return !($constraintClass === 'Symfony\Component\Validator\Constraints\Length' && $this->isType($form, 'choice'));
return !(
$constraintClass === 'Symfony\Component\Validator\Constraints\Length' &&
(FormHelper::isSymfony3Compatible() ?
$this->isType($form, 'Symfony\Component\Form\Extension\Core\Type\ChoiceType') :
$this->isType($form, 'choice')
)
);
}

protected function isType(FormInterface $type, $typeName)
Expand Down
11 changes: 10 additions & 1 deletion src/Form/Rule/Mapping/MinLengthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Length;

/**
* @author Warnar Boekkooi <[email protected]>
Expand Down Expand Up @@ -48,7 +51,13 @@ public function supports(Constraint $constraint, FormInterface $form)
return false;
}

return !($constraintClass === 'Symfony\Component\Validator\Constraints\Length' && $this->isType($form, 'choice'));
return !(
$constraintClass === 'Symfony\Component\Validator\Constraints\Length' &&
(FormHelper::isSymfony3Compatible() ?
$this->isType($form, 'Symfony\Component\Form\Extension\Core\Type\ChoiceType') :
$this->isType($form, 'choice')
)
);
}

protected function isType(FormInterface $type, $typeName)
Expand Down
23 changes: 22 additions & 1 deletion src/Form/Util/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@
*/
final class FormHelper
{
public static function isSymfony3Compatible()
{
return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
}

public static function isSymfony2Compatible()
{
return method_exists('Symfony\Component\Form\ResolvedFormTypeInterface', 'getName');
}

public static function isType(ResolvedFormTypeInterface $type, $typeName)
{
do {
if ($type->getName() === $typeName) {
if (
self::isSymfony3Compatible() &&
get_class($type->getInnerType()) === $typeName
) {
return true;
}

if (
self::isSymfony2Compatible() &&
$type->getName() === $typeName
) {
return true;
}

$type = $type->getParent();
} while ($type !== null);

Expand Down
12 changes: 6 additions & 6 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ services:
boekkooi.jquery_validation.form.form_extension:
class: %boekkooi.jquery_validation.form.form_extension.class%
arguments:
- @boekkooi.jquery_validation.rule_processor
- @boekkooi.jquery_validation.rule_compiler
- @boekkooi.jquery_validation.constraint_finder
- "@boekkooi.jquery_validation.rule_processor"
- "@boekkooi.jquery_validation.rule_compiler"
- "@boekkooi.jquery_validation.constraint_finder"
- %boekkooi.jquery_validation.enabled%
tags:
- { name: form.type_extension, alias: form }
- { name: form.type_extension, alias: form, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }

boekkooi.jquery_validation.form.button_extension:
class: %boekkooi.jquery_validation.form.button_extension.class%
tags:
- { name: form.type_extension, alias: button }
- { name: form.type_extension, alias: button, extended_type: Symfony\Component\Form\Extension\Core\Type\ButtonType }

boekkooi.jquery_validation.rule_processor:
class: %boekkooi.jquery_validation.rule_processor.class%
Expand All @@ -36,4 +36,4 @@ services:
boekkooi.jquery_validation.constraint_finder:
class: %boekkooi.jquery_validation.constraint_finder.class%
arguments:
- @validator.mapping.class_metadata_factory
- "@validator.mapping.class_metadata_factory"

0 comments on commit 6b03968

Please sign in to comment.