37
loading...
This website collects cookies to deliver better user experience
composer require
.vendor/bin
. Examples of projects using this feature include PestPHP and Laravel Sail, both using Composer bin commands to distribute executable scripts as application dependencies.minicli/minicli
.minicli/minicli
in your existing application. In case you don't have an existing application and you would like to create a standalone tool with Minicli, create a new directory for your application and run Composer from there, so it will generate a new composer.json
file for you.composer require minicli/minicli
bin
directory in the root of your application folder.mkdir bin
minicli
in this folder:touch bin/minicli
vendor/bin
(as a required dependency) or if it's being called from the application folder itself (as a standalone project). vendor/autoload.php
file. If that file doesn't exist, it's likely that the application is being called as a dependency in the vendor
folder, which make the autoload script available at a different location.minicli
script file inside the bin
folder, which is currently empty. The following code will implement the autoload verification and bootstrap a new Minicli application using the provided app_path
as command location, similar to the basic demo from official docs.#!/usr/bin/php
<?php
if (php_sapi_name() !== 'cli') {
exit;
}
$root_app = dirname(__DIR__);
if (!is_file($root_app . '/vendor/autoload.php')) {
$root_app = dirname(__DIR__, 4);
}
require $root_app . '/vendor/autoload.php';
use Minicli\App;
use Minicli\Command\CommandCall;
$app = new App();
$input = new CommandCall($argv);
$app->registerCommand('help', function() use ($app) {
$app->getPrinter()->info("minicli help");
});
$app->runCommand($input->getRawArgs());
minicli
script and don't forget to save the file. Then, run the registered help
command with:php bin/minicli help
search=term
parameter to search for specific advice. Replace the current content of your minicli
script with the following code, which implements the new "advice" command:#!/usr/bin/php
<?php
if (php_sapi_name() !== 'cli') {
exit;
}
$root_app = dirname(__DIR__);
if (!is_file($root_app . '/vendor/autoload.php')) {
$root_app = dirname(__DIR__, 4);
}
require $root_app . '/vendor/autoload.php';
use Minicli\App;
use Minicli\Command\CommandCall;
$app = new App();
$app->setSignature('./bin/minicli advice');
$input = new CommandCall($argv);
$app->registerCommand('help', function() use ($app) {
$app->getPrinter()->info("./bin/minicli advice");
});
$app->registerCommand('advice', function() use ($app, $input) {
$query_url = "https://api.adviceslip.com/advice";
if ($input->hasParam('search')) {
$query_url = "https://api.adviceslip.com/advice/search/" . $input->getParam('search');
}
$result = file_get_contents($query_url);
if ($result) {
$content = json_decode($result, true);
if (isset($content['total_results'])) {
$search_results = $content['slips'];
$advice = $search_results[array_rand($search_results)]['advice'];
} else {
if (isset($content['slip'])) {
$advice = $content['slip']['advice'];
}
}
$app->getPrinter()->newline();
$app->getPrinter()->info($advice ?? "No results found.");
$app->getPrinter()->newline();
}
return 0;
});
$app->runCommand($input->getRawArgs());
php bin/minicli advice
php bin/minicli advice search=spiders
composer.json
to include the Bin definition.composer require
.composer.json
file looks after the update (I set this package name to minicli/advice-slip
, you should use a different name). Notice the bin
entry including the location of the minicli
script. You could include other scripts here if you wanted to.{
"name": "minicli/advice-slip",
"description": "Demo - Advice Slip",
"license": "MIT",
"homepage": "https://github.com/minicli/advice-slip",
"keywords": ["cli","command-line", "demo"],
"require": {
"minicli/minicli": "^2.0",
"ext-json": "*"
},
"bin": [
"bin/minicli"
]
}
0.1.0
). If you don't have a release, Composer will complain about the minimum stability whenever a user requires this package on their own projects.composer require org/project
:composer require minicli/advice-slip
Using version ^0.1.0 for minicli/advice-slip
./composer.json has been updated
Running composer update minicli/advice-slip
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking minicli/advice-slip (0.1.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading minicli/advice-slip (0.1.0)
- Installing minicli/advice-slip (0.1.0): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
./vendor/bin/minicli advice
php vendor/bin/minicli advice