fixes for scss and autoloading

Signed-off-by: Andy Miller <rhuk@mac.com>
This commit is contained in:
Andy Miller
2025-12-29 02:59:32 -07:00
parent 6c0349ac4c
commit c5cb89338e
3 changed files with 73 additions and 15 deletions

View File

@@ -180,6 +180,44 @@ class AdminPlugin extends Plugin
*/
public function autoload(): ClassLoader
{
// Register a fallback autoloader for vendor dependencies that might be missing during upgrades.
// This helps prevent "class not found" errors when upgrading between versions with different dependencies.
// The fallback reads the autoload maps from the NEW vendor directory on disk.
$vendorDir = __DIR__ . '/vendor';
$psr4File = $vendorDir . '/composer/autoload_psr4.php';
$classmapFile = $vendorDir . '/composer/autoload_classmap.php';
$psr4Map = file_exists($psr4File) ? require $psr4File : [];
$classMap = file_exists($classmapFile) ? require $classmapFile : [];
if ($psr4Map || $classMap) {
spl_autoload_register(function ($class) use ($psr4Map, $classMap) {
// First check classmap for exact class match
if (isset($classMap[$class]) && file_exists($classMap[$class])) {
require_once $classMap[$class];
return true;
}
// Then try PSR-4 namespaces
foreach ($psr4Map as $prefix => $paths) {
$prefixLen = strlen($prefix);
if (strncmp($prefix, $class, $prefixLen) === 0) {
$relativeClass = substr($class, $prefixLen);
$relativePath = str_replace('\\', '/', $relativeClass) . '.php';
foreach ($paths as $path) {
$file = $path . '/' . $relativePath;
if (file_exists($file)) {
require_once $file;
return true;
}
}
}
}
return false;
}, true, false); // prepend=true to run before other autoloaders
}
return require __DIR__ . '/vendor/autoload.php';
}
@@ -1137,13 +1175,17 @@ class AdminPlugin extends Plugin
return new Themes($this->grav);
};
// Initialize white label functionality
$this->grav['admin-whitelabel'] = new WhiteLabel();
// Initialize white label functionality (lazy-loaded to avoid loading scssphp during upgrades)
$this->grav['admin-whitelabel'] = function () {
return new WhiteLabel();
};
// Compile a missing preset.css file
// Compile a missing preset.css file - skip during AJAX task requests to avoid autoloader conflicts during upgrades
$task = $this->uri->param('task') ?? $this->uri->query('task');
$isTaskRequest = !empty($task);
$preset_css = 'asset://admin-preset.css';
$preset_path = $this->grav['locator']->findResource($preset_css);
if (!$preset_path) {
if (!$preset_path && !$isTaskRequest) {
$this->grav['admin-whitelabel']->compilePresetScss($this->config->get('plugins.admin.whitelabel'));
}

View File

@@ -1323,15 +1323,16 @@ class AdminController extends AdminBaseController
$result = Gpm::install(array_keys($dependencies), ['theme' => $type === 'themes']);
if ($result) {
$this->admin->json_response = ['status' => 'success', 'message' => 'Dependencies installed successfully'];
$json_response = ['status' => 'success', 'message' => 'Dependencies installed successfully'];
} else {
$this->admin->json_response = [
$json_response = [
'status' => 'error',
'message' => $this->admin::translate('PLUGIN_ADMIN.INSTALLATION_FAILED')
];
}
return true;
// Exit early to prevent any post-install code from running with potentially mismatched autoloaders
$this->sendJsonResponse($json_response);
}
/**
@@ -1376,19 +1377,20 @@ class AdminController extends AdminBaseController
}
if ($result) {
$this->admin->json_response = [
$json_response = [
'status' => 'success',
'message' => $this->admin::translate(is_string($result) ? $result : sprintf($this->admin::translate($reinstall ?: 'PLUGIN_ADMIN.PACKAGE_X_REINSTALLED_SUCCESSFULLY',
null), $package))
];
} else {
$this->admin->json_response = [
$json_response = [
'status' => 'error',
'message' => $this->admin::translate($reinstall ?: 'PLUGIN_ADMIN.INSTALLATION_FAILED')
];
}
return true;
// Exit early to prevent any post-install code from running with potentially mismatched autoloaders
$this->sendJsonResponse($json_response);
}
/**
@@ -1501,19 +1503,20 @@ class AdminController extends AdminBaseController
$result = Gpm::directInstall($url);
if ($result === true) {
$this->admin->json_response = [
$json_response = [
'status' => 'success',
'message' => $this->admin::translate(sprintf($this->admin::translate('PLUGIN_ADMIN.PACKAGE_X_REINSTALLED_SUCCESSFULLY',
null), $package_name))
];
} else {
$this->admin->json_response = [
$json_response = [
'status' => 'error',
'message' => $this->admin::translate('PLUGIN_ADMIN.REINSTALLATION_FAILED')
];
}
return true;
// Exit early to prevent any post-install code from running with potentially mismatched autoloaders
$this->sendJsonResponse($json_response);
}
/**

View File

@@ -23,7 +23,20 @@ class WhiteLabel
public function __construct()
{
$this->grav = Grav::instance();
$this->scss = new ScssCompiler();
// ScssCompiler is now lazy-loaded to avoid loading scssphp classes until actually needed
}
/**
* Get the ScssCompiler instance (lazy-loaded)
*
* @return ScssCompiler
*/
protected function getScss(): ScssCompiler
{
if ($this->scss === null) {
$this->scss = new ScssCompiler();
}
return $this->scss;
}
public function compilePresetScss($config, $options = [
@@ -61,7 +74,7 @@ class WhiteLabel
}
try {
$compiler = $this->scss->reset();
$compiler = $this->getScss()->reset();
$compiler->setVariables($color_scheme['colors'] + $color_scheme['accents']);
$compiler->setImportPaths($imports);