Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 Composer scripts

I've just installed Symfony 4 project and found this section in composer.json:

"scripts": {
    "auto-scripts": {
        "cache:clear": "symfony-cmd",
        "assets:install %PUBLIC_DIR%": "symfony-cmd"
    },
    "post-install-cmd": [
        "@auto-scripts"
    ],
    "post-update-cmd": [
        "@auto-scripts"
    ],
...

I've found that section auto-scripts is handled somehow differently by the Composer: key is a command to bin/console and value is a command "type" (In this case it's Symfony's). As it is not documented on Composer website, I assume it's not legal definition, but it works, and my question is how the Composer knows how to execute such commands? How Composer knows what is symfony-cmd?

like image 255
spajak Avatar asked Jun 05 '18 14:06

spajak


People also ask

What is Composer scripts?

A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command. Scripts are useful for executing a package's custom code or package-specific commands during the Composer execution process.

How do I run Composer json?

To configure Composer for your PHP app json file specifies required packages. Verify that a composer. json file is present in the root of your git repository. Run composer install (on your local machine) to install the required packages and generate a composer.

What is Symfony CMD?

symfony-cmd is a part of Symfony Flex. Your composer. json does not contain any requirement for Flex, so running composer require symfony/flex might resolve that problem. Follow this answer to receive notifications.

How do I check my Composer?

You can check your installed composer version using a command composer -v at the current path. Such as: composer -v.


1 Answers

It's as Cerad says. auto-script is parsed by Symfony/Flex and the "right side" of a command:executable pair is processed by this switch-case.

switch ($type) {
    case 'symfony-cmd':
        return $this->expandSymfonyCmd($cmd);
    case 'php-script':
        return $this->expandPhpScript($cmd);
    case 'script':
        return $cmd;

Therefore Composer documentation can not say a word about that as it is Symfony-specific.

Personally don't like the mixing of standard Composer sections with custom ones on the composer.json root level. It simply confuses me and draws me into silent Composer documentation. Also the naming itself, auto-script, should be better. Something less magic and more self-explanatory, like flex-script.

like image 66
Jaroslav Týc Avatar answered Nov 15 '22 23:11

Jaroslav Týc