Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a Symfony Console command after composer install?

Tags:

My composer.json contains the following declaration:

    "post-install-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
    ],

I want to run a custom console command that I have in src/MyBundle/Command/MyCommand.php. How do I add this to the scripts to run in composer?

like image 710
juuga Avatar asked Mar 16 '15 13:03

juuga


People also ask

What is Console in Symfony?

The Console component eases the creation of beautiful and testable command line interfaces. The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.

What is composer Symfony?

Composer is the package manager used by modern PHP applications. Use Composer to manage dependencies in your Symfony applications and to install Symfony Components in your PHP projects. Since this article was first published, Composer installation has improved a lot.

What is flex in Symfony?

Symfony Flex is a Composer plugin that modifies the behavior of the require , update , and remove commands. When installing or removing dependencies in a Flex-enabled application, Symfony can perform tasks before and after the execution of Composer tasks.

How do I know what version of Symfony I have?

If you have file system access to the project Look inside the file for a line like: const VERSION = '5.0. 4'; that's the Symfony version number.


1 Answers

You can see how the postinstall hook work for the Sensio DistributionBundle.

As example, this is how you can call the Hello World command of the Acme Demo bundle:

ScriptHandler

<?php

namespace Acme\DemoBundle\Composer;

use Composer\Script\CommandEvent;

class ScriptHandler extends \Sensio\Bundle\DistributionBundle\Composer\ScriptHandler {


    /**
     * Call the demo command of the Acme Demo Bundle.
     *
     * @param $event CommandEvent A instance
     */
    public static function helloWorld(CommandEvent $event)
    {
        $options = self::getOptions($event);
        $consoleDir = self::getConsoleDir($event, 'hello world');

        if (null === $consoleDir) {
            return;
        }

//        $extraParam = '';
//        if (!$options['who']) {
//            $extraParam = ' --who';
//        }

        static::executeCommand($event, $consoleDir, 'acme:hello', $options['process-timeout']);
    }

}

You can manage extra param in the json file itself.

composer.json

"post-install-cmd": [
    "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles",
    "Acme\\DemoBundle\\Composer\\ScriptHandler::helloWorld"
],

Tested

I extend the ScriptHandler class of the sensio-distribution bundle of version:

sensio/distribution-bundle (v3.0.18)

hope this help

like image 131
Matteo Avatar answered Oct 26 '22 19:10

Matteo