Skip to content

Instantly share code, notes, and snippets.

@mohdjas
Forked from sroze/MyCommand.php
Created March 15, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohdjas/3cb20957ccdbe28c2bbeeecb10cd5602 to your computer and use it in GitHub Desktop.
Save mohdjas/3cb20957ccdbe28c2bbeeecb10cd5602 to your computer and use it in GitHub Desktop.
Symfony Command that read file from file name or STDIN
<?php
namespace AmceBundle\Command;
class MyCommand
{
// ...
protected function execute(InputInterface $input, OutputInterface $output)
{
$filename = $input->getArgument('filename');
if ($filename = $input->getArgument('filename')) {
$contents = file_get_contents($filename);
} else if (0 === ftell(STDIN)) {
$contents = '';
while (!feof(STDIN)) {
$contents .= fread(STDIN, 1024);
}
} else {
throw new \RuntimeException("Please provide a filename or pipe template content to STDIN.");
}
// Do whatever you want with `$contents`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment