Skip to content

Commit

Permalink
Added compatibility with Laravel 8 (#118)
Browse files Browse the repository at this point in the history
* Added compatibility with Laravel 8

* Test specific laravel versions
  • Loading branch information
wouterj committed Nov 7, 2020
1 parent d2cdccd commit 8a10ab9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
40 changes: 25 additions & 15 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,70 @@ on:
env:
SYMFONY_PHPUNIT_REMOVE: "symfony/yaml"
SYMFONY_DEPRECATIONS_HELPER: "max[direct]=0"
PHP_VERSION: 7.4

jobs:
build:
runs-on: ubuntu-latest

continue-on-error: ${{ matrix.experimental }}
name: 'Tests using deps=${{ matrix.deps }}'
name: '${{ matrix.deps }} deps: PHP=${{ matrix.php }}, Symfony=${{ matrix.symfony }}, Laravel=${{ matrix.laravel }}'
strategy:
fail-fast: false
matrix:
deps: ['low', '4.x', '5.x']
experimental: [false]
include:
- deps: dev
- deps: 'low'
php: 7.2
experimental: false

- deps: 'stable'
php: 7.4
laravel: '7.x'
symfony: '5.x'
experimental: false

- deps: 'stable'
php: 7.4
symfony: '5.x'
laravel: '8.x'
experimental: false

- deps: 'dev'
php: 8.0
experimental: true

steps:
- uses: actions/checkout@v2

- name: Set-up env variables
id: ctx
run: |
case "${{ matrix.deps }}" in
"low")
echo "::set-output name=php_version::7.2"
echo "::set-output name=composer_flags::--prefer-lowest"
echo "COMPOSER_FLAGS=--prefer-lowest" >> $GITHUB_ENV
echo "SYMFONY_DEPRECATIONS_HELPER=disabled=1" >> $GITHUB_ENV
;;
"dev")
echo "::set-output name=php_version::8.0"
echo "SYMFONY_DEPRECATIONS_HELPER=max[total]=999" >> $GITHUB_ENV
perl -pi -e "s/^}\$/,\"minimum-stability\":\"dev\"}/" composer.json
composer config platform.php 7.4.99
;;
*)
echo "::set-output name=php_version::$PHP_VERSION"
echo "SYMFONY_REQUIRE=$(echo '${{ matrix.deps }}' | tr x \\*)" >> $GITHUB_ENV
echo "SYMFONY_REQUIRE=$(echo '${{ matrix.symfony }}' | tr x \\*)" >> $GITHUB_ENV
echo "LARAVEL_REQUIRE=$(echo '${{ matrix.laravel }}' | tr x \\*)" >> $GITHUB_ENV
esac
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ steps.ctx.outputs.php_version }}
php-version: ${{ matrix.php }}
extensions: xml, pdo_sqlite
tools: "composer:v2"

- name: Install dependencies
run: |
composer global require --no-progress --no-scripts --no-plugins symfony/flex
composer update --prefer-dist --no-progress ${{ steps.ctx.outputs.composer_flags }}
if [ "$LARAVEL_REQUIRE" != "" ]; then composer require illuminate/database:$LARAVEL_REQUIRE; fi
composer update --prefer-dist --no-progress $COMPOSER_FLAGS
./vendor/bin/simple-phpunit install
- name: Show info
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

"require": {
"php": "^7.2 || ^8.0",
"illuminate/database": "^6.18 || ^7.15",
"illuminate/events": "^6.18 || ^7.15",
"illuminate/console": "^6.18 || ^7.15",
"illuminate/database": "^6.18 || ^7.15 || ^8.12",
"illuminate/events": "^6.18 || ^7.15 || ^8.12",
"illuminate/console": "^6.18 || ^7.15 || ^8.12",
"symfony/framework-bundle": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"jdorn/sql-formatter": "^1.2.17"
Expand Down
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<referencedMethod name="Symfony\Bundle\MakerBundle\FileManager::getRelativePathForFutureClass"/>
</errorLevel>
</InternalMethod>
<UndefinedMethod>
<errorLevel type="suppress">
<referencedMethod name="WouterJ\EloquentBundle\Seeder::run"/>
</errorLevel>
</UndefinedMethod>
</issueHandlers>

<plugins>
Expand Down
9 changes: 8 additions & 1 deletion src/Maker/MakeSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WouterJ\EloquentBundle\Maker;

use Illuminate\Database\Seeder as IlluminateSeeder;
use Illuminate\Database\Console\DumpCommand;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\FileManager;
Expand Down Expand Up @@ -65,7 +66,13 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$stub = str_replace(IlluminateSeeder::class, Seeder::class, $stub);

if ($namespace = Str::getNamespace($seederClassDetails->getFullName())) {
$stub = str_replace('<?php', "<?php\n\nnamespace ".$namespace.';', $stub);
if (class_exists(DumpCommand::class)) {
// Laravel 8
$stub = str_replace('namespace Database\Seeders;', 'namespace '.$namespace.';', $stub);
} else {
// Laravel 6 & 7
$stub = str_replace('<?php', "<?php\n\nnamespace ".$namespace.';', $stub);
}
}

$path = $this->fileManager->getRelativePathForFutureClass($seederClassDetails->getFullName());
Expand Down
10 changes: 4 additions & 6 deletions src/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ abstract class Seeder extends BaseSeeder
/** @var ConnectionInterface */
protected $connection;

abstract public function run();

public function __invoke()
public function __invoke(array $parameters = [])
{
return $this->run();
return $this->run(...$parameters);
}

public function call($class, $silent = false)
public function call($class, $silent = false, array $parameters = [])
{
$classes = is_array($class) ? $class : [$class];

Expand All @@ -47,7 +45,7 @@ public function call($class, $silent = false)

$startTime = microtime(true);

($seeder)();
($seeder)($parameters);

$runTime = round(microtime(true) - $startTime, 2);

Expand Down

0 comments on commit 8a10ab9

Please sign in to comment.