Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
scryba opened this issue Sep 22, 2022 · 2 comments
Open

Comments

@scryba
Copy link

scryba commented Sep 22, 2022

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.

@scryba scryba changed the title can't override viewDatabaseSchedule in custom or app service provider can't override viewDatabaseSchedule gate in custom or app service provider Sep 22, 2022
@scryba
Copy link
Author

scryba commented Sep 22, 2022

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;
    }
}

@robersonfaria
Copy link
Owner

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.

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

No branches or pull requests

2 participants