Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App type-based configs #15346

Merged
merged 2 commits into from
Jul 15, 2024
Merged

Conversation

brandonkelly
Copy link
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'))
    // ...
;

Copy link

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
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
8 checks passed
@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