Skip to content

Commit

Permalink
Fix DefaultManipulator by DoctrineORMManipulator (#8)
Browse files Browse the repository at this point in the history
* Fix DefaultManipulator by DoctrineORMManipulator
* Services simplification
* Other small changes
  • Loading branch information
webda2l authored Mar 22, 2018
1 parent 1347f21 commit a38afe6
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 101 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
2 changes: 1 addition & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ return PhpCsFixer\Config::create()
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'general_phpdoc_annotation_remove' => ['param', 'method', 'return', 'inheritdoc'],
'general_phpdoc_annotation_remove' => ['param', 'return'],
'header_comment' => ['header' => $header],
'linebreak_after_opening_tag' => true,
'list_syntax' => ['syntax' => 'short'],
Expand Down
64 changes: 32 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,41 @@ env:
- SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit"

matrix:
fast_finish: true
include:
# Minimum supported dependencies with the latest and oldest PHP version
- php: 7.1
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
- php: 7.2
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"

# Test the latest stable release
- php: 7.1
- php: 7.2
env: CHECK_PHP_SYNTAX="true"

# Latest commit to master
- php: 7.2
env: STABILITY="dev"

allow_failures:
# dev-master is allowed to fail.
- env: STABILITY="dev"
fast_finish: true
include:
# Minimum supported dependencies with the latest and oldest PHP version
- php: 7.1
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
- php: 7.2
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"

# Test the latest stable release
- php: 7.1
- php: 7.2
env: CHECK_PHP_SYNTAX="true"

# Latest commit to master
- php: 7.2
env: STABILITY="dev"

allow_failures:
# dev-master is allowed to fail.
- env: STABILITY="dev"

before_install:
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
- if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi;
- if ! [ -v "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi;
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
- if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi;
- if ! [ -v "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi;

install:
# To be removed when this issue will be resolved: https://github.com/composer/composer/issues/5355
- if [[ "$COMPOSER_FLAGS" == *"--prefer-lowest"* ]]; then composer update --prefer-dist --no-interaction --prefer-stable --quiet; fi
- composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
# Force most recent version of PHPUnit
- SYMFONY_PHPUNIT_VERSION=6.5 ./vendor/bin/simple-phpunit install
# To be removed when this issue will be resolved: https://github.com/composer/composer/issues/5355
- if [[ "$COMPOSER_FLAGS" == *"--prefer-lowest"* ]]; then composer update --prefer-dist --no-interaction --prefer-stable --quiet; fi
- composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
# Force most recent version of PHPUnit
- SYMFONY_PHPUNIT_VERSION=6.5 ./vendor/bin/simple-phpunit install

script:
- composer validate --strict --no-check-lock
# Force most recent version of PHPUnit
- SYMFONY_PHPUNIT_VERSION=6.5 ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
- if [[ "$CHECK_PHP_SYNTAX" == "true" ]]; then php ./vendor/bin/php-cs-fixer fix --no-interaction --dry-run --diff -v; fi;
- composer validate --strict --no-check-lock
# Force most recent version of PHPUnit
- SYMFONY_PHPUNIT_VERSION=6.5 ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
- if [[ "$CHECK_PHP_SYNTAX" == "true" ]]; then php ./vendor/bin/php-cs-fixer fix --no-interaction --dry-run --diff -v; fi;
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A2lix Auto Form Bundle

Automate form building
Automate form building.

[![Latest Stable Version](https://poser.pugx.org/a2lix/auto-form-bundle/v/stable)](https://packagist.org/packages/a2lix/auto-form-bundle)
[![Latest Unstable Version](https://poser.pugx.org/a2lix/auto-form-bundle/v/unstable)](https://packagist.org/packages/a2lix/auto-form-bundle)
Expand All @@ -11,9 +11,9 @@ Automate form building
[![Monthly Downloads](https://poser.pugx.org/a2lix/auto-form-bundle/d/monthly)](https://packagist.org/packages/a2lix/auto-form-bundle)
[![Daily Downloads](https://poser.pugx.org/a2lix/auto-form-bundle/d/daily)](https://packagist.org/packages/a2lix/auto-form-bundle)

Branch | Travis | Coveralls | Scrutinizer
------ | ------ | --------- | ----------- |
master | [![Build Status][travis_unstable_badge]][travis_unstable_link] | [![Coverage Status][coveralls_unstable_badge]][coveralls_unstable_link] | [![Scrutinizer Status][scrutinizer_unstable_badge]][scrutinizer_unstable_link] |
| Branch | Tools |
| --- | --- |
| master | [![Build Status](https://travis-ci.org/a2lix/AutoFormBundle.svg?branch=master)](https://travis-ci.org/a2lix/AutoFormBundle) [![Code Coverage](https://scrutinizer-ci.com/g/a2lix/AutoFormBundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/a2lix/AutoFormBundle/?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/a2lix/AutoFormBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/a2lix/AutoFormBundle/?branch=master) |

## Documentation

Expand All @@ -22,12 +22,3 @@ Check out the documentation on the [official website](http://a2lix.fr/bundles/au
## License

This package is available under the [MIT license](LICENSE).

[travis_unstable_badge]: https://travis-ci.org/a2lix/AutoFormBundle.svg?branch=master
[travis_unstable_link]: https://travis-ci.org/a2lix/AutoFormBundle?branch=master

[coveralls_unstable_badge]: https://coveralls.io/repos/github/a2lix/AutoFormBundle/badge.svg?branch=master
[coveralls_unstable_link]: https://coveralls.io/github/a2lix/AutoFormBundle?branch=master

[scrutinizer_unstable_badge]: https://scrutinizer-ci.com/g/a2lix/AutoFormBundle/badges/quality-score.png?b=master
[scrutinizer_unstable_link]: https://scrutinizer-ci.com/g/a2lix/AutoFormBundle/?branch=master
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
],
"require": {
"php": "^7.1.3",
"symfony/doctrine-bridge": "~3.4|~4.0",
"symfony/form": "~3.4|~4.0"
"symfony/config": "^3.4|^4.0",
"symfony/dependency-injection": "^3.4|^4.0",
"symfony/doctrine-bridge": "^3.4|^4.0",
"symfony/form": "^3.4|^4.0",
"symfony/http-kernel": "^3.4|^4.0"
},
"require-dev": {
"doctrine/orm": "~2.4",
"doctrine/orm": "^2.4",
"friendsofphp/php-cs-fixer": "^2.10",
"symfony/phpunit-bridge": "^4.0",
"symfony/validator": "~3.4|~4.0"
"symfony/validator": "^3.4|^4.0",
"vimeo/psalm": "^1.0"
},
"suggest": {
"a2lix/translation-form-bundle": "For translation form"
Expand Down
17 changes: 15 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@
<ini name="error_reporting" value="-1" />
</php>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>

<filter>
<whitelist>
<directory>src/</directory>
<exclude>
<directory>src/Resources</directory>
</exclude>
</whitelist>
</filter>

<testsuites>
<testsuite name="Project Test Suite">
<testsuite name="Bundle Test Suite">
<directory>tests/</directory>
<exclude>tests/fixtures</exclude>
<exclude>tests/Fixtures</exclude>
<exclude>tests/tmp</exclude>
</testsuite>
</testsuites>
Expand Down
37 changes: 37 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/home/david/Works/Demo/app/packages/AutoFormBundle/vendor/vimeo/psalm/src/config.xsd"
>
<projectFiles>
<directory name="src" />
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->
<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />

<MissingClosureReturnType errorLevel="info" />
<MissingReturnType errorLevel="info" />
<MissingPropertyType errorLevel="info" />
<InvalidDocblock errorLevel="info" />
<MisplacedRequiredParam errorLevel="info" />

<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor errorLevel="info" />
<MissingClosureParamType errorLevel="info" />
<MissingParamType errorLevel="info" />

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UnresolvableInclude errorLevel="info" />

<RawObjectIteration errorLevel="info" />
</issueHandlers>
</psalm>
6 changes: 4 additions & 2 deletions src/DependencyInjection/A2lixAutoFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('a2lix_form.xml');
$loader->load('object_info.xml');

$defaultManipulator = $container->getDefinition('a2lix_auto_form.default.manipulator');
$defaultManipulator->replaceArgument(1, $config['excluded_fields']);
$definition = $container->getDefinition('A2lix\AutoFormBundle\Form\Manipulator\DoctrineORMManipulator');
$definition->replaceArgument(1, $config['excluded_fields']);

$container->setAlias('a2lix_auto_form.manipulator.default', 'A2lix\AutoFormBundle\Form\Manipulator\DoctrineORMManipulator');
}
}
1 change: 1 addition & 0 deletions src/Form/EventListener/AutoFormListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

class AutoFormListener implements EventSubscriberInterface
{
/** @var FormManipulatorInterface */
private $formManipulator;

public function __construct(FormManipulatorInterface $formManipulator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@

namespace A2lix\AutoFormBundle\Form\Manipulator;

use A2lix\AutoFormBundle\ObjectInfo\ObjectInfoInterface;
use A2lix\AutoFormBundle\ObjectInfo\DoctrineORMInfo;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\Form\FormInterface;

class DefaultManipulator implements FormManipulatorInterface
class DoctrineORMManipulator implements FormManipulatorInterface
{
private $objectInfo;
/** @var DoctrineORMInfo */
private $doctrineORMInfo;
/** @var array */
private $globalExcludedFields;

public function __construct(ObjectInfoInterface $objectInfo, array $globalExcludedFields = [])
public function __construct(DoctrineORMInfo $doctrineORMInfo, array $globalExcludedFields = [])
{
$this->objectInfo = $objectInfo;
$this->doctrineORMInfo = $doctrineORMInfo;
$this->globalExcludedFields = $globalExcludedFields;
}

Expand All @@ -34,7 +36,7 @@ public function getFieldsConfig(FormInterface $form): array
$formOptions = $form->getConfig()->getOptions();

// Filtering to remove excludedFields
$objectFieldsConfig = $this->objectInfo->getFieldsConfig($class);
$objectFieldsConfig = $this->doctrineORMInfo->getFieldsConfig($class);
$usuableObjectFieldsConfig = $this->filteringUsuableFields($objectFieldsConfig, $formOptions['excluded_fields']);

if (empty($formOptions['fields'])) {
Expand Down Expand Up @@ -79,8 +81,10 @@ private function getDataClass(FormInterface $form): string
continue;
}

return $this->objectInfo->getAssociationTargetClass($dataClass, $form->getName());
return $this->doctrineORMInfo->getAssociationTargetClass($dataClass, $form->getName());
}

throw new \RuntimeException('Unable to get dataClass');
}

private function filteringUsuableFields(array $objectFieldsConfig, array $formExcludedFields): array
Expand Down
1 change: 1 addition & 0 deletions src/Form/Type/AutoFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

class AutoFormType extends AbstractType
{
/** @var AutoFormListener */
private $autoFormListener;

public function __construct(AutoFormListener $autoFormListener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

namespace A2lix\AutoFormBundle\ObjectInfo;

use A2lix\AutoFormBundle\Form\Type\AutoFormType;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class DoctrineInfo implements ObjectInfoInterface
class DoctrineORMInfo
{
/** @var ClassMetadataFactory */
private $classMetadataFactory;

public function __construct(ClassMetadataFactory $classMetadataFactory)
Expand Down Expand Up @@ -48,7 +51,7 @@ public function getAssociationTargetClass(string $class, string $fieldName): str
$metadata = $this->classMetadataFactory->getMetadataFor($class);

if (!$metadata->hasAssociation($fieldName)) {
throw new \Exception(sprintf('Unable to find the association target class of "%s" in %s.', $fieldName, $class));
throw new \RuntimeException(sprintf('Unable to find the association target class of "%s" in %s.', $fieldName, $class));
}

return $metadata->getAssociationTargetClass($fieldName);
Expand All @@ -69,7 +72,7 @@ private function getAssocsConfig(ClassMetadata $metadata, array $assocNames): ar
$nullable = ($metadata instanceof ClassMetadataInfo) && isset($metadata->discriminatorColumn['nullable']) && $metadata->discriminatorColumn['nullable'];

$assocsConfigs[$assocName] = [
'field_type' => 'A2lix\AutoFormBundle\Form\Type\AutoFormType',
'field_type' => AutoFormType::class,
'data_class' => $class,
'required' => !$nullable,
];
Expand All @@ -78,8 +81,8 @@ private function getAssocsConfig(ClassMetadata $metadata, array $assocNames): ar
}

$assocsConfigs[$assocName] = [
'field_type' => 'Symfony\Component\Form\Extension\Core\Type\CollectionType',
'entry_type' => 'A2lix\AutoFormBundle\Form\Type\AutoFormType',
'field_type' => CollectionType::class,
'entry_type' => AutoFormType::class,
'entry_options' => [
'data_class' => $class,
],
Expand Down
18 changes: 0 additions & 18 deletions src/ObjectInfo/ObjectInfoInterface.php

This file was deleted.

12 changes: 6 additions & 6 deletions src/Resources/config/a2lix_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

<services>
<!-- Form Manipulators -->
<service id="a2lix_auto_form.default.manipulator" class="A2lix\AutoFormBundle\Form\Manipulator\DefaultManipulator" public="false">
<argument type="service" id="a2lix_auto_form.doctrine_info" />
<service id="A2lix\AutoFormBundle\Form\Manipulator\DoctrineORMManipulator">
<argument type="service" id="A2lix\AutoFormBundle\ObjectInfo\DoctrineORMInfo" />
<argument type="collection"></argument>
</service>

<!-- Form Listeners -->
<service id="a2lix_auto_form.default.listener.autoform" class="A2lix\AutoFormBundle\Form\EventListener\AutoFormListener">
<argument type="service" id="a2lix_auto_form.default.manipulator" />
<service id="A2lix\AutoFormBundle\Form\EventListener\AutoFormListener">
<argument type="service" id="a2lix_auto_form.manipulator.default" />
</service>

<!-- Form Types -->
<service id="a2lix_auto_form.default.type.autoform" class="A2lix\AutoFormBundle\Form\Type\AutoFormType">
<argument type="service" id="a2lix_auto_form.default.listener.autoform" />
<service id="A2lix\AutoFormBundle\Form\Type\AutoFormType">
<argument type="service" id="A2lix\AutoFormBundle\Form\EventListener\AutoFormListener" />

<tag name="form.type" />
</service>
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/object_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="a2lix_auto_form.doctrine.metadata_factory" class="Doctrine\Common\Persistence\Mapping\ClassMetadataFactory" public="false">
<service id="a2lix_auto_form.doctrine.metadata_factory" class="Doctrine\Common\Persistence\Mapping\ClassMetadataFactory">
<factory service="doctrine.orm.default_entity_manager" method="getMetadataFactory" />
</service>

<service id="a2lix_auto_form.doctrine_info" class="A2lix\AutoFormBundle\ObjectInfo\DoctrineInfo" public="false">
<service id="A2lix\AutoFormBundle\ObjectInfo\DoctrineORMInfo">
<argument type="service" id="a2lix_auto_form.doctrine.metadata_factory" />
</service>
</services>
Expand Down
Loading

0 comments on commit a38afe6

Please sign in to comment.