- Add complete Auth module with login, logout, lock screen functionality - Fix 401 error on error monitoring page by adding /error-monitoring/ to public patterns - Improve lock screen with proper form handling and unlock functionality - Add RoleModel and role management views - Add BaseModel for common model functionality - Update authentication system and middleware - Add troubleshooting documentation - Update assets and build tools
322 lines
10 KiB
PHP
322 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* Modular CSS Build System
|
|
*
|
|
* Compiles SCSS files from all modules into individual CSS files
|
|
* Integrates with the existing npm build system
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../crm/vendor/autoload.php';
|
|
|
|
use App\Core\AssetManager;
|
|
use App\Core\PathManager;
|
|
|
|
class ModularCssBuildSystem
|
|
{
|
|
private string $outputDir;
|
|
private array $compiledFiles = [];
|
|
private array $errors = [];
|
|
|
|
public function __construct()
|
|
{
|
|
$this->outputDir = PathManager::getPublicPath() . '/assets/compiled/modules';
|
|
echo "🎨 Modular CSS Build System - BitReport CRM\n";
|
|
echo "==========================================\n\n";
|
|
}
|
|
|
|
public function build(): bool
|
|
{
|
|
echo "🚀 Starting modular CSS compilation...\n\n";
|
|
|
|
// Initialize AssetManager to discover all module assets
|
|
AssetManager::initialize();
|
|
$assetManager = AssetManager::getInstance();
|
|
|
|
// Get all registered assets
|
|
$registeredAssets = $assetManager->getRegisteredAssets();
|
|
|
|
if (empty($registeredAssets)) {
|
|
echo "⚠️ No module assets found to compile.\n";
|
|
return true;
|
|
}
|
|
|
|
// Create output directory
|
|
if (!is_dir($this->outputDir)) {
|
|
mkdir($this->outputDir, 0755, true);
|
|
}
|
|
|
|
// Compile assets for each module
|
|
foreach ($registeredAssets as $moduleName => $assets) {
|
|
echo "📁 Processing module: {$moduleName}\n";
|
|
$this->compileModuleAssets($moduleName, $assets);
|
|
}
|
|
|
|
// Create symlinks for raw CSS/JS files
|
|
$this->createAssetSymlinks();
|
|
|
|
// Generate build report
|
|
$this->generateBuildReport();
|
|
|
|
if (empty($this->errors)) {
|
|
echo "\n✅ Modular CSS build completed successfully!\n";
|
|
echo "📊 Compiled " . count($this->compiledFiles) . " files\n";
|
|
return true;
|
|
} else {
|
|
echo "\n❌ Build completed with errors:\n";
|
|
foreach ($this->errors as $error) {
|
|
echo " • {$error}\n";
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function compileModuleAssets(string $moduleName, array $assets): void
|
|
{
|
|
$moduleOutputDir = $this->outputDir . '/' . $moduleName;
|
|
|
|
// Create module output directories
|
|
if (!is_dir($moduleOutputDir . '/css')) {
|
|
mkdir($moduleOutputDir . '/css', 0755, true);
|
|
}
|
|
if (!is_dir($moduleOutputDir . '/js')) {
|
|
mkdir($moduleOutputDir . '/js', 0755, true);
|
|
}
|
|
|
|
// Compile SCSS files
|
|
if (isset($assets['scss'])) {
|
|
foreach ($assets['scss'] as $scss) {
|
|
if (isset($scss['compile']) && $scss['compile']) {
|
|
$this->compileScssFile($scss, $moduleName);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Copy CSS files
|
|
if (isset($assets['css'])) {
|
|
foreach ($assets['css'] as $css) {
|
|
$this->copyCssFile($css, $moduleName);
|
|
}
|
|
}
|
|
|
|
// Copy JS files
|
|
if (isset($assets['js'])) {
|
|
foreach ($assets['js'] as $js) {
|
|
$this->copyJsFile($js, $moduleName);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function compileScssFile(array $scss, string $moduleName): void
|
|
{
|
|
$inputPath = $scss['path'];
|
|
$filename = basename($inputPath, '.scss') . '.css';
|
|
$outputPath = $this->outputDir . '/' . $moduleName . '/css/' . $filename;
|
|
|
|
if (!file_exists($inputPath)) {
|
|
$this->errors[] = "SCSS file not found: {$inputPath}";
|
|
return;
|
|
}
|
|
|
|
echo " 🔨 Compiling: {$scss['name']}\n";
|
|
|
|
// Use sass command to compile
|
|
$command = "npx sass --no-source-map \"{$inputPath}\" \"{$outputPath}\" 2>&1";
|
|
$result = shell_exec($command);
|
|
|
|
if (!file_exists($outputPath)) {
|
|
$this->errors[] = "SCSS compilation failed for {$inputPath}: {$result}";
|
|
return;
|
|
}
|
|
|
|
$this->compiledFiles[] = [
|
|
'type' => 'scss',
|
|
'module' => $moduleName,
|
|
'input' => $inputPath,
|
|
'output' => $outputPath,
|
|
'size' => filesize($outputPath),
|
|
];
|
|
|
|
echo " ✅ Compiled to: {$filename}\n";
|
|
}
|
|
|
|
private function copyCssFile(array $css, string $moduleName): void
|
|
{
|
|
if (!isset($css['path'])) {
|
|
return; // Skip if no source path
|
|
}
|
|
|
|
$inputPath = $css['path'];
|
|
$filename = $css['name'];
|
|
$outputPath = $this->outputDir . '/' . $moduleName . '/css/' . $filename;
|
|
|
|
if (!file_exists($inputPath)) {
|
|
$this->errors[] = "CSS file not found: {$inputPath}";
|
|
return;
|
|
}
|
|
|
|
echo " 📄 Copying: {$filename}\n";
|
|
|
|
if (copy($inputPath, $outputPath)) {
|
|
$this->compiledFiles[] = [
|
|
'type' => 'css',
|
|
'module' => $moduleName,
|
|
'input' => $inputPath,
|
|
'output' => $outputPath,
|
|
'size' => filesize($outputPath),
|
|
];
|
|
echo " ✅ Copied to: {$filename}\n";
|
|
} else {
|
|
$this->errors[] = "Failed to copy CSS file: {$inputPath}";
|
|
}
|
|
}
|
|
|
|
private function copyJsFile(array $js, string $moduleName): void
|
|
{
|
|
if (!isset($js['path'])) {
|
|
return; // Skip if no source path
|
|
}
|
|
|
|
$inputPath = $js['path'];
|
|
$filename = $js['name'];
|
|
$outputPath = $this->outputDir . '/' . $moduleName . '/js/' . $filename;
|
|
|
|
if (!file_exists($inputPath)) {
|
|
$this->errors[] = "JS file not found: {$inputPath}";
|
|
return;
|
|
}
|
|
|
|
echo " 📜 Copying: {$filename}\n";
|
|
|
|
if (copy($inputPath, $outputPath)) {
|
|
$this->compiledFiles[] = [
|
|
'type' => 'js',
|
|
'module' => $moduleName,
|
|
'input' => $inputPath,
|
|
'output' => $outputPath,
|
|
'size' => filesize($outputPath),
|
|
];
|
|
echo " ✅ Copied to: {$filename}\n";
|
|
} else {
|
|
$this->errors[] = "Failed to copy JS file: {$inputPath}";
|
|
}
|
|
}
|
|
|
|
private function createAssetSymlinks(): void
|
|
{
|
|
echo "\n🔗 Creating asset symlinks...\n";
|
|
|
|
$modulesPath = PathManager::getModulesPath();
|
|
$publicAssetsPath = PathManager::getPublicPath() . '/assets/modules';
|
|
|
|
if (!is_dir($publicAssetsPath)) {
|
|
mkdir($publicAssetsPath, 0755, true);
|
|
}
|
|
|
|
$modules = scandir($modulesPath);
|
|
|
|
foreach ($modules as $module) {
|
|
if ($module === '.' || $module === '..') {
|
|
continue;
|
|
}
|
|
|
|
$moduleAssetsPath = $modulesPath . '/' . $module . '/assets';
|
|
$modulePublicPath = $publicAssetsPath . '/' . $module;
|
|
|
|
if (is_dir($moduleAssetsPath) && !is_link($modulePublicPath)) {
|
|
if (symlink($moduleAssetsPath, $modulePublicPath)) {
|
|
echo " 🔗 Created symlink: modules/{$module}\n";
|
|
} else {
|
|
$this->errors[] = "Failed to create symlink for module: {$module}";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function generateBuildReport(): void
|
|
{
|
|
$reportPath = PathManager::getStoragePath() . '/logs/modular-css-build-' . date('Y-m-d-H-i-s') . '.json';
|
|
|
|
$report = [
|
|
'timestamp' => date('c'),
|
|
'compiled_files' => $this->compiledFiles,
|
|
'errors' => $this->errors,
|
|
'summary' => [
|
|
'total_files' => count($this->compiledFiles),
|
|
'total_size' => array_sum(array_column($this->compiledFiles, 'size')),
|
|
'modules_processed' => count(array_unique(array_column($this->compiledFiles, 'module'))),
|
|
'errors_count' => count($this->errors),
|
|
],
|
|
];
|
|
|
|
if (!is_dir(dirname($reportPath))) {
|
|
mkdir(dirname($reportPath), 0755, true);
|
|
}
|
|
|
|
file_put_contents($reportPath, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
echo "\n📊 Build report saved: " . basename($reportPath) . "\n";
|
|
}
|
|
|
|
public function clean(): void
|
|
{
|
|
echo "🧹 Cleaning compiled assets...\n";
|
|
|
|
if (is_dir($this->outputDir)) {
|
|
$this->removeDirectory($this->outputDir);
|
|
echo "✅ Cleaned: {$this->outputDir}\n";
|
|
}
|
|
|
|
$publicAssetsPath = PathManager::getPublicPath() . '/assets/modules';
|
|
if (is_dir($publicAssetsPath)) {
|
|
$this->removeDirectory($publicAssetsPath);
|
|
echo "✅ Cleaned: {$publicAssetsPath}\n";
|
|
}
|
|
}
|
|
|
|
private function removeDirectory(string $dir): void
|
|
{
|
|
if (!is_dir($dir)) {
|
|
return;
|
|
}
|
|
|
|
$files = array_diff(scandir($dir), ['.', '..']);
|
|
|
|
foreach ($files as $file) {
|
|
$path = $dir . '/' . $file;
|
|
if (is_dir($path)) {
|
|
$this->removeDirectory($path);
|
|
} else {
|
|
unlink($path);
|
|
}
|
|
}
|
|
|
|
rmdir($dir);
|
|
}
|
|
}
|
|
|
|
// Command line interface
|
|
if (php_sapi_name() === 'cli') {
|
|
$builder = new ModularCssBuildSystem();
|
|
|
|
$command = $argv[1] ?? 'build';
|
|
|
|
switch ($command) {
|
|
case 'build':
|
|
$success = $builder->build();
|
|
exit($success ? 0 : 1);
|
|
|
|
case 'clean':
|
|
$builder->clean();
|
|
exit(0);
|
|
|
|
case 'rebuild':
|
|
$builder->clean();
|
|
$success = $builder->build();
|
|
exit($success ? 0 : 1);
|
|
|
|
default:
|
|
echo "Usage: php build-modular-css.php [build|clean|rebuild]\n";
|
|
exit(1);
|
|
}
|
|
}
|