API Reference

Layers

Discover and inspect the layers of the project from your own code.

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
}
MethodReturnsDescription
find(string $name)LayerThe layer with this full name, or the only layer with this package name. Throws LayerNotFoundException or AmbiguousLayerNameException.
findAll(array $names)Layersfind() for each name, without duplicates
names()list<string>The layer names
owning(string $class)?LayerThe layer whose longest psr-4 prefix the class starts with, or null
sharingPackage(LayerName $name)LayersThe other layers with the same package name
paths(LayerFolder $folder)list<string>The absolute path of a folder in each layer
filter(Closure $keep)LayersThe layers the callback keeps
map(Closure $callback)arrayThe callback's result for each layer
count(), isEmpty()int, bool
invalidManifests()list<InvalidLayerManifestException>The layer composer.json files that could not be read
failOnInvalidManifest()LayersThe 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:

ExceptionMessage
LayerNotFoundExceptionLayer [functional/oders] not found. Available layers: functional/orders, functional/users. Did you mean [functional/orders]?
AmbiguousLayerNameExceptionLayer [orders] is ambiguous: it matches functional/orders, technical/orders. Pass the full layer name.
InvalidLayerManifestExceptionInvalid 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.

PropertyTypeDescription
$nameLayerNameThe layer name
$directoryLayerDirectoryThe layer folder
$rootNamespacestringThe shortest autoload.psr-4 prefix: Functional\Orders\
$sourceDirectorystringThe absolute path of that prefix's directory
$autoload, $devAutoloadarray<string, string>autoload.psr-4 and autoload-dev.psr-4, prefix to directory
$providerslist<string>extra.laravel.providers
$requires, $devRequireslist<string>The package names in require and require-dev
MethodReturnsDescription
namespaces(), devNamespaces()list<string>The psr-4 prefixes
classFile(string $class)?stringThe file of a class under the longest matching prefix, or null
ownershipOf(string $class)intThe 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:

MemberReturnsExample
$rootstring/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)stringfunctional/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();