stilliard / generic-loading-bar
Generic loading bar PHP component
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/stilliard/generic-loading-bar
Requires (Dev)
- phpunit/phpunit: ^9.6
README
A generic PHP loader/progress bar utility package to help with applications that need to display a loading bar during a long running process.
Install with composer:
composer require stilliard/generic-loading-bar
Basic usage
use GLB\LoadingBar; $loading = new LoadingBar; $loading->step(); // each call is 1/100 (or whatever steps is set to) $loading->set(40); // set a specific loaded % $loading->complete(); // sets to 100 to mark as complete $loading->reset(); // sets to 0 to start over
or with options:
$loading = new LoadingBar([ 'codename' => 'my_loading_bar', 'min' => 0, 'max' => 100, 'steps' => 150, // default: 100 'dataHandler' => DBDataHandler::class, // default ProcessDataHandler 'displayHandler' => HTMLDisplayHandler::class, // default EchoDisplayHandler ]);
$loading->step(); // each call is then 1/150 (or whatever steps is set to)
Display the loading bar
echo $loading->display(); // displays the html loading bar with auto refresh
or
echo $loading; // same as above
Ranged loading calculations
Ranged calculations are the original reason for this package. Want to split your loading into 4 parts, each with 25% but then have maybe thousands of records to process inside? This handles this for you.
$totalProducts = count($products); foreach ($products as $i => $product) { // fill in the range between 25% to 50% as the % of the total products handled so far. [index, total] $loading->calc([25, 50], [$i + 1, $totalProducts]); }
Demo
Check out the demo folder for a basic CLI demo in cli.php, or a full web and background process demo using the Redis data handler in the html folder, there's a README.md file in there with more information.
Data Handlers
- Process
ProcessDataHandlerruns just in process and doesn't save anywhere - Redis
RedisDataHandleruses redis to store the current loading % - PDO
PDODataHandleruses a database with a PDO instance, lets you pass a DB PDO instance/object in via the construct options to then query an assumedloading_barstable withnameandvaluecolumns. - DB
DBDataHandlerlike above PDO one but instead of passing apdoinstance, it assumes you have aDBglobal class
Build your own by extending the abstract BaseDataHandler class, see how the above ones work as an example in the src/DataHandler folders.
Display Handlers
- Echo
EchoDisplayHandlersimply echos/outputs the current % at the point of->display()called - Console
ConsoleDisplayHandlera CLI/Console ascii loading/progress bar e.g.[===> ] 50% - HTML
HTMLDisplayHandlershows a<progress>element at point of display, could then be auto refreshed from there through ajax
Build your own by extending the abstract BaseDisplayHandler class, see how the above ones work as an example in the src/DisplayHandler folders.
License
This project is open-sourced software licensed under the MIT license.