-
-
Save mohdjas/3cb20957ccdbe28c2bbeeecb10cd5602 to your computer and use it in GitHub Desktop.
Symfony Command that read file from file name or STDIN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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