|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OffloadProject\Mandate\Commands\Concerns; |
| 6 | + |
| 7 | +use Illuminate\Support\Str; |
| 8 | + |
| 9 | +/** |
| 10 | + * Provides shared functionality for resolving configured paths to namespaces. |
| 11 | + * |
| 12 | + * Used by generator commands to respect the paths defined in config('mandate.code_first.paths'). |
| 13 | + */ |
| 14 | +trait ResolvesConfiguredPaths |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Convert a filesystem path to a PSR-4 namespace. |
| 18 | + * |
| 19 | + * Handles paths both inside and outside the app directory by checking |
| 20 | + * the application's PSR-4 autoload configuration. |
| 21 | + */ |
| 22 | + protected function pathToNamespace(string $path): string |
| 23 | + { |
| 24 | + $path = rtrim($path, DIRECTORY_SEPARATOR); |
| 25 | + |
| 26 | + // Try app directory first (most common case) |
| 27 | + $appPath = $this->laravel->basePath('app'); |
| 28 | + |
| 29 | + if (str_starts_with($path, $appPath)) { |
| 30 | + $relativePath = mb_substr($path, mb_strlen($appPath)); |
| 31 | + $relativePath = trim($relativePath, DIRECTORY_SEPARATOR); |
| 32 | + $namespace = str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath); |
| 33 | + |
| 34 | + return trim($this->laravel->getNamespace().$namespace, '\\'); |
| 35 | + } |
| 36 | + |
| 37 | + // For paths outside app directory, try to resolve from composer autoload |
| 38 | + $basePath = $this->laravel->basePath(); |
| 39 | + |
| 40 | + if (str_starts_with($path, $basePath)) { |
| 41 | + $relativePath = mb_substr($path, mb_strlen($basePath)); |
| 42 | + $relativePath = trim($relativePath, DIRECTORY_SEPARATOR); |
| 43 | + |
| 44 | + // Check composer.json for PSR-4 mappings |
| 45 | + $namespace = $this->resolveNamespaceFromComposer($relativePath); |
| 46 | + |
| 47 | + if ($namespace !== null) { |
| 48 | + return $namespace; |
| 49 | + } |
| 50 | + |
| 51 | + // Fall back to converting path directly (e.g., src/Permissions -> Src\Permissions) |
| 52 | + return str_replace(DIRECTORY_SEPARATOR, '\\', Str::studly($relativePath)); |
| 53 | + } |
| 54 | + |
| 55 | + // Absolute path outside project - use the path segments as namespace |
| 56 | + $segments = explode(DIRECTORY_SEPARATOR, $path); |
| 57 | + $relevantSegments = array_slice($segments, -2); // Take last 2 segments |
| 58 | + |
| 59 | + return implode('\\', array_map([Str::class, 'studly'], $relevantSegments)); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Attempt to resolve namespace from composer.json PSR-4 autoload config. |
| 64 | + */ |
| 65 | + protected function resolveNamespaceFromComposer(string $relativePath): ?string |
| 66 | + { |
| 67 | + $composerPath = $this->laravel->basePath('composer.json'); |
| 68 | + |
| 69 | + if (! file_exists($composerPath)) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + $contents = file_get_contents($composerPath); |
| 74 | + |
| 75 | + if ($contents === false) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + $composer = json_decode($contents, true); |
| 80 | + $autoload = $composer['autoload']['psr-4'] ?? []; |
| 81 | + |
| 82 | + foreach ($autoload as $namespace => $paths) { |
| 83 | + $paths = (array) $paths; |
| 84 | + |
| 85 | + foreach ($paths as $autoloadPath) { |
| 86 | + $autoloadPath = trim($autoloadPath, '/'); |
| 87 | + |
| 88 | + if (str_starts_with($relativePath, $autoloadPath)) { |
| 89 | + $remainder = mb_substr($relativePath, mb_strlen($autoloadPath)); |
| 90 | + $remainder = trim($remainder, DIRECTORY_SEPARATOR); |
| 91 | + $namespaceSuffix = str_replace(DIRECTORY_SEPARATOR, '\\', $remainder); |
| 92 | + |
| 93 | + return trim($namespace.$namespaceSuffix, '\\'); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return null; |
| 99 | + } |
| 100 | +} |
0 commit comments