Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: Using
---

If you're familiar with modern Laravel, you'll know that there are dozens of things in Laravel that are useful in a variety of contexts, all of which are part of the _"Laravel Way"_. That's why _Fraction_ provides you with several useful helpers for creating your actions.

## Deferred Actions

As part of Laravel 11, you can trigger deferred actions simply by using the `deferred` method following the action declaration:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
// ...
})->deferred(name: 'send welcome email', always: true);
```

Behind the scenes, this will register the action as a deferred action, using the `Illuminate\Support\defer` function.

> Different the _sync actions_, `deferred` actions only returns `true` when executed successfully.

## Queued Actions

You can trigger queued actions simply by using the `queued` method following the action declaration:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
// ...
})->queued(delay: 10, queue: 'actions', connection: 'redis');
```

Behind the scenes, this will register the action to dispatch the `Fraction\Jobs\FractionJob` job, which will execute the action in the background.

> Different the _sync actions_, `queued` actions only returns `true` when executed successfully.

## Rescued Actions

You can trigger rescued actions simply by using the `rescued` method following the action declaration:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
// ...
})->rescued();
```

Behind the scenes, this will register the action to execute the function inside the `rescue` Laravel's function, which aims to do not stop the execution of the application in case of an error.

You can also pass a default value to the `rescued` method, which will be returned in case of an error:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
throw new Exception('ops!');
})->rescued(default: false);
```

```php
$result = run('send welcome email'); // false
```
10 changes: 6 additions & 4 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Action Hooks

# Concept

Similar to the Laravel's Event and Listener system, the _Fraction_ hook system allows you to execute actions in sequence, also similarly to the concept of a _pipeline_. For this, you shoul call the `then` method after registering an action:
Similar to Laravel's Event and Listener system, the _Fraction_ hook system allows you to execute actions in sequence, much like the concept of a _pipeline_. For this, you should call the `then` method after registering an action:

```php
<?php
Expand All @@ -21,7 +21,7 @@ execute('create user', function (Request $request) {
```php
<?php

// app/Actions//Emails.php
// app/Actions/Emails.php

execute('send welcome user email', function (Request $request) {
// ...
Expand All @@ -40,10 +40,12 @@ execute('enable free trial', function (Request $request) {

> An action cannot call itself in a hook.

Although the above example uses _sync actions_, you can also use `deferred` or `queued` actions with the hook system.

## Shared Parameters

As you can see in the example above, the `Illuminate\Http\Request` instance is repeated between both the `create user` and `send welcome email` actions. This happens because all the parameters sent to one action are passed to the others using the hook system.
As you can see in the example above, the `Illuminate\Http\Request` instance is repeated between both the `create user`, `send welcome email`, and `enable free trial` actions. This happens because all the parameters sent to one action are passed to the others using the hook system.

## Undetected Loop

By default, the only loop detected by _Fraction_ is the attempt to make `then` call the function that triggered it. There is nothing stopping you from creating a `ping` `pong` effect, this is completely up to you, considering that you can create an infinite loop with this.
By default, the only loop detected by _Fraction_ is the attempt to make `then` call the function that triggered it. Nothing is stopping you from creating a `ping` `pong` effect, this is completely up to you, considering that you can make an infinite loop with this.
5 changes: 4 additions & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ There is nothing special in its installation, just run the following command in
```bash
composer require fraction/fraction:^1.0
```
After that, [you can start using the package in your Laravel application](/using/).

> _Fraction_ is not yet ready to be installed via _composer_.

After that, [you can start using the package in your Laravel application](using.md).
7 changes: 7 additions & 0 deletions docs/overrides/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}

{% block announce %}
<div style="text-align: center; color: white; font-weight: bold">
<i>Fraction</i> is <u>not yet ready for production</u>!
</div>
{% endblock %}
5 changes: 5 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Testing
---

WIP.
70 changes: 1 addition & 69 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ execute('send welcome email', function (Request $request) {
});
```

Obviously, the _Fraction_ can also resolve the new container's attribute:
_Fraction_ can also resolve the new container's attribute:

```php
<?php
Expand All @@ -146,71 +146,3 @@ execute('send welcome email', function (#[CurrentUser] User $user) {
// ...
});
```

## Deferred Actions

As part of Laravel 11, you can trigger deferred actions simply by using the `deferred` method following the action declaration:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
// ...
})->deferred();
```

Behind the scenes, this will register the action as a deferred action, using the `Illuminate\Support\defer` function.

> You can pass parameters to the deferred method to personalize the deferred execution. Additionally, different than the normal actions, `deferred` actions only returns `true` when executed successfully.

## Queued Actions

You can trigger queued actions simply by using the `queued` method following the action declaration:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
// ...
})->queued();
```

Behind the scenes, this will register the action to dispatch the `Fraction\Jobs\FractionJob` job, which will execute the action in the background.

> You can pass parameters to the queued method to personalize the queued execution. Additionally, different than the normal actions, `queued` actions only returns `true` when executed successfully.

## Rescued Actions

You can trigger rescued actions simply by using the `rescued` method following the action declaration:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
// ...
})->rescued();
```

Behind the scenes, this will register the action to execute the function inside the `rescue` Laravel's function, which aims to do not stop the execution of the application in case of an error.

You can also pass a default value to the `rescued` method, which will be returned in case of an error:

```php
<?php

// app/Actions/Emails.php

execute('send welcome email', function () {
throw new Exception('ops!');
})->rescued(default: false);
```

```php
$result = run('send welcome email'); // false
```
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ theme:
- toc.integrate
icon:
repo: fontawesome/brands/git-alt
custom_dir: docs/overrides
extra:
generator: false
social:
Expand All @@ -39,5 +40,7 @@ nav:
- About: index.md
- Installation: installation.md
- Using: using.md
- Helpers: helpers.md
- Hooks: hooks.md
- Deployment: deployment.md
- Testing: testing.md