Skip to content

Instantly share code, notes, and snippets.

@pretzelhands
Last active January 21, 2021 12:47
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 pretzelhands/43d0025ba958538efe371fe867a2dae6 to your computer and use it in GitHub Desktop.
Save pretzelhands/43d0025ba958538efe371fe867a2dae6 to your computer and use it in GitHub Desktop.
Distributable PHAR for any app with composer dependencies
#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
const JOKE_API_ENDPOINT = 'https://official-joke-api.appspot.com/jokes/%s/random';
function command(InputInterface $input, OutputInterface $output)
{
$topic = $input->getOption('topic');
$jokeResponse = file_get_contents(sprintf(JOKE_API_ENDPOINT, $topic));
[$joke] = json_decode($jokeResponse);
$output->writeln($joke->setup);
$output->writeln("<info>{$joke->punchline}</info>\n");
}
(new SingleCommandApplication())
->setName('Joke Fetcher')
->addOption(
name: 'topic',
mode: InputOption::VALUE_OPTIONAL,
default: 'general'
)
->setCode('command')
->run();
{
"name": "pretzelhands/joke-fetcher",
"require": {
"symfony/console": "^5.2"
},
"scripts": {
"phar": "phar-composer build"
},
"bin": ["cli.php"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment