Commit Graph

41215 Commits

Author SHA1 Message Date
Fabien Potencier
96dee1ee20 [Messenger] fixed missing use statement 2019-03-31 22:37:12 +02:00
Fabien Potencier
1044dfb93d [Messenger] simplified code 2019-03-31 22:18:13 +02:00
Vincent Touzet
27466498d0 [Messenger] Fix get in Doctrine Transport 2019-03-31 20:44:41 +02:00
Fabien Potencier
9669e13554 [Messenger] made a const private 2019-03-31 19:35:08 +02:00
Fabien Potencier
dafc357fb9 minor #30803 [Messenger] Remove unused option in the Doctrine transport (vincenttouzet)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] Remove unused option in the Doctrine transport

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This PR remove the unused option `loop_sleep` in the Messenger Doctrine transport

Commits
-------

4811400372 [Messenger] Remove unused option in the Doctrine transport
2019-03-31 19:33:19 +02:00
Fabien Potencier
c54a6a2e1a minor #30802 [Messenger] Add psr/cache on Messenger's dependencies (sroze)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] Add `psr/cache` on Messenger's dependencies

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30754
| License       | MIT
| Doc PR        | ø

Commits
-------

0b5004f59f Add `psr/cache` on Messenger's dependencies
2019-03-31 19:32:16 +02:00
Fabien Potencier
563900ffaa fixed CS 2019-03-31 19:31:00 +02:00
Fabien Potencier
dad5b01b9c feature #26890 [Inflector] Support pluralization in the inflector (mbabker)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Inflector] Support pluralization in the inflector

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | not yet
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | Not Yet

At present the inflector only supports singularizing plural words, this PR adds the capability to pluralize singular words.

Commits
-------

06920a79c4 Support pluralization in the inflector
2019-03-31 19:29:55 +02:00
Samuel ROZE
0b5004f59f Add psr/cache on Messenger's dependencies 2019-03-31 18:23:22 +01:00
Fabien Potencier
592e72fc9c feature #28637 [Validator] add number constraints (jschaedl)
This PR was squashed before being merged into the 4.3-dev branch (closes #28637).

Discussion
----------

[Validator] add number constraints

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28608
| License       | MIT
| Doc PR        | tbd.

I added the following constraints:
* `Positive`
* `PositiveOrZero`
* `Negative`
* `NegativeOrZero`

Commits
-------

01870398eb [Validator] add number constraints
2019-03-31 19:19:07 +02:00
Jan Schädlich
01870398eb [Validator] add number constraints 2019-03-31 19:18:59 +02:00
Nicolas Grekas
6070151495 minor #30800 [Messenger] fix review (nicolas-grekas)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] fix review

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Fixes my review left in #30754
/cc @weaverryan FYI

Commits
-------

a3de9020c8 [Messenger] fix review
2019-03-31 19:13:48 +02:00
Vincent Touzet
4811400372 [Messenger] Remove unused option in the Doctrine transport 2019-03-31 19:12:50 +02:00
Samuel ROZE
a9459c0980 minor #30799 [Messenger] Fix the Doctrine transport to use the new interface (sroze)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] Fix the Doctrine transport to use the new interface

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29007
| License       | MIT
| Doc PR        | ø

Commits
-------

75e3355da5 Fix the Doctrine transport to use the new interface
2019-03-31 18:12:31 +01:00
Nicolas Grekas
a3de9020c8 [Messenger] fix review 2019-03-31 19:00:04 +02:00
Samuel ROZE
75e3355da5 Fix the Doctrine transport to use the new interface 2019-03-31 17:52:51 +01:00
Fabien Potencier
7c18377274 minor #30781 [Intl] Update the ICU data to 64.1 (jakzal)
This PR was squashed before being merged into the 3.4 branch (closes #30781).

Discussion
----------

[Intl] Update the ICU data to 64.1

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes, including the intl-data group
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

http://site.icu-project.org/download/64

Commits
-------

ae2cb6f5c5 [Intl] Update the ICU data to 64.1
2019-03-31 18:47:52 +02:00
Jakub Zalas
ae2cb6f5c5 [Intl] Update the ICU data to 64.1 2019-03-31 18:47:37 +02:00
Fabien Potencier
343d28e3d1 feature #30754 [Messenger] New messenger:stop-workers Command (weaverryan)
This PR was squashed before being merged into the 4.3-dev branch (closes #30754).

Discussion
----------

[Messenger] New messenger:stop-workers Command

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | Kinda of #29451
| License       | MIT
| Doc PR        | symfony/symfony-docs#11236

o/ me again.

This requires and is built on top of #30708

When you deploy, all workers need to be stopped and restarted. That's not currently possible, unless you manually track the pids and send a SIGTERM signal. We can make that much easier :).

Now run:

```
bin/console messenger:stop-workers
```

And it will signal to all workers (even if they're distributed on other servers) that they should stop, once they finish processing their current message. This is done via a key in `cache.app`.

Cheers!

Commits
-------

58971627f5 [Messenger] New messenger:stop-workers Command
2019-03-31 18:41:56 +02:00
Ryan Weaver
58971627f5 [Messenger] New messenger:stop-workers Command 2019-03-31 18:41:31 +02:00
Fabien Potencier
8c2ade9173 bug #30794 [EventDispatcher] Fix BC/FC layer (chalasr)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[EventDispatcher] Fix BC/FC layer

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Fixes
> TypeError: Argument 3 passed to Symfony\Component\EventDispatcher\EventDispatcher::doDispatch() must be an instance of Symfony\Component\EventDispatcher\Event

Spotted in https://github.com/lexik/LexikJWTAuthenticationBundle/pull/637

Commits
-------

caa0aded89 [EventDispatcher] Fix BC layer
2019-03-31 18:36:43 +02:00
Samuel ROZE
fd4146c768 Add back missing uses 2019-03-31 17:35:18 +01:00
Fabien Potencier
825335374a bug #30756 Changing to MessageDecodingFailedException so that invalid messages are rejected (weaverryan)
This PR was merged into the 4.3-dev branch.

Discussion
----------

Changing to MessageDecodingFailedException so that invalid messages are rejected

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  |  no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #30649
| License       | MIT
| Doc PR        | not needed for bug fix

Bug fix if a message body is completely blank. I'm fixing this on master only, because in 4.2 and earlier, there is actually no system in place to fail serialization and cause the messages to be rejected. In 4.3, we just need to throw this exception.

Cheers!

Commits
-------

4be827d3ca Changing to MessageDecodingFailedException so that invalid messages are rejected
2019-03-31 18:34:24 +02:00
Fabien Potencier
177a0d9320 bug #30798 [Messenger] Updating SyncTransport for recent changes + tests (weaverryan)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] Updating SyncTransport for recent changes + tests

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | not needed

When making the `SyncTransport`, I neglected having at least one test in each of these classes, which allowed the test suite to pass, even after some interface changes made these classes fail. Fixed all of that :)

Commits
-------

2df023be46 Updating SyncTransport for recent changes + tests
2019-03-31 18:31:45 +02:00
Fabien Potencier
3abca64c5d feature #30707 [Messenger][DX] Allow stamps to be passed directly to MessageBusInterface::dispatch() (weaverryan)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger][DX] Allow stamps to be passed directly to MessageBusInterface::dispatch()

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | TODO

Me again o/!

This proposal is *purely* for DX. With `DelayStamp`, the proposal of QueueNameStamp and future things like `AmqpRoutingKeyStamp`, stamps are becoming more common for end users to use. This changes how it looks to use them:

```php
// before
$bus->dispatch(new Envelope(new SendSmsNotification('Hi!'), new DelayStamp(10), new QueueNameStamp('low')));

// after
$bus->dispatch(new SendSmsNotification('Hi!'), [new DelayStamp(10), new QueueNameStamp('low')]);
```

It's definitely a BC break, which is allowed because the component is experimental, though it should be minimized. This BC break shouldn't be felt by most end users, as creating your own bus is an advanced use-case. Even if you decorated it, you'll get an obvious error.

Commits
-------

e861de7e61 Allow stamps to be passed directly to MessageBusInterface::dispatch()
2019-03-31 18:29:02 +02:00
Samuel ROZE
e897e6cc4e Add missing word in CHANGELOG 2019-03-31 17:26:00 +01:00
Samuel ROZE
d9e2732a7d feature #29007 [Messenger] Add a Doctrine transport (vincenttouzet)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] Add a Doctrine transport

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | symfony/symfony-docs#10616
| DoctrineBundle PR | doctrine/DoctrineBundle#868

As discussed with @sroze at PHPForum in Paris I've worked on adding a Doctrine transport to the Messenger component.

Actually `AMQP` is the only supported transport and it could be a good thing to support multiple transports. Having a Doctrine transport could help users to start using the component IMHO (Almost all projects use a database).

# How it works

The code is splitted betwwen this PR and the one on the DoctrineBundle : doctrine/DoctrineBundle#868

## Configuration

To configure a Doctrine transport the dsn MUST have the format `doctrine://<entity_manager_name>` where `<entity_manager_name>` is the name of the entity manager (usually `default`)
```yml
        # config/packages/messenger.yaml
        framework:
            messenger:
                transports:
                    my_transport: "doctrine://default?queue=important"
```

## Table schema

Dispatched messages are stored into a database table with the following schema:

| Column       | Type     | Options                  | Description                                                       |
|--------------|----------|--------------------------|-------------------------------------------------------------------|
| id           | bigint   | AUTO_INCREMENT, NOT NULL | Primary key                                                       |
| body         | text     | NOT NULL                 | Body of the message                                               |
| headers      | text     | NOT NULL                 | Headers of the message                                            |
| queue      | varchar(32)     | NOT NULL                 | Headers of the message                                            |
| created_at   | datetime | NOT NULL                 | When the message was inserted onto the table. (automatically set) |
| available_at       | datetime   | NOT NULL                 | When the message is available to be handled                      |
| delivered_at | datetime | NULL                     | When the message was delivered to a worker                        |

## Message dispatching

When dispatching a message a new row is inserted into the table. See `Symfony\Component\Messenger\Transport\Doctrine::publish`

## Message consuming

The message is retrieved by the `Symfony\Component\Messenger\Transport\Doctrine\DoctrineReceiver`. It calls the `Symfony\Component\Messenger\Transport\Doctrine::get` method to get the next message to handle.

### Getting the next message

* Start a transaction
* Lock the table to get the first message to handle (The lock is done with the `SELECT ... FOR UPDATE` query)
* Update the message in database to update the delivered_at columns
* Commit the transaction

### Handling the message

The retrieved message is then passed to the handler. If the message is correctly handled the receiver call the `Symfony\Component\Messenger\Transport\Doctrine::ack` which delete the message from the table.

If an error occured the receiver call the `Symfony\Component\Messenger\Transport\Doctrine::nack` method which update the message to set the delivered_at column to `null`.

## Message requeueing

It may happen that a message is stuck in `delivered` state but the handler does not really handle the message (Database connection error, server crash, ...). To requeue messages the `DoctrineReceiver` call the `Symfony\Component\Messenger\Transport\Doctrine::requeueMessages`. This method update all the message with a  `delivered_at` not null since more than the "redeliver timeout" (default to 3600 seconds)

# TODO

- [x] Add tests
- [x] Create DOC PR
- [x] PR on doctrine-bundle for transport factory
- [x] Add a `available_at` column
- [x] Add a `queue` column
- [x] Implement the retry functionnality : See #30557
- [x] Rebase after #29476

Commits
-------

88d008c828 [Messenger] Add a Doctrine transport
2019-03-31 17:25:18 +01:00
Ryan Weaver
e861de7e61 Allow stamps to be passed directly to MessageBusInterface::dispatch()
And changing signature of Envelope::__construct() to accept an array of envelopes
2019-03-31 12:05:21 -04:00
Ryan Weaver
4be827d3ca Changing to MessageDecodingFailedException so that invalid messages are rejected 2019-03-31 11:54:28 -04:00
Ryan Weaver
2df023be46 Updating SyncTransport for recent changes + tests 2019-03-31 11:45:52 -04:00
Fabien Potencier
88042a317a feature #30628 Making the serializer configurable by transport (weaverryan)
This PR was merged into the 4.3-dev branch.

Discussion
----------

Making the serializer configurable by transport

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30293 (already closed, but I don't think this was reasonably possible before)
| License       | MIT
| Doc PR        | TODO - as many of the new messenger PRs will be done at once

Use cases:
* #30293
* Transport A will be consumed by your Symfony app (so native php serialization is cool) but transport B will be consumed by another app, so you want to serialize as JSON
* Upgrading from Symfony 4.2 to 4.3. The change to the `PhpSerialize` means that messages that were sent to the queue on 4.2, will fail on 4.3. The solution is to use the old serializer in your config. This would allow you to make your existing transport use the old serializer, then migrate to a new transport using the new serializer (then remove the old one later).

Thanks!

Commits
-------

ef6f23e8b9 Making the serializer configurable by transport
2019-03-31 16:55:57 +02:00
Ryan Weaver
ef6f23e8b9 Making the serializer configurable by transport 2019-03-31 16:54:31 +02:00
Nicholas Ruunu
49ab2cd9d9 Make MessengerPass less strict when auto-register handlers 2019-03-31 16:44:42 +02:00
Vincent Touzet
88d008c828 [Messenger] Add a Doctrine transport 2019-03-31 16:05:11 +02:00
Fabien Potencier
07276642de fixed typo 2019-03-31 15:49:16 +02:00
Fabien Potencier
dd47fda441 fixed bad merge 2019-03-31 14:18:54 +02:00
Fabien Potencier
460da96f45 feature #30569 [FrameworkBundle][HttpKernel] Provide intuitive error message when a controller fails because it's not registered as a service (moynzzz)
This PR was squashed before being merged into the 4.3-dev branch (closes #30569).

Discussion
----------

[FrameworkBundle][HttpKernel] Provide intuitive error message when a controller fails because it's not registered as a service

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #27787, symfony/symfony-docs#7988 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | none <!-- required for new features -->

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest 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 the master branch.
-->

Create a very late controller argument value resolver to throw an intuitive error message when controller fails because it is not registered as a service.

Commits
-------

fbfc623b72 [FrameworkBundle][HttpKernel] Provide intuitive error message when a controller fails because it's not registered as a service
2019-03-31 14:12:04 +02:00
Simeon Kolev
fbfc623b72 [FrameworkBundle][HttpKernel] Provide intuitive error message when a controller fails because it's not registered as a service 2019-03-31 14:11:56 +02:00
Robin Chalas
caa0aded89 [EventDispatcher] Fix BC layer 2019-03-31 14:11:52 +02:00
Fabien Potencier
b14d5cd358 feature #26484 [Validator] String normalization options for string-based validators (renan-taranto)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Validator] String normalization options for string-based validators

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26239
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/9433

Todo:
- [x] Document the new options
- [x] Update Doc PR

Add trimming options to the string constraints.

Commits
-------

708d759b8d [Validator] String normalization options for string-based validators
2019-03-31 13:55:59 +02:00
Fabien Potencier
6bc2ae4018 feature #30320 [Form][TwigBridge] Add row_attr to form theme (alexander-schranz)
This PR was squashed before being merged into the 4.3-dev branch (closes #30320).

Discussion
----------

[Form][TwigBridge] Add row_attr to form theme

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | waiting for confirmation to implement
| Fixed tickets |
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Currently you need to copy the whole form_row block if you just want to add a class to the form_row div. Instead we could introduce a `row_attr` which can then simple be set the following in the theme e.g.:

```twig
{% block form_row %}
    {% set row_attr = { class: 'form__field' } %}
    {{ parent() }}
{% endblock %}
```

or in php:

```
$builder->add('test', TextType::class, ['row_attr' => ['class' => 'form__field']]);
```

Commits
-------

7ab1b00e02 [Form][TwigBridge] Add row_attr to form theme
2019-03-31 13:48:32 +02:00
Alexander Schranz
7ab1b00e02 [Form][TwigBridge] Add row_attr to form theme 2019-03-31 13:48:18 +02:00
Fabien Potencier
ce24e15775 feature #30371 [OptionsResolver] Add a new method addNormalizer and normalization hierarchy (yceruto)
This PR was squashed before being merged into the 4.3-dev branch (closes #30371).

Discussion
----------

[OptionsResolver] Add a new method addNormalizer and normalization hierarchy

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30310
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/11103

### 3rd-party package

<details><summary>Given: (CLICK ME)</summary>
<p>

Generic type:
```php
class FooType extends AbstractType
{
    private $registry;

    public function __construct(ManagerRegistry $registry)
    {
        $this->registry = $registry;
    }

    // buildForm ...

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setRequired('class');
        $resolver->setDefaults([
            'em' => null,
            'query' => null,
        ]);
        $resolver->setAllowedTypes('em', ['null', 'string']);
        $resolver->setAllowedTypes('query', ['null', 'callable']);

        $resolver->setNormalizer('em', function (Options $options, $em) {
            if (null !== $em) {
                return $this->registry->getManager($em);
            }

            return $this->registry->getManagerForClass($options['class']);
        });

        $resolver->setNormalizer('query', function (Options $options, $query) {
            if (\is_callable($query)) {
                $query = $query($options['em']->getRepository($options['class']));

                if (!$query instanceof Query) {
                    throw new UnexpectedTypeException($query, 'Doctrine\ORM\Query');
                }
            }

            return $query;
        });
    }
}
```
</p>
</details>

### App context

<details><summary>Before (CLICK ME)</summary>
<p>

Normalizing the new allowed value will require to override the parent's normalizer:
```php
class BarType extends AbstractType
{
    private $registry;

    public function __construct(ManagerRegistry $registry)
    {
        $this->registry = $registry;
    }

    // buildForm ...

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->addAllowedTypes('em', 'Doctrine\ORM\EntityManagerInterface');

        $resolver->setNormalizer('em', function (Options $options, $em) {
            if ($em instanceof EntityManagerInterface) {
                return $em;
            }

            if (null !== $em) {
                return $this->registry->getManager($em);
            }

            return $this->registry->getManagerForClass($options['class']);
        });

        $resolver->addAllowedTypes('query', 'string');

        $resolver->setNormalizer('query', function (Options $options, $query) {
            if (\is_callable($query)) {
                $query = $query($options['em']->getRepository($options['class']));

                if (!$query instanceof Query) {
                    throw new UnexpectedTypeException($query, 'Doctrine\ORM\Query');
                }
            }

            if (\is_string($query)) {
                $query = $options['em']->createQuery($query);
            }

            return $query;
        });
    }

    public function getParent()
    {
        return FooType::class;
    }
}
```
</p>
</details>

<details><summary>After (CLICK ME)</summary>
<p>

The new normalizer is added to the stack and it'll receive the previously normalized value or if `forcePrepend = true` the validated value:
```php
class BarType extends AbstractType
{
    // buildForm ...

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->addAllowedTypes('em', 'Doctrine\ORM\EntityManagerInterface');

        $resolver->addNormalizer('em', function (Options $options, $em) {
            if ($em instanceof EntityManagerInterface) {
                return $em;
            }

            return $em;
        }, true); // $forcePrepend = true (3rd argument)

        $resolver->addAllowedTypes('query', 'string');

        $resolver->addNormalizer('query', function (Options $options, $query) {
            if (\is_string($query)) {
                $query = $options['em']->createQuery($query);
            }

            return $query;
        });
    }

    public function getParent()
    {
        return FooType::class;
    }
}
```
</p>
</details>

Commits
-------

cf41254223 [OptionsResolver] Add a new method addNormalizer and normalization hierarchy
2019-03-31 13:47:22 +02:00
Yonel Ceruto
cf41254223 [OptionsResolver] Add a new method addNormalizer and normalization hierarchy 2019-03-31 13:47:13 +02:00
Fabien Potencier
7554cf6f1d feature #27735 [Validator][DoctrineBridge][FWBundle] Automatic data validation (dunglas)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Validator][DoctrineBridge][FWBundle] Automatic data validation

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes<!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/11132

This feature automatically adds some validation constraints by inferring existing metadata. To do so, it uses the PropertyInfo component and Doctrine metadata, but it has been designed to be easily extendable.

Example:

```php
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Dummy
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    public $id;

    /**
     * @ORM\Column(nullable=true)
     */
    public $columnNullable;

    /**
     * @ORM\Column(length=20)
     */
    public $columnLength;

    /**
     * @ORM\Column(unique=true)
     */
    public $columnUnique;
}

$manager = $this->managerRegistry->getManager();
$manager->getRepository(Dummy::class);

$firstOne = new Dummy();
$firstOne->columnUnique = 'unique';
$firstOne->columnLength = '0';

$manager->persist($firstOne);
$manager->flush();

$dummy = new Dummy();
$dummy->columnNullable = 1; // type mistmatch
$dummy->columnLength = '012345678901234567890'; // too long
$dummy->columnUnique = 'unique'; // not unique

$res = $this->validator->validate($dummy);
dump((string) $res);

/*
Object(App\Entity\Dummy).columnUnique:\n
    This value is already used. (code 23bd9dbf-6b9b-41cd-a99e-4844bcf3077f)\n
Object(App\Entity\Dummy).columnLength:\n
    This value is too long. It should have 20 characters or less. (code d94b19cc-114f-4f44-9cc4-4138e80a87b9)\n
Object(App\Entity\Dummy).id:\n
    This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)\n
Object(App\Entity\Dummy).columnNullable:\n
    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n
*/
```

It also works for DTOs:

```php

class MyDto
{
    /** @var string */
    public $name;
}

$dto = new MyDto();
$dto->name = 1; // type error

dump($validator->validate($dto));

/*
Object(MyDto).name:\n
    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n
*/
```

Supported constraints currently are:

* `@NotNull` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)
* `@Type` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)
* `@UniqueEntity` (using Doctrine's `unique` metadata)
* `@Length` (using Doctrine's `length` metadata)

Many users don't understand that the Doctrine mapping doesn't validate anything (it's just a hint for the schema generator). It leads to usability and security issues (that are not entirely fixed by this PR!!).
Even the ones who add constraints often omit important ones like `@Length`, or `@Type` (important when building web APIs).
This PR aims to improve things a bit, and ease the development process in RAD and when prototyping. It provides an upgrade path to use proper validation constraints.

I plan to make it opt-in, disabled by default, but enabled in the default Flex recipe. (= off by default when using components, on by default when using the full stack framework)

TODO:

* [x] Add configuration flags
* [x] Move the Doctrine-related DI logic from the extension to DoctrineBundle: doctrine/DoctrineBundle#831
* [x] Commit the tests

Commits
-------

2d64e703c2 [Validator][DoctrineBridge][FWBundle] Automatic data validation
2019-03-31 13:41:16 +02:00
Fabien Potencier
6f0863020a bug #30660 [Bridge][Twig] DebugCommand - fix escaping and filter (SpacePossum)
This PR was squashed before being merged into the 3.4 branch (closes #30660).

Discussion
----------

[Bridge][Twig] DebugCommand - fix escaping and filter

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

The PR fixes:
- output escaping was not done for decorated consoles
- filter was not applied when using format json

+ added some tests for paths currently not tested

Commits
-------

7bdb06641d [Bridge][Twig] DebugCommand - fix escaping and filter
2019-03-31 13:33:25 +02:00
SpacePossum
7bdb06641d [Bridge][Twig] DebugCommand - fix escaping and filter 2019-03-31 13:33:18 +02:00
Fabien Potencier
cbbfe84154 minor #30793 [Messenger] Remove the mention of handler in the ReceiverInterface::get phpdoc. (sroze)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] Remove the mention of handler in the `ReceiverInterface::get` phpdoc.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/pull/30708#pullrequestreview-220830730
| License       | MIT
| Doc PR        | ø

As spotted by @Tobion, we don't have an handler as an argument anymore.

Commits
-------

9c63112d7c Remove the mention of handler in the phpdoc.
2019-03-31 12:26:30 +02:00
Fabien Potencier
3df44f5679 feature #30758 [PropertyAccess] Allow Can Accessor in Property Access (ragboyjr)
This PR was merged into the 4.3-dev branch.

Discussion
----------

[PropertyAccess] Allow Can Accessor in Property Access

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |
| License       | MIT
| Doc PR        |

- Added ability to support `can` methods in the property access
  in order to be compatible with PropertyInfo component
  which allows for `can` accessors

Commits
-------

a4c95745bb [PropertyAccess] Allow Can Accessor in Property Access
2019-03-31 12:12:59 +02:00
Samuel ROZE
9c63112d7c Remove the mention of handler in the phpdoc. 2019-03-31 11:12:50 +01:00