Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the running path of a Symfony Console application?

Is there any way to get the running path in a Symfony Console application? For example (assuming php interpreter in PATH):

cd /tmp
php /home/user/myapplication/app/console.php mycommand

Should return /tmp as console.php was launched from /tmp.

like image 305
gremo Avatar asked Sep 23 '13 18:09

gremo


People also ask

What is bin console in Symfony?

The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console component. You can also use it to create your own commands.

What is Symphony process?

Process Management Software. Symphony allows you and your team to create well-structured work routines and automate recurring processes to increase productivity and team communication. Start using Symphony. Start using Symphony.

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

getcwd() will do what you need. You can execute app/console from any directory, and PHP will know which one it is.

I used the following example to verify this.

<?php

namespace Acme\DemoBundle\Command;

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

class DemoCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('demo:cwd')
            ->setDescription('Get Current Working Directory')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln(getcwd());
    }
}
like image 127
Adam Elsodaney Avatar answered Nov 15 '22 05:11

Adam Elsodaney