1. Go to this page and download the library: Download phpdot/console library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpdot / console example snippets
use PHPdot\Console\Application;
use PHPdot\Console\ConsoleConfig;
$app = new Application(new ConsoleConfig(name: 'MyApp', version: '1.0.0'));
$app->add(new GreetCommand());
$app->run();
use PHPdot\Console\Application;
use PHPdot\Console\ConsoleConfig;
$app = new Application(
new ConsoleConfig(
name: 'MyApp',
version: '1.0.0',
cachePath: __DIR__ . '/var/cache/commands.php',
),
);
$app->discover([__DIR__ . '/src/Command']);
$app->run();
use PHPdot\Console\Application;
use PHPdot\Console\ConsoleConfig;
$container = /* any PSR-11 container */;
$app = new Application(
config: new ConsoleConfig(
name: 'MyApp',
version: '1.0.0',
cachePath: __DIR__ . '/var/cache/commands.php',
),
container: $container,
);
$app->discover([__DIR__ . '/src/Command']);
$app->run();
$app->discover([__DIR__ . '/vendor/phpdot']);
// Add an alternate name. Both `container:list` and `c:l` work.
$app->alias('container:list', 'c:l');
// Replace the canonical name. The old name no longer resolves.
$app->rename('legacy:run', 'run');
// Change description / help text without touching the name.
$app->override('container:list', description: 'Show every entry in the live container');
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use PHPdot\Console\Command;
#[AsCommand(name: 'users:import', description: 'Import users from CSV')]
final class ImportUsersCommand extends Command
{
public function __construct(
private readonly UserRepository $users,
) {
parent::__construct();
}
protected function configure(): void
{
$this->addArgument('file', InputArgument::REQUIRED, 'CSV file path');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$file = $input->getArgument('file');
$rows = $this->parser->parse($file);
$this->info($output, "Importing from {$file}...");
$this->withProgress($output, $rows, function (array $row) {
$this->users->create($row);
});
$this->success($output, 'Import complete.');
return self::SUCCESS;
}
}