Skip to content

Mount another application

Castor can mount another application. This pattern is useful when you have a majestic monolith and you want to split it into smaller applications:

├─ projects/
│  ├─ A/
│  │  ├─ castor.php
│  ├─ B/
│  │  ├─ castor.php
├─ castor.php

In this example, project/A/castor.php and project/B/castor.php are two standalone castor applications. All functions defined in project/A/castor.php will be run inside the project/A directory whatever your current directory is.

To achieve this, you need to use the mount() function in your main castor.php:

use function Castor\mount;


You can also prefix a namespace to all task found in each mounted application:

use function Castor\mount;

mount('projects/A', 'project:a');
mount('projects/B', 'project:b');

mount() vs import()

You may wonder when to use mount() vs import(). This really depends on the working directory. If in your path/to/another/castor/app you keep repeating the kind of code:

use function Castor\Attribute\AsTask;

use function Castor\run;

function foobar() {
    run($command, workingDirectory: __DIR__);

Then you should use mount() to avoid repeating the workingDirectory argument.

Otherwise, you can continue to use import().