Download the PHP package sqonk/phext-context without Composer
On this page you can find all versions of the php package sqonk/phext-context. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sqonk/phext-context
More information about sqonk/phext-context
Files in sqonk/phext-context
Package phext-context
Short Description Contexts create a block level scope on a resource and automatically manage the creation and cleanup of that resource irrespective of any exceptions that arise while in use.
License MIT
Informations about the package phext-context
PHEXT Context
Contexts create a block level scope on a resource and automatically manage the creation and cleanup of that resource irrespective of any exceptions that arise while in use, the primary intention being to help remove the possibility of resource leaks. On top of that they aid in keeping your code lean by automating the standard logic involved with operating such resources.
They are a pseudo implementation of the context managers in Python. While PHP 7 and earlier do not currently allow for a 1:1 elegant solution, the below interpretation makes use of function callbacks to achieve something similar.
While alternative implementations make clever use of generators and 'yield' to push the resources to a 1-cycle foreach loop, the readability is somewhat lost when you follow the code.
This implementation attempts to keep the code readable and be self-explanatory as to what is happening when being studied by another person, even if it is a little more verbose.
In many of the methods a special dedicated error handler is temporarily injected to convert PHP errors into raised Exceptions. Keep in mind that while inside a context manager any standard error handling systems your code is using will be overriden until the manager exits back into to the parent scope.
Install
Via Composer
Context Features
Out of the box the library supports the following managers for:
- Files
- Streams
- GD Images
- MySQL Transactions
- PDO Transactions
- Error/Exception supression
- Output buffer supression
- Output buffer capture
- Zip Files
- CURL
Available Methods
file
Open a file in the desired mode and then pass it to the callback. The callback should accept one parameter, which is the file handle (resource).
Exceptions and errors can be thrown but the file will be safely closed off.
Example:
tmpfile
Open a temporary file and pass it to the callback. The callback should accept one parameter, which is the file handle (resource).
Exceptions and errors can be thrown but the file will be safely closed off.
Example:
stream
Open a file in 'read' mode and download the contents in chunks, passing each chunk to the callback as it is received.
The default read chunk size is 1024 * 1024, which can be adjusted by passing in your own chunk multiplier. Just be aware that what ever value you pass in will be squared to form the final chunk size.
This method uses a file context as its parent context manager and thus does not introduce any further exception handling.
Example:
image
Open a image resource (using GD) and pass it to the callback. The callback should accept just one parameter: the image resouce.
Example:
new_image
Create a new image resource (using GD) with the specified width and height and pass it to the callback.
The callback should accept just one parameter: the image resouce.
Example:
supress_errors
Perform a block of code in the callback and ignore all possible errors and exceptions that occur.
Example:
no_output
Perform a block of code while preventing any output to std out (console in CLI SAPI or the browser for the web.)
captured_output
Perform a block of code from within a nested output buffer and return the result from ob_get_contents()
.
Example:
pdo_transaction
Execute and attempt to commit a PDO database transaction. If an error is thrown at any point the transaction will be rolled back.
Example:
mysql_transaction
Execute and attempt to commit a MySQL database transaction. If an error is thrown at any point the transaction will be rolled back.
curl
Initialise a cURL handle. This curl handle is set to the given URL but no further options are set.
zip
Open a zip file at the specified location and in the desired mode, then pass it to the callback. The callback should accept one parameter, which is the zip handle (resource).
The default behaviour is to open or create a zip archive for outputting data to. The mode can be changed by passing in the relevant ZipArchive constant.
Exceptions and errors will be thrown but the file will be safely closed off.
Credits
Theo Howell
License
The MIT License (MIT). Please see License File for more information.