Skip to content

Commit

Permalink
Add laravel 8 support. Simplify usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Nagirnyi committed Dec 3, 2020
1 parent 72e5c89 commit 7af3436
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 73 deletions.
20 changes: 1 addition & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,7 @@ Store your schedules in database (cache friendly)

3) Apply migration ```php artisan migrate```

4) Add new Schedule command to your app/Console/Kernel.php

```php
<?php
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\TheRezor\DatabaseSchedule\Console\ScheduleRunCommand::class,
];
}
?>
```

5) Use `TheRezor\DatabaseSchedule\Models\Schedule` to manage your database schedule
4) Use `TheRezor\DatabaseSchedule\Models\Schedule` to manage your database schedule

```php
<?php
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"license": "MIT",
"require": {
"php": ">=7.0",
"illuminate/support": "^5.6|^5.7|^5.8|^6.0|^7.0|^8.0"
"illuminate/support": "^5.6|^5.7|^5.8|^6.0",
"illuminate/database": "^5.6|^5.7|^5.8|^6.0",
"illuminate/console": "^5.6|^5.7|^5.8|^6.0"
},
"authors": [
{
Expand Down
40 changes: 0 additions & 40 deletions src/Console/ScheduleRunCommand.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Models/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace TheRezor\DatabaseSchedule\Models;

use App\Filters\System\NotificationFilter;
use EloquentFilter\Filterable;
use Illuminate\Console\Scheduling\ManagesFrequencies;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;

Expand Down
47 changes: 47 additions & 0 deletions src/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace TheRezor\DatabaseSchedule\Scheduling;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Console\Scheduling\Schedule as BaseSchedule;

class Schedule extends BaseSchedule
{
protected $isScheduleAdded = false;

public function dueEvents($app)
{
if ($this->isScheduleAdded) {
return parent::dueEvents($app);
}

$model = $app->make(Config::get('database-schedule.model'));
$schedules = Config::get('database-schedule.cache.enabled') ? $this->getFromCache($model) : $model->all()->toArray();

foreach ($schedules as $s) {
$event = $this->command($s['command'], $s['params'])->cron($s['expression']);

if ($s['even_in_maintenance_mode']) {
$event->evenInMaintenanceMode();
}

if ($s['without_overlapping']) {
$event->withoutOverlapping();
}
}

return parent::dueEvents($app);
}

protected function getFromCache(Model $model)
{
$store = Config::get('database-schedule.cache.store', 'file');
$key = Config::get('database-schedule.cache.key', 'database_schedule');

return Cache::store($store)->rememberForever($key, function () use ($model) {
return $model->all()->toArray();
});
}
}
33 changes: 23 additions & 10 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace TheRezor\DatabaseSchedule;

use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Illuminate\Support\Facades\Config;
use TheRezor\DatabaseSchedule\Observer\ScheduleObserver;
use Illuminate\Console\Scheduling\Schedule as BaseSchedule;
use TheRezor\DatabaseSchedule\Scheduling\Schedule;

class ServiceProvider extends LaravelServiceProvider
{
Expand All @@ -15,26 +18,36 @@ class ServiceProvider extends LaravelServiceProvider
*/
public function boot()
{
$configPath = __DIR__.'/../config/database-schedule.php';
$this->publishes([
__DIR__ . '/../config/database-schedule.php' => config_path('database-schedule.php'),
$configPath => App::configPath('database-schedule.php'),
], 'config');
$this->mergeConfigFrom($configPath, 'database-schedule');

$this->publishes([
__DIR__ . '/../migrations/' => database_path('migrations'),
__DIR__.'/../migrations/' => App::databasePath('migrations'),
], 'migrations');

if (Config::get('database-schedule.cache.enabled')) {
$model = Config::get('database-schedule.model');
$config = $this->app['config'];

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

$this->app->extend(BaseSchedule::class, function () use ($config) {
return (new Schedule($this->scheduleTimezone($config)))
->useCache($this->scheduleCache());
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
protected function scheduleTimezone($config)
{
return $config->get('app.schedule_timezone', $config->get('app.timezone'));
}

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

0 comments on commit 7af3436

Please sign in to comment.