MagratheaPHP2
Up to date Narrative last written —; no source changes since. Method signatures below are reflected live.

Compressors — CSS & JavaScript Compression

Files: src/Compressors/MagratheaCompressor.php, src/Compressors/CssCompressor.php, src/Compressors/JavascriptCompressor.php Namespace: Magrathea2

File bundling and minification utilities for CSS and JavaScript assets. Used primarily by the Admin panel but available to application code as well.


Base: MagratheaCompressor

Abstract base class providing the common file management interface.

Methods

AddFile(string $file): static

Register a file to be bundled.

$compressor->AddFile("/path/to/style.css");

GetFiles(): array

Returns all registered file paths.

GetOutput(): string

Combine and compress all registered files, returning the result as a string.


CssCompressor

Bundles and minifies CSS files using scssphp (supports both SCSS and plain CSS).

Usage

use Magrathea2\Compressors\CssCompressor;

$css = new CssCompressor();
$css->AddFile("/assets/bootstrap.css")
    ->AddFile("/assets/app.scss");

echo "<style>" . $css->GetOutput() . "</style>";

SCSS Support

Any .scss files are automatically compiled to CSS via scssphp/scssphp before minification.


JavascriptCompressor

Bundles and minifies JavaScript files using tedivm/jshrink.

Usage

use Magrathea2\Compressors\JavascriptCompressor;

$js = new JavascriptCompressor();
$js->AddFile("/assets/jquery.min.js")
   ->AddFile("/assets/app.js");

echo "<script>" . $js->GetOutput() . "</script>";

Admin Panel Usage

The Admin panel uses both compressors internally via AdminManager:

use Magrathea2\Admin\AdminManager;

$manager = AdminManager::Instance();

// Add custom JS
$manager->AddJs("/my-feature/custom.js");

// Add custom CSS
$manager->AddCss("/my-feature/custom.css");

// Output in templates
echo $manager->GetJs();  // all JS bundled & minified
echo $manager->GetCss(); // all CSS bundled & minified

Example: Asset Pipeline for a Custom Page

use Magrathea2\Compressors\CssCompressor;
use Magrathea2\Compressors\JavascriptCompressor;

$appRoot = MagratheaPHP::Instance()->GetAppRoot();

$css = new CssCompressor();
$css->AddFile($appRoot . "assets/vendor/normalize.css")
    ->AddFile($appRoot . "assets/app/main.scss");

$js = new JavascriptCompressor();
$js->AddFile($appRoot . "assets/vendor/htmx.min.js")
   ->AddFile($appRoot . "assets/app/main.js");
?>
<!DOCTYPE html>
<html>
<head>
    <style><?= $css->GetOutput() ?></style>
</head>
<body>
    <!-- page content -->
    <script><?= $js->GetOutput() ?></script>
</body>
</html>

Notes

Caching Compressed Output

$cacheFile = "/tmp/bundle.min.css";

if (!file_exists($cacheFile)) {
    $css = new CssCompressor();
    $css->AddFile("/assets/app.scss");
    file_put_contents($cacheFile, $css->GetOutput());
}

echo file_get_contents($cacheFile);

Class Reference — CssCompressor

Magrathea2\Compressors\CssCompressor extends MagratheaCompressor /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Compressors/CssCompressor.php
GetMinCode(): string

return minified code

Class Reference — JavascriptCompressor

Magrathea2\Compressors\JavascriptCompressor extends MagratheaCompressor /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Compressors/JavascriptCompressor.php
GetMinCode(): string

return minified code

Class Reference — MagratheaCompressor

Magrathea2\Compressors\MagratheaCompressor /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Compressors/MagratheaCompressor.php
AddArray(array $fs): Magrathea2\Compressors\MagratheaCompressor

add an array of file paths

ParamTypeDefault
$fs array required
AddFile(string $file): Magrathea2\Compressors\MagratheaCompressor

add a file path

ParamTypeDefault
$file string required
GetCode(): string

return minified code

GetMinCode(): string

return minified code

GetRawCode(): string

Get the raw code from all the files