-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
base: 7.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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'; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
use Symfony\Component\Console\Input\InputArgument; | ||||||||||||||||||||||||||||||||||
use Symfony\Component\Console\Input\InputInterface; | ||||||||||||||||||||||||||||||||||
|
@@ -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()) | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
->run(); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Now run it with | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
php bin/file-counter.php | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
php bin/file-counter.php --all | ||||||||||||||||||||||||||||||||||
Comment on lines
+38
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
You can still register a command as usual:: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
#!/usr/bin/env php | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.