first commit
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster;
|
||||
|
||||
use Laravel\Roster\Enums\Approaches;
|
||||
|
||||
class Approach
|
||||
{
|
||||
public function __construct(protected Approaches $approach) {}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return $this->approach->name;
|
||||
}
|
||||
|
||||
public function approach(): Approaches
|
||||
{
|
||||
return $this->approach;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Laravel\Roster\Roster;
|
||||
|
||||
class ScanCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'roster:scan {directory}';
|
||||
|
||||
protected $description = 'Detect packages & approaches in use and output as JSON';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$directory = $this->argument('directory');
|
||||
if (! is_string($directory)) {
|
||||
$this->error('Pass a directory');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if (! is_dir($directory) || ! is_readable($directory)) {
|
||||
$this->error("Directory '{$directory}' isn't a directory");
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$roster = Roster::scan($directory);
|
||||
$this->line($roster->json());
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Enums;
|
||||
|
||||
enum Approaches: string
|
||||
{
|
||||
case ACTION = 'action';
|
||||
case DDD = 'ddd';
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Enums;
|
||||
|
||||
enum Ides: string
|
||||
{
|
||||
case PHPSTORM = 'phpstorm';
|
||||
case CURSOR = 'cursor';
|
||||
case WINDSURF = 'windsurf';
|
||||
case VSCODE = 'vscode';
|
||||
case CLAUDE_CODE = 'claudecode';
|
||||
case CODEX = 'codex';
|
||||
case OPENCODE = 'opencode';
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Enums;
|
||||
|
||||
use Laravel\Roster\Scanners\BunPackageLock;
|
||||
use Laravel\Roster\Scanners\NpmPackageLock;
|
||||
use Laravel\Roster\Scanners\PnpmPackageLock;
|
||||
use Laravel\Roster\Scanners\YarnPackageLock;
|
||||
|
||||
enum NodePackageManager: string
|
||||
{
|
||||
case NPM = 'npm';
|
||||
case PNPM = 'pnpm';
|
||||
case YARN = 'yarn';
|
||||
case BUN = 'bun';
|
||||
|
||||
public function scanner(string $path): NpmPackageLock|PnpmPackageLock|YarnPackageLock|BunPackageLock
|
||||
{
|
||||
return match ($this) {
|
||||
self::NPM => new NpmPackageLock($path),
|
||||
self::PNPM => new PnpmPackageLock($path),
|
||||
self::YARN => new YarnPackageLock($path),
|
||||
self::BUN => new BunPackageLock($path),
|
||||
};
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Enums;
|
||||
|
||||
enum Packages: string
|
||||
{
|
||||
// Compound
|
||||
case INERTIA = 'inertia';
|
||||
case WAYFINDER = 'wayfinder';
|
||||
|
||||
// BACKEND
|
||||
case BREEZE = 'breeze';
|
||||
case CASHIER = 'cashier';
|
||||
case DUSK = 'dusk';
|
||||
case ENVOY = 'envoy';
|
||||
case FILAMENT = 'filament';
|
||||
case FOLIO = 'folio';
|
||||
case FORTIFY = 'fortify';
|
||||
case FLUXUI_FREE = 'flux_free';
|
||||
case FLUXUI_PRO = 'flux_pro';
|
||||
case HORIZON = 'horizon';
|
||||
case INERTIA_LARAVEL = 'inertia-laravel';
|
||||
case LARASTAN = 'larastan';
|
||||
case LARAVEL = 'laravel';
|
||||
case LIVEWIRE = 'livewire';
|
||||
case MCP = 'mcp';
|
||||
case NIGHTWATCH = 'nightwatch';
|
||||
case NOVA = 'nova';
|
||||
case OCTANE = 'octane';
|
||||
case PASSPORT = 'passport';
|
||||
case PENNANT = 'pennant';
|
||||
case PEST = 'pest';
|
||||
case PHPUNIT = 'phpunit';
|
||||
case PINT = 'pint';
|
||||
case PROMPTS = 'prompts';
|
||||
case PULSE = 'pulse';
|
||||
case RECTOR = 'rector';
|
||||
case REVERB = 'reverb';
|
||||
case SAIL = 'sail';
|
||||
case SANCTUM = 'sanctum';
|
||||
case SCOUT = 'scout';
|
||||
case SOCIALITE = 'socialite';
|
||||
case STATAMIC = 'statamic';
|
||||
case TELESCOPE = 'telescope';
|
||||
case VOLT = 'volt';
|
||||
case WAYFINDER_LARAVEL = 'wayfinder_laravel';
|
||||
case ZIGGY = 'ziggy';
|
||||
|
||||
// NPM
|
||||
case ALPINEJS = 'alpinejs';
|
||||
case ECHO = 'laravel-echo';
|
||||
case ESLINT = 'eslint';
|
||||
case INERTIA_REACT = 'inertia-react';
|
||||
case INERTIA_SVELTE = 'inertia-svelte';
|
||||
case INERTIA_VUE = 'inertia-vue';
|
||||
case PRETTIER = 'prettier';
|
||||
case REACT = 'react';
|
||||
case TAILWINDCSS = 'tailwindcss';
|
||||
case VUE = 'vue';
|
||||
case WAYFINDER_VITE = 'wayfinder_vite';
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster;
|
||||
|
||||
use Laravel\Roster\Enums\Packages;
|
||||
|
||||
class Package
|
||||
{
|
||||
protected bool $direct = false;
|
||||
|
||||
protected string $constraint = '';
|
||||
|
||||
public function __construct(protected Packages $package, protected string $packageName, protected string $version, protected bool $dev = false) {}
|
||||
|
||||
public function setDev(bool $dev = true): self
|
||||
{
|
||||
$this->dev = $dev;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDirect(bool $direct = true): self
|
||||
{
|
||||
$this->direct = $direct;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setConstraint(string $constraint = ''): self
|
||||
{
|
||||
$this->constraint = $constraint;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return $this->package->name;
|
||||
}
|
||||
|
||||
public function package(): Packages
|
||||
{
|
||||
return $this->package;
|
||||
}
|
||||
|
||||
public function version(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
public function direct(): bool
|
||||
{
|
||||
return $this->direct;
|
||||
}
|
||||
|
||||
public function indirect(): bool
|
||||
{
|
||||
return ! $this->direct;
|
||||
}
|
||||
|
||||
public function constraint(): string
|
||||
{
|
||||
return $this->constraint;
|
||||
}
|
||||
|
||||
public function majorVersion(): string
|
||||
{
|
||||
return explode('.', $this->version)[0];
|
||||
}
|
||||
|
||||
public function isDev(): bool
|
||||
{
|
||||
return $this->dev;
|
||||
}
|
||||
|
||||
public function rawName(): string
|
||||
{
|
||||
return $this->packageName;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* @extends Collection<int, Package>
|
||||
*/
|
||||
class PackageCollection extends Collection
|
||||
{
|
||||
public function dev(): static
|
||||
{
|
||||
return $this->filter(fn (Package $package) => $package->isDev());
|
||||
}
|
||||
|
||||
public function production(): static
|
||||
{
|
||||
return $this->filter(fn (Package $package) => ! $package->isDev());
|
||||
}
|
||||
}
|
||||
Vendored
+163
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Roster\Enums\Approaches;
|
||||
use Laravel\Roster\Enums\NodePackageManager;
|
||||
use Laravel\Roster\Enums\Packages;
|
||||
use Laravel\Roster\Scanners\Composer;
|
||||
use Laravel\Roster\Scanners\DirectoryStructure;
|
||||
use Laravel\Roster\Scanners\PackageLock;
|
||||
|
||||
/**
|
||||
* Package and approach detection service for Laravel projects.
|
||||
*
|
||||
* Scans composer.lock, package-lock.json, and directory structure to identify
|
||||
* packages and development approaches in use.
|
||||
*/
|
||||
class Roster
|
||||
{
|
||||
/**
|
||||
* @var Collection<int, \Laravel\Roster\Approach>
|
||||
*/
|
||||
protected Collection $approaches;
|
||||
|
||||
protected PackageCollection $packages;
|
||||
|
||||
protected ?NodePackageManager $nodePackageManager = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->approaches = collect();
|
||||
$this->packages = new PackageCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function add(Package|Approach $item): self
|
||||
{
|
||||
return match (get_class($item)) {
|
||||
Package::class => $this->addPackage($item),
|
||||
Approach::class => $this->addApproach($item),
|
||||
default => throw new \InvalidArgumentException('Unexpected match value'),
|
||||
};
|
||||
}
|
||||
|
||||
public function uses(Packages|Approaches $item): bool
|
||||
{
|
||||
return $this->findItem($item) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function usesVersion(Packages $package, string $version, string $operator = '='): bool
|
||||
{
|
||||
if (! preg_match('/[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}/', $version)) {
|
||||
throw new \InvalidArgumentException('SEMVER required');
|
||||
}
|
||||
|
||||
$validOperators = ['<', '<=', '>', '>=', '==', '=', '!=', '<>'];
|
||||
if (! in_array($operator, $validOperators)) {
|
||||
throw new \InvalidArgumentException('Invalid operator');
|
||||
}
|
||||
|
||||
$package = $this->findItem($package);
|
||||
if (is_null($package)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var \Laravel\Roster\Package $package */
|
||||
return version_compare($package->version(), $version, $operator);
|
||||
}
|
||||
|
||||
protected function findItem(Packages|Approaches $item): Package|Approach|null
|
||||
{
|
||||
return match (get_class($item)) {
|
||||
Packages::class => $this->package($item),
|
||||
Approaches::class => $this->approach($item),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
protected function addPackage(Package $package): self
|
||||
{
|
||||
$this->packages->push($package);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function addApproach(Approach $approach): self
|
||||
{
|
||||
$this->approaches->push($approach);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, \Laravel\Roster\Approach>
|
||||
*/
|
||||
public function approaches(): Collection
|
||||
{
|
||||
return $this->approaches;
|
||||
}
|
||||
|
||||
public function packages(): PackageCollection
|
||||
{
|
||||
return $this->packages;
|
||||
}
|
||||
|
||||
public function package(Packages $package): ?Package
|
||||
{
|
||||
return $this->packages->first(fn (Package $item) => $item->package()->value === $package->value);
|
||||
}
|
||||
|
||||
public function approach(Approaches $approach): ?Approach
|
||||
{
|
||||
return $this->approaches->first(fn (Approach $item) => $item->approach()->value === $approach->value);
|
||||
}
|
||||
|
||||
public function nodePackageManager(): ?NodePackageManager
|
||||
{
|
||||
return $this->nodePackageManager;
|
||||
}
|
||||
|
||||
public function json(): string
|
||||
{
|
||||
return json_encode([
|
||||
'approaches' => $this->approaches->map(fn (Approach $approach) => [
|
||||
'name' => $approach->name(),
|
||||
])->toArray(),
|
||||
'packages' => $this->packages->map(fn (Package $package) => [
|
||||
'name' => $package->name(),
|
||||
'version' => $package->version(),
|
||||
])->toArray(),
|
||||
'nodePackageManager' => $this->nodePackageManager?->value,
|
||||
], JSON_PRETTY_PRINT) ?: '{}';
|
||||
}
|
||||
|
||||
public static function scan(?string $basePath = null): self
|
||||
{
|
||||
$roster = new self;
|
||||
$basePath = ($basePath ?? base_path()).DIRECTORY_SEPARATOR;
|
||||
|
||||
(new Composer($basePath.'composer.lock'))
|
||||
->scan()
|
||||
->each(fn ($item) => $roster->add($item));
|
||||
|
||||
$packageLock = new PackageLock($basePath);
|
||||
|
||||
$packageLock->scan()
|
||||
->each(fn ($item) => $roster->add($item));
|
||||
|
||||
(new DirectoryStructure($basePath))
|
||||
->scan()
|
||||
->each(fn ($item) => $roster->add($item));
|
||||
|
||||
$roster->nodePackageManager = $packageLock->detect();
|
||||
|
||||
return $roster;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class RosterServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap any package services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the package's commands.
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->commands([
|
||||
Console\ScanCommand::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laravel\Roster\Approach;
|
||||
use Laravel\Roster\Enums\Approaches;
|
||||
use Laravel\Roster\Enums\Packages;
|
||||
use Laravel\Roster\Package;
|
||||
|
||||
abstract class BasePackageScanner
|
||||
{
|
||||
/**
|
||||
* Map of package names to enums
|
||||
*
|
||||
* @var array<string, Packages|Approaches|array<int, Packages|Approaches>>
|
||||
*/
|
||||
protected array $map = [
|
||||
'alpinejs' => Packages::ALPINEJS,
|
||||
'eslint' => Packages::ESLINT,
|
||||
'@inertiajs/react' => [Packages::INERTIA, Packages::INERTIA_REACT],
|
||||
'@inertiajs/svelte' => [Packages::INERTIA, Packages::INERTIA_SVELTE],
|
||||
'@inertiajs/vue3' => [Packages::INERTIA, Packages::INERTIA_VUE],
|
||||
'laravel-echo' => Packages::ECHO,
|
||||
'@laravel/vite-plugin-wayfinder' => [Packages::WAYFINDER, Packages::WAYFINDER_VITE],
|
||||
'prettier' => Packages::PRETTIER,
|
||||
'react' => Packages::REACT,
|
||||
'tailwindcss' => [Packages::TAILWINDCSS],
|
||||
'vue' => Packages::VUE,
|
||||
];
|
||||
|
||||
public function __construct(protected string $path) {}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
abstract public function scan(): Collection;
|
||||
|
||||
/**
|
||||
* Check if the scanner can handle the given path
|
||||
*/
|
||||
abstract public function canScan(): bool;
|
||||
|
||||
/**
|
||||
* Process dependencies and add them to the mapped items collection
|
||||
*
|
||||
* @param array<string, string> $dependencies
|
||||
* @param Collection<int, Package|Approach> $mappedItems
|
||||
* @param ?callable $versionCb - callback to override version
|
||||
*/
|
||||
protected function processDependencies(array $dependencies, Collection $mappedItems, bool $isDev, ?callable $versionCb = null): void
|
||||
{
|
||||
foreach ($dependencies as $packageName => $version) {
|
||||
$mappedPackage = $this->map[$packageName] ?? null;
|
||||
if (is_null($mappedPackage)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! is_array($mappedPackage)) {
|
||||
$mappedPackage = [$mappedPackage];
|
||||
}
|
||||
|
||||
if (! is_null($versionCb)) {
|
||||
$version = $versionCb($packageName, $version);
|
||||
}
|
||||
|
||||
foreach ($mappedPackage as $mapped) {
|
||||
$niceVersion = preg_replace('/[^0-9.]/', '', $version) ?? '';
|
||||
$mappedItems->push(match (get_class($mapped)) {
|
||||
Packages::class => new Package($mapped, $packageName, $niceVersion, $isDev),
|
||||
Approaches::class => new Approach($mapped),
|
||||
default => throw new \InvalidArgumentException('Unsupported mapping')
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Common file validation logic
|
||||
*/
|
||||
protected function validateFile(string $path, string $type = 'Package'): ?string
|
||||
{
|
||||
if (! file_exists($path)) {
|
||||
Log::warning("Failed to scan $type: $path");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_readable($path)) {
|
||||
Log::warning("File not readable: $path");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$contents = file_get_contents($path);
|
||||
if ($contents === false) {
|
||||
Log::warning("Failed to read $type: $path");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class BunPackageLock extends BasePackageScanner
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
public function scan(): Collection
|
||||
{
|
||||
$mappedItems = collect();
|
||||
$lockFilePath = $this->path.'bun.lock';
|
||||
|
||||
$contents = $this->validateFile($lockFilePath);
|
||||
if ($contents === null) {
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
// Remove trailing commas before decoding
|
||||
/** @var string $contents */
|
||||
$contents = preg_replace('/,\s*([]}])/m', '$1', $contents);
|
||||
$json = json_decode($contents, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE || ! is_array($json)) {
|
||||
Log::warning('Failed to decode Package: '.$lockFilePath.'. '.json_last_error_msg());
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/** @var array<string, array<string, mixed>> $json */
|
||||
if (! isset($json['workspaces']['']) || ! isset($json['packages'])) {
|
||||
Log::warning('Malformed bun.lock');
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/** @var array<string, mixed> $workspace */
|
||||
$workspace = $json['workspaces'][''];
|
||||
|
||||
/** @var array<string, string> $dependencies */
|
||||
$dependencies = $workspace['dependencies'] ?? [];
|
||||
/** @var array<string, string> $devDependencies */
|
||||
$devDependencies = $workspace['devDependencies'] ?? [];
|
||||
|
||||
$this->processDependencies($dependencies, $mappedItems, false);
|
||||
$this->processDependencies($devDependencies, $mappedItems, true);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the scanner can handle the given path
|
||||
*/
|
||||
public function canScan(): bool
|
||||
{
|
||||
return file_exists($this->path.'bun.lock');
|
||||
}
|
||||
}
|
||||
+196
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laravel\Roster\Approach;
|
||||
use Laravel\Roster\Enums\Approaches;
|
||||
use Laravel\Roster\Enums\Packages;
|
||||
use Laravel\Roster\Package;
|
||||
|
||||
class Composer
|
||||
{
|
||||
/**
|
||||
* Map of composer package names to enums
|
||||
*
|
||||
* @var array<string, Packages|Approaches|array<int, Packages|Approaches>|null>
|
||||
*/
|
||||
protected array $map = [
|
||||
'filament/filament' => Packages::FILAMENT,
|
||||
'inertiajs/inertia-laravel' => [Packages::INERTIA, Packages::INERTIA_LARAVEL],
|
||||
'larastan/larastan' => Packages::LARASTAN,
|
||||
'laravel/breeze' => Packages::BREEZE,
|
||||
'laravel/cashier' => Packages::CASHIER,
|
||||
'laravel/dusk' => Packages::DUSK,
|
||||
'laravel/envoy' => Packages::ENVOY,
|
||||
'laravel/folio' => Packages::FOLIO,
|
||||
'laravel/fortify' => Packages::FORTIFY,
|
||||
'laravel/framework' => Packages::LARAVEL,
|
||||
'laravel/horizon' => Packages::HORIZON,
|
||||
'laravel/mcp' => Packages::MCP,
|
||||
'laravel/nightwatch' => Packages::NIGHTWATCH,
|
||||
'laravel/nova' => Packages::NOVA,
|
||||
'laravel/octane' => Packages::OCTANE,
|
||||
'laravel/passport' => Packages::PASSPORT,
|
||||
'laravel/pennant' => Packages::PENNANT,
|
||||
'laravel/pint' => Packages::PINT,
|
||||
'laravel/prompts' => Packages::PROMPTS,
|
||||
'laravel/pulse' => Packages::PULSE,
|
||||
'laravel/reverb' => Packages::REVERB,
|
||||
'laravel/sail' => Packages::SAIL,
|
||||
'laravel/sanctum' => Packages::SANCTUM,
|
||||
'laravel/scout' => Packages::SCOUT,
|
||||
'laravel/socialite' => Packages::SOCIALITE,
|
||||
'laravel/telescope' => Packages::TELESCOPE,
|
||||
'laravel/wayfinder' => [Packages::WAYFINDER, Packages::WAYFINDER_LARAVEL],
|
||||
'livewire/flux' => Packages::FLUXUI_FREE,
|
||||
'livewire/flux-pro' => Packages::FLUXUI_PRO,
|
||||
'livewire/livewire' => Packages::LIVEWIRE,
|
||||
'livewire/volt' => Packages::VOLT,
|
||||
'pestphp/pest' => Packages::PEST,
|
||||
'phpunit/phpunit' => Packages::PHPUNIT,
|
||||
'rector/rector' => Packages::RECTOR,
|
||||
'statamic/cms' => Packages::STATAMIC,
|
||||
'tightenco/ziggy' => Packages::ZIGGY,
|
||||
];
|
||||
|
||||
/** @var array<string, array{constraint: string, isDev: bool}> */
|
||||
protected array $directPackages = [];
|
||||
|
||||
/**
|
||||
* @param string $path - composer.lock
|
||||
*/
|
||||
public function __construct(protected string $path) {}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
public function scan(): Collection
|
||||
{
|
||||
$mappedItems = collect([]);
|
||||
|
||||
if (! file_exists($this->path)) {
|
||||
Log::warning('Failed to scan Composer: '.$this->path);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
if (! is_readable($this->path)) {
|
||||
Log::warning('File not readable: '.$this->path);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
$contents = file_get_contents($this->path);
|
||||
if ($contents === false) {
|
||||
Log::warning('Failed to read Composer: '.$this->path);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
$json = json_decode($contents, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE || ! is_array($json)) {
|
||||
Log::warning('Failed to decode Composer: '.$this->path.'. '.json_last_error_msg());
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
if (! array_key_exists('packages', $json)) {
|
||||
Log::warning('Malformed composer.lock');
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
$this->directPackages = $this->direct();
|
||||
$packages = $json['packages'] ?? [];
|
||||
$devPackages = $json['packages-dev'] ?? [];
|
||||
|
||||
$this->processPackages($packages, $mappedItems, false);
|
||||
$this->processPackages($devPackages, $mappedItems, true);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns direct dependencies as defined in composer.json
|
||||
*
|
||||
* @return array<string, array{constraint: string, isDev: bool}>
|
||||
* */
|
||||
protected function direct(): array
|
||||
{
|
||||
$packages = [];
|
||||
$filename = realpath(dirname($this->path)).DIRECTORY_SEPARATOR.'composer.json';
|
||||
if (file_exists($filename) === false || is_readable($filename) === false) {
|
||||
return $packages;
|
||||
}
|
||||
|
||||
$json = file_get_contents($filename);
|
||||
if ($json === false) {
|
||||
return $packages;
|
||||
}
|
||||
|
||||
$json = json_decode($json, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE || ! is_array($json)) {
|
||||
return $packages;
|
||||
}
|
||||
|
||||
foreach (($json['require'] ?? []) as $name => $constraint) {
|
||||
$packages[$name] = [
|
||||
'constraint' => $constraint,
|
||||
'isDev' => false,
|
||||
];
|
||||
}
|
||||
|
||||
foreach (($json['require-dev'] ?? []) as $name => $constraint) {
|
||||
$packages[$name] = [
|
||||
'constraint' => $constraint,
|
||||
'isDev' => true,
|
||||
];
|
||||
}
|
||||
|
||||
return $packages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process packages and add them to the mapped items collection
|
||||
*
|
||||
* @param array<int, array<string, string>> $packages
|
||||
* @param Collection<int, Package|Approach> $mappedItems
|
||||
* @return Collection<int, Package|Approach>
|
||||
*/
|
||||
private function processPackages(array $packages, Collection $mappedItems, bool $isDev): Collection
|
||||
{
|
||||
foreach ($packages as $package) {
|
||||
$packageName = $package['name'] ?? '';
|
||||
$version = $package['version'] ?? '';
|
||||
$mappedPackage = $this->map[$packageName] ?? null;
|
||||
$direct = false;
|
||||
$constraint = $version;
|
||||
|
||||
if (is_null($mappedPackage)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! is_array($mappedPackage)) {
|
||||
$mappedPackage = [$mappedPackage];
|
||||
}
|
||||
|
||||
if (array_key_exists($packageName, $this->directPackages) === true) {
|
||||
$direct = true;
|
||||
$constraint = $this->directPackages[$packageName]['constraint'];
|
||||
}
|
||||
|
||||
foreach ($mappedPackage as $mapped) {
|
||||
$niceVersion = preg_replace('/[^0-9.]/', '', $version) ?? '';
|
||||
$mappedItems->push(match (get_class($mapped)) {
|
||||
Packages::class => (new Package($mapped, $packageName, $niceVersion, $isDev))->setDirect($direct)->setConstraint($constraint),
|
||||
Approaches::class => new Approach($mapped),
|
||||
default => throw new \InvalidArgumentException('Unsupported mapping')
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Roster\Approach;
|
||||
use Laravel\Roster\Enums\Approaches;
|
||||
|
||||
class DirectoryStructure
|
||||
{
|
||||
public function __construct(protected string $path) {}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
public function scan(): Collection
|
||||
{
|
||||
$items = collect();
|
||||
|
||||
$actions = $this->path.DIRECTORY_SEPARATOR.'/app/'.DIRECTORY_SEPARATOR.'Actions';
|
||||
if (is_dir($actions)) {
|
||||
$items->push(new Approach(Approaches::ACTION));
|
||||
}
|
||||
|
||||
$domains = $this->path.DIRECTORY_SEPARATOR.'/app/'.DIRECTORY_SEPARATOR.'Domains';
|
||||
if (is_dir($domains)) {
|
||||
$items->push(new Approach(Approaches::DDD));
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class NpmPackageLock extends BasePackageScanner
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
public function scan(): Collection
|
||||
{
|
||||
$mappedItems = collect();
|
||||
$lockFilePath = $this->path.'package-lock.json';
|
||||
|
||||
$contents = $this->validateFile($lockFilePath);
|
||||
if ($contents === null) {
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
$json = json_decode($contents, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE || ! is_array($json)) {
|
||||
Log::warning('Failed to decode Package: '.$lockFilePath.'. '.json_last_error_msg());
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
if (! array_key_exists('packages', $json)) {
|
||||
Log::warning('Malformed package-lock');
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
$dependencies = $json['packages']['']['dependencies'] ?? [];
|
||||
$devDependencies = $json['packages']['']['devDependencies'] ?? [];
|
||||
$packages = array_filter($json['packages'], fn ($key) => $key !== '', ARRAY_FILTER_USE_KEY);
|
||||
|
||||
$versionCb = function (string $packageName, string $version) use ($packages): string {
|
||||
$key = "node_modules/{$packageName}";
|
||||
if (array_key_exists($key, $packages)) {
|
||||
return $packages[$key]['version'];
|
||||
}
|
||||
|
||||
return $version;
|
||||
};
|
||||
|
||||
$this->processDependencies($dependencies, $mappedItems, false, $versionCb);
|
||||
$this->processDependencies($devDependencies, $mappedItems, true, $versionCb);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the scanner can handle the given path
|
||||
*/
|
||||
public function canScan(): bool
|
||||
{
|
||||
return file_exists($this->path.'package-lock.json');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Roster\Enums\NodePackageManager;
|
||||
|
||||
class PackageLock
|
||||
{
|
||||
/**
|
||||
* @param string $path - Base path to scan for lock files (package-lock.json, pnpm-lock.yaml, yarn.lock, ...)
|
||||
*/
|
||||
public function __construct(protected string $path) {}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
public function scan(): Collection
|
||||
{
|
||||
foreach (NodePackageManager::cases() as $case) {
|
||||
$scanner = $case->scanner($this->path);
|
||||
if ($scanner->canScan()) {
|
||||
return $scanner->scan();
|
||||
}
|
||||
}
|
||||
|
||||
return collect();
|
||||
}
|
||||
|
||||
public function detect(): NodePackageManager
|
||||
{
|
||||
foreach (NodePackageManager::cases() as $case) {
|
||||
$scanner = $case->scanner($this->path);
|
||||
if ($scanner->canScan()) {
|
||||
return $case;
|
||||
}
|
||||
}
|
||||
|
||||
return NodePackageManager::NPM;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class PnpmPackageLock extends BasePackageScanner
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
public function scan(): Collection
|
||||
{
|
||||
$mappedItems = collect();
|
||||
$lockFilePath = $this->path.'pnpm-lock.yaml';
|
||||
|
||||
$contents = $this->validateFile($lockFilePath, 'PNPM lock');
|
||||
if ($contents === null) {
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var array<string, mixed> $parsed */
|
||||
$parsed = Yaml::parse($contents);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to parse YAML: '.$e->getMessage());
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/** @var array<string, string> $dependencies */
|
||||
$dependencies = [];
|
||||
/** @var array<string, string> $devDependencies */
|
||||
$devDependencies = [];
|
||||
|
||||
/** @var array<string, array<string, mixed>> $importers */
|
||||
$importers = $parsed['importers'] ?? [];
|
||||
$root = $importers['.'] ?? [];
|
||||
/** @var array<string, array<string, mixed>> $rootDependencies */
|
||||
$rootDependencies = $root['dependencies'] ?? [];
|
||||
/** @var array<string, array<string, mixed>> $rootDevDependencies */
|
||||
$rootDevDependencies = $root['devDependencies'] ?? [];
|
||||
|
||||
foreach ($rootDependencies as $name => $data) {
|
||||
if (isset($data['version'])) {
|
||||
$dependencies[$name] = $data['version'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($rootDevDependencies as $name => $data) {
|
||||
if (isset($data['version'])) {
|
||||
$devDependencies[$name] = $data['version'];
|
||||
}
|
||||
}
|
||||
|
||||
/** @var array<string, string> $dependencies */
|
||||
/** @var array<string, string> $devDependencies */
|
||||
$this->processDependencies($dependencies, $mappedItems, false);
|
||||
$this->processDependencies($devDependencies, $mappedItems, true);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the scanner can handle the given path
|
||||
*/
|
||||
public function canScan(): bool
|
||||
{
|
||||
return file_exists($this->path.'pnpm-lock.yaml');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Roster\Scanners;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class YarnPackageLock extends BasePackageScanner
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection<int, \Laravel\Roster\Package|\Laravel\Roster\Approach>
|
||||
*/
|
||||
public function scan(): Collection
|
||||
{
|
||||
$mappedItems = collect([]);
|
||||
$lockFilePath = $this->path.'yarn.lock';
|
||||
|
||||
$contents = $this->validateFile($lockFilePath, 'Yarn lock');
|
||||
if ($contents === null) {
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
$dependencies = [];
|
||||
$lines = explode("\n", $contents);
|
||||
$currentPackage = null;
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
|
||||
// Skip comments and empty lines
|
||||
if ($line === '' || str_starts_with($line, '#')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Package header line (e.g. tailwindcss@^3.4.3:)
|
||||
if (preg_match('/^("?)([^@"]+)(@[^:]+)?:\1$/', $line, $matches)) {
|
||||
$currentPackage = $matches[2];
|
||||
}
|
||||
// Version line
|
||||
elseif ($currentPackage && preg_match('/^version\s+"?([^"]+)"?$/', $line, $matches)) {
|
||||
$version = $matches[1];
|
||||
$dependencies[$currentPackage] = $version;
|
||||
$currentPackage = null; // Reset until next package block
|
||||
}
|
||||
}
|
||||
|
||||
// Yarn lock does not distinguish devDependencies :/
|
||||
$this->processDependencies($dependencies, $mappedItems, false);
|
||||
|
||||
return $mappedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the scanner can handle the given path
|
||||
*/
|
||||
public function canScan(): bool
|
||||
{
|
||||
return file_exists($this->path.'yarn.lock');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user