5

Symfony Consoleアプリケーションで実行中のパスを取得する方法はありますか? 例(phpインタープリターを想定PATH):

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

から起動された/tmpとおりに戻る必要があります。console.php/tmp

4

1 に答える 1

12

getcwd()あなたが必要とすることをします。任意のディレクトリから app/console を実行でき、PHP はそれがどれであるかを認識します。

これを確認するために、次の例を使用しました。

<?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());
    }
}
于 2013-09-23T19:11:56.903 に答える