Files
MoneyFlix/crm/Core/PathManager.php
T
XikaandCursor a5e2e0623b fix(crm): resolve PathManager root for MoneyFlix standalone clone
findRootPath() fell back to /opt/bitreport when composer.json lives
under crm/ with Modules/, breaking auth layout resolution and /login 500.

Co-authored-by: Cursor <[email protected]>
2026-06-10 17:54:57 +00:00

548 lines
13 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Core;
class PathManager
{
private static ?string $rootPath = null;
private static ?string $appPath = null;
private static ?string $modulesPath = null;
private static ?string $storagePath = null;
private static ?string $publicPath = null;
private static ?string $vendorPath = null;
private static ?string $configPath = null;
private static ?string $docsPath = null;
private static ?string $toolsPath = null;
private static ?string $binPath = null;
/**
* Initialize all paths
*/
public static function initialize(): void
{
if (self::$rootPath !== null) {
return; // Already initialized
}
// Get the root path (where composer.json is located)
self::$rootPath = self::findRootPath();
// Define all other paths relative to root
// Check if we're in the crm structure or app structure
if (is_dir(self::$rootPath . '/crm')) {
// CRM structure: root/crm/
self::$appPath = self::$rootPath . '/crm';
} else {
// App structure: root/app/
self::$appPath = self::$rootPath . '/app';
}
self::$modulesPath = self::$appPath . '/Modules';
self::$storagePath = self::$rootPath . '/storage';
self::$publicPath = self::$rootPath . '/public';
self::$vendorPath = self::$rootPath . '/vendor';
self::$configPath = self::$rootPath . '/config';
self::$docsPath = self::$rootPath . '/docs';
self::$toolsPath = self::$rootPath . '/tools';
self::$binPath = self::$rootPath . '/bin';
}
/**
* Find the project root path by looking for composer.json
*/
private static function findRootPath(): string
{
$currentPath = __DIR__;
while ($currentPath !== '/' && $currentPath !== '') {
if (!file_exists($currentPath . '/composer.json')) {
$currentPath = dirname($currentPath);
continue;
}
// Standard monorepo (CRM_Project): composer.json at repo root + app/
if (is_dir($currentPath . '/app')) {
return $currentPath;
}
// Standalone CRM clone (MoneyFlix): composer.json lives in crm/ with Modules/
if (is_dir($currentPath . '/Modules')) {
$parentPath = dirname($currentPath);
if ($parentPath !== $currentPath && is_dir($parentPath . '/storage')) {
return $parentPath;
}
return $currentPath;
}
$currentPath = dirname($currentPath);
}
return dirname(__DIR__, 2);
}
/**
* Get project root path
*/
public static function getRootPath(): string
{
self::initialize();
return self::$rootPath;
}
/**
* Get app directory path
*/
public static function getAppPath(): string
{
self::initialize();
return self::$appPath;
}
/**
* Get modules directory path
*/
public static function getModulesPath(): string
{
self::initialize();
return self::$modulesPath;
}
/**
* Get storage directory path
*/
public static function getStoragePath(): string
{
self::initialize();
return self::$storagePath;
}
/**
* Get public directory path
*/
public static function getPublicPath(): string
{
self::initialize();
return self::$publicPath;
}
/**
* Get vendor directory path
*/
public static function getVendorPath(): string
{
self::initialize();
return self::$vendorPath;
}
/**
* Get config directory path
*/
public static function getConfigPath(): string
{
self::initialize();
return self::$configPath;
}
/**
* Get docs directory path
*/
public static function getDocsPath(): string
{
self::initialize();
return self::$docsPath;
}
/**
* Get tools directory path
*/
public static function getToolsPath(): string
{
self::initialize();
return self::$toolsPath;
}
/**
* Get bin directory path
*/
public static function getBinPath(): string
{
self::initialize();
return self::$binPath;
}
/**
* Get module path by name
*/
public static function getModulePath(string $moduleName): string
{
return self::getModulesPath() . '/' . $moduleName;
}
/**
* Get module migrations path
*/
public static function getModuleMigrationsPath(string $moduleName): string
{
return self::getModulePath($moduleName) . '/Database/Migrations';
}
/**
* Get module views path
*/
public static function getModuleViewsPath(string $moduleName): string
{
return self::getModulePath($moduleName) . '/Views';
}
/**
* Get module controllers path
*/
public static function getModuleControllersPath(string $moduleName): string
{
return self::getModulePath($moduleName) . '/Controllers';
}
/**
* Get module models path
*/
public static function getModuleModelsPath(string $moduleName): string
{
return self::getModulePath($moduleName) . '/Models';
}
/**
* Get module routes path
*/
public static function getModuleRoutesPath(string $moduleName): string
{
return self::getModulePath($moduleName) . '/Routes';
}
/**
* Get storage logs path
*/
public static function getLogsPath(): string
{
return self::getStoragePath() . '/logs';
}
/**
* Get storage cache path
*/
public static function getCachePath(): string
{
return self::getStoragePath() . '/cache';
}
/**
* Get public assets path
*/
public static function getAssetsPath(): string
{
return self::getPublicPath() . '/assets';
}
/**
* Get public assets CSS path
*/
public static function getAssetsCssPath(): string
{
return self::getAssetsPath() . '/css';
}
/**
* Get public assets JS path
*/
public static function getAssetsJsPath(): string
{
return self::getAssetsPath() . '/js';
}
/**
* Get public assets images path
*/
public static function getAssetsImagesPath(): string
{
return self::getAssetsPath() . '/images';
}
/**
* Get src directory path
*/
public static function getSrcPath(): string
{
return self::getRootPath() . '/src';
}
/**
* Get src JS path
*/
public static function getSrcJsPath(): string
{
return self::getSrcPath() . '/js';
}
/**
* Get src JS Alpine path
*/
public static function getSrcJsAlpinePath(): string
{
return self::getSrcJsPath() . '/alpine';
}
/**
* Get src JS components path
*/
public static function getSrcJsComponentsPath(): string
{
return self::getSrcJsPath() . '/components';
}
/**
* Get src JS pages path
*/
public static function getSrcJsPagesPath(): string
{
return self::getSrcJsPath() . '/pages';
}
/**
* Get src JS modules path
*/
public static function getSrcJsModulesPath(): string
{
return self::getSrcJsPath() . '/modules';
}
/**
* Get src JS utils path
*/
public static function getSrcJsUtilsPath(): string
{
return self::getSrcJsPath() . '/utils';
}
/**
* Get projects directory path
*/
public static function getProjectsPath(): string
{
return self::getRootPath() . '/projects';
}
/**
* Get template directory path
*/
public static function getTemplatePath(): string
{
return self::getRootPath() . '/template';
}
/**
* Get tests directory path
*/
public static function getTestsPath(): string
{
return self::getRootPath() . '/tests';
}
/**
* Get venv directory path
*/
public static function getVenvPath(): string
{
return self::getRootPath() . '/venv_pdu';
}
/**
* Check if path exists
*/
public static function pathExists(string $path): bool
{
return file_exists($path);
}
/**
* Check if path is directory
*/
public static function isDirectory(string $path): bool
{
return is_dir($path);
}
/**
* Check if path is file
*/
public static function isFile(string $path): bool
{
return is_file($path);
}
/**
* Create directory if it doesn't exist
*/
public static function ensureDirectoryExists(string $path): bool
{
if (!self::pathExists($path)) {
return mkdir($path, 0755, true);
}
return true;
}
/**
* Get all module names
*/
public static function getModuleNames(): array
{
$modulesPath = self::getModulesPath();
$modules = [];
if (self::isDirectory($modulesPath)) {
$items = scandir($modulesPath);
foreach ($items as $item) {
if ($item !== '.' && $item !== '..' && self::isDirectory($modulesPath . '/' . $item)) {
$modules[] = $item;
}
}
}
return $modules;
}
/**
* Get all migration files from all modules
*/
public static function getAllMigrationFiles(): array
{
$files = [];
$modules = self::getModuleNames();
foreach ($modules as $module) {
$migrationsPath = self::getModuleMigrationsPath($module);
if (self::isDirectory($migrationsPath)) {
$moduleFiles = glob($migrationsPath . '/*.php');
$files = array_merge($files, $moduleFiles);
}
}
// Remove duplicates and sort
$files = array_unique($files);
sort($files);
return $files;
}
/**
* Get relative path from root
*/
public static function getRelativePath(string $absolutePath): string
{
$rootPath = self::getRootPath();
if (strpos($absolutePath, $rootPath) === 0) {
return substr($absolutePath, strlen($rootPath) + 1);
}
return $absolutePath;
}
/**
* Get absolute path from relative path
*/
public static function getAbsolutePath(string $relativePath): string
{
if (self::isAbsolutePath($relativePath)) {
return $relativePath;
}
return self::getRootPath() . '/' . ltrim($relativePath, '/');
}
/**
* Check if path is absolute
*/
public static function isAbsolutePath(string $path): bool
{
return $path[0] === '/' || (strlen($path) > 1 && $path[1] === ':');
}
/**
* Normalize path (remove double slashes, etc.)
*/
public static function normalizePath(string $path): string
{
return realpath($path) ?: $path;
}
/**
* Get file extension
*/
public static function getFileExtension(string $path): string
{
return pathinfo($path, PATHINFO_EXTENSION);
}
/**
* Get file name without extension
*/
public static function getFileNameWithoutExtension(string $path): string
{
return pathinfo($path, PATHINFO_FILENAME);
}
/**
* Get file name with extension
*/
public static function getFileName(string $path): string
{
return pathinfo($path, PATHINFO_BASENAME);
}
/**
* Get directory name
*/
public static function getDirectoryName(string $path): string
{
return pathinfo($path, PATHINFO_DIRNAME);
}
/**
* Debug: Print all paths
*/
public static function debugPaths(): void
{
self::initialize();
echo "🔍 PathManager Debug Information:\n";
echo "================================\n";
echo "Root Path: " . self::$rootPath . "\n";
echo "App Path: " . self::$appPath . "\n";
echo "Modules Path: " . self::$modulesPath . "\n";
echo "Storage Path: " . self::$storagePath . "\n";
echo "Public Path: " . self::$publicPath . "\n";
echo "Vendor Path: " . self::$vendorPath . "\n";
echo "Config Path: " . self::$configPath . "\n";
echo "Docs Path: " . self::$docsPath . "\n";
echo "Tools Path: " . self::$toolsPath . "\n";
echo "Bin Path: " . self::$binPath . "\n";
echo "Src Path: " . self::getSrcPath() . "\n";
echo "Projects Path: " . self::getProjectsPath() . "\n";
echo "Template Path: " . self::getTemplatePath() . "\n";
echo "Tests Path: " . self::getTestsPath() . "\n";
echo "Venv Path: " . self::getVenvPath() . "\n";
echo "\n";
echo "📁 Available Modules:\n";
$modules = self::getModuleNames();
foreach ($modules as $module) {
echo " - $module\n";
}
echo "\n";
echo "📄 Migration Files Found: " . count(self::getAllMigrationFiles()) . "\n";
}
}