decodelabs/monarch

A single shared source of truth for your PHP apps

v0.1.2 2025-04-11 14:25 UTC

This package is auto-updated.

Last update: 2025-04-11 14:26:56 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

A single shared source of truth for your PHP apps

Monarch provides a single shared source of truth for your PHP applications. It allow commonly referenced paths and items to be centralised into a predictable location, making it easier to manage accessing that data from code that needs to maintain minimal coupling to the rest of the application.

Get news and updates on the DecodeLabs blog.

Installation

Install via Composer:

composer require decodelabs/monarch

Usage

Monarch should be populated in your bootstrap otherwise it will use reasonable defaults where possible. If you use Genesis to bootstrap your application, Monarch will be automatically populated for you.

For example:

use DecodeLabs\Monarch;

Monarch::setApplicationName('My Cool App');
Monarch::$paths->root = '/var/www/my-cool-app';
Monarch::$paths->run = '/var/www/my-cool-app/dist';
Monarch::$paths->localData = '/var/www/my-cool-app/data/local';
Monarch::$paths->sharedData = '/var/www/my-cool-app/data/shared';

Path aliasing

Monarch allows you to define aliases for commonly used paths. This is useful for avoiding hardcoded paths in your codebase. @root and @run are automatically defined for you, but you can define your own aliases as needed.

use DecodeLabs\Monarch;

Monarch::$paths->alias('@components', '@root/components');
Monarch::$paths->alias('@assets', '@root/assets');

You can then use these aliases in your code:

use DecodeLabs\Monarch;
$path = Monarch::$paths->resolve('@components/MyComponent.php');
// /var/www/my-cool-app/components/MyComponent.php

Container

Monarch attempts to centralise a PSR container for easy access to all components of your app. Due to the PSR spec not providing an interface for setting items, care must be taken when storing objects as the container instance could be of any type; the only guarantee is that it implements Psr\Container\ContainerInterface.

If you use Genesis to bootstrap your application, Monarch will contain a Pandora container, otherwise Monarch will use a simple stand-in container that implements the Psr\Container\ContainerInterface interface.

use DecodeLabs\Monarch;
use DecodeLabs\Pandora\Container;

if(Monarch::$container instanceof Container) {
    Monarch::$container->bind('myService', new MyService());
}

If you are working in a Fabric app, it is better to reference Fabric::$container as that is guaranteed to be a Pandora container. However libraries should not be coupled to Fabric and should use the read-oriented methods of the Monarch interface instead.

use DecodeLabs\Monarch;

$service = Monarch::$container->get('myService');

Licensing

Monarch is licensed under the MIT License. See LICENSE for the full license text.