├── .github └── workflows │ ├── php-cs-fixer.yml │ └── run-tests.yml ├── .phpunit.cache └── test-results ├── .styleci.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Commands ├── TaskMakeCommand.php └── stubs │ └── task.stub ├── Facades └── TaskLoader.php ├── SchedulingTasksServiceProvider.php ├── TaskContract.php └── TaskLoader.php /.github/workflows/php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [push] 4 | 5 | jobs: 6 | style: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v2 12 | 13 | - name: Fix style 14 | uses: docker://oskarstark/php-cs-fixer-ga 15 | with: 16 | args: --config=.php_cs --allow-risky=yes 17 | 18 | - name: Extract branch name 19 | shell: bash 20 | run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" 21 | id: extract_branch 22 | 23 | - name: Commit changes 24 | uses: stefanzweifel/git-auto-commit-action@v2.3.0 25 | with: 26 | commit_message: Fix styling 27 | branch: ${{ steps.extract_branch.outputs.branch }} 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | fail-fast: true 10 | matrix: 11 | os: [ubuntu-latest] 12 | php: [7.4] 13 | laravel: [8.*, 7.*] 14 | dependency-version: [prefer-lowest, prefer-stable] 15 | include: 16 | - laravel: 8.* 17 | testbench: 6.* 18 | - laravel: 7.* 19 | testbench: 5.* 20 | 21 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 22 | 23 | steps: 24 | - name: Checkout code 25 | uses: actions/checkout@v2 26 | 27 | - name: Cache dependencies 28 | uses: actions/cache@v2 29 | with: 30 | path: ~/.composer/cache/files 31 | key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 32 | 33 | - name: Setup PHP 34 | uses: shivammathur/setup-php@v2 35 | with: 36 | php-version: ${{ matrix.php }} 37 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 38 | coverage: none 39 | 40 | - name: Install dependencies 41 | run: | 42 | composer config --global --auth http-basic.repo.packagist.com token ${{ secrets.COMPOSER_TOKEN }} 43 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 44 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest 45 | - name: Execute tests 46 | run: vendor/bin/phpunit 47 | -------------------------------------------------------------------------------- /.phpunit.cache/test-results: -------------------------------------------------------------------------------- 1 | {"version":1,"defects":[],"times":{"Signifly\\SchedulingTasks\\Test\\SchedulingTasksTest::it_skips_loading_excludes":0.013,"Signifly\\SchedulingTasks\\Test\\SchedulingTasksTest::it_invokes_a_found_task":0}} -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-scheduling-tasks` will be documented in this file 4 | 5 | ## 1.5.1 - 2020-09-14 6 | - Add support for Laravel 8 7 | 8 | ## 1.2.0 - 2019-03-06 9 | - Add support for Laravel 5.8 10 | 11 | ## 1.1.1 - 2019-02-01 12 | - Add the possibility to define if the Task should only run in production 13 | 14 | ## 1.1.0 - 2018-09-27 15 | - Add support for Laravel 5.7 16 | 17 | ## 1.0.0 - 2018-08-13 18 | - Release v1 19 | 20 | ## 0.0.3 - 2018-05-04 21 | 22 | - Added: Exclude tasks from getting loaded 23 | 24 | ## 0.0.2 - 2018-05-04 25 | 26 | - Changed: Make TaskLoader a singleton and add a facade 27 | 28 | ## 0.0.1 - 2018-05-04 29 | 30 | - Experimental release! 31 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Signifly 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Organize your Laravel scheduling tasks 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/signifly/laravel-scheduling-tasks.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-scheduling-tasks) 4 | ![Tests](https://github.com/signifly/laravel-janitor/workflows/Tests/badge.svg) 5 | [![StyleCI](https://styleci.io/repos/132112236/shield?branch=master)](https://styleci.io/repos/132112236) 6 | [![Quality Score](https://img.shields.io/scrutinizer/g/signifly/laravel-scheduling-tasks.svg?style=flat-square)](https://scrutinizer-ci.com/g/signifly/laravel-scheduling-tasks) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/signifly/laravel-scheduling-tasks.svg?style=flat-square)](https://packagist.org/packages/signifly/laravel-scheduling-tasks) 8 | 9 | The `signifly/laravel-scheduling-tasks` package allows you to easily organize your scheduling tasks and comes with a handy `make:task` command. 10 | 11 | Below is a small example of how to use it. 12 | 13 | ```php 14 | // Inside the app/Console/Kernel.php file add this 15 | use Signifly\SchedulingTasks\Facades\TaskLoader; 16 | 17 | protected function schedule(Schedule $schedule) 18 | { 19 | TaskLoader::loadFor($schedule); 20 | } 21 | ``` 22 | 23 | In order to make a new task, use the command that comes with the package: 24 | 25 | ```bash 26 | $ php artisan make:task BackupDaily 27 | ``` 28 | 29 | It generates a new task located at `app/Console/Tasks/BackupDaily.php`, which can be configured this way: 30 | 31 | ```php 32 | command('backup:run') 44 | ->daily() 45 | ->at('01:00'); 46 | } 47 | } 48 | ``` 49 | 50 | In case you have a task that you want to exclude from getting loaded, it can be achieved like this: 51 | 52 | ```php 53 | protected function schedule(Schedule $schedule) 54 | { 55 | TaskLoader::loadFor($schedule, [ 56 | \App\Console\Tasks\BackupDaily::class, 57 | ]); 58 | 59 | // \App\Console\Tasks\BackupDaily::class will not get loaded. 60 | } 61 | ``` 62 | 63 | 64 | ## Installation 65 | 66 | You can install the package via composer: 67 | 68 | ```bash 69 | $ composer require signifly/laravel-scheduling-tasks 70 | ``` 71 | 72 | The package will automatically register itself. 73 | 74 | ## Testing 75 | ```bash 76 | $ composer test 77 | ``` 78 | 79 | ## Security 80 | 81 | If you discover any security issues, please email dev@signifly.com instead of using the issue tracker. 82 | 83 | ## Credits 84 | 85 | - [Morten Poul Jensen](https://github.com/pactode) 86 | - [All contributors](../../contributors) 87 | 88 | ## License 89 | 90 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 91 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "signifly/laravel-scheduling-tasks", 3 | "description": "Organize your Laravel scheduling tasks.", 4 | "homepage": "https://github.com/signifly/laravel-scheduling-tasks", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Morten Poul Jensen", 9 | "email": "mpj@signifly.com", 10 | "role": "Developer" 11 | } 12 | ], 13 | "require": { 14 | "php": "^7.2.5|^8.0", 15 | "illuminate/console": "^8.0|^9.0|^10.0", 16 | "illuminate/support": "^8.0|^9.0|^10.0" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0", 20 | "orchestra/testbench": "^6.0|^7.0|^8.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Signifly\\SchedulingTasks\\": "src" 25 | } 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Signifly\\SchedulingTasks\\Test\\": "tests" 30 | } 31 | }, 32 | "scripts": { 33 | "test": "vendor/bin/phpunit" 34 | }, 35 | "config": { 36 | "sort-packages": true 37 | }, 38 | "extra": { 39 | "laravel": { 40 | "providers": [ 41 | "Signifly\\SchedulingTasks\\SchedulingTasksServiceProvider" 42 | ], 43 | "aliases": { 44 | "TaskLoader": "Signifly\\SchedulingTasks\\Facades\\TaskLoader" 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Commands/TaskMakeCommand.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 18 | $this->commands([ 19 | TaskMakeCommand::class, 20 | ]); 21 | } 22 | } 23 | 24 | /** 25 | * Register the service provider. 26 | * 27 | * @return void 28 | */ 29 | public function register() 30 | { 31 | $this->app->singleton(TaskLoader::class, function ($app) { 32 | return new TaskLoader($app); 33 | }); 34 | 35 | $this->app->alias(TaskLoader::class, 'task-loader'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/TaskContract.php: -------------------------------------------------------------------------------- 1 | app = $app; 18 | } 19 | 20 | public function loadFor(Schedule $schedule, array $exclude = []) 21 | { 22 | $namespace = $this->app->getNamespace(); 23 | 24 | $path = $this->app->path('Console/Tasks'); 25 | 26 | if (! is_dir($path)) { 27 | return; 28 | } 29 | 30 | foreach ((new Finder)->in($path)->files() as $taskFile) { 31 | $task = $namespace.str_replace( 32 | ['/', '.php'], 33 | ['\\', ''], 34 | Str::after($taskFile->getPathname(), $this->app->path().DIRECTORY_SEPARATOR) 35 | ); 36 | 37 | if (in_array($task, $exclude)) { 38 | continue; 39 | } 40 | 41 | if (is_subclass_of($task, TaskContract::class) && 42 | ! (new ReflectionClass($task))->isAbstract()) { 43 | 44 | // Invoke task 45 | app($task)($schedule); 46 | } 47 | } 48 | } 49 | } 50 | --------------------------------------------------------------------------------