class StdinHandler (View source)

StdinHandler is a thin wrapper around php://stdin. It provides methods for redirecting input from a file, possibly conditionally under the control of an Input object.

Example trivial usage (always reads from stdin):

 class Example implements StdinAwareInterface
 {
     /**
      * @command cat
      * @param string $file
      * @default $file -
      * /
     public function cat()
     {
          print($this->stdin()->contents());
     }
 }

Command that reads from stdin or file via an option:

 /**
  * @command cat
  * @param string $file
  * @default $file -
  * /
 public function cat(InputInterface $input)
 {
     $data = $this->stdin()->select($input, 'file')->contents();
 }

Command that reads from stdin or file via an option:

 /**
  * @command cat
  * @option string $file
  * @default $file -
  * /
 public function cat(InputInterface $input)
 {
     $data = $this->stdin()->select($input, 'file')->contents();
 }

It is also possible to inject the selected stream into the input object, e.g. if you want the contents of the source file to be fed to any Question helper et. al. that the $input object is used with.

 /**
  * @command example
  * @option string $file
  * @default $file -
  * /
 public function example(InputInterface $input)
 {
     $this->stdin()->setStream($input, 'file');
 }

Inject an alternate source for standard input in tests. Presumes that the object under test gets a reference to the StdinHandler via dependency injection from the container.

 $container->get('stdinHandler')->redirect($pathToTestStdinFileFixture);

You may also inject your stdin file fixture stream into the $input object as usual, and then use it with 'select()' or 'setStream()' as shown above.

Finally, this class may also be used in absence of a dependency injection container by using the static 'selectStream()' method:

 /**
  * @command example
  * @option string $file
  * @default $file -
  * /
 public function example(InputInterface $input)
 {
     $data = StdinHandler::selectStream($input, 'file')->contents();
 }

To test a method that uses this technique, simply inject your stdin fixture into the $input object in your test:

 $input->setStream(fopen($pathToFixture, 'r'));

Properties

protected $path
protected $stream

Methods

static 
selectStream(InputInterface $input, $optionOrArg)

No description

bool
hasPath()

hasPath returns 'true' if the stdin handler has a path to a file.

bool
hasStream()

hasStream returns 'true' if the stdin handler has opened a stream.

string
path()

path returns the path to any file that was set as a redirection source, or php://stdin if none have been.

close()

close closes the input stream if it was opened.

$this
redirect($path)

redirect specifies a path to a file that should serve as the source to read from. If the input path is '-' or empty, then output will be taken from php://stdin (or whichever source was provided via the 'redirect' method).

$this
select(InputInterface $input, string $optionOrArg)

select chooses the source of the input stream based on whether or not the user provided the specified option or argument on the commandline.

getStream()

getStream opens and returns the stdin stream (or redirect file).

setStream(InputInterface $input, $optionOrArg)

setStream functions like 'select', and also sets up the $input object to read from the selected input stream e.g. when used with a question helper.

string
contents()

contents reads the entire contents of the standard input stream.

pathProvided($path)

Returns 'true' if a path was specfied, and that path was not '-'.

getOptionOrArg(InputInterface $input, $optionOrArg)

No description

Details

static selectStream(InputInterface $input, $optionOrArg)

No description

Parameters

InputInterface $input
$optionOrArg

bool hasPath()

hasPath returns 'true' if the stdin handler has a path to a file.

Return Value

bool

bool hasStream()

hasStream returns 'true' if the stdin handler has opened a stream.

Return Value

bool

string path()

path returns the path to any file that was set as a redirection source, or php://stdin if none have been.

Return Value

string

close()

close closes the input stream if it was opened.

$this redirect($path)

redirect specifies a path to a file that should serve as the source to read from. If the input path is '-' or empty, then output will be taken from php://stdin (or whichever source was provided via the 'redirect' method).

Parameters

$path

Return Value

$this

$this select(InputInterface $input, string $optionOrArg)

select chooses the source of the input stream based on whether or not the user provided the specified option or argument on the commandline.

Stdin is selected if there is no user selection.

Parameters

InputInterface $input
string $optionOrArg

Return Value

$this

getStream()

getStream opens and returns the stdin stream (or redirect file).

setStream(InputInterface $input, $optionOrArg)

setStream functions like 'select', and also sets up the $input object to read from the selected input stream e.g. when used with a question helper.

Parameters

InputInterface $input
$optionOrArg

string contents()

contents reads the entire contents of the standard input stream.

Return Value

string

protected pathProvided($path)

Returns 'true' if a path was specfied, and that path was not '-'.

Parameters

$path

protected getOptionOrArg(InputInterface $input, $optionOrArg)

No description

Parameters

InputInterface $input
$optionOrArg