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.

PHP development free consulting

Read more:

3 Common Challenges of Software Product Development for Startups

The Best Type of Projects for Java

7 Startups & Scaleups that Will Shake the Marketplace Scene in 2022

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

5 examples of Ruby’s best usage

Have you ever wondered what we can do with Ruby? Well, the sky is probably the limit, but we are happy to talk about some more or less known cases where we can use this powerful language. Let me give you some examples.

Pawel Muszynski
Software Development

Maintaining a Project in PHP: 5 Mistakes to Avoid

More than one article has been written about the mistakes made during the process of running a project, but rarely does one look at the project requirements and manage the risks given the technology chosen.

Sebastian Luczak
Software Development

5 reasons why you will find qualified Ruby developers in Poland

Real Ruby professionals are rare birds on the market. Ruby is not the most popular technology, so companies often struggle with the problem of finding developers who have both high-level skills and deep experience; oh, and by the...

Jakub
Software Development

9 Mistakes to Avoid While Programming in Java

What mistakes should be avoided while programming in Java? In the following piece we answers this question.

Rafal Sawicki
Software Development

A Deeper Look at the Most Popular React Hooks

In the course of many interviews, I noticed that even experienced programmers have a problem with distinguishing Hooks, not to mention their more advanced capabilities. So, I will try to explain in this article how Hooks should...

Pawel Rybczynski

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.