Skip to content

Commit

Permalink
fixed README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tito10047 committed Aug 27, 2024
1 parent 84379f6 commit 85084c1
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 174 deletions.
38 changes: 19 additions & 19 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ permissions:
contents: read

jobs:
run:
run:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']
php-versions: [ '8.1', '8.2', '8.3' ]
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: phpunit-bridge
extensions: mbstring, xml, ctype, iconv, intl
coverage: xdebug

- name: Get composer cache directory
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: phpunit-bridge
extensions: mbstring, xml, ctype, iconv, intl
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache composer dependencies

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
Expand All @@ -35,11 +35,11 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

- name: Run test suite
run: composer run-script test
- name: Run test suite
run: composer run-script test
81 changes: 42 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ PHP defer function schedules a function call (the deferred function) to be run i
executing the defer returns. It's an unusual but effective way to deal with situations such as resources that must be
released regardless of which path a function takes to return. The canonical examples are unlocking a mutex or closing a
file.

```php
// Contents returns the file's contents as a string.
function contents($filename) {
$f = fopen($filename, "r");
if ($f === false) {
throw new Exception("Error opening the file");
}
defer(fclose(...),$f); // fclose will run when we're finished.
$defer = defer(fclose(...),$f); // fclose will run when we're finished.

$result = "";

Expand All @@ -33,7 +34,9 @@ function contents($filename) {
}
```

Deferring a call to a function such as Close has two advantages. First, it guarantees that you will never forget to close the file, a mistake that's easy to make if you later edit the function to add a new return path. Second, it means that the close sits near the open, which is much clearer than placing it at the end of the function.
Deferring a call to a function such as Close has two advantages. First, it guarantees that you will never forget to
close the file, a mistake that's easy to make if you later edit the function to add a new return path. Second, it means
that the close sits near the open, which is much clearer than placing it at the end of the function.

---

Expand All @@ -47,14 +50,14 @@ composer require tito10074/defer

```php
function foo($a){
echo "in defer {$a}".PHP_EOL;
echo "in defer {$a}".PHP_EOL;
}
function a() {
echo "before defer".PHP_EOL;
$defer = defer(foo(...),1);
$defer(foo(...),2);
$defer(foo(...),3);
echo "after defer".PHP_EOL;
echo "before defer".PHP_EOL;
$defer = defer(foo(...),1);
$defer(foo(...),2);
$defer(foo(...),3);
echo "after defer".PHP_EOL;
};

echo "start".PHP_EOL;
Expand Down Expand Up @@ -86,9 +89,9 @@ The deferred call will print `0` after the function returns.

```php
function a(){
$i=0;
defer(printf(...),$i);
$i++;
$i=0;
$_ = defer(printf(...),$i);
$i++;
}
```

Expand All @@ -102,9 +105,9 @@ This function prints `3210`:
```php
function b(){
$defer = defer();
for($i=0;$i<4;$i++){
$defer(printf(...),$i);
}
for($i=0;$i<4;$i++){
$defer(printf(...),$i);
}
}
```

Expand All @@ -116,16 +119,16 @@ function returns but not modify returned `$i`. This example print `2-3`:

```php
function c() {
$i=1;
$o=new \stdClass();
$o->i=2;
defer(function () use (&$i, $o) {
$o->i++;
$i++;
});

$i++;
return [$i,$o];
$i=1;
$o=new \stdClass();
$o->i=2;
$defer = defer(function () use (&$i, $o) {
$o->i++;
$i++;
});

$i++;
return [$i,$o];
}
list($i,$o) = c();
echo "{$i}-{$o->i}".PHP_EOL;
Expand All @@ -149,23 +152,23 @@ require_once __DIR__.'/../vendor/autoload.php';

function myFunc(){}
class Foo{
public function myMethod(){}
public function myMethod(){}
}
function a(){
// defer custom function without parameter
// function name must be with his namespace
$defer = defer('test\myFunc');
// defer function with one parameter
$defer(printf(...),"test");
// defer function with more parameters
$defer('printf',"%s-%s",10,12);
// defer with anonymous function
$defer(function (){});
$func = function (){};
$defer($func);
//defer method
$foo = new Foo();
$defer([$foo,'myMethod']);
// defer custom function without parameter
// function name must be with his namespace
$defer = defer('test\myFunc');
// defer function with one parameter
$defer(printf(...),"test");
// defer function with more parameters
$defer('printf',"%s-%s",10,12);
// defer with anonymous function
$defer(function (){});
$func = function (){};
$defer($func);
//defer method
$foo = new Foo();
$defer([$foo,'myMethod']);
}
a();
```
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"license": "Apache-2.0",
"description": "A defer function defers the execution of a function until the surrounding function returns.",
"keywords": [
"php","defer"
"php",
"defer"
],
"require": {
"php": ">=8.1"
Expand All @@ -19,7 +20,9 @@
"psr-4": {
"Defer\\": "src/"
},
"files": ["shortcuts.php"]
"files": [
"shortcuts.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
85 changes: 44 additions & 41 deletions examples/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,60 @@
* Time: 10:15
*/

require_once __DIR__.'/../Defer.php';
require_once __DIR__.'/../shortcuts.php';
require_once __DIR__ . '/../Defer.php';
require_once __DIR__ . '/../shortcuts.php';

/**
* @param $dstName
* @param $srcName
*
* @return bool
*/
function copyFileBad($srcName, $dstName){
$src = fopen($srcName, 'r');
if ($src===false){
return false;
}
$dst = fopen($dstName, 'w');
if ($dst===false){
return false;
}
$size=filesize($srcName);
while($size>0){
$s = $size>1000?1000:$size;
fwrite($dst,fread($src,$s));
$size-=1000;
}
function copyFileBad($srcName, $dstName)
{
$src = fopen($srcName, 'r');
if ($src === false) {
return false;
}
$dst = fopen($dstName, 'w');
if ($dst === false) {
return false;
}
$size = filesize($srcName);
while ($size > 0) {
$s = $size > 1000 ? 1000 : $size;
fwrite($dst, fread($src, $s));
$size -= 1000;
}

fclose($src);
fclose($dst);
return true;
fclose($src);
fclose($dst);
return true;
}
function copyFile($srcName, $dstName){
$src = fopen($srcName, 'r');
if ($src===false){
return false;
}
$defer = defer(fclose(...),$src);

$dst = fopen($dstName, 'w');
if ($dst===false){
return false;
}
$defer(fclose(...),$dst);
function copyFile($srcName, $dstName)
{
$src = fopen($srcName, 'r');
if ($src === false) {
return false;
}
$defer = defer(fclose(...), $src);

$size=filesize($srcName);
while($size>0){
$s = $size>1000?1000:$size;
$b=fwrite($dst,fread($src,$s));
if ($s!=$b){
return false;
}
$size-=1000;
}
$dst = fopen($dstName, 'w');
if ($dst === false) {
return false;
}
$defer(fclose(...), $dst);

return true;
$size = filesize($srcName);
while ($size > 0) {
$s = $size > 1000 ? 1000 : $size;
$b = fwrite($dst, fread($src, $s));
if ($s != $b) {
return false;
}
$size -= 1000;
}

return true;
}
31 changes: 18 additions & 13 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
* Time: 10:35
*/

require_once __DIR__.'/../Defer.php';
require_once __DIR__.'/../shortcuts.php';
require_once __DIR__ . '/../Defer.php';
require_once __DIR__ . '/../shortcuts.php';

echo "start".PHP_EOL;
function foo($a){
echo "in defer 3-{$a}".PHP_EOL;
echo "start" . PHP_EOL;
function foo($a)
{
echo "in defer 3-{$a}" . PHP_EOL;
}
function a() {
echo "before defer".PHP_EOL;
$defer = defer( "foo",1);
$defer( "foo",2);
$defer("foo",3);
echo "after defer".PHP_EOL;
};

function a()
{
echo "before defer" . PHP_EOL;
$defer = defer("foo", 1);
$defer("foo", 2);
$defer("foo", 3);
echo "after defer" . PHP_EOL;
}

;
a();
echo "end".PHP_EOL;
echo "end" . PHP_EOL;
Loading

0 comments on commit 85084c1

Please sign in to comment.