Hamza Amrouche  - 3451634 - New in Symfony 4.1: Prepared commands (Symfony Blog)

In 4.1 we improved the Process component to allow writing ;prepared
, a concept similar to prepared statements in SQL. The basic idea is
to replace some parts of the command with placeholders whose values you provide
later when actually running the command (or via environment variables):

use SymfonyComponentProcessProcess;

$process = new Process('ls -lsa {{ path }}');
$process->run(null, ['path' => '/path/to/some/dir']);

Placeholders must follow the {{ placeholder_name }} syntax strictly, which
has nothing to do with Twig, except for the coincidence in the use of brackets.
If some placeholder value is missing when running the command, you’ll get an

Placeholder values are looked for first in the second argument of the run()
method. If they don’t exist, values are looked for in the environment variables
(if they don’t exist either, you’ll get an exception):

$process = new Process('mysqldump --user={{ DB_USER }} --password={{ DB_PASS }} {{ db_name}} > {{ db_backup_path }}');
// DB_USER and DB_PASS placeholder values are taken from env vars
$process->run(null, [
    'db_name' => 'symfony',
    'db_backup_path' => '/var/backup/db-'.().'.sql',

If you don’t like the idea of using placeholders in your commands for some
reason, the Process component also allows since Symfony 3.3 to pass an array
where the first element is the command to run and the rest of the array
are its options and arguments, allowing you to build a complex command

$process = new Process(array(
    '> '.$dbBackupPath,

Source link


Please enter your comment!
Please enter your name here