Go to content
The Codest
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
2022-04-14
Software Development

PHP Development. Symfony Console Component - Tips & Tricks

Sebastian Luczak

PHP Unit Leader

PHP Development. Symfony Console Component - Tips & Tricks - Image

This article was created with the aim to show you the most useful and retrieving tips and tricks about Symfony Console Development.

If you are a PHP developer that wants to bring your PHP software development to the next level this article is for you. Without further ado let's cut to the chase.

Introduction

You often hear: ​

PHP is only used to make web pages

image

​ This is completely untrue because nowadays PHP development is used in many different business areas, often not so trivial at first sight.

Both the PHP language and its environment perfectly support HTTP communication which, together with the use of the CLI environment, makes it possible to quickly create web bots, web crawlers or tools that synchronize data in external environments. ​ To support this I have some statistics that show that the Console component of the Symfony framework, which allows easy access to the command line, is in the TOP5 most used and downloaded Symfony packages of all time.

​ image

​

​ At this point, we'd like to share with you a few tricks that every PHP developer should know when writing code in PHP that is supposed to work in the CLI. ​

Interactive selection table with custom data source

​ image ​

Table rendering with the ability to choose entries from can be achieved in an easy way using Symfony Command: ​

// src/App/Command/TestCommand.php
​
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title("Interactive selection table example");
​
        $table = $io->createTable();
        $table->setHeaderTitle("Interactive selection table example");
        $table->setRows(iterator_to_array($this->tagsTableRows()));
        $table->render();
        $io->newLine();
​
        return Command::SUCCESS;
    }

​ In above example we're taking advantage of PHP Generators as source of data - which helps with scalability in future. Simple data generator for above example: ​

    protected function tagsTableRows(): Generator
    {
        $apiTagsResponse = $this->someInjectedService->getTags();
        foreach ($apiTagsResponse as $apiTagResponse) {
            yield [ $apiTagResponse->getName(), $apiTagResponse->getId() ];
        }
    }

​ The end result is a Table rendered in CLI. ​ image ​

To make it interactive we need to use QuestionHelper, provided as wrapper around SymfonyStyle output. ​

  (...)
    $choice = new ChoiceQuestion(
        question: 'Which selection you choose?',
        choices: array_reduce(
            array: $rows,
            callback: function($carry, $item) {
                $carry[] = $item[0];
​
                return $carry;
            }
        )
    );
    $answer = $io->askQuestion($choice);
   (...)

As result, we're getting an interactive choice field with a table display from the same source. ​

Always use hidden prompt for sensitive user data

​ image ​

Hiding prompt while providing sensitive data is a common practive and it's also easily achievable in Symfony Console. You can use built-in wrapper in SymfonyStyle class by the following example: ​

  (...)
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title("Asking user for sensitive data");
        $io->askHidden(
            question: "Provide your API key here",
            validator: function($answer) { return (42 == strlen($answer)) ?? $answer; }
        );
​
        return Command::SUCCESS;
    }

​

Style your Progress Bars

​ image ​

To communicate some progress to the user we can use Progress Bars. Symfony Console has a great way to show progress to the user, but always remember to style your progress output correctly. You can have full controler over different parts and how they're rendered using Formatter. The progress bar format just a string of different placeholders. The available placeholders are: current, max, bar, percent, elapsed, remaining, estimated, memory and message. Fiddle with them with the example below. ​

  (...)
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title("Styled progress bar example");
​
        $progressBar = new ProgressBar($io, 10000);
        $progressBar->setFormat('<comment>%current%/%max% [%bar%] %percent:3s%%</comment>
                    <info>%elapsed:6s%/%estimated:-6s%</info> <error>%memory:6s%</error>');
        $progressBar->start();
        for ($i = 0; $i < 10000; $i++) {
            $progressBar->advance();
            usleep(420);
        }
​
        $progressBar->finish();
        $io->newLine();
​
        return Command::SUCCESS;
    }

​ You can use almost anything as and formatter as long as your terminal is capable of displaying it. Official Symfony documentation ​

Provide suggestions to your Console Commands

​ image ​

We're used to console suggestion, autocompletion features, and such in our development life. If you're creating console application you should consider adding it so that your users will not be confused about how to use your low-level CLI solution. PHP Symfony Console can provide that too, out of the box, using CompletionInput class. ​

  (...)
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
    {
        if ($input->mustSuggestArgumentValuesFor(argumentName: 'someArgument')) {
            $suggestions->suggestValues(['someSuggestion', 'otherSuggestion']);
        }
    }

​

Print anywhere you want

​ Symfony 5.1 introduced a new way of handling cursor position in CLI applications. Since then it's possible to read and write at a specific place at screen using handy Cursor class: ​

  (...)
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title("Cursor example");
​
        $cursor = new Cursor($io);
        $cursor->clearScreen();
        for ($x = 0; $x <= 10; $x++) {
            for ($y = 0; $y <= 10; $y++) {
                $cursor->moveToPosition($x, $y);
​
                if ($y === $x) {
                    $io->write(".");
                }
            }
        }
        $io->write("Hello, World!");
        $io->newLine();
​
        return Command::SUCCESS;
    }

image ​ ​

This short list of tricks is just the tip of the iceberg. The possibilities of Symfony Console are endless, as evidenced by the numerous projects like Psalm, PHPStan or Composer that are based on PHP and used by millions of PHP developers around the world.

cooperation banner

Related articles

Software Development

3 Useful HTML Tags You Might Not Know Even Existed

Nowadays, accessibility (A11y) is crucial on all stages of building custom software products. Starting from the UX/UI design part, it trespasses into advanced levels of building features in code. It provides tons of benefits for...

Jacek Ludzik
Software Development

Hiring Internal vs. External Developers

Hiring internally or externally? It's an ultimate dilemma! Find out advantages of outsourcing or building an in-house team in the following article.

Grzegorz Rozmus
Software Development

What You Shroud Know about PHP Development?

This article is devoted to enriching your PHP business knowledge. Find out where to find PHP developers, what are the pros and cons of this language and many more.

Sebastian Luczak
Software Development

Two Reasons You Should Not Use PHP

As a PHP developer I still remember the days of PHP4, the first (optional!) GC implementation in PHP5, and I lived through the _hype_ of the advent of PHP7 and its ramifications.

Sebastian Luczak

Subscribe to our knowledge base and stay up to date on the expertise from industry.

About us

Tech company specializing in scaling tech teams for clients and partners thanks to top-class development engineers.

    United Kingdom - Headquarters

  • Office 303B, 182-184 High Street North E6 2JA London, England

    Poland - Local Tech Hubs

  • Business Link High5ive, Pawia 9, 31-154 Kraków, Poland
  • Brain Embassy, Konstruktorska 11, 02-673 Warsaw, Poland
  • Aleja Grunwaldzka 472B, 80-309 Gdańsk, Poland

    The Codest

  • Home
  • About us
  • Services
  • Case studies
  • Know how
  • Careers

    Services

  • PHP development
  • Java development
  • Python development
  • Ruby on Rails development
  • React Developers
  • Vue Developers
  • TypeScript Developers
  • DevOps
  • QA Engineers

    Resources

  • What are top CTOs and CIOs Challenges? [2022 updated]
  • Facts and Myths about Cooperating with External Software Development Partner
  • From the USA to Europe: Why do American startups decide to relocate to Europe
  • Privacy policy
  • Website terms of use

Copyright © 2022 by The Codest. All rights reserved.

We use cookies on the site for marketing, analytical and statistical purposes. By continuing to use, without changing your privacy settings, our site, you consent to the storage of cookies in your browser. You can always change the cookie settings in your browser. You can find more information in our Privacy Policy.