PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 2, 2022

[FIXED] Doing a "composer install" inside a Symfony console

 March 02, 2022     composer-php, console, php, silex, symfony     No comments   

Issue

I have to launch inside a working directory a composer install after a jQuery success (I'm developing a git panel under Silex). I have been told it could be well done with Symfony Console, because it can keep some options. But I have really no idea how to call it.

I created a class which extends Command, I think I got to implement it under execute method...

class ComposerCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('composer:install')
            ->setDescription('Composer install')
            ->addArgument()
            ->addOption()
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        
    }
}

I tried this:

    <?php
namespace App\Console\Command;

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

class TestCommand extends Command {


    protected function configure() {
        $this->setName("test")
            ->setDescription("Sample description for our command named test")
            ->setDefinition(array(
                new InputOption('flag', 'f', InputOption::VALUE_NONE, 'Raise a flag'),
                new InputArgument('activities', InputArgument::IS_ARRAY, 'Space-separated activities to perform', null),
            ))

            ->setHelp(<<<EOT
The <info>test</info> command does things and stuff
EOT
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        try {
            \Phar::mapPhar('composer.phar');
            include 'phar://composer.phar/demarrage.php';
        } catch (\Exception $e) {
            throw new \Exception($e);
        }

    }
}

Still doesn't work.


Solution

Okay, I found something interesting.

namespace App\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class ComposerCommand extends Command {

    /**
     * Configures the console Application
     *
     */
    protected function configure() {
        $this->setName("composer:install")
            ->setDescription("Composer install inside a symfony console.")
            ->addOption('path', 'p', InputOption::VALUE_REQUIRED)
            ->setHelp(<<<EOT
The <info>composer:install</info> command makes "composer install"
EOT
            );
    }

    /**
     * Executes the console Application
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @throws \Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output) {
        try {
            $path = $input->getOption('path');

            $process = new Process('php composer.phar install --no-interaction');
            $process->setWorkingDirectory($path);
            $process->run();

            // executes after the command finishes
            if (!$process->isSuccessful()) {
                throw new ProcessFailedException($process);
            }

            $output->writeln($process->getOutput());
            $output->writeln($process->getErrorOutput());
        } catch (\Exception $e) {
            throw new \Exception($e);
        }

    }
}


Answered By - Amelie
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing