Skip to content

Commit

Permalink
Implement PrependRunner - none of the drawbacks of the others
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Dec 5, 2019
1 parent 01fe612 commit edfe98f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
10 changes: 4 additions & 6 deletions docs/pragmas.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ Pogo accepts instructions using a `#!foo` notation. The following are supported:
* __Example__: `#!run dash-b`
* __Example__: `#!run {with: dash-b, buffer: true}`
* __Options__:
* `auto`: Let `pogo` pick a mechanism. Generally, it will use `include` or `eval` (depending on whether the
script begins with `#!/usr/bin/env pogo...` (or similar).
* `include`: Loosely, this runs `php -r 'require_once $autoloader; include $your_script;`. Among these runners,
it should behave the most intuitively with respect to debugging, avoiding unnecessary file IO/duplication, CLI
inputs/outputs, etc. However, if `$your_script` is a standalone program (`#!/usr/bin/env pogo`), then
it will erroneously output the first line.
* `auto`: Let `pogo` pick a mechanism. Generally, this is `prepend`.
* `prepend`: Loosely, this runs `php -d auto_prepend_file=$autoloader $your_script`. Better than all other existing runners.
* `include`: Loosely, this runs `php -r 'require_once $autoloader; include $your_script;`. Almost as good as `prepend;
however, if `$your_script` is a standalone program (`#!/usr/bin/env pogo`), then it will erroneously output the first line.
* `eval`: Loosely, this runs `php -r 'require_once $autoloader; eval(cleanup($your_script))'`. This fixes the
erroneous output, but backtraces and debugging may not be as pleasant.
* `dash-b`: Loosely, this runs `echo | php -B 'require_once $autoloader;' -F $your_script`. This avoids the
Expand Down
10 changes: 3 additions & 7 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Pogo\Command;

use Pogo\Runner\PrependRunner;
use Pogo\Runner\EvalRunner;
use Pogo\Runner\FileRunner;
use Pogo\Runner\DashBRunner;
Expand Down Expand Up @@ -54,6 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
'eval' => new EvalRunner(),
'file' => new FileRunner(),
'include' => new IncludeRunner(),
'prepend' => new PrependRunner(),
];
if ($input->getOption('run-mode')) {
$project->scriptMetadata->parseCode("<?php\n#!run " . $input->getOption('run-mode'));
Expand All @@ -77,13 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

public function pickRunner($file) {
$code = file_get_contents($file);
if (substr($code, 0, 3) === '#!/') {
return 'eval';
}
else {
return 'include';
}
return 'prepend';
}

}
32 changes: 32 additions & 0 deletions src/Runner/PrependRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Pogo\Runner;

/**
* Class AutoPrependRunner
* @package Pogo\Runner
*
* Execute via 'php -d auto_prepend_file=$autoload $script;'
*/
class PrependRunner {

/**
* @param string $autoloader
* @param \Pogo\ScriptMetadata $scriptMetadata
* @param array $cliArgs
* @return int
*/
public function run($autoloader, $scriptMetadata, $cliArgs) {
$script = $scriptMetadata->file;

$defines = \Pogo\Php::iniToArgv($scriptMetadata->ini + ['auto_prepend_file' => $autoloader]);
$cmd = sprintf('POGO_SCRIPT=%s php %s %s', escapeshellarg($script), $defines, escapeshellarg($script));

if ($cliArgs) {
$cmd .= ' ' . implode(' ', array_map('escapeshellarg', $cliArgs));
}
// printf("[%s] Running command: $cmd\n", __CLASS__, $cmd);
$process = proc_open($cmd, [STDIN, STDOUT, STDERR], $pipes);
return proc_close($process);
}

}
5 changes: 5 additions & 0 deletions tests/RunExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function getExamples() {
"hello\n",
];

$exs['pragmas-eval'] = [
"pogo --run-mode=prepend examples/pragmas.php",
"hello\n",
];

$exs['pragmas-eval'] = [
"pogo --run-mode=eval examples/pragmas.php",
"hello\n",
Expand Down

0 comments on commit edfe98f

Please sign in to comment.