Getting started
In this tutorial we build a small Laravel app with two modules, Billing and Catalog. We write a rule that keeps them apart, watch it fail, fix the code, and watch it pass. It takes about fifteen minutes.
You need PHP 8.3 or later and Composer.
Create the app
Section titled “Create the app”-
Create a new Laravel app and go into its folder:
Terminal window composer create-project laravel/laravel shopcd shop -
Larch is a Pest plugin. Replace PHPUnit with Pest:
Terminal window composer remove phpunit/phpunit --devcomposer require pestphp/pest --dev --with-all-dependencies./vendor/bin/pest --initPest creates
tests/Pest.php. -
Install Larch:
Terminal window composer require nwidart/larch --dev
Add two modules
Section titled “Add two modules”A module is a namespace. Ours live under App\Modules, in app/Modules.
-
Catalog prices orders. Create
app/Modules/Catalog/Pricing/PriceEngine.php:<?phpnamespace App\Modules\Catalog\Pricing;class PriceEngine{public function quote(array $lines): int{return array_sum(array_column($lines, 'price'));}} -
Billing charges orders, and asks Catalog for the price. Create
app/Modules/Billing/Actions/ChargeOrder.php:<?phpnamespace App\Modules\Billing\Actions;use App\Modules\Catalog\Pricing\PriceEngine;class ChargeOrder{public function handle(array $lines): int{return (new PriceEngine)->quote($lines);}}
Tell Larch about the modules
Section titled “Tell Larch about the modules”Add these lines at the end of tests/Pest.php:
Nwidart\Larch\larch() ->modules('App\Modules\*') ->publicApi('Contracts');Each namespace directly under App\Modules is now a module: Billing and Catalog.
Other modules may use only the classes in a module’s Contracts namespace. That is its public API.
Write the first rule
Section titled “Write the first rule”Create tests/Arch/ModulesTest.php:
<?php
use function Nwidart\Larch\modules;
arch('modules use each other only through their public API') ->expect(modules()) ->toOnlyUsePublicApis();Run it:
./vendor/bin/pest tests/ArchThe test fails, and tells us where and why:
Modules use other modules only through their public API. 1 link breaks it:
app/Modules/Billing/Actions/ChargeOrder.php:11 App\Modules\Billing\Actions\ChargeOrder uses App\Modules\Catalog\Pricing\PriceEngine It is internal to Catalog. The public API of a module is: Contracts.Billing reaches into Catalog’s Pricing namespace. PriceEngine is internal to Catalog.
Fix the code
Section titled “Fix the code”Catalog will offer an interface in its public API, and Billing will use that.
-
Create
app/Modules/Catalog/Contracts/PricesOrders.php:<?phpnamespace App\Modules\Catalog\Contracts;interface PricesOrders{public function quote(array $lines): int;} -
Make
PriceEngineimplement it:app/Modules/Catalog/Pricing/PriceEngine.php <?phpnamespace App\Modules\Catalog\Pricing;use App\Modules\Catalog\Contracts\PricesOrders;class PriceEngine implements PricesOrders{public function quote(array $lines): int{return array_sum(array_column($lines, 'price'));}} -
Make
ChargeOrderask for the interface:app/Modules/Billing/Actions/ChargeOrder.php <?phpnamespace App\Modules\Billing\Actions;use App\Modules\Catalog\Contracts\PricesOrders;class ChargeOrder{public function __construct(private PricesOrders $prices) {}public function handle(array $lines): int{return $this->prices->quote($lines);}} -
Run the rule again:
Terminal window ./vendor/bin/pest tests/ArchIt passes.
Say what Billing may use
Section titled “Say what Billing may use”The first rule checks how modules use each other. Now we say which modules Billing may use at all.
-
Add a second rule to
tests/Arch/ModulesTest.php:tests/Arch/ModulesTest.php <?phpuse function Nwidart\Larch\module;use function Nwidart\Larch\modules;arch('modules use each other only through their public API')->expect(modules())->toOnlyUsePublicApis();arch('billing uses only the public API of catalog')->expect(module('Billing'))->toOnlyDependOn(module('Catalog')->publicApi());Run the tests. Both pass.
-
Add a third module. Create
app/Modules/Shipping/Actions/ShipOrder.php:<?phpnamespace App\Modules\Shipping\Actions;class ShipOrder{public function handle(int $orderId): void{//}} -
Make Billing ship the order once it is charged:
app/Modules/Billing/Actions/ChargeOrder.php <?phpnamespace App\Modules\Billing\Actions;use App\Modules\Catalog\Contracts\PricesOrders;use App\Modules\Shipping\Actions\ShipOrder;class ChargeOrder{public function __construct(private PricesOrders $prices,private ShipOrder $shipping,) {}public function handle(int $orderId, array $lines): int{$total = $this->prices->quote($lines);$this->shipping->handle($orderId);return $total;}} -
Run the tests. Both rules fail now. The second one says:
Module Billing may only depend on Catalog (public API). 1 link breaks it:app/Modules/Billing/Actions/ChargeOrder.php:12App\Modules\Billing\Actions\ChargeOrder uses App\Modules\Shipping\Actions\ShipOrderBilling may not depend on Shipping.The rule caught a new link between modules the moment it appeared.
-
Undo step 3: put back the
ChargeOrderfrom the section above. Run the tests. Both pass again.
What we did
Section titled “What we did”We split an app into modules, gave a module a public API, and wrote two rules: one on how modules use each other, one on which modules Billing may use. Each failure named the file, the line and the classes of the link.
Next steps
Section titled “Next steps”- Define your modules in a real app, or with
nwidart/laravel-modules. - Check the links the app makes: facades, the container, events and jobs.
- Adopt Larch in an existing app that already breaks rules.