In addition to the main features announced in previous posts of this series, Symfony 8.2 includes many smaller changes that make day-to-day work easier. This post highlights a second batch of them.
Disable Individual Workflow Events
The Workflow component dispatches several events on each transition
(leave, transition, enter, entered, completed and
announce). The events_to_dispatch option lets you list the only events
that a workflow should dispatch. However, if you wanted to turn off a single
event, you had to list all the others and update that list whenever Symfony
added a new event.
In Symfony 8.2, prefix an event name with ! to disable it and keep
dispatching all the others, including the events added in future versions:
1 2 3 4 5 6 7 8
# config/packages/workflow.yaml
framework:
workflows:
order:
type: state_machine
# dispatch all events except "announce"
events_to_dispatch: ['!workflow.announce']
# ...
The same syntax works in the $eventsToDispatch constructor argument when
using the component in a standalone way:
1 2 3 4 5
use Symfony\Component\Workflow\Workflow;
$workflow = new Workflow($definition, $markingStore, $dispatcher, 'order', eventsToDispatch: [
'!workflow.announce',
]);
You can't mix both kinds of entries in the same list: either all of them start
with ! or none of them do. The guard event is always dispatched, so
!workflow.guard is rejected too.
UUID and ULID Support in JsonStreamer
The JsonStreamer component reads and writes JSON using very little memory,
but it works best with simple objects made of public properties. That's why it
needs transformers to convert value objects, like dates, into JSON values and
back. Symfony 8.2 adds built-in transformers for the Uuid and Ulid
objects of the Uid component, including all their child classes
(UuidV4, UuidV7, etc.):
1 2 3 4 5 6 7 8 9 10 11 12
namespace App\Model;
use Symfony\Component\JsonStreamer\Attribute\JsonStreamable;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\UuidV7;
#[JsonStreamable]
class Order
{
public UuidV7 $id;
public Ulid $trackingCode;
}
UIDs are written in their canonical format by default. Use the new
uid_format option to select another one (base58, base32 or
rfc4122):
1 2 3 4 5 6 7 8 9 10 11
use App\Model\Order;
use Symfony\Component\JsonStreamer\Transformer\UuidValueObjectTransformer;
use Symfony\Component\TypeInfo\Type;
$json = $jsonStreamWriter->write($order, Type::object(Order::class));
// {"id":"018f7a5e-3c1e-7a4e-8b1a-2c3d4e5f6a7b","trackingCode":"01ARZ3NDEKTSV4RRFFQ69G5FAV"}
// write both UIDs in Base58 instead
$json = $jsonStreamWriter->write($order, Type::object(Order::class), [
UuidValueObjectTransformer::FORMAT_KEY => UuidValueObjectTransformer::FORMAT_BASE58,
]);
Injectable Admin Recipients in Notifier
The Notifier component lets you configure a list of admin recipients. Symfony
uses them to tell you about failed Messenger messages or important log messages.
However, the getAdminRecipients() method was only defined in the Notifier
class and not in any interface. If you injected NotifierInterface in your
services, you couldn't get those recipients.
Symfony 8.2 adds an AdminRecipientsProviderInterface with that single method
and an autowiring alias for it, so you can send your own notifications to the
admins:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
namespace App\Monitoring;
use Symfony\Component\Notifier\AdminRecipientsProviderInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
class DiskSpaceMonitor
{
public function __construct(
private NotifierInterface $notifier,
private AdminRecipientsProviderInterface $adminRecipients,
) {
}
public function check(): void
{
// ...
$notification = new Notification('The disk is almost full', ['email']);
$this->notifier->send($notification, ...$this->adminRecipients->getAdminRecipients());
}
}
If you decorate the notifier service or use a custom notifier, implement the
new interface too. Otherwise, Symfony sends the admin notifications without
recipients. Defining getAdminRecipients() without implementing the interface
still works, but it's deprecated.
Global From Header in Email Notifications
Notifications sent by email need a sender. Until now, you had to set it in the
asEmailMessage() method of each notification or in the envelope.sender
option of the mailer. If you had only configured a global From header for
all your emails, the email channel ignored it and threw an exception.
In Symfony 8.2, the email channel uses that global From header when there's
no envelope sender, so this config is all you need:
1 2 3 4 5
# config/packages/mailer.yaml
framework:
mailer:
headers:
From: 'Conferences <noreply@conferences.example>'
The sender set in asEmailMessage() still wins over the global options.
Deprecated the framework.ide Option
Symfony can turn the file paths shown in exception pages and in the profiler
into links that open the file in your editor. There were two ways to select
that editor: the framework.ide config option and the SYMFONY_IDE
environment variable. The config option couldn't reach the code that runs very
early, before the container is available, so some error pages ignored it.
That's why Symfony 8.2 deprecates the framework.ide option. The env var is
read everywhere and accepts the same values. Remove the option from your config
and define the env var instead (for example in .env.local, because each
developer uses a different editor):
1 2
# .env.local
SYMFONY_IDE=phpstorm
The deprecation is only triggered when you set the option explicitly. The option will be removed in Symfony 9.0.