konsulting/project-root
Resolve the correct root path when developing a Composer package or using it as a dependency. Project Root lets you target a package name and resolve paths relative to the host project, avoiding repeated “dirty” path-detection logic.
base_path()/storage_path() are project-root-centric but packages need to reference the host project’s root (e.g., for CLI tools, config generators, or asset pipelines). Ideal for:
public_path()) from within vendored code.storage/framework/cache) without hardcoding.forPackage()->resolve()) over Composer’s autoloader, reducing cognitive load compared to manual path logic (e.g., dirname(__DIR__, 4))..env keys, service provider bindings, or Laravel-specific setup required. Plug-and-play for path resolution.\Konsulting\ProjectRoot, avoiding conflicts with Laravel’s Path facade or app() helper.| Risk | Impact | Mitigation |
|---|---|---|
| Autoloader Dependency | Fails if Composer’s autoloader is corrupted or custom-repo paths are misconfigured. | Fallback to __DIR__ with deprecation warning; document troubleshooting for symlinked vendors. |
| Laravel-Specific Edge Cases | May conflict with Laravel’s bootstrap/app.php path overrides or APP_BASE_PATH. |
Test with APP_BASE_PATH overrides; prioritize ProjectRoot for dependency-aware paths only. |
| Maintenance Risk | No recent releases (2019) or community activity (0 stars). | Fork and maintain; propose upstream fixes for PHP 8.2+ compatibility. |
| Path Resolution Ambiguity | Multi-level dependencies or custom Composer repos may yield incorrect roots. | Add validation (e.g., check composer.json names) and logging for debugging. |
| Performance | Minimal, but repeated calls could be optimized. | Cache resolved paths in a static property (e.g., static private $cache = [];). |
ProjectRoot be used vs. Laravel’s base_path()?
ProjectRoot only for dependency-aware paths (e.g., vendor/package/storage). Reserve base_path() for project-wide paths.APP_BASE_PATH or bootstrap/app.php?
APP_BASE_PATH; ensure ProjectRoot respects Composer’s autoloader hierarchy.__DIR__ with a deprecation warning; log the failure for debugging.optimize or dump-autoload?
| Component | Compatibility Notes |
|---|---|
| PHP | 7.4+ (Laravel 8+ or standalone). No PHP 8.2+ tests, but likely compatible. |
| Composer | Requires autoload to resolve package roots. Works with any Composer project, including Laravel’s optimized autoloader. |
| Laravel | No core conflicts. Useful for: |
ProjectRoot::forPackage('my-package')->resolve(__DIR__ . '/config')).php artisan my:package-command).base_path() when the package is a dependency. |
| Symfony Components | Compatible (uses PSR-4 autoloading). |base_path('vendor/package/...')__DIR__ or getcwd() hacks in dependency contexts.ProjectRoot.composer install --prefer-dist
composer install --prefer-source
ProjectRoot for dependency-aware paths.ProjectRoot for vendored packages; reserve base_path() for project-wide paths").Illuminate\Support\Facades\Path but does not override it.autoload is up-to-date:
composer dump-autoload --optimize
composer.json:
"require": {
"konsulting/project-root": "^1.1"
}
Run:
composer require konsulting/project-root
$path = base_path('vendor/my-package/storage/logs');
$path = \Konsulting\ProjectRoot::forPackage('my-package')->resolve(__DIR__ . '/storage/logs');
composer test
__DIR__ with warning) if path resolution fails.composer show -v my-package
Verify autoloader paths match expectations.$path = str_replace('\\', '/', $resolvedPath);
realpath() or disable symlinks in Composer).--prefer-source).ProjectRoot for dependency-aware paths; escalate if resolution fails."static private $resolvedPaths = [];
public function resolve($path) {
$cacheKey = $this->packageName . '|' . $path;
How can I help you explore Laravel packages today?