Over the past weeks, we've published tens of articles about the most notable new features of Symfony 5.1, which will be released in a few days. In these final articles of the series we'll showcase some minor but useful new features that will make your development work easier.

Added constants for command exit codes

Ivan
Contributed by Ivan in #35478

In Symfony 4.4 we changed console commands to require them to return their exit status code as an integer. In Symfony 5.1 we've added some constants for the most common exit codes (Command::SUCCESS and Command::FAILURE) to make your code more readable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// src/Command/CreateUserCommand.php
namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CreateUserCommand extends Command
{
    protected static $defaultName = 'app:create-user';

    // ...

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...

        // Before
        return 0;

        // After
        return Command::SUCCESS;
    }
}

YAML 1.2 octal notation

Christian Flothmann
Contributed by Christian Flothmann in #34813

In YAML 1.1 documents, 0 was the prefix used to define octal numbers (e.g. 072). This notation is deprecated in YAML 1.2, which replaces 0 prefix by 0o (e.g. 0o72). That's why in Symfony 5.1 we've added support for YAML 1.2 notation and we've deprecated support for YAML 1.1 format.

Improved tempnam()

Jon Dufresne
Contributed by Jon Dufresne in #33003

The PHP tempnam() function creates a file with a unique name in the given directory. The tempnam() function provided by the Symfony Filesystem component provides the same features, but in Symfony 5.1 we improved it with a new optional argument that lets you add a suffix to the generated file name (e.g. to add a file extension to it):

1
2
3
4
5
6
use Symfony\Component\Filesystem\Filesystem;

$fs = new Filesystem();

$fs->tempnam('/tmp', 'report_');         // '/tmp/report_Um3nlH'
$fs->tempnam('/tmp', 'report_', '.pdf'); // '/tmp/report_Um3nlH.pdf'

Custom dictionaries for random strings

Saif
Contributed by Saif in #36471

Symfony String component is the best way of working with strings in PHP applications. In Symfony 5.1 we've improved the feature that generates random alphanumeric strings so you can pass a custom dictionary of characters to be included in the generated string:

1
2
3
$random = ByteString::fromRandom(6);                   // 'g6UkL2'
$randomPin = ByteString::fromRandom(4, '0123456789');  // '7385'
$randomKey = ByteString::fromRandom(1, 'WASD');        // 'S'
Published in #Living on the edge