Skip to content

Refactoring Plan: Laravel Model Settings #131

@glorand

Description

@glorand

Refactoring Plan: Laravel Model Settings

This document outlines the plan to refactor the package to use a more flexible, configuration-driven driver system, aligning with standard Laravel patterns.

Core Objective

To simplify the package by using a single HasSettings trait and allowing the storage mechanism (driver) to be selected via configuration, while still permitting per-model overrides.

Step-by-Step Plan

  1. Modify config/model_settings.php:

    • Add a new driver key to specify the default storage driver (e.g., 'driver' => 'field').
    • Add a drivers array to map driver names to their corresponding manager classes. This will allow for easy extension in the future.
    'driver' => env('MODEL_SETTINGS_DRIVER', 'field'),
    
    'drivers' => [
        'field' => [
            'class' => \Glorand\Model\Settings\Managers\FieldSettingsManager::class,
        ],
        'table' => [
            'class' => \Glorand\Model\Settings\Managers\TableSettingsManager::class,
        ],
        'redis' => [
            'class' => \Glorand\Model\Settings\Managers\RedisSettingsManager::class,
        ],
    ],
  2. Create a SettingsManagerFactory:

    • This new class will be responsible for creating the correct manager instance based on the configured driver.
    • It will have a create method that takes the model instance, reads the required driver (from the model's $settingsDriver property or the config), and instantiates the correct manager class from the drivers config array.
  3. Update the HasSettings Trait:

    • This will become the only trait that developers need to use.
    • Remove the abstract methods (settings and getSettingsValue).
    • Implement a concrete settings() method. This method will be the main entry point. It will use the new SettingsManagerFactory to get the appropriate manager instance and return it. It should cache the manager instance per model instance to avoid repeated object creation.
    • Implement a concrete getSettingsValue() method that delegates the call to the resolved settings manager.
  4. Remove Old Driver-Specific Traits:

    • Delete the following files, as their functionality will be handled by the new factory-based system:
      • src/Traits/HasSettingsField.php
      • src/Traits/HasSettingsTable.php
      • src/Traits/HasSettingsRedis.php
  5. Update Documentation:

    • Modify README.md to reflect the new, simpler usage.
    • Explain how to use the single HasSettings trait.
    • Document the new driver configuration option.
    • Show how to override the driver on a per-model basis using the $settingsDriver property.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions