ryangjchandler/
using
g4t.io

Enforce the Disposal of Objects in PHP

19 views 53 stars by Paul Redmond

Enforced disposal of objects in PHP. 🐘

README from ryangjchandler/using Open on GitHub β†’

Enforced disposal of objects in PHP.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a Disposable interface and using() global function that can be used to enforce the disposal of objects.

Installation

You can install the package via composer:

composer require ryangjchandler/using

Usage

You should first implement the RyanChandler\Using\Disposable interface on your class. This contract requires an implementation of a public function dispose(): void method.

class TextFile implements Disposable
{
    private $resource;

    public function dispose(): void
    {
        $this->resource = fclose($this->resource);
    }
}

You can then use the using() helper function with your Disposable object to enforce disposal.

// This code might create a file pointer and store it on the class.
$file = new TextFile('hello.txt');

// We can then "use" the `$file` object inside of this callback. After the callback has been
// invoked, the `TextFile::dispose()` method will be called.
using($file, function (TextFile $file) {
    DB::create('messages', [
        'message' => $file->contents(),
    ]);
});

// The `$resource` property is no-longer a valid stream, since we closed
// the handle in the `dispose` method.
var_dump($file->resource);

Handling Exceptions

The using() function will wrap the invokation of your callback in a try..finally statement.

This ensures that your object is disposed of regardless of any exceptions.

Any exceptions thrown inside of your callback will still propagate up to the top-level.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

More from the ecosystem

durable-workflow/
workflow
g4t.io
9 1,292

Durable Workflow: Long-Running Workflows for Laravel

Embedded durable workflow runtime and orchestration engine for Laravel applications.

durable-workflow/workflow via Laravel News
packstub/
session-replay
g4t.io
14 52

Session Replay for Laravel: Self-Hosted Browser Recordings

Session replay for Laravel: record what a person did in the browser with rrweb, keep the recording on your own disk and database, and watch it behind a gate you define β€” masked inputs by default, errors, failed Livewire requests and web vitals marked on the timeline, a link to the replay in every log line. packstub/filament-session-replay adds the sessions to a Filament panel.

packstub/session-replay via Laravel News
cyberwizard-dev/
schedule-catchup
g4t.io
17

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Schedule Catchup replays missed Laravel scheduled tasks after downtime, with opt-in recovery, bounded retries, and access to the original scheduled date.

cyberwizard-dev/schedule-catchup via Laravel News