Commit Graph

50958 Commits

Author SHA1 Message Date
Christian Flothmann
86a7e3289b drop logger mock in favor of using the BufferingLogger 2020-09-18 14:06:50 +02:00
Alexander M. Turek
14642fb6dd Added missing changelog entries. 2020-09-18 12:11:46 +02:00
Christian Flothmann
e299544639 minor #38231 [Validator] catch ValueError thrown on PHP 8 (xabbuh)
This PR was merged into the 4.4 branch.

Discussion
----------

[Validator] catch ValueError thrown on PHP 8

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

see php/php-src@95f4ee38bb

Commits
-------

fb0d7beaaf catch ValueError thrown on PHP 8
2020-09-18 12:06:31 +02:00
Fabien Potencier
2eeb75dd6f bug #38228 [Yaml Parser] Fix edge cases when parsing multiple documents (digilist)
This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml Parser] Fix edge cases when parsing multiple documents

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

I identified some edge cases when parsing multiple YAML documents with the same parser instance, because the totalNumberOfLines was not reset and so any subsequent parsing considered the number of lines of the first document.

Consider this document:
```yaml
a:
    b: |
        row
        row2
c: d
```

Normally, `a.b` would be parsed as `row\nrow2\n`. But if the parser parsed a shorter document before, the `\n` after row2 was missing, as the parser considered it as the end of the file (that's why the `c: d` at the end is important).

So this fix resets the `totalNumberOfLines` in the YAML parser to `null` so that any subsequent parsing will initialize the value for the new document and does not use the file length of the first parsed document.

I stumbled upon this because of a flickering unit test that was using the translation component. Sometimes the translated string contained a trailing `\n` and sometimes not. In the end it was based on this bug, as the translation files were not loaded in the same order every time (not really sure why. It's somehow related to the cache state, but even with a warm cache it was not totally deterministic).

Commits
-------

012ee4fa59 [Yaml Parser] Fix edge cases when parsing multiple documents
2020-09-18 12:04:33 +02:00
Fabien Potencier
2384f0812f bug #38226 [FrameworkBundle] loadRoutes shoud receive RoutingPhpFileLoader (grachevko)
This PR was merged into the 5.1 branch.

Discussion
----------

[FrameworkBundle] loadRoutes shoud receive RoutingPhpFileLoader

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| License       | MIT
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->

In case when `sensio_framework_extra.router.annotations` options is set to `true` or not defined will throw this error

> In RoutingConfigurator.php line 28:
>
>  Argument 2 passed to Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator::__construct() must be an instance of Symfony\Component\Routing\Loader\PhpFileLoader, instance of Symfony\Component\Routing\Loader\AnnotationFileLoader given, called in /opt/app/vendor/symfony/framework-bundle/Kernel/MicroKernelTrait.php on line 176

Commits
-------

25a1131e47 loadRoutes shoud receive RoutingPhpFileLoader
2020-09-18 12:02:31 +02:00
Christian Flothmann
fb0d7beaaf catch ValueError thrown on PHP 8 2020-09-18 11:45:13 +02:00
Christian Flothmann
74f41f3313 bug #38229 [Yaml] fix parsing comments not prefixed by a space (xabbuh)
This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml] fix parsing comments not prefixed by a space

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #38223
| License       | MIT
| Doc PR        |

Commits
-------

35b223aaa4 fix parsing comments not prefixed by a space
2020-09-18 11:29:40 +02:00
Markus Fasselt
012ee4fa59 [Yaml Parser] Fix edge cases when parsing multiple documents 2020-09-18 11:21:31 +02:00
Christian Flothmann
35b223aaa4 fix parsing comments not prefixed by a space 2020-09-18 11:15:17 +02:00
Fabien Potencier
d6e2c5e7db feature #35740 [MonologBridge] Use composition instead of inheritance in monolog bridge (pvgnd, mm-pvgnd)
This PR was merged into the 5.2-dev branch.

Discussion
----------

[MonologBridge] Use composition instead of inheritance in monolog bridge

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       |
| License       | MIT
| Doc PR        |

In order to allow better customization in monolog activation strategy, this PR introduce composition instead of inheritance for these 2 classes :
- HttpCodeActivationStrategy
- NotFoundActivationStrategy

Commits
-------

0fb14394ca Use composition instead of inheritance in HttpCodeActivationStrategy
2020-09-18 10:50:13 +02:00
pvgnd
0fb14394ca Use composition instead of inheritance in HttpCodeActivationStrategy 2020-09-18 10:50:00 +02:00
Fabien Potencier
785a06653c bug #38127 [Translator] Make sure a null locale is handled properly (jschaedl)
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Translator] Make sure a null locale is handled properly

| Q             | A
| ------------- | ---
| Branch?       | 4.4<!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #38124  <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | - <!-- required for new features -->
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->

Commits
-------

080ea5a0f7 [Translator] Make sure a null locale is handled properly
2020-09-18 10:46:54 +02:00
Jan Schädlich
080ea5a0f7 [Translator] Make sure a null locale is handled properly 2020-09-18 10:46:47 +02:00
Christian Flothmann
948e107a1b Merge branch '3.4' into 4.4
* 3.4:
  deal with errors being thrown on PHP 8
2020-09-18 10:35:10 +02:00
Christian Flothmann
ba6575752a minor #38227 [VarDumper] deal with errors being thrown on PHP 8 (xabbuh)
This PR was merged into the 3.4 branch.

Discussion
----------

[VarDumper] deal with errors being thrown on PHP 8

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Commits
-------

6175d528e8 deal with errors being thrown on PHP 8
2020-09-18 10:32:31 +02:00
Christian Flothmann
6175d528e8 deal with errors being thrown on PHP 8 2020-09-18 10:10:16 +02:00
Fabien Potencier
c56a621176 bug #38225 WebProfiler 5.2 is incompatible with HttpKernel 5.1 (derrabus)
This PR was merged into the 5.2-dev branch.

Discussion
----------

WebProfiler 5.2 is incompatible with HttpKernel 5.1

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

I have upgraded WebProfilerBundle to master in my project. Afterwards, the profiler crashes with the following error.

<img width="1049" alt="Bildschirmfoto 2020-09-17 um 21 45 23" src="https://user-images.githubusercontent.com/1506493/93520652-1a67a280-f92f-11ea-8f05-c72cbd996f19.png">

Looks like the changes of #36364 require HttpKernel to be bumped to 5.2 as well, so this PR suggests to do just that.

Commits
-------

115d6859d9 WebProfiler 5.2 is incompatible with HttpKernel 5.1
2020-09-18 08:10:00 +02:00
Konstantin Grachev
25a1131e47 loadRoutes shoud receive RoutingPhpFileLoader 2020-09-17 23:43:39 +03:00
Alexander M. Turek
115d6859d9 WebProfiler 5.2 is incompatible with HttpKernel 5.1 2020-09-17 21:51:26 +02:00
Fabien Potencier
791761277d bug #38221 [Cache] Allow cache tags to be objects implementing __toString() (lstrojny)
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Cache] Allow cache tags to be objects implementing __toString()

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | maybe
| New feature?  | maybe
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

`\Symfony\Contracts\Cache\CacheInterface::get(string $key, …)` implicitly converts objects with `__toString` while `CacheItem::tag()` will throw an exception. That’s a bit of a sharp edge.

Commits
-------

c2c03e050f [Cache] Allow cache tags to be objects implementing __toString()
2020-09-17 12:06:33 +02:00
Lars Strojny
c2c03e050f [Cache] Allow cache tags to be objects implementing __toString() 2020-09-17 12:06:27 +02:00
Fabien Potencier
2f67bf3a37 bug #38212 [HttpKernel] Do not override max_redirects option in HttpClientKernel (dmolineus)
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpKernel] Do not override max_redirects option in HttpClientKernel

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #38207
| License       | MIT
| Doc PR        | -

As [proposed](https://github.com/symfony/symfony/issues/38207#issuecomment-693382336) by @nicolas-grekas this pull request removes the `max_redirects` setting in the `\Symfony\Component\HttpKernel\HttpClientKernel::handle` method.

It solves the issue that requests made by the `\Symfony\Component\HttpClient\CachingHttpClient` wouldn't follow redirects as described in the linked issue.

Commits
-------

981a11beed [HttpKernel] Do not override max_redirects option in HttpClientKernel
2020-09-17 12:05:06 +02:00
David Molineus
981a11beed [HttpKernel] Do not override max_redirects option in HttpClientKernel 2020-09-17 12:05:00 +02:00
Fabien Potencier
4d1a5222b3 minor #38220 [Security] Log notice when no entry point is configured (wouterj)
This PR was merged into the 5.1 branch.

Discussion
----------

[Security] Log notice when no entry point is configured

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37068
| License       | MIT
| Doc PR        | -

In the new security system, authenticators are not necessarily authentication entry points. This can cause unexpected behavior if no entry point is configured. It's not really an error, that's why I choose the "notice" level: "Normal but significant events".

Commits
-------

68f891ff65 Log notice when no entry point is configured
2020-09-17 12:03:51 +02:00
Wouter de Jong
68f891ff65 Log notice when no entry point is configured 2020-09-17 11:39:20 +02:00
Fabien Potencier
8d450130e0 feature #38182 [HttpClient] Added RetryHttpClient (jderusse)
This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[HttpClient] Added RetryHttpClient

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | /
| License       | MIT
| Doc PR        | TODO

This PR adds a new HttpClient decorator to automatically retry failed requests.

When calling API, A very small % of requests are expected to timeout due to transient network issues. Some providers like AWS recommends retrying these requests and use a lower connection timeout so that the clients can fail fast and retry.

I used the almost the same configuration as Messenger
```yaml
framework:
    http_client:
        default_options:
            retry_failed:
                enabled: true // default false
                decider_service: null
                backoff_service: null
                http_codes: [423, 425, 429, 500, 502, 503, 504, 507, 510]
                max_retries: 3
                delay: 1000
                multiplier: 2
                max_delay: 0
                response_header: true
        scoped_clients:
            github:
                scope: 'https://api\.github\.com'
                retry_failed:
                    max_delay: 2000
```

Commits
-------

712ac5999d [HttpClient] Added RetryHttpClient
2020-09-17 11:07:30 +02:00
Jérémy Derussé
712ac5999d [HttpClient] Added RetryHttpClient 2020-09-17 11:07:22 +02:00
Fabien Potencier
86c7113994 Merge branch '5.1'
* 5.1:
  [String] improve slugger's portability accross implementations of iconv()
  Fix errors parsing in FirebaseTransport
2020-09-17 09:22:48 +02:00
Fabien Potencier
237d91f3b2 feature #38204 [Security] Added login throttling feature (wouterj)
This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[Security] Added login throttling feature

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #37266
| License       | MIT
| Doc PR        | tbd

This "recreates" #37444 based on the RateLimiter component from #37546 <s>(commits are included in this branch atm)</s>.

Login throttling can be enabled on any user-based authenticator (thanks to the `UserBadge`) with this configuration:

```yaml
security:
    firewalls:
        default:
            # default limits to 5 login attempts per minute, the number can be configured via "max_attempts"
            login_throttling: ~

            # or you can define your own RateLimiter on framework.rate_limiter and configure it instead:
            login_throttling:
                limiter: login
```

Commits
-------

afdd805b1c [Security] Added login throttling feature
2020-09-17 07:36:54 +02:00
Wouter de Jong
afdd805b1c [Security] Added login throttling feature 2020-09-17 07:36:46 +02:00
Fabien Potencier
ad226185af bug #38215 [HttpClient] Support for CURLOPT_LOCALPORT (derrabus)
This PR was merged into the 4.4 branch.

Discussion
----------

[HttpClient] Support for CURLOPT_LOCALPORT

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | https://github.com/symfony/symfony/issues/38081#issuecomment-688166943
| License       | MIT
| Doc PR        | N/A

Commits
-------

45fa6b8f24 [HttpClient] Support for CURLOPT_LOCALPORT.
2020-09-17 06:58:25 +02:00
Fabien Potencier
c965be8aaa minor #38214 Upgrade PHPUnit to 8.5 (php 7.2) and 9.3 (php >= 7.3) (derrabus)
This PR was merged into the 4.4 branch.

Discussion
----------

Upgrade PHPUnit to 8.5 (php 7.2) and 9.3 (php >= 7.3)

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | #37564
| License       | MIT
| Doc PR        | N/A

This is #38103 on the 4.4 branch.

Commits
-------

73647e5ffe Upgrade PHPUnit to 8.5 (php 7.2) and 9.3 (php >= 7.3).
2020-09-17 06:54:14 +02:00
Fabien Potencier
ee0f5a2a4d Merge branch '3.4' into 4.4
* 3.4:
  remove superfluous cast
  Change 'cache_key' to AbstractRendererEngine::CACHE_KEY_VAR
  Upgrade PHPUnit to 8.5 (php 7.2) and 9.3 (php >= 7.3).
2020-09-17 06:53:48 +02:00
Fabien Potencier
6b8857c974 minor #38216 Patch YAML superfluous cast (ktomk)
This PR was merged into the 3.4 branch.

Discussion
----------

Patch YAML superfluous cast

| Q             | A
| ------------- | ---
| Branch?       | master for features / 3.4, 4.4 or 5.1 for bug fixes <!-- see below --> - 3.4 as branch to patch against as documented
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | -/- <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT

Commits
-------

05a9660a1e remove superfluous cast
2020-09-17 06:49:02 +02:00
Tom Klingenberg
05a9660a1e remove superfluous cast
in YAML component.

minor.
2020-09-17 01:52:20 +02:00
Alexander M. Turek
45fa6b8f24 [HttpClient] Support for CURLOPT_LOCALPORT. 2020-09-17 00:10:08 +02:00
Alexander M. Turek
73647e5ffe Upgrade PHPUnit to 8.5 (php 7.2) and 9.3 (php >= 7.3). 2020-09-16 23:25:34 +02:00
Fabien Potencier
9b81056503 Add a missing replace rule in the main composer.json file 2020-09-16 18:15:11 +02:00
Fabien Potencier
83910df242 feature #38007 [Amqp] Add amqps support (vasilvestre-OS)
This PR was merged into the 5.2-dev branch.

Discussion
----------

[Amqp] Add amqps support

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #37002
| License       | MIT
| Doc PR        | symfony/symfony-docs#... TODO <!-- required for new features -->

### Cases

_Case 1 (simple):_
I want to set up my app with a locally installed amqp broker supporting SSL. No auth is needed, no vhost, only SSL certificate file located at /etc/ssl/cafile.
'amqps://'

_Case 2 (complex):_
I use a special port, special file locations for SSL. I pass it through php.ini or configuration.

### Questions

1) Actually AmqpTransportFactory use `supports` function to see if 'amqp://' is contained in dsn string and accept cacert argument to use SSL
How are we supposed to cover AMQPS's case in your opinion?

cacert argument is settable through php.ini and code.

### Observations

I think this PR should aim at:

- Having correct defaults ssl values in case of 'amqps://' eg : case 1
- Being able to pass cacert file path and verify argument. eg : case 2
- Test: but test what?

#### EDIT

As discuted with @nicolas-grekas, we should check that cacert is existing in php.ini or is passed in configuration and throw an exception to give a nice feedback about what's wrong. Php amqp lib isn't actually precise about why it fails when you forget it.

### Steps

- [x] Ensure AMQPS can be used out of the box without DSN.
- [x] If not, try to modify vendor directly to fix it.
- [x] Modify vendor to make AMQPS works with DSN.
- [x] Check cacert to throw a better exception.
- [x] Add test related to last point.
- [ ] Pass CI

Commits
-------

99fc3f3b89 [Amqp] Add amqps support
2020-09-16 15:50:25 +02:00
Fabien Potencier
7cd5bbf458 feature #37546 [RFC] Introduce a RateLimiter component (wouterj)
This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[RFC] Introduce a RateLimiter component

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Refs #37444
| License       | MIT
| Doc PR        | tbd

Based on the discussions in #37444, I decided to write a general purpose RateLimiter component. This implementation uses the token bucket algorithm, inspired by the [Go's time/rate](3af7569d3a/rate/rate.go) library and the [PHP `bandwidth-throttle/token-bucket` package](https://github.com/bandwidth-throttle/token-bucket) (which is [unmaintained for years](https://github.com/bandwidth-throttle/token-bucket/issues/19)).

### Usage

The component has two main methods:

* `Limiter::reserve(int $tokens, int $maxTime)`, allocates `$tokens` and returns a `Reservation` containing the wait time. Use this method if your process wants to wait before consuming the token.
* `Limiter::consume(int $tokens)`, checks if `$tokens` are available now and discards the reservation if that's not the case. Use this method if you want to skip when there are not enough tokens at this moment.

The component uses the Lock component to make sure it can be used in parallel processes.

Example:

```php
<?php

namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\RateLimiter\LimiterFactory;

class LimitListener
{
    private $limiterFactory;

    public function __construct(LimiterFactory $apiLimiterFactory)
    {
        $this->limiterFactory = $apiLimiterFactory;
    }

    public function __invoke(RequestEvent $event)
    {
        $ip = $event->getRequest()->getClientIp();

        $limiter = $this->limiterFactory->createLimiter(preg_replace('/[^a-zA-Z0-9]/', '-', $ip));
        if (!$limiter->consume()) {
            $event->setResponse(new Response('Too many requests', 429));
        }
    }
}
```

### Usefullness of the component

I think a generic rate limiter is usefull in quite some places:

* Add a login throttling feature in Symfony
* <s>Rate limiting outgoing API calls (e.g. HttpClient), to prevent hitting upstream API limits.</s> See #37471 (and https://blog.heroku.com/rate-throttle-api-client )
* Allowing users to easily implement API rate limits in their own Symfony-based APIs.

### State of the art

There are some rate limiting packages in PHP, but I think there is no precendent for this component:

* [`graham-campbell/throttle`](https://github.com/GrahamCampbell/Laravel-Throttle) is heavily relying on Laravel. It is however very popular, proofing there is a need for such feature
* [`nikolaposa/rate-limit`](https://github.com/nikolaposa/rate-limit) does not implement reservation of tokens and as such less feature complete. Also its architecture combines the rate limiter and storage, making it harder to implement different storages.

### Todo

If it is agreed that this component can bring something to Symfony, it needs some more love:

* [x] Add more tests
* [x] Integrate with the FrameworkBundle
* [x] Add sliding window implementation
* [x] Add integration with the Security component
* <s>Maybe add more storage implementations? I didn't want to duplicate storage functionalities already existing in the Lock and Cache component, thus I for now focused mostly on integrating the Cache adapters. But maybe a special Doctrine adapter makes sense?</s>

Commits
-------

67417a693e [RFC] Introduce a RateLimiter component
2020-09-16 15:45:08 +02:00
Wouter de Jong
67417a693e [RFC] Introduce a RateLimiter component 2020-09-16 15:45:01 +02:00
Fabien Potencier
128bbd11eb feature #38193 Make RetryTillSaveStore implements the SharedLockStoreInterface (jderusse)
This PR was merged into the 5.2-dev branch.

Discussion
----------

Make RetryTillSaveStore implements the SharedLockStoreInterface

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | /
| License       | MIT
| Doc PR        | /

The `RetryTillSaveStore` decorates a non blocking store (ie. `RedisStore`) to make it blockable.
The issue is: if the decorate store manage SharedLock, the Decorator does not implements this interface. It's currently not possible to get both `blocking` and `sharable` store.

Commits
-------

a0321e66c9 Make RetryTillSaveStore implements the SharedLockStoreInterface
2020-09-16 14:38:44 +02:00
Fabien Potencier
c34865f175 feature #37968 [Form] Add new way of mapping data using callback functions (yceruto)
This PR was merged into the 5.2-dev branch.

Discussion
----------

[Form] Add new way of mapping data using callback functions

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | yes
| Tickets       | Fix #37597 (partially)
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/14241

Replaces https://github.com/symfony/symfony/pull/37614

## What this solves

Objects and Forms have different mechanisms for structuring data. When you build an object model with a lot of business logic it's valuable to use these mechanisms to better collect the data and the behavior that goes with it. Doing so leads to variant schemas; that is, the object model schema and the form schema don't match up.

You still need to transfer data between the two schemas, and this data transfer becomes a complexity in its own right. If the objects know about the form structure, changes in one tend to ripple to the other.

Currently, the Data Mapper layer separates the objects from the form, transfering data between the two and also isolating them from each other. That's fine, but at present the default data mapper has a limitation: _it's very tied to one property path_ (see [`PropertyPathMapper`](https://github.com/symfony/symfony/blob/5.1/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php)).

That said, you'll have to write your own data mapper in the following situations:
 * When the property path differs for reading and writing
 * When several form fields are mapped to a single method
 * When you need to read data based on the model's state
 * When the mapping of the model depends on the submitted form data
 * ...

Also, when we create a new data mapper, we usually forget about checking the status of the given data and forms. Whether the data is empty or not; throw an exception if the given data is not an object/array and whether the form field is submitted/synchronized/disabled or not. Not doing that could lead to unwanted behavior.

## What this proposes

Create a new way to write and read values to/from an object/array using callback functions. This feature would be tied to each form field and would also mean a new way of mapping data, but a very convenient one, in which it won't be necessary to define a new data mapper and take into account all what it would imply when you only need to map one field in a different manner or perhaps in only one direction (writing or reading the value).

This PR adds two new options for each form type: `getter` and `setter`, allowed to be `null` or `callable`:
```php
$builder->add('name', TextType::class, [
    'getter' => function (Person $person, FormInterface $form): string {
        return $person->firstName().' '.$person->lastName();
    },
    'setter' => function (Person &$person, ?string $name, FormInterface $form): void {
        $person->rename($name);
    },
]);
```
This would give us the same possibilities as data mappers, but within the form field scope, where:
 * `$person` is the view data, basically the underlying data to the form.
 * `$form` is the current child form that is being mapped.
 * `$name` is the submitted data that belongs to that field.

These two callbacks will be executed following the same rules as for property paths before read and write any value (e.i. early return if empty data, skip mapping if the form field is not mapped or it's disabled, etc).

## What this also proposes

I based the implementation on solving this problem first:

> https://github.com/symfony/symfony/pull/37614#issuecomment-662957865
> [...] the property_path option defines the rules on how it's accessed. From there, the actual way it is accessed (direct property access, accessors, reflection, whatever) are hidden from view. All that matters is the property path (which is deduced from the name if not explicitly set). [...]

So splitting the default data mapper `PropertyPathMapper` into two artifacts: "[DataMapper](https://github.com/yceruto/symfony/blob/data_accessor/src/Symfony/Component/Form/Extension/Core/DataMapper/DataMapper.php)" and "[DataAccessor](https://github.com/yceruto/symfony/blob/data_accessor/src/Symfony/Component/Form/DataAccessorInterface.php)" would allow us adding multiple data accessors along the way (the code that varies in this case) without having to reinvent the wheel over and over again (the data mapper code).

You can also think about a new `ReflectionAccessor` for instance? or use this `CallbackAccessor` to map your form partially from an external API? yes, you could do it :)

Here is a view of the proposed changes:

![data_accessor](https://user-images.githubusercontent.com/2028198/91452859-16bf8f00-e84d-11ea-8564-d497c2f73730.png)

Where "DataMapper" will take care of common checks, iterates the given child forms, manages the form data and all what is needed for mapping a standard form, whereas "DataAccessor" will take care of how to read and write values to/from the underlying object or array.

## BC

The `PropertyPathMapper` is being deprecated in favor of `DataMapper` class, which uses the `PropertyPathAccessor` by default.

Although `DataMapper` is now the default for each compound form, the behavior must remains the same (tests prove it). So that if `getter` or `setter` option is null (they're by default) the `CallbackAccessor` will falls back to `PropertyPathAccessor` either for reading or writing values.

---

Sorry for the long description, but I think that sometimes it is necessary for too complex issues and big changes. Besides, now you know a little more about what these changes is about.

/cc @xabbuh as creator of [rich-model-forms-bundle](https://github.com/sensiolabs-de/rich-model-forms-bundle) and @alcaeus as you had worked on this issue.

WDYT?

Commits
-------

878effaf47 add new way of mapping data using callback functions
2020-09-16 14:32:39 +02:00
Jérémy Derussé
a0321e66c9
Make RetryTillSaveStore implements the SharedLockStoreInterface 2020-09-16 13:06:13 +02:00
Fabien Potencier
cae08a0279 minor #38208 [Messenger] Minor fix of exception message formatting (wouterj)
This PR was merged into the 4.4 branch.

Discussion
----------

[Messenger] Minor fix of exception message formatting

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

See e.g. 2f27613557

Commits
-------

69187ac9ce Fixed exception message formatting
2020-09-16 12:58:28 +02:00
Wouter de Jong
69187ac9ce Fixed exception message formatting 2020-09-16 12:50:20 +02:00
Fabien Potencier
9c8cd0827f feature #38198 [String] allow translit rules to be given as closure (nicolas-grekas)
This PR was merged into the 5.2-dev branch.

Discussion
----------

[String] allow translit rules to be given as closure

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #35061
| License       | MIT
| Doc PR        | -

Instead of trying to fix #35061 at our level, I propose to add a hook so that ppl can use a transliterator that fits their need (eg voku/portable-ascii or behat/transliterator) while still relying on the `SluggerInterface`.

Commits
-------

0bb48df2b0 [String] allow translit rules to be given as closure
2020-09-16 07:43:03 +02:00
Fabien Potencier
1202404671 bug #38202 [FrameworkBundle] Fix xsd definition which prevent to add more than one workflow metadata (l-vo)
This PR was merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] Fix xsd definition which prevent to add more than one workflow metadata

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Fix xsd definition which prevent to add more than one workflow metadata (only in xml configuration format)

Commits
-------

94e390b96a [FrameworkBundle] Fix error in xsd which prevent to register more than one metadata
2020-09-16 07:39:26 +02:00
Fabien Potencier
f27ca4e12d bug #38195 [String] improve slugger's portability accross implementations of iconv() (nicolas-grekas)
This PR was merged into the 5.1 branch.

Discussion
----------

[String] improve slugger's portability accross implementations of iconv()

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36880, fix #38060
| License       | MIT
| Doc PR        | -

Ping @Surfoo @edguar, can you please confirm that this fixes your issues?

Commits
-------

21e4e320b4 [String] improve slugger's portability accross implementations of iconv()
2020-09-16 07:37:48 +02:00
Laurent VOULLEMIER
94e390b96a [FrameworkBundle] Fix error in xsd which prevent to register more than one metadata 2020-09-15 17:33:03 +02:00