├── .php-cs-fixer.dist.php ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── src ├── Disposable.php └── functions.php └── undo-configure.php /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `using` will be documented in this file. 4 | 5 | ## 1.0.0 - 202X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ryangjchandler 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 | # Enforced disposal of objects in PHP. 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/using.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/using) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/using/run-tests?label=tests)](https://github.com/ryangjchandler/using/actions?query=workflow%3ATests+branch%3Amaster) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/using/Check%20&%20fix%20styling?label=code%20style)](https://github.com/ryangjchandler/using/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amaster) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/using.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/using) 7 | 8 | This package provides a `Disposable` interface and `using()` global function that can be used to enforce the disposal of objects. 9 | 10 | ## Installation 11 | 12 | You can install the package via composer: 13 | 14 | ```bash 15 | composer require ryangjchandler/using 16 | ``` 17 | 18 | ## Usage 19 | 20 | You should first implement the `RyanChandler\Using\Disposable` interface on your class. This contract requires an implementation of a `public function dispose(): void` method. 21 | 22 | ```php 23 | class TextFile implements Disposable 24 | { 25 | private $resource; 26 | 27 | public function dispose(): void 28 | { 29 | $this->resource = fclose($this->resource); 30 | } 31 | } 32 | ``` 33 | 34 | You can then use the `using()` helper function with your `Disposable` object to enforce disposal. 35 | 36 | ```php 37 | // This code might create a file pointer and store it on the class. 38 | $file = new TextFile('hello.txt'); 39 | 40 | // We can then "use" the `$file` object inside of this callback. After the callback has been 41 | // invoked, the `TextFile::dispose()` method will be called. 42 | using($file, function (TextFile $file) { 43 | DB::create('messages', [ 44 | 'message' => $file->contents(), 45 | ]); 46 | }); 47 | 48 | // The `$resource` property is no-longer a valid stream, since we closed 49 | // the handle in the `dispose` method. 50 | var_dump($file->resource); 51 | ``` 52 | 53 | ### Handling Exceptions 54 | 55 | The `using()` function will wrap the invokation of your callback in a `try..finally` statement. 56 | 57 | This ensures that your object is disposed of regardless of any exceptions. 58 | 59 | Any exceptions thrown inside of your callback will still propagate up to the top-level. 60 | 61 | ## Testing 62 | 63 | ```bash 64 | composer test 65 | ``` 66 | 67 | ## Changelog 68 | 69 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 70 | 71 | ## Contributing 72 | 73 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 74 | 75 | ## Security Vulnerabilities 76 | 77 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 78 | 79 | ## Credits 80 | 81 | - [Ryan Chandler](https://github.com/ryangjchandler) 82 | - [All Contributors](../../contributors) 83 | 84 | ## License 85 | 86 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 87 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ryangjchandler/using", 3 | "description": "Enforced disposal of objects in PHP.", 4 | "keywords": [ 5 | "ryangjchandler", 6 | "using" 7 | ], 8 | "homepage": "https://github.com/ryangjchandler/using", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Ryan Chandler", 13 | "email": "support@ryangjchandler.co.uk", 14 | "role": "Developer" 15 | } 16 | ], 17 | "require": { 18 | "php": "^8.0" 19 | }, 20 | "require-dev": { 21 | "friendsofphp/php-cs-fixer": "^3.0", 22 | "pestphp/pest": "^1.20", 23 | "phpstan/phpstan": "^0.12.99", 24 | "spatie/ray": "^1.28" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "RyanChandler\\Using\\": "src" 29 | }, 30 | "files": [ 31 | "src/functions.php" 32 | ] 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "RyanChandler\\Using\\Tests\\": "tests" 37 | } 38 | }, 39 | "scripts": { 40 | "lint": "vendor/bin/phpstan analyse --level 5 src tests", 41 | "test": "vendor/bin/pest", 42 | "test-coverage": "vendor/bin/pest --coverage", 43 | "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" 44 | }, 45 | "config": { 46 | "sort-packages": true 47 | }, 48 | "minimum-stability": "dev", 49 | "prefer-stable": true 50 | } 51 | -------------------------------------------------------------------------------- /src/Disposable.php: -------------------------------------------------------------------------------- 1 | dispose(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /undo-configure.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 |