Package Data | |
---|---|
Maintainer Username: | Vinelab |
Maintainer Contact: | halawi.abed@gmail.com (Abed Halawi) |
Package Create Date: | 2020-10-30 |
Package Last Update: | 2024-05-24 |
Home Page: | https://docs.lucidarch.dev |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-09 15:08:59 |
Package Statistics | |
---|---|
Total Downloads: | 256,389 |
Monthly Downloads: | 4,235 |
Daily Downloads: | 77 |
Total Stars: | 320 |
Total Watchers: | 15 |
Total Forks: | 50 |
Total Open Issues: | 10 |
Lucid is a software architecture to build scalable Laravel projects. It incorporates Command Bus and Domain Driven Design at the core, upon which it builds a stack of directories and classes to organize business logic. It also derives from SOA (Service Oriented Architecture) the notion of encapsulating functionality within a service and enriches the concept with more than the service being a class.
Use Lucid to:
This architecture is in an amalgamation of best practices, design patterns and proven methods.
Feature
, Job
or Operation
.If you prefer a video, watch the announcement at Laracon EU 2016:
In a typical MVC application, Lucid will be the bond between the application's entrypoints and the units that do the work, securing code form meandring in drastic directions:
At a glance...
Provides the "kernel" to do the heavy lifting of the tedious stuff such as request/response lifecycle, dependency injection, and other core functionalities.
Extends the framework to provide higher level abstractions that are custom to the application and can be shared across the entire stack rather than being case-specific.
Examples of what could go into foundation are:
DateTime
a support class for common date and time functionsJsonSerializableInterface
that is used to identify an object to be serializable from and to JSON formatProvide separation to categorize jobs and corresponding classes that belong to the same topic. A domain operates in isolation from other domains and exposes its functionalities to features and operations through Lucid jobs only.
Consider the structure below for an example on what a domain may look like:
app/Domains/GitHub
├── GitHubClient
├── Jobs
│ ├── FetchGitHubRepoInfoJob
│ └── LoginWithGitHubJob
├── Exceptions
│ ├── InvalidTokenException
│ └── RepositoryNotFoundException
└── Tests
└── GitHubClientTest
└── Jobs
├── FetchGitHubReposJobTest
└── LoginWithGitHubJobTest
documentation contains more details on working with domains.
Are directories rich in functionality, used to separate a [Monolith]({{<ref "/micro-vs-monolith/#monolith">}}) into areas of focus in a multi-purpose application.
Consider the example of an application where we enter food recipes and would want our members to have discussions in a forum, we would have two services: 1) Kitchen, 2) Forum where the kitchen would manage all that's related to recipes, and forum is obvious:
app/Services
├── Forum
└── Kitchen
and following is a single service's structure, highlighted are the Lucid specific directories:
documentation has more examples of services and their contents.
Represent a human-readable application feature in a class. It contains the logic that implements the feature but with the least amount of detail, by running jobs from domains and operations at the application or service level.
Serving the Feature class will be the only line in a controller's method (in MVC), consequently achieving the thinnest form of controllers.
class AddRecipeFeature extends Feature
{
public function handle(AddRecipe $request)
{
$price = $this->run(CalculateRecipePriceOperation::class, [
'ingredients' => $request->input('ingredients'),
]);
$this->run(SaveRecipeJob::class, [
'price' => $price,
'user' => Auth::user(),
'title' => $request->input('title'),
'ingredients' => $request->input('ingredients'),
'instructions' => $request->input('instructions'),
]);
return $this->run(RedirectBackJob::class);
}
}
documentation about features expands on how to serve them as classes from anywhere.
Their purpose is to increase the degree of code reusability by piecing jobs together to provide composite functionalities from across domains.
class NotifySubscribersOperation extends Operation
{
private int $authorId;
public function __construct(int $authorId)
{
$this->authorId = $authorId;
}
/**
* Sends notifications to subscribers.
*
* @return int Number of notification jobs enqueued.
*/
public function handle(): int
{
$author = $this->run(GetAuthorByIDJob::class, [
'id' => $this->authorId,
]);
do {
$result = $this->run(PaginateSubscribersJob::class, [
'authorId' => $this->authorId,
]);
if ($result->subscribers->isNotEmpty()) {
// it's a queueable job so it will be enqueued, no waiting time
$this->run(SendNotificationJob::class, [
'from' => $author,
'to' => $result->subscribers,
'notification' => 'article.published',
]);
}
} while ($result->hasMorePages());
return $result->total;
}
}
documentation goes over this simple yet powerful concept.
For a scalable set of interconnected data elements, we've created a place for them in app/Data
,
because most likely over time writing the application there could develop a need for more than Models in data,
such as Repositories, Value Objects, Collections and more.
app/Data
├── Models
├── Values
├── Collections
└── Repositories
There are valuable advantages to what may seem as overengineering.
By dissecting our application into small building blocks of code - a.k.a units - we've instantly opened the door for a high degree of code sharing across the application with Data and Domains, as well as replaceability with the least amount of friction and technical debt.
By setting boundaries you would've taken a step towards proetcting application code from growing unbearably large and made it easier for new devs to onboard. Most importantly, that you've reduced technical debt to the minimum so that you don't have to pay with bugs and sleepless nights; code doesn't run on good intentions nor wishes.
When our application scales we'd typically have a bunch of instances of it running in different locations, at some point we would want to activate certain parts of our codebase in some areas and shut off others.
Here’s a humble example of running Api, Back Office and Web App instances of the same application, which in Lucid terminology are services that share functionality through data and domains: