Download the PHP package slashequip/attempt without Composer
On this page you can find all versions of the php package slashequip/attempt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download slashequip/attempt
More information about slashequip/attempt
Files in slashequip/attempt
Package attempt
Short Description Attempt is a simple, fluent class for attempting to run code multiple times whilst handling exceptions.
License MIT
Informations about the package attempt
Attempt
Attempt is a simple, fluent class for attempting to run code multiple times whilst handling exceptions. It attempts to mimic PHPs built-in try/catch syntax where possible but sprinkles in some additional magic on top.
Installation
Attempt has been added to packagist and can be installed via composer:
Getting an instance
Depending on your preference you can grab an Attempt instance in a couple of ways:
Building your Attempt
Once you have your instance you can begin to build your Attempt.
Try
This is the only required method, the try
method accepts a callable argument, the code that you want to run.
Then Return
You may have noticed in the example above the method thenReturn
, this method is what tells the Attempt to run.
It will also return the value you return from the callable you pass to the try
method.
There is also a then
method, this too accepts a callable which is executed and passed the value returned
by your try
callable.
If it's your kind of jam, an attempt is also invokable which means at any point you can invoke the Attempt and it will run.
Times
You can set the amount of times the Attempt should be made whilst an exception is being encountered see catch.
Catch
The catch
method allows you to define exceptions you are expecting to encounter during the attempts, when
exceptions have been passed to the catch method the Attempt will throw any other types of exceptions it
comes across early rather than performing all attempts.
The catch
method can be called multiple times to add multiple expected exceptions.
If you do not provide any expected exception via the catch
method then the Attempt will ignore all exceptions
until all attempts have been made.
The catch
method also allows you to define a callback that will be called when the specified exception
is eventually thrown. This can be useful for error logging or you could also return a default value
if your code is to continue.
No Throw
Attempt can be configured to never throw exceptions, there are situations when you want to execute some code but still
continue with the rest of your logic. For these situations you can use noThrow
.
Finally
The finally
method allows you to run a callback at the end of the attempt no matter the result, whether the attempt
was successful or an exception was thrown the finally
callback will always be run.
Wait Between
The waitBetween
method takes an integer indicating the desired number of milliseconds to wait between attempts. The
pause happens before the code runs but does not delay the starting of the Attempt.