Skip to content

Symfony Console

An overview of available infrastructural code when using Symfony Console.


Various standard console commands are available and can be used depending on implemented domain infrastructure. They are defined in the MsgPhp\Domain\Infrastructure\Console\Command\ namespace.


Synchronizes domain objects and their projections using the projection synchronization utility service.

bin/console projection:synchronize

Context Definition

A context definition is bound to MsgPhp\Domain\Infrastructure\Console\Definition\DomainContextDefinition. Its purpose is to (interactively) build a context array using the CLI.


configure(InputDefinition $definition): void

Configures a command input definition. See also InputDefinition. Should be called before using getContext().

getContext(InputInterface $input, StyleInterface $io, array $values = []): array

Resolves the actual context from the console IO. See also InputInterface and StyleInterface. Any element value provided by $values takes precedence and should be used as-is.



Creates a context based on any class method signature. It configures the CLI signature by mapping required method arguments to command arguments, whereas optional ones are mapped to command options.

In both cases a value is optional, if the actual class method argument is required and no value is given it will be asked interactively. If interaction is not possible an exception will be thrown instead.

  • __construct(string $class, string $method, array $classMapping = [], int $flags = 0, ClassContextElementFactory $elementFactory = null)
    • $class / $method: The class method to resolve
    • $classMapping: Global class mapping. Usually used to map abstracts to concretes.
    • $flags: A bit mask value to toggle various flags
      • ClassContextBuilder::ALWAYS_OPTIONAL: Always map class method arguments to command options
      • ClassContextBuilder::NO_DEFAULTS: Leave out default values when calling getContext()
      • ClassContextBuilder::REUSE_DEFINITION: Reuse the original input definition for matching class method arguments
    • $elementFactory: A custom element factory to use. See also Customizing context elements.
Customizing Context Elements

Per-element configuration can be provided by implementing a MsgPhp\Domain\Infrastructure\Console\Context\ClassContextElementFactory.

  • getElement(string $class, string $method, string $argument): ContextElement
    • Get a custom ContextElement to apply to a specific class/method/argument pair

A default implementation is provided by MsgPhp\Domain\Infrastructure\Console\Context\ClassContextElementFactory which simply transforms argument names to human readable values so that $argumentName becomes Argument Name.

Basic Example

use MsgPhp\Domain\Infrastructure\Console\Definition\ClassContextDefinition;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

// --- SETUP ---

class MyObject
    public function __construct(string $argument, $option = null)

class MyCommand extends Command
    private $definition;

    public function __construct()
        $this->definition = new ClassContextDefinition(MyObject::class, '__construct');


    protected function configure(): void

    protected function execute(InputInterface $input,OutputInterface $output): int
        $io = new SymfonyStyle($input, $output);
        $context = $this->definition->getContext($input, $io);
        $object = new MyObject(...array_values($context));

        // do something

        return 0;

// --- USAGE ---

// $ bin/console my-command [--option=OPTION] [--] [<argument>]


Use the Doctrine context definition to provide a class discriminator value into the final context. Typically this implementation is used when working with ORM inheritance.

  • __construct(DomainContextDefinition $definition, EntityManagerInterface $em, string $class)
    • $definition: The decorated context definition
    • $em: The entity manager to use
    • $class: The entity class to use