Skip to content

Commit 24071e1

Browse files
committed
scss fallback for upgrading
Signed-off-by: Andy Miller <[email protected]>
1 parent 1ec8fab commit 24071e1

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

admin.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,44 @@ public function getFormFieldTypes()
180180
*/
181181
public function autoload(): ClassLoader
182182
{
183+
// Register a fallback autoloader for vendor dependencies that might be missing during upgrades.
184+
// This helps prevent "class not found" errors when upgrading between versions with different dependencies.
185+
// The fallback reads the autoload maps from the NEW vendor directory on disk.
186+
$vendorDir = __DIR__ . '/vendor';
187+
$psr4File = $vendorDir . '/composer/autoload_psr4.php';
188+
$classmapFile = $vendorDir . '/composer/autoload_classmap.php';
189+
190+
$psr4Map = file_exists($psr4File) ? require $psr4File : [];
191+
$classMap = file_exists($classmapFile) ? require $classmapFile : [];
192+
193+
if ($psr4Map || $classMap) {
194+
spl_autoload_register(function ($class) use ($psr4Map, $classMap) {
195+
// First check classmap for exact class match
196+
if (isset($classMap[$class]) && file_exists($classMap[$class])) {
197+
require_once $classMap[$class];
198+
return true;
199+
}
200+
201+
// Then try PSR-4 namespaces
202+
foreach ($psr4Map as $prefix => $paths) {
203+
$prefixLen = strlen($prefix);
204+
if (strncmp($prefix, $class, $prefixLen) === 0) {
205+
$relativeClass = substr($class, $prefixLen);
206+
$relativePath = str_replace('\\', '/', $relativeClass) . '.php';
207+
208+
foreach ($paths as $path) {
209+
$file = $path . '/' . $relativePath;
210+
if (file_exists($file)) {
211+
require_once $file;
212+
return true;
213+
}
214+
}
215+
}
216+
}
217+
return false;
218+
}, true, false); // prepend=true to run before other autoloaders
219+
}
220+
183221
return require __DIR__ . '/vendor/autoload.php';
184222
}
185223

@@ -1140,13 +1178,17 @@ protected function initializeAdmin()
11401178
return new Themes($this->grav);
11411179
};
11421180

1143-
// Initialize white label functionality
1144-
$this->grav['admin-whitelabel'] = new WhiteLabel();
1181+
// Initialize white label functionality (lazy-loaded to avoid loading scssphp during upgrades)
1182+
$this->grav['admin-whitelabel'] = function () {
1183+
return new WhiteLabel();
1184+
};
11451185

1146-
// Compile a missing preset.css file
1186+
// Compile a missing preset.css file - skip during AJAX task requests to avoid autoloader conflicts during upgrades
1187+
$task = $this->uri->param('task') ?? $this->uri->query('task');
1188+
$isTaskRequest = !empty($task);
11471189
$preset_css = 'asset://admin-preset.css';
11481190
$preset_path = $this->grav['locator']->findResource($preset_css);
1149-
if (!$preset_path) {
1191+
if (!$preset_path && !$isTaskRequest) {
11501192
$this->grav['admin-whitelabel']->compilePresetScss($this->config->get('plugins.admin.whitelabel'));
11511193
}
11521194

classes/plugin/AdminController.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,15 +1323,16 @@ protected function taskInstallDependenciesOfPackages()
13231323
$result = Gpm::install(array_keys($dependencies), ['theme' => $type === 'themes']);
13241324

13251325
if ($result) {
1326-
$this->admin->json_response = ['status' => 'success', 'message' => 'Dependencies installed successfully'];
1326+
$json_response = ['status' => 'success', 'message' => 'Dependencies installed successfully'];
13271327
} else {
1328-
$this->admin->json_response = [
1328+
$json_response = [
13291329
'status' => 'error',
13301330
'message' => $this->admin::translate('PLUGIN_ADMIN.INSTALLATION_FAILED')
13311331
];
13321332
}
13331333

1334-
return true;
1334+
// Exit early to prevent any post-install code from running with potentially mismatched autoloaders
1335+
$this->sendJsonResponse($json_response);
13351336
}
13361337

13371338
/**
@@ -1376,19 +1377,20 @@ protected function taskInstallPackage($reinstall = false)
13761377
}
13771378

13781379
if ($result) {
1379-
$this->admin->json_response = [
1380+
$json_response = [
13801381
'status' => 'success',
13811382
'message' => $this->admin::translate(is_string($result) ? $result : sprintf($this->admin::translate($reinstall ?: 'PLUGIN_ADMIN.PACKAGE_X_REINSTALLED_SUCCESSFULLY',
13821383
null), $package))
13831384
];
13841385
} else {
1385-
$this->admin->json_response = [
1386+
$json_response = [
13861387
'status' => 'error',
13871388
'message' => $this->admin::translate($reinstall ?: 'PLUGIN_ADMIN.INSTALLATION_FAILED')
13881389
];
13891390
}
13901391

1391-
return true;
1392+
// Exit early to prevent any post-install code from running with potentially mismatched autoloaders
1393+
$this->sendJsonResponse($json_response);
13921394
}
13931395

13941396
/**
@@ -1501,19 +1503,20 @@ protected function taskReinstallPackage()
15011503
$result = Gpm::directInstall($url);
15021504

15031505
if ($result === true) {
1504-
$this->admin->json_response = [
1506+
$json_response = [
15051507
'status' => 'success',
15061508
'message' => $this->admin::translate(sprintf($this->admin::translate('PLUGIN_ADMIN.PACKAGE_X_REINSTALLED_SUCCESSFULLY',
15071509
null), $package_name))
15081510
];
15091511
} else {
1510-
$this->admin->json_response = [
1512+
$json_response = [
15111513
'status' => 'error',
15121514
'message' => $this->admin::translate('PLUGIN_ADMIN.REINSTALLATION_FAILED')
15131515
];
15141516
}
15151517

1516-
return true;
1518+
// Exit early to prevent any post-install code from running with potentially mismatched autoloaders
1519+
$this->sendJsonResponse($json_response);
15171520
}
15181521

15191522
/**

classes/plugin/WhiteLabel.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,20 @@ class WhiteLabel
2323
public function __construct()
2424
{
2525
$this->grav = Grav::instance();
26-
$this->scss = new ScssCompiler();
26+
// ScssCompiler is now lazy-loaded to avoid loading scssphp classes until actually needed
27+
}
28+
29+
/**
30+
* Get the ScssCompiler instance (lazy-loaded)
31+
*
32+
* @return ScssCompiler
33+
*/
34+
protected function getScss(): ScssCompiler
35+
{
36+
if ($this->scss === null) {
37+
$this->scss = new ScssCompiler();
38+
}
39+
return $this->scss;
2740
}
2841

2942
public function compilePresetScss($config, $options = [
@@ -61,7 +74,7 @@ public function compilePresetScss($config, $options = [
6174
}
6275

6376
try {
64-
$compiler = $this->scss->reset();
77+
$compiler = $this->getScss()->reset();
6578

6679
$compiler->setVariables($color_scheme['colors'] + $color_scheme['accents']);
6780
$compiler->setImportPaths($imports);

0 commit comments

Comments
 (0)