laravel-database-schedule icon indicating copy to clipboard operation
laravel-database-schedule copied to clipboard

can't override viewDatabaseSchedule gate in custom or app service provider

Open klytron opened this issue 3 years ago • 2 comments

the existence of viewDatabaseSchedule in DatabaseScheduleApplicationServiceProvider.php is causing difficulty in overriding in AppServiceProvider or any custom ServiceProvider.

i think you should remove it allow users to set it rather.

klytron avatar Sep 22 '22 21:09 klytron

Better still DatabaseSchedulingServiceProvider should extend Illuminate\Support\ServiceProvider and not DatabaseScheduleApplicationServiceProvider.

DatabaseScheduleApplicationServiceProvider will be extended by the user and defined in config/app.php by the user but not automatically.

it should finally be:

namespace RobersonFaria\DatabaseSchedule;

use Cron\CronExpression;
use RobersonFaria\DatabaseSchedule\Console\Commands\PhpUnitTestJobCommand;
use RobersonFaria\DatabaseSchedule\View\Helpers;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;
use RobersonFaria\DatabaseSchedule\Observer\ScheduleObserver;
use Illuminate\Console\Scheduling\Schedule as BaseSchedule;
use RobersonFaria\DatabaseSchedule\Console\Commands\TestJobCommand;
use RobersonFaria\DatabaseSchedule\Console\Commands\ScheduleClearCacheCommand;
use RobersonFaria\DatabaseSchedule\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider;

class DatabaseSchedulingServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerRoutes();

        Route::model('schedule', config('database-schedule.model'));

        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');

        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'schedule');

        $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'schedule');

        Validator::extend('cron', function ($attribute, $value, $parameters, $validator) {
            return CronExpression::isValidExpression($value);
        });

        $this->publishes([
            __DIR__ . '/../config/database-schedule.php' => config_path('database-schedule.php'),
        ], 'config');

        $this->publishes([
            __DIR__ . '/../resources/lang/' => resource_path('lang/vendor/schedule'),
        ], 'translates');

        $this->publishes([
            __DIR__ . '/../resources/views' => resource_path('views/vendor/schedule'),
        ], 'views');

        $config = $this->app['config'];

        if ($config->get('database-schedule.cache.enabled')) {
            $model = $config->get('database-schedule.model');
            $model::observe(ScheduleObserver::class);
        }

        if (substr(app()->version(), 0, 1) >= 8) {
            \Illuminate\Pagination\Paginator::useBootstrap();
        }

        $this->app->resolving(BaseSchedule::class, function ($schedule) {
            $schedule = app(Schedule::class, ['schedule' => $schedule]);
            return $schedule->execute();
        });

        $this->commands([
            TestJobCommand::class,
            PhpUnitTestJobCommand::class,
            ScheduleClearCacheCommand::class,
        ]);
    }

    protected function registerRoutes()
    {
        Route::group([
            'prefix' => config('database-schedule.route.prefix'),
            'namespace' => 'RobersonFaria\DatabaseSchedule\Http\Controllers',
            'middleware' => config('database-schedule.middleware', 'web'),
        ], function () {
            $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->mergeConfigFrom(
            __DIR__ . '/../config/database-schedule.php',
            'database-schedule'
        );

        // @link https://stackoverflow.com/questions/21574413/when-making-a-laravel-package-how-do-i-register-the-service-provider-and-alias
        AliasLoader::getInstance()->alias('Helpers', Helpers::class);
    }

    protected function scheduleTimezone($config)
    {
        return $config->get('schedule.timezone');
    }

    protected function scheduleCache()
    {
        return $_ENV[''] ?? null;
    }
}

klytron avatar Sep 22 '22 22:09 klytron

Hello, unfortunately I'm working on several projects at the moment and I'm not able to give this package enough attention. Please open a pull request with your suggestion, if possible with automated tests, which I will be happy to approve.

robersonfaria avatar Sep 23 '22 00:09 robersonfaria