Skip to content
Merged
2 changes: 1 addition & 1 deletion .github/workflows/bump-docs-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
ref: main
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2.5.0
uses: dependabot/fetch-metadata@v3.0.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
working-directory: ./docs
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
fetch-depth: 0 # Not needed if lastUpdated is not enabled
- name: Setup Node
Expand All @@ -38,7 +38,7 @@ jobs:
cache: npm
cache-dependency-path: docs/package-lock.json
- name: Setup Pages
uses: actions/configure-pages@v5
uses: actions/configure-pages@v6
- name: Install dependencies
run: npm install
- name: Build with VitePress
Expand All @@ -59,4 +59,4 @@ jobs:
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
uses: actions/deploy-pages@v5
2 changes: 1 addition & 1 deletion .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
ref: ${{ github.head_ref }}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -22,7 +22,7 @@ jobs:
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v3
uses: ramsey/composer-install@v4

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
8 changes: 5 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.4]
laravel: [12.*]
php: [8.4, 8.5]
laravel: [12.*, 13.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 12.*
testbench: 10.*
- laravel: 13.*
testbench: 11.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v5
uses: actions/checkout@v6

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v5
uses: actions/checkout@v6
with:
ref: main

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
],
"require": {
"php": "^8.4",
"illuminate/contracts": "^12.1",
"illuminate/contracts": "^12.1||^13.0",
"spatie/laravel-package-tools": "^1.16"
},
"require-dev": {
"laravel/pint": "^1.14",
"nunomaduro/collision": "^8.1.1||^7.10.0",
"larastan/larastan": "^2.9||^3.0",
"orchestra/testbench": "^10.0.0||^9.0.0||^8.22.0",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-arch": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"orchestra/testbench": "^10.0.0||^11.0.0",
"pestphp/pest": "^4.0",
"pestphp/pest-plugin-arch": "^4.0",
"pestphp/pest-plugin-laravel": "^4.0",
"phpstan/extension-installer": "^1.3||^2.0",
"phpstan/phpstan-deprecation-rules": "^1.1||^2.0",
"phpstan/phpstan-phpunit": "^1.3||^2.0"
Expand Down
20 changes: 11 additions & 9 deletions src/Concerns/StatefullyLoadsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Farbcode\StatefulResources\Contracts\ResourceState;
use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
use Illuminate\Http\Resources\MergeValue;
use Illuminate\Http\Resources\MissingValue;

/**
* @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes
* @see ConditionallyLoadsAttributes
*/
trait StatefullyLoadsAttributes
{
Expand All @@ -18,7 +20,7 @@ trait StatefullyLoadsAttributes
* @param string|ResourceState $state
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
* @return MissingValue|mixed
*/
protected function whenState($state, $value, $default = null)
{
Expand All @@ -37,7 +39,7 @@ protected function whenState($state, $value, $default = null)
* @param string|ResourceState $state
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
* @return MissingValue|mixed
*/
protected function unlessState($state, $value, $default = null)
{
Expand All @@ -56,7 +58,7 @@ protected function unlessState($state, $value, $default = null)
* @param array<string|ResourceState> $states
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
* @return MissingValue|mixed
*/
protected function whenStateIn(array $states, $value, $default = null)
{
Expand All @@ -77,7 +79,7 @@ protected function whenStateIn(array $states, $value, $default = null)
* @param array<string|ResourceState> $states
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
* @return MissingValue|mixed
*/
protected function unlessStateIn(array $states, $value, $default = null)
{
Expand All @@ -98,7 +100,7 @@ protected function unlessStateIn(array $states, $value, $default = null)
* @param string|ResourceState $state
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MergeValue|mixed
* @return MergeValue|mixed
*/
protected function mergeWhenState($state, $value, $default = null)
{
Expand All @@ -117,7 +119,7 @@ protected function mergeWhenState($state, $value, $default = null)
* @param string|ResourceState $state
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MergeValue|mixed
* @return MergeValue|mixed
*/
protected function mergeUnlessState($state, $value, $default = null)
{
Expand All @@ -136,7 +138,7 @@ protected function mergeUnlessState($state, $value, $default = null)
* @param array<string|ResourceState> $states
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MergeValue|mixed
* @return MergeValue|mixed
*/
protected function mergeWhenStateIn(array $states, $value, $default = null)
{
Expand All @@ -157,7 +159,7 @@ protected function mergeWhenStateIn(array $states, $value, $default = null)
* @param array<string|ResourceState> $states
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MergeValue|mixed
* @return MergeValue|mixed
*/
protected function mergeUnlessStateIn(array $states, $value, $default = null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/StatefulResourceMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Farbcode\StatefulResources\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'make:stateful-resource')]
class StatefulResourceMakeCommand extends \Illuminate\Console\GeneratorCommand
class StatefulResourceMakeCommand extends GeneratorCommand
{
/**
* The console command name.
Expand Down
2 changes: 1 addition & 1 deletion src/StatefulJsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct($resource)
*
* @param string $method
* @param array $parameters
* @return \Farbcode\StatefulResources\Builder
* @return Builder
*
* @throws \BadMethodCallException
*/
Expand Down
8 changes: 4 additions & 4 deletions src/StatefulResourcesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public function configurePackage(Package $package): void

public function bootingPackage(): void
{
$states = config()->array('stateful-resources.states');
$states = config()->collection('stateful-resources.states');

$this->app->singleton(StateRegistry::class, function () use ($states) {
$registry = new StateRegistry;

foreach ($states as $state) {
$states->each(function (string|ResourceState $state) use ($registry) {
if ($state instanceof ResourceState) {
$state = $state->value;
}
$registry->register($state);
}
});

return $registry;
});

$this->app->singleton(ActiveState::class, function () {
$this->app->scoped(ActiveState::class, function () {
return new ActiveState;
});
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/SharedStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,16 @@

expect(ActiveState::getShared())->toBe('table');
});

it('resets the active state between requests', function () {
$cat = Cat::factory()->new()->createOne();

CatResource::state('table')->make($cat);

expect(ActiveState::getShared())->toBe('table');

simulateNewOctaneRequest();

expect(ActiveState::getShared())->toBe(app(StateRegistry::class)->getDefaultState());
expect(ActiveState::getForResource(CatResource::class))->toBe(app(StateRegistry::class)->getDefaultState());
});
12 changes: 12 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
<?php

use Farbcode\StatefulResources\Tests\TestCase;
use Illuminate\Support\Facades\Facade;

uses(TestCase::class)->in(__DIR__);

/*
|--------------------------------------------------------------------------
| Utility Functions
|--------------------------------------------------------------------------
*/
function simulateNewOctaneRequest(): void
{
app()->forgetScopedInstances();
Facade::clearResolvedInstances();
}

/*
|--------------------------------------------------------------------------
| Expectations
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Farbcode\StatefulResources\Enums\State;
use Farbcode\StatefulResources\StatefulResourcesServiceProvider;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as Orchestra;
Expand Down Expand Up @@ -39,7 +40,7 @@ protected function getEnvironmentSetUp($app)
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @param Application $app
* @return void
*/
protected function defineEnvironment($app)
Expand Down
5 changes: 3 additions & 2 deletions workbench/app/Models/Cat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
* @property int $id
* @property string $name
* @property string $breed
* @property string $fluffyness
* @property string $color
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
class Cat extends Model
{
Expand Down
8 changes: 5 additions & 3 deletions workbench/app/Models/Dog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace Workbench\App\Models;

use Database\Factories\DogFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
* @property int $id
* @property string $name
* @property string $breed
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*/
class Dog extends Model
{
/** @use HasFactory<\Database\Factories\DogFactory> */
/** @use HasFactory<DogFactory> */
use HasFactory;

/**
Expand Down
3 changes: 2 additions & 1 deletion workbench/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Workbench\Database\Factories\UserFactory;

class User extends Authenticatable
{
/** @use HasFactory<\Workbench\Database\Factories\UserFactory> */
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;

/**
Expand Down
2 changes: 1 addition & 1 deletion workbench/database/factories/CatFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @template TModel of \Workbench\App\Models\Cat
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
* @extends Factory<TModel>
*/
class CatFactory extends Factory
{
Expand Down
2 changes: 1 addition & 1 deletion workbench/database/factories/DogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @template TModel of \Workbench\App\Models\Dog
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
* @extends Factory<TModel>
*/
class DogFactory extends Factory
{
Expand Down
2 changes: 1 addition & 1 deletion workbench/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @template TModel of \Workbench\App\Models\User
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
* @extends Factory<TModel>
*/
class UserFactory extends Factory
{
Expand Down