Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,22 @@ Link: <https://docs.example.com/sunset-policy>; rel="sunset"
- Error responses get the headers too, because both describe the resource, not the outcome of
one request.

A URI's query is part of what identifies the resource (RFC 3986 section 3), so a query form of a
route is a resource of its own, and can be deprecated while the route is not. Give the attributes
the query parameter, and they apply only to requests that carry it:

```php
#[Deprecation(at: '2026-10-29T00:00:00Z', link: 'https://docs.example.com/deprecations/contact-filter', query: 'organization_id')]
#[Sunset(at: '2027-01-31T23:59:59Z', query: 'organization_id')]
final class ListContactsController
{
// GET /contacts?organization_id=... is deprecated; GET /contacts is not.
}
```

Both attributes are repeatable, so one controller can carry a notice for itself and another for a
query form. A notice for a query form the request carries wins over one without a query.

The attributes are named after the header fields because `#[Deprecated]` is taken: PHP 8.4's
native `#[\Deprecated]` raises `E_USER_DEPRECATED` every time the method is called, which on a
controller means on every request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class OrderController
- Only controller routes are read. Closure routes are not, because `route:cache` breaks their attributes. Move a closure route to a controller to deprecate it.
- A date without an offset is UTC. A deprecation date can be in the future. A sunset earlier than the deprecation throws.
- `#[Deprecation(link: ...)]` with no date publishes a deprecation policy only.
- To retire a query form of a route, such as `/contacts?organization_id=...`, while the route stays, pass `query: 'organization_id'` to both attributes. They are repeatable, and the query form's notice wins when the request carries the parameter. A field has no header: deprecate it in the OpenAPI document instead.
- With the `global` option on (the default), nothing is added to routes. With it off, add the `deprecation` middleware.
- Reading deprecations from elsewhere, such as a manifest: bind `JustSteveKing\Toolbox\Deprecation\Contracts\DeprecationSource`, which returns a `JustSteveKing\Toolbox\Deprecation\Notice` for a route.

Expand Down
79 changes: 58 additions & 21 deletions src/Deprecation/AttributeSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace JustSteveKing\Toolbox\Deprecation;

use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use JustSteveKing\Toolbox\Deprecation\Attributes\Deprecation;
use JustSteveKing\Toolbox\Deprecation\Attributes\Sunset;
Expand All @@ -15,7 +17,9 @@
* Reads #[Deprecation] and #[Sunset] from a route's controller and action.
*
* An attribute on the action wins over the same attribute on the controller; the two kinds are
* resolved separately, so a controller-wide Sunset can sit beside a per-action Deprecation.
* resolved separately, so a controller-wide Sunset can sit beside a per-action Deprecation. An
* attribute with a query applies only when the request carries that query parameter, and wins
* over one without.
*
* Only controller routes are read, as with Laravel's own #[Middleware] controller attributes.
* Closure routes are not, because `route:cache` serialises closures and their attributes cannot
Expand All @@ -24,7 +28,9 @@
final class AttributeSource implements DeprecationSource
{
/**
* @var array<string, Notice|null>
* The attributes on each action, read once, by kind; the notice itself depends on the request.
*
* @var array<string, array{deprecation: list<Deprecation>, sunset: list<Sunset>}>
*/
private array $resolved = [];

Expand All @@ -43,21 +49,22 @@ public function forRoute(Route $route): ?Notice
$key = $class.'@'.$method;

if (! array_key_exists($key, $this->resolved)) {
$this->resolved[$key] = $this->read(new ReflectionClass($class), $method);
}
$reflection = new ReflectionClass($class);
$action = $reflection->hasMethod($method) ? $reflection->getMethod($method) : null;

return $this->resolved[$key];
}
$this->resolved[$key] = [
'deprecation' => $this->attributes(Deprecation::class, $reflection, $action),
'sunset' => $this->attributes(Sunset::class, $reflection, $action),
];
}

/**
* @param ReflectionClass<object> $class
*/
private function read(ReflectionClass $class, string $method): ?Notice
{
$action = $class->hasMethod($method) ? $class->getMethod($method) : null;
// Read when asked rather than held: this source is a singleton, and under Octane one
// instance serves many requests.
$request = Container::getInstance()->bound('request') ? Container::getInstance()->make('request') : null;
$parameters = $request instanceof Request ? array_map(strval(...), $request->query->keys()) : [];

$deprecation = $this->attribute(Deprecation::class, $class, $action);
$sunset = $this->attribute(Sunset::class, $class, $action);
$deprecation = $this->choose($this->resolved[$key]['deprecation'], $parameters);
$sunset = $this->choose($this->resolved[$key]['sunset'], $parameters);

if ($deprecation === null && $sunset === null) {
return null;
Expand All @@ -72,22 +79,52 @@ private function read(ReflectionClass $class, string $method): ?Notice
}

/**
* The attribute that applies to this request: one scoped to a query parameter the request
* carries, or else one for the route itself. Attributes come action first, so within each of
* those an action's attribute wins over its controller's.
*
* @template T of Deprecation|Sunset
*
* @param list<T> $attributes
* @param list<string> $parameters the query parameters the request carries
* @return T|null
*/
private function choose(array $attributes, array $parameters): ?object
{
foreach ($attributes as $attribute) {
if ($attribute->query !== null && in_array($attribute->query, $parameters, true)) {
return $attribute;
}
}

foreach ($attributes as $attribute) {
if ($attribute->query === null) {
return $attribute;
}
}

return null;
}

/**
* Every attribute of a kind, the action's before its controller's.
*
* @template T of object
*
* @param class-string<T> $attribute
* @param ReflectionClass<object> $class
* @return T|null
* @return list<T>
*/
private function attribute(string $attribute, ReflectionClass $class, ?ReflectionMethod $action): ?object
private function attributes(string $attribute, ReflectionClass $class, ?ReflectionMethod $action): array
{
foreach ([$action, $class] as $target) {
$found = $target?->getAttributes($attribute) ?? [];
$found = [];

if ($found !== []) {
return $found[0]->newInstance();
foreach ([$action, $class] as $target) {
foreach ($target?->getAttributes($attribute) ?? [] as $reflected) {
$found[] = $reflected->newInstance();
}
}

return null;
return $found;
}
}
11 changes: 10 additions & 1 deletion src/Deprecation/Attributes/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
* $at may be in the future: section 2.1 says the date says when the resource "will be or has
* been deprecated". A date without an offset is read as UTC. With no $at, only the Link is sent,
* which section 3.1 describes as publishing a deprecation policy before deprecating anything.
*
* With $query, it applies only to requests that carry that query parameter. A URI's query is part
* of what identifies the resource (RFC 3986, section 3), so /contacts?organization_id=X is a
* resource of its own, and can be deprecated while /contacts is not:
*
* #[Deprecation(at: '2026-10-29T00:00:00Z', query: 'organization_id')]
*
* Repeatable, so one controller can carry a notice for the route and another for a query form.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final readonly class Deprecation
{
public function __construct(
public ?string $at = null,
public ?string $link = null,
public ?string $query = null,
) {}
}
11 changes: 10 additions & 1 deletion src/Deprecation/Attributes/Sunset.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@
* #[Sunset(at: '2027-01-01T00:00:00Z', link: 'https://docs.example.com/sunset-policy')]
*
* A date without an offset is read as UTC.
*
* With $query, it applies only to requests that carry that query parameter. A URI's query is part
* of what identifies the resource (RFC 3986, section 3), so /contacts?organization_id=X is a
* resource of its own, and can be deprecated while /contacts is not:
*
* #[Sunset(at: '2027-01-31T23:59:59Z', query: 'organization_id')]
*
* Repeatable, so one controller can carry a notice for the route and another for a query form.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final readonly class Sunset
{
public function __construct(
public string $at,
public ?string $link = null,
public ?string $query = null,
) {}
}
25 changes: 25 additions & 0 deletions tests/Feature/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use JustSteveKing\Toolbox\Deprecation\Contracts\DeprecationSource;
use JustSteveKing\Toolbox\Deprecation\Notice;
use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\CurrentController;
use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\FilteredContactsController;
use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\LegacyOrdersController;
use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\MisconfiguredController;
use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\OffsetController;
Expand Down Expand Up @@ -119,3 +120,27 @@ public function forRoute(RoutingRoute $route): ?Notice

expect($this->get('api/orders')->headers->all('link'))->toHaveCount(2);
});

it('deprecates a query form of a route on its own, since its URI is a resource of its own', function (): void {
Route::get('api/contacts', [FilteredContactsController::class, 'index']);

$filtered = $this->get('api/contacts?organization_id=42')->assertOk();

expect($filtered->headers->get('Deprecation'))->toBe('@1793232000')
->and($filtered->headers->get('Sunset'))->toBe('Sun, 31 Jan 2027 23:59:59 GMT')
->and($filtered->headers->all('link'))->toBe(['<https://developer.example.com/filters>; rel="deprecation"']);

$plain = $this->get('api/contacts?search=ada')->assertOk();

expect($plain->headers->has('Deprecation'))->toBeFalse()
->and($plain->headers->has('Sunset'))->toBeFalse()
->and($plain->headers->has('Link'))->toBeFalse();
});

it('prefers a query form\'s notice to the route\'s own, and falls back to it without the query', function (): void {
Route::get('api/contacts/export', [FilteredContactsController::class, 'export']);

// The action's own Deprecation applies without the query; with it, the class's scoped one does.
expect($this->get('api/contacts/export')->headers->get('Deprecation'))->toBe('@1767225600')
->and($this->get('api/contacts/export?organization_id=42')->headers->get('Deprecation'))->toBe('@1793232000');
});
27 changes: 27 additions & 0 deletions tests/Fixtures/Controllers/FilteredContactsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace JustSteveKing\Toolbox\Tests\Fixtures\Controllers;

use JustSteveKing\Toolbox\Deprecation\Attributes\Deprecation;
use JustSteveKing\Toolbox\Deprecation\Attributes\Sunset;

/**
* A list whose filter is being retired, and an action that is deprecated whole as well.
*/
#[Deprecation(at: '2026-10-29T00:00:00Z', link: 'https://developer.example.com/filters', query: 'organization_id')]
#[Sunset(at: '2027-01-31T23:59:59Z', query: 'organization_id')]
final class FilteredContactsController
{
public function index(): string
{
return 'contacts';
}

#[Deprecation(at: '2026-01-01T00:00:00Z', link: 'https://developer.example.com/export')]
public function export(): string
{
return 'export';
}
}
Loading