Skip to content

TotemServiceProvider should not make database connection check during register() #305

Closed
@cyruscollier

Description

@cyruscollier

https://github.com/codestudiohq/laravel-totem/blob/8.0/src/Providers/TotemServiceProvider.php#L57

if (! defined('TOTEM_DATABASE_CONNECTION')) {
    define('TOTEM_DATABASE_CONNECTION', config('totem.database_connection', Schema::getConnection()->getName()));
}

This forces a database connection check during the Laravel bootstrapping process, which affects package discovery, composer install, all artisan commands, etc. This in turn breaks the application when attempting to do things like run composer or artisan commands while not in a real environment with a database connection, such as in a CI container or in a local terminal if using vagrant/docker/etc. for the actual local server. I'm sure there are other cases where this would break as well, but these examples are what I experienced in my own workflow.

Laravel's documentation on this matter states that a Service Provider's register() method "should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method. Otherwise, you may accidentally use a service that is provided by a service provider which has not loaded yet." While this is not technically a service provider load order issue, attempting to connect to the database clearly still falls in the category of functionality not related to service container binding.

I believe the same behavior can be achieved without connecting to the database by relying solely on config:

if (! defined('TOTEM_DATABASE_CONNECTION')) {
    define('TOTEM_DATABASE_CONNECTION', config('totem.database_connection', config('database.default'));
}

I'm happy to submit a PR if this seems like the right solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions