Beamtic File Handler
Reference on the Doorkeeper File Handler class.

By. Jacob
Edited: 2019-11-21 23:42
The Doorkeeper File Handler should generally be used whenever you want to write or read files from the file system.
It has simple handling of concurrency via file locking. If two "users" happen to write to the same file at the same time, one will wait for a random amount of time, while periodically trying obtaining the lock.
The "random" amount of time is a way to deal with race conditions on high traffic sites, but it is not a perfect solution.
Note. file locking requires write access.
All possible errors should be properly handled. If an error occurs, an array containing information about the error is returned to the calling script/function, and the developer can either choose to ignore the error, or show a useful error message to the user.
This class only relies on the php_helpers class to manage default arguments.
Improvements
Note. I still maintain all code i write. I just might not get around improving/adding features unless I need them myself.
This file handler is not perfect. If you can improve it, feel free to help out. It could use improvements in how it handles concurrency to better deal with race conditions. A queue system would probably work best.
Currently, there's a small chance a user could end up waiting for "a long time" to access a file, or until the script times out. This is a known bug that might affect high traffic sites. When I wrote this, I did not intend it to be used on high-traffic sites, but rather to deal with rare race conditions.
Methods
Note that methods may accept an array as parameter. The stated values for array parameters are default values. I.e. 4096 is the default value for the optional max_line_length.
method | Parameters | Returns |
---|---|---|
simple_delete() | (string) file path | true on success, and an array with error information on failure. |
read_file_lines() | (array) path = string (required) start_line = 0 max_line_length = 4096 lines_to_read = false | Returns the file content on success, and an array with error information on failure. |
count_lines() | (array) path = string (required) start_line = 0 max_line_length = 4096 lines_to_read = false | Returns the line count of the file on success, and an array with error information on failure. |
write_file() |
(array) path = string (required) content = string mode = w | true on success, and an array with error information on failure. |
create_directory() |
(array) path = string (required) permissions = 0775 | true on success, and an array with error information on failure. |
obtain_lock() | File Pointer | true on success, and an array with error information on failure. |
Examples
Dependencies
- php_helpers
- superglobals
Links
- Doorkeeper/lib/file_handler/ - GitHub
Comments