zooxsmart/los-domain

Middleware for use php configuration based on domain or subdomain

1.0.0 2023-10-30 17:52 UTC

This package is auto-updated.

Last update: 2024-11-30 02:19:03 UTC


README

Build Status Latest Stable Version Total Downloads Coverage Status Scrutinizer Code Quality SensioLabs Insight Dependency Status

Introduction

This module provides php configuration files based on the domain (or subdomain) being accessed.

For Zend Framework 2/3 framework.zend.com, please use version 1.0.

Todo

Todo

  • Option to manually disable a domain, redirecting it to a page. (eg. expired subscription)
  • Integration with LosLicense (in development) to check the availibity of a domain and automatically disable it.
  • Storages to retrieve options for domains (interface, file, pdo, so on)

Instalation

composer require los/losdomain:^2.0

Usage

For example, in Zend Expressive, add the following code to your config/config.php:

foreach (\LosDomain\DomainService::configFiles('config/autoload/domains') as $file) {
    $config = ArrayUtils::merge($config, include $file);
}

Because the included files cannot be cached, it's better to include the code after the cache read/write. So a complete config.php will looks like:

<?php

use Laminas\Stdlib\ArrayUtils;
use Laminas\Stdlib\Glob;

/**
 * Configuration files are loaded in a specific order. First ``global.php``, then ``*.global.php``.
 * then ``local.php`` and finally ``*.local.php``. This way local settings overwrite global settings.
 *
 * The configuration can be cached. This can be done by setting ``config_cache_enabled`` to ``true``.
 *
 * Obviously, if you use closures in your config you can't cache it.
 */

require __DIR__.'/env.php';

$cachedConfigFile = 'data/cache/app_config.php';

$config = [];
if (is_file($cachedConfigFile)) {
    // Try to load the cached config
    $config = include $cachedConfigFile;
} else {
    // Load configuration from autoload path
    foreach (Glob::glob('config/autoload/{{,*.}global,{,*.}local}.php', Glob::GLOB_BRACE) as $file) {
        $config = ArrayUtils::merge($config, include $file);
    }

    // Cache config if enabled
    if (isset($config['config_cache_enabled']) && $config['config_cache_enabled'] === true) {
        file_put_contents($cachedConfigFile, '<?php return ' . var_export($config, true) . ';');
    }
}

foreach (\LosDomain\DomainService::configFiles('config/autoload/domains') as $file) {
    $config = ArrayUtils::merge($config, include $file);
}

// Return an ArrayObject so we can inject the config as a service in Aura.Di
// and still use array checks like ``is_array``.
return new ArrayObject($config, ArrayObject::ARRAY_AS_PROPS);

The service will look into the folder 'config/autoload/domains' for your domains. If there is a folder named identically as your domain, it will be imported.

For example, assume these domains:

Each domain (or subdomain) can have a different configuration (factories, database configurations, so on):

Since the client2.test.local does not have it's configuration, it will use the default from the project.

Domain Alias

It's possible to assign an alias to a domain. Supose you have two domains that share the same configuration:

The first one you create as usual (creating the config files inside config/autoload/domain/test.local/global.php) and the alias is configured in a file config/autoload/domains/config.php

<?php
return [
    'los_domain' => [
        'aliases' => [
            'www.test.local' => 'test.local',
        ],
    ],
];

With this configuration, when the project is access through 'www.test.local', the service will load the 'test.local' files.