nunomaduro/phpinsights

Add support for custom presets

Open

#370 opened on Mar 5, 2020

View on GitHub
 (3 comments) (5 reactions) (0 assignees)PHP (5,092 stars) (308 forks)batch import
enhancementgood first issue

Description

Q A
Bug report? no
Feature request? yes

Right now we can create custom configuration based on presets, however I think it would be worth allowing custom presets also.
This would mean opening up the Preset interface so for example a framework could maintain their own Preset file and users can then use the preset, just by pointing their preset configuration to the class.

Implementation

  • In Configuration::resolveConfig, the OptionsResolver now has to allow preset to also be a valid class.
  • In ConfigResolver::resolve, add a new private method resolvePreset, which has the current logic for resolving a preset, but also adds support for the preset variable could be a class fqn.
  • Add docs
  • Add a test in ConfigResolverTest for setting preset from a class
  • Remove internal from phpdoc in Preset interface
  • Consider if we should also remove internal from Composer class
  • Remove shouldBeApplied and getName from Preset interface and add it to a new internal interface, which our presets implements. (this method is used for guessing the preset from composer, but does not make sense with custom preset as their are not registered in our application.)

Usage

Using this should be rather straight forward, creating a custom Preset should be like this

class LaravelLumenPreset implements Preset
{
    public static function get(Composer $composer): array
    {
        $config = [
            // My custom configs
        ];

        return ConfigResolver::mergeConfig(LaravelPreset::get($composer), $config);
    }
}

And our config file would then look like

return [
    'preset' => FQN\LaravelLumenPreset::class,
     ...

Contributor guide