Skip to content

App type-based configs#15346

Merged
brandonkelly merged 2 commits into4.11from
feature/cms-1312-app-type-based-configs
Jul 15, 2024
Merged

App type-based configs#15346
brandonkelly merged 2 commits into4.11from
feature/cms-1312-app-type-based-configs

Conversation

@brandonkelly
Copy link
Copy Markdown
Member

Adds support for app type-based configs, for general and db (e.g. general.web.php/general.console.php).

In addition to returning an array or new GeneralConfig/DbConfig object, general and db config files can now return a callable which accepts an existing config object, so it’s possible for app type-based configs to use the fluent syntax on the same config object created by the original config file (general.php/db.php):

// config/general.php

return GeneralConfig::create()
    ->aliases([
        '@webroot' => dirname(__DIR__) . '/web',
    ])
    // ...
;

// or

return fn(GeneralConfig $config) => $config
    ->aliases([
        '@webroot' => dirname(__DIR__) . '/web',
    ])
    // ...
;
// config/general.console.php

return fn(GeneralConfig $config) => $config
    ->addAlias('@web', craft\helpers\App::env('CLI_WEB_URL'))
    // ...
;

@brandonkelly brandonkelly requested a review from timkelty July 13, 2024 12:07
@linear
Copy link
Copy Markdown

linear Bot commented Jul 13, 2024

@brandonkelly brandonkelly changed the title Feature/cms 1312 app type based configs App type-based configs Jul 13, 2024
Copy link
Copy Markdown
Contributor

@timkelty timkelty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Related thought: we could provide some if methods for fluent config to facilitate the same kind of thing but in a single file. Could also be handy for environment conditions, eg:

<?php
return GeneralConfig::create()
    ->if(
        true, // expression
        fn(GeneralConfig $config) => $config->devMode(true), // if
        fn(GeneralConfig $config) => $config->devMode(false), // else
    )
    ->ifEnvironment(
        'dev', // CRAFT_ENVIRONMENT=dev
        fn(GeneralConfig $config) => $config->devMode(true)
    )
    ->ifWeb(fn(GeneralConfig $config) => $config->('@web', 'https://example.com'))
    ->ifConsole(fn(GeneralConfig $config) => $config->addAlias('@webroot', '/var/www/html'));

@brandonkelly brandonkelly merged commit 6c16479 into 4.11 Jul 15, 2024
@brandonkelly brandonkelly deleted the feature/cms-1312-app-type-based-configs branch July 15, 2024 15:42
brandonkelly added a commit that referenced this pull request Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants