sabberworm/php-css-parser
Parse and manipulate CSS in PHP with a fast, flexible parser. Convert CSS into an object model, inspect and edit rules, selectors, and declarations, then render back to CSS. Useful for minifying, rewriting assets, or building CSS tooling.
composer require sabberworm/php-css-parser.CssParser::parse($css)).Css::transform($rules)) for consistency with Laravel’s conventions.view.rendered or assets.processed events.| Risk Area | Mitigation Strategy |
|---|---|
| Performance Overhead | Benchmark parsing large CSS files; use withMultibyteSupport(false) for speed. |
| Strict Parsing | Default to non-strict mode unless validating CSS syntax rigorously. |
| Output Formatting | Predefine OutputFormat instances (e.g., compact() for production). |
| Backward Compatibility | Test against legacy CSS (e.g., IE hacks like filter: alpha()). |
| Memory Usage | Stream large CSS files via fopen() + fread() instead of file_get_contents(). |
font-* rules") or programmatic (e.g., "modify all em units")?| Laravel Component | Integration Strategy |
|---|---|
| Blade Templates | Use {{ Css::transform($css, $rules) }} to inject dynamic styles. |
| Laravel Mix/Vite | Add a postcss plugin to pipe CSS through the parser before minification. |
| Livewire/Alpine | Dynamically update CSS classes via Css::addSelector($element, $styles). |
| API Routes | Expose an endpoint (/api/css/transform) for client-side CSS customization. |
| Service Containers | Bind the parser to Laravel’s container for dependency injection. |
OutputFormat::createCompact().resources/css/app.css via a build script.{{ Css::scope($componentId) }} wraps component styles with a unique ID./css/transform endpoint for frontend apps to send CSS for server-side processing.{ "css": "...", "rules": { "remove": ["font-*"] } }.calc(), @supports, and custom properties).Settings::create()->beLenient().// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(\Sabberworm\CSS\Parser::class, function () {
return new \Sabberworm\CSS\Parser('', \Sabberworm\CSS\Settings::create());
});
}
// app/Facades/Css.php
public static function transform(string $css, array $rules): string {
$parser = app(\Sabberworm\CSS\Parser::class);
$document = $parser->parse($css);
// Apply $rules (e.g., removeRule('font-'))
return $document->render();
}
@inject('css', 'App\Facades\Css')
<style>{{ $css->transform($dynamicCss, ['remove' => ['debug-*']]) }}</style>
Settings (e.g., charset, strict mode) in a config file (config/css.php).CssRules::removeUnused(), CssRules::scopeComponent()).var_dump($document) to inspect parsed structures (add a debug() facade method).try-catch around parse() with Sentry).php artisan css:scope ComponentName # Auto-scopes a component’s CSS
php artisan css:minify # Optimizes all CSS files
CssTransformationJob).$cacheKey = md5($css);
return Cache::remember($cacheKey, now()->addHours(1), function () use ($css) {
return app(\Sabberworm\CSS\Parser::class)->parse($css);
});
| Scenario | Mitigation |
|---|---|
| Malformed CSS | Fallback to original CSS if parsing fails (wrap in try-catch). |
| Memory Limits | Stream large files or split into chunks. |
| Race Conditions | Use Laravel’s cache locks for concurrent transformations. |
| Output Corruption | Validate rendered CSS against original (e.g., assertEquals($original, $rendered)). |
CSS_TRANSFORMATIONS.md with examples (e.g., "How to scope a component").css:scope, css:minify).memory_get_usage() for large files.How can I help you explore Laravel packages today?