-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
1,350 additions
and
660 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
id: http-client | ||
title: "🙅🏽♂️ Http Client" | ||
--- | ||
|
||
import SuggestionFeature from "@site/src/components/Partials/SuggestionFeature"; | ||
|
||
CQRS (ségrégation des responsabilités des requêtes de commande). C'est un modèle que j'ai entendu pour la première fois décrit par Greg Young. | ||
Au cœur se trouve l'idée selon laquelle vous pouvez utiliser un modèle différent pour mettre à jour les informations que celui que vous utilisez pour lire les informations. | ||
Dans certaines situations, cette séparation peut être utile, mais sachez que pour la plupart des systèmes, CQRS ajoute une complexité risquée. | ||
|
||
[Pour plus d'information](https://www.martinfowler.com/bliki/CQRS.html) | ||
|
||
## Install | ||
|
||
Le package utilise php >= 8.1 | ||
|
||
```bash | ||
composer require bowphp/cqrs | ||
``` | ||
|
||
## Help | ||
|
||
Tout d’abord, créez l’exemple de commande: | ||
|
||
```php | ||
use Bow\CQRS\Command\CommandInterface; | ||
|
||
class CreateUserCommand implements CommandInterface | ||
{ | ||
public function __construct( | ||
public string $username, | ||
public string $email | ||
) {} | ||
} | ||
``` | ||
|
||
Créez le gestionnaire ici : | ||
|
||
```php | ||
use Bow\CQRS\Command\CommandHandlerInterface; | ||
|
||
class CreateUserCommandHandler implements CommandHandlerInterface | ||
{ | ||
public function __construct(public UserService $userService) {} | ||
|
||
public function process(CommandInterface $command): mixed | ||
{ | ||
if ($this->userService->exists($command->email)) { | ||
throw new UserServiceException( | ||
"The user already exists" | ||
); | ||
} | ||
|
||
return $this->userService->create([ | ||
"username" => $command->username, | ||
"email" => $command->email | ||
]); | ||
} | ||
} | ||
``` | ||
|
||
Ajouter une commande au registre dans `App\Configurations\ApplicationConfiguration::class`: | ||
|
||
```php | ||
use Bow\CQRS\Registration as CQRSRegistration; | ||
|
||
public function run() | ||
{ | ||
CQRSRegistration::commands([ | ||
CreateUserCommand::class => CreateUserCommandHandler::class | ||
]); | ||
} | ||
``` | ||
|
||
Exécuter la commande dans le contrôleur: | ||
|
||
```php | ||
namespace App\Controllers; | ||
|
||
use App\Controllers\Controller; | ||
use App\Commands\CreateUserCommand; | ||
|
||
class UserController extends Controller | ||
{ | ||
public function __construct(private CommandBus $commandBus) {} | ||
|
||
public function __invoke(Request $request) | ||
{ | ||
$payload = $request->only(['username', 'email']); | ||
|
||
$command = new CreateUserCommand( | ||
$payload['username'], | ||
$payload['email'] | ||
); | ||
|
||
$result = $this->commandBus->execute($command); | ||
|
||
return redirect() | ||
->back() | ||
->withFlash("message", "User created"); | ||
} | ||
} | ||
``` | ||
|
||
Mettre un nouvel itinéraire : | ||
|
||
```php | ||
$app->post("/users/create", UserController::class); | ||
``` | ||
|
||
<SuggestionFeature /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
id: messaging | ||
title: "🎯 Messaging" | ||
--- | ||
|
||
import SuggestionFeature from "@site/src/components/Partials/SuggestionFeature"; | ||
|
||
|
||
<SuggestionFeature /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,65 @@ | ||
--- | ||
id: service | ||
title: "🏢 Approche service" | ||
title: "🏢 Approche Service" | ||
--- | ||
|
||
import SuggestionFeature from "@site/src/components/Partials/SuggestionFeature"; | ||
|
||
# Service | ||
|
||
Les services sont des classes simple qui vous voulez injectées dans vos Controllers, dans d'autre services que peuvent nous permettre de découpler les logiquees et donc optimisé votre code source. | ||
Vous pouvez aussi injectées des modèles dans vos services. | ||
|
||
## Utilisation | ||
|
||
Pour ajouter un service rien de plus simple que d'untilisé la commande suivante : | ||
|
||
```bash | ||
php bow add:service UserService | ||
``` | ||
|
||
Un service `UserService` sera créé dans le dossier par default dans le dossier `app/Services`. Et nous allons ajouter la méthode suivante `fetchAllUsers` pour récupérer tout les utilisateurs enregistrés dans la base de donnée. | ||
|
||
```php title="app/Services/UserService" | ||
namesapce App\Services; | ||
|
||
use App\Models\User; | ||
|
||
class UserService | ||
{ | ||
public function fetchAllUsers() | ||
{ | ||
return User::all(); | ||
} | ||
} | ||
``` | ||
|
||
## Utilisation du service | ||
|
||
Pour utiliser le service nous allons l'inject là ou nous voulons l'utiliser, dans notre cas dans un controller. | ||
|
||
Lancer la commande suivante pour créer un nouveau controller | ||
|
||
```bash | ||
php bow add:controller UserController | ||
``` | ||
|
||
Dans le controleur nous allons maintenant injecter le service: | ||
|
||
```php | ||
class UserController extends Controller | ||
{ | ||
public function __construct( | ||
private UserService $userService | ||
) {} | ||
|
||
public function showIndex() | ||
{ | ||
$users = $this->userService->fetchAllUsers(); | ||
|
||
// do something | ||
} | ||
} | ||
``` | ||
|
||
<SuggestionFeature /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.