Layers
The commands find the layers through the Lomkit\Osdd\Layers\Layers service. Use it in your own commands, tests or tooling.
use Lomkit\Osdd\Layers\Layers;
$layers = app(Layers::class); // or inject Layers
$layers->names(); // ['functional/orders', 'functional/users', 'technical/osdd']
$orders = $layers->find('orders'); // full name, or package name when it is unique
$orders->rootNamespace; // 'Functional\Orders\'
$orders->sourceDirectory; // '/var/www/functional/orders/src'
The layers are discovered once per process, from the folders of osdd.layers.paths, or read from bootstrap/cache/osdd.php when osdd:cache cached them. See Discovery for the rules.
Layers
Lomkit\Osdd\Layers\Layers holds the discovered layers, sorted by name. It is countable and iterable:
foreach (app(Layers::class) as $layer) {
echo $layer->name->toString(); // functional/orders
}
| Method | Returns | Description |
|---|---|---|
find(string $name) | Layer | The layer with this full name, or the only layer with this package name. Throws LayerNotFoundException or AmbiguousLayerNameException. |
findAll(array $names) | Layers | find() for each name, without duplicates |
names() | list<string> | The layer names |
owning(string $class) | ?Layer | The layer whose longest psr-4 prefix the class starts with, or null |
sharingPackage(LayerName $name) | Layers | The other layers with the same package name |
paths(LayerFolder $folder) | list<string> | The absolute path of a folder in each layer |
filter(Closure $keep) | Layers | The layers the callback keeps |
map(Closure $callback) | array | The callback's result for each layer |
count(), isEmpty() | int, bool | |
invalidManifests() | list<InvalidLayerManifestException> | The layer composer.json files that could not be read |
failOnInvalidManifest() | Layers | The same layers, or throws the first invalid manifest |
use Functional\Orders\Models\Order;
use Lomkit\Osdd\Layers\LayerFolder;
$layers->owning(Order::class)?->name->toString(); // functional/orders
$layers->paths(LayerFolder::Migrations); // ['/var/www/functional/orders/database/migrations', ...]
Exceptions
All three live in Lomkit\Osdd\Layers:
| Exception | Message |
|---|---|
LayerNotFoundException | Layer [functional/oders] not found. Available layers: functional/orders, functional/users. Did you mean [functional/orders]? |
AmbiguousLayerNameException | Layer [orders] is ambiguous: it matches functional/orders, technical/orders. Pass the full layer name. |
InvalidLayerManifestException | Invalid layer manifest [functional/orders/composer.json]: it has no "name". |
Layer
Lomkit\Osdd\Layers\Layer is one discovered layer, read from its composer.json. It is immutable.
| Property | Type | Description |
|---|---|---|
$name | LayerName | The layer name |
$directory | LayerDirectory | The layer folder |
$rootNamespace | string | The shortest autoload.psr-4 prefix: Functional\Orders\ |
$sourceDirectory | string | The absolute path of that prefix's directory |
$autoload, $devAutoload | array<string, string> | autoload.psr-4 and autoload-dev.psr-4, prefix to directory |
$providers | list<string> | extra.laravel.providers |
$requires, $devRequires | list<string> | The package names in require and require-dev |
| Method | Returns | Description |
|---|---|---|
namespaces(), devNamespaces() | list<string> | The psr-4 prefixes |
classFile(string $class) | ?string | The file of a class under the longest matching prefix, or null |
ownershipOf(string $class) | int | The length of the longest matching prefix, 0 when the layer does not own the class |
LayerName
Lomkit\Osdd\Layers\LayerName is a validated layer name.
$layer->name->vendor; // functional
$layer->name->package; // orders
$layer->name->toString(); // functional/orders
$layer->name->namespace(); // Functional\Orders
LayerName::fromString('functional/orders') throws Lomkit\Osdd\Layers\InvalidLayerNameException for a name that is not a valid layer name.
LayerDirectory and LayerFolder
$layer->directory is a Lomkit\Osdd\Layers\LayerDirectory:
| Member | Returns | Example |
|---|---|---|
$root | string | /var/www/functional/orders |
path(LayerFolder $folder) | string | /var/www/functional/orders/database/migrations |
pathTo(string $relativePath) | string | /var/www/functional/orders/routes/web.php |
manifestFile() | string | /var/www/functional/orders/composer.json |
relativeTo(string $base) | string | functional/orders |
Lomkit\Osdd\Layers\LayerFolder lists the conventional folders: Config, Database, Factories, Lang, Migrations, Routes, Seeders, Tests, Views.
Discovering other folders
To discover the layers of folders that are not in the config, build a LayerDiscovery yourself:
use Illuminate\Filesystem\Filesystem;
use Lomkit\Osdd\Layers\LayerBuckets;
use Lomkit\Osdd\Layers\LayerDiscovery;
$layers = (new LayerDiscovery(
new LayerBuckets(['functional' => base_path('legacy/functional')]),
new Filesystem,
))->discover();