Skip to content

Composing extensions

Follow this guide to run friendsofmago/mago-symfony alongside other Mago extensions, such as friendsofmago/mago-phpunit or drupal/mago, in one worker process.

Using one custom worker file lets all extensions share a single PHP worker pool instead of spawning one pool per [extension-hosts.*] table.

Pass extensions to Mago\Sdk\Worker

Use MagoSymfony\SymfonyExtension::create() when you want default Symfony extension configuration from environment variables and auto-discovery.

  1. Create .mago/worker.php in your project:
<?php

declare(strict_types=1);

use Mago\Sdk\Worker;
use MagoSymfony\SymfonyExtension;

ini_set('display_errors', 'stderr');

ob_start(static function (string $output): string {
    if ($output !== '') {
        fwrite(STDERR, $output);
    }

    return '';
}, 1);

require dirname(__DIR__) . '/vendor/autoload.php';

try {
    (new Worker(
        SymfonyExtension::create(),
        // Add other Mago\Sdk\Extension instances here, such as:
        // MagoPHPUnit\PHPUnitExtension::create(),
        // DrupalMago\DrupalExtension::create(),
    ))->run();
} catch (Throwable $exception) {
    fwrite(STDERR, $exception->getMessage() . "\n");
    fflush(STDERR);
    exit(1);
}
  1. Point mago.toml at your custom worker instead of registering separate extension hosts:
[extension-hosts.project]
command = ["php", "-d", "xdebug.mode=off", "-d", "pcov.enabled=0", "-d", "memory_limit=512M", ".mago/worker.php"]
workers = 1
  1. Validate that Mago loads every extension without duplicate identifiers:
vendor/bin/mago extension validate
vendor/bin/mago extension list

Configure plugins with SymfonyExtension::plugins()

Use MagoSymfony\SymfonyExtension::plugins() when you build your own Mago\Sdk\Extension or pass explicit options instead of environment variables.

The method signature is:

SymfonyExtension::plugins(
    bool $treeBuilder = true,
    ?bool $containerAutoDiscovery = null,
    ?string $containerXml = null,
    ?string $containerMap = null,
    ?string $userClass = null,
): array
Parameter Purpose
$treeBuilder Includes TreeBuilderRootTypePlugin when true. Set to false to skip TreeBuilder analysis.
$containerAutoDiscovery Enables auto-discovery of .mago/symfony-container.json and var/cache/{dev,test}/*Container.xml when true or null. Pass false to disable auto-discovery.
$containerXml Explicit path to a compiled container XML file. Overrides environment variables and auto-discovery.
$containerMap Explicit path to a JSON container map or .xml dump. Used when $containerXml is unset.
$userClass Explicit UserInterface implementation FQCN. Overrides MAGO_SYMFONY_USER_CLASS.

Example custom worker combining Symfony plugins with project-specific plugins in one Mago\Sdk\Extension:

<?php

declare(strict_types=1);

use Mago\Sdk\Extension;
use Mago\Sdk\Worker;
use MagoSymfony\SymfonyExtension;

ini_set('display_errors', 'stderr');
ob_start(static function (string $output): string {
    if ($output !== '') {
        fwrite(STDERR, $output);
    }

    return '';
}, 1);

require dirname(__DIR__) . '/vendor/autoload.php';

$extension = new Extension(
    identifier: 'app/mago-extensions',
    name: 'App Extensions',
    version: '1.0.0',
    analyzerPlugins: [
        ...SymfonyExtension::plugins(
            containerXml: dirname(__DIR__) . '/var/cache/dev/App_KernelDevDebugContainer.xml',
            userClass: 'App\\Entity\\User',
        ),
        // Add custom Mago\Sdk\Analyzer\Plugin instances here.
    ],
);

(new Worker($extension))->run();

Avoid duplicate registrations

Mago rejects a worker if two extensions register the same extension identifier, linter rule code, or analyzer plugin identifier.

  • If you compose SymfonyExtension::create() or SymfonyExtension::plugins() in .mago/worker.php, remove any separate [extension-hosts.symfony] block from mago.toml.
  • If you also compose drupal/mago, omit friendsofmago/mago-phpunit when drupal/mago already bundles its PHPUnit plugin.