Search code examples
shopwareshopware6

Run CLI command when event triggers


I've created an Event Subscriber which looks at the ThemeConfigChangedEvent. This is all working fine. The next step is to run a CLI command when the event triggers.

I'm using Symfony\Component\Process\Process to process a new instance and run the command. But I've got an exception that the bin/console command is not found. Which is true, because I saw the code is executed in the public directory.

What's the best way to run the command from the root directory?

EDIT

Now I'm just moving a level up to the root directory like this:

$directory = '../';
chdir($directory);

Is this the way or is there an better solution to run the CLI command?


Solution

  • You can inject the parameter kernel.project_dir.

    <service id="SwagBasicExample\Subscriber\MySubscriber">
        <argument>%kernel.project_dir%</argument>
        <tag name="kernel.event_subscriber"/>
    </service>
    
    public function __construct(string $projectDir)
    {
        $this->consolePath = implode(\DIRECTORY_SEPARATOR, [$projectDir, 'bin', 'console']);
    }