Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a better Single File Application example #20424

Open
wants to merge 2 commits into
base: 7.2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions components/console/single_command_tool.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ In such a case, having to pass the command name each time is tedious. Fortunatel
it is possible to remove this need by declaring a single command application::

#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
<?php // bin/file-counter.php
require __DIR__.'/../vendor/autoload.php';
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<?php // bin/file-counter.php
require __DIR__.'/../vendor/autoload.php';
// bin/file-counter.php
<?php
require __DIR__.'/../vendor/autoload.php';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion seems to me to be incorrect as the string // bin/file-counter.php would be printed by PHP when running the file. It's better to just give the file name outside the example, it doesn't matter that much.


use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -16,15 +16,31 @@ it is possible to remove this need by declaring a single command application::
use Symfony\Component\Console\SingleCommandApplication;

(new SingleCommandApplication())
->setName('My Super Command') // Optional
->setName('File Counter') // Optional
->setVersion('1.0.0') // Optional
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
->addOption('bar', null, InputOption::VALUE_REQUIRED)
->addArgument('dir', InputArgument::OPTIONAL, 'The directory', default: '.')
->addOption('all', 'a', InputOption::VALUE_NONE, 'count all files')
->setCode(function (InputInterface $input, OutputInterface $output): int {
// output arguments and options
$dir = realpath($input->getArgument('dir'));
$all = $input->getOption('all');
$finder = (new Symfony\Component\Finder\Finder())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a use statement

->in($dir)
->files()
->ignoreVCSIgnored(!$all)
;
$count = iterator_count($finder);
$output->writeln( "$dir has $count " .
($all ? "files" : "files in source control"));
return SingleCommandApplication::SUCCESS;
Comment on lines +30 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
;
$count = iterator_count($finder);
$output->writeln( "$dir has $count " .
($all ? "files" : "files in source control"));
return SingleCommandApplication::SUCCESS;
;
$count = iterator_count($finder);
$output->writeln("$dir has $count " . ($all ? "files" : "files in source control"));
return SingleCommandApplication::SUCCESS;

})
->run();

Now run it with

php bin/file-counter.php

php bin/file-counter.php --all
Comment on lines +38 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now run it with
php bin/file-counter.php
php bin/file-counter.php --all
Now run it with:
.. code-block:: bash
php bin/file-counter.php
or
.. code-block:: bash
php bin/file-counter.php --all


You can still register a command as usual::

#!/usr/bin/env php
Expand Down
Loading