Last active
January 21, 2021 12:47
-
-
Save pretzelhands/43d0025ba958538efe371fe867a2dae6 to your computer and use it in GitHub Desktop.
Distributable PHAR for any app with composer dependencies
This file contains hidden or 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
#!/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(); |
This file contains hidden or 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
{ | |
"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