Skip to content

Conversation

alissn
Copy link
Contributor

@alissn alissn commented Sep 26, 2025

Hi,

In this pull request (related to #2040), I added support for using class-based replacements instead of Closures.

Previously, when using Closures in replacement configuration, running the php artisan config:cache command caused the following error:

Your configuration files are not serializable.

To solve this, I introduced a new ReplacementKeyCommand class.
Replacements can now be implemented as dedicated classes, ensuring compatibility with config caching.


Example

Replacement classes:

<?php

namespace Modules\Test\Console\Replacements;

use Nwidart\Modules\Support\ReplacementKeyCommand;

class AuthorName extends ReplacementKeyCommand
{
    public function handle(): string
    {
        return 'Ali SSN';
    }
}
<?php

namespace Modules\Test\Console\Replacements;

use Nwidart\Modules\Support\ReplacementKeyCommand;

class ModuleStudlyName extends ReplacementKeyCommand
{
    public function handle(): string
    {
        return $this->generator->getName();
    }
}

Config file usage:

'composer' => [
    'LOWER_NAME',
    'STUDLY_NAME'   => \Modules\Test\Console\Replacements\ModuleStudlyName::class,
    'VENDOR',
    'AUTHOR_NAME'   => \Modules\Test\Console\Replacements\AuthorName::class,
    'AUTHOR_EMAIL',
    'MODULE_NAMESPACE',
    'PROVIDER_NAMESPACE',
    'APP_FOLDER_NAME',
],

This resolves the serialization issue and makes replacements more extendable and testable.
Fixes #2117.

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.

Using closures in the configuration file will cause an error when running php artisan config:cache

1 participant