We will extend our simple phar app from this article:
Now you can create composer.json with composer init, or add composer.json manually:
{
"name": "scriptun/phar"
}
We need Symfony Console Component, so:
composer require symfony/console
Now we have vendor folder and composer.json updated. Edit index.php:
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->run();
After running index.php you should see Symfony response:
Console Tool
Usage:
command [options] [arguments]
...
So everything works fine, now we need our example console command to use.
Create: app/Command/TestCommand.php
<?php
namespace Scriptun\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class TestCommand extends Command
{
protected static $defaultName = 'app:test-command';
protected function configure()
{
$this
// the short description shown while running "php bin/console list"
->setDescription('Shows test message')
// the full command description shown when running the command with
// the "--help" option
->setHelp('This command allows you to show test message...');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('This is a TEST message');
}
}
We need to load this class inside index.php, to do this, add autoload definition to your composer.json:
{
"name": "scriptun/phar",
"autoload": {
"psr-4": {"Scriptun\\": "app/"}
},
"require": {
"symfony/console": "^4.2"
}
}
Register new command inside index.php:
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
use Scriptun\Command\TestCommand;
$application = new Application();
$application->add(new TestCommand());
$application->run();
Run it with:
php index.php app:test-command
> This is a TEST message
Works, now we can create phar by using our create.php file:
php create.php
php php scriptun.phar app:test-command
> This is a TEST message
Add new comment