Installation:
composer require alex198710/csstidy:dev-master
Note: The package is in dev-master state; ensure compatibility with your project’s PHP version (≥5.3.2).
First Use Case:
use Alex198710\Csstidy\Csstidy;
$css = new Csstidy();
$css->parse('a { color: black; }');
echo $css->print->formatted(); // Outputs formatted CSS
Where to Look First:
Csstidy class methods (e.g., parse(), print->formatted()).set_cfg() for options like remove_last_; (removes trailing semicolons).reformat_selectors, remove_comments).Parsing & Processing:
$css = new Csstidy();
$css->parse($rawCss); // Parse raw CSS string
$css->set_cfg('reformat_selectors', true); // Configure rules
$cleanCss = $css->print->formatted(); // Get formatted output
Integration with Laravel:
// app/Providers/CsstidyServiceProvider.php
public function register()
{
$this->app->singleton('csstidy', function () {
return new \Alex198710\Csstidy\Csstidy();
});
}
// app/Facades/Csstidy.php
public static function format($css) {
return app('csstidy')->parse($css)->print->formatted();
}
Batch Processing:
// Process multiple CSS files (e.g., in a queue job)
$files = Storage::files('public/css');
foreach ($files as $file) {
$css = new Csstidy();
$css->parse(file_get_contents($file));
file_put_contents($file, $css->print->formatted());
}
Middleware for Dynamic CSS:
// app/Http/Middleware/FormatCss.php
public function handle($request, Closure $next) {
if ($request->is('css/*')) {
$css = new Csstidy();
$css->parse($request->getContent());
return response($css->print->formatted(), 200, ['Content-Type' => 'text/css']);
}
return $next($request);
}
PHP Version Dependency:
CSSTidy Configuration Conflicts:
set_cfg() options (e.g., remove_last_;) may not work as expected if the underlying CSSTidy library is outdated. Verify against CSSTidy docs.Memory Limits:
memory_limit. Increase it temporarily:
ini_set('memory_limit', '256M');
Namespace Collisions:
csstidy (lowercase) in the example conflicts with Laravel’s auto-loading. Use the fully qualified namespace:
use Alex198710\Csstidy\Csstidy; // Correct
// Avoid: $css = new csstidy(); // May fail
Validate Input:
if (!filter_var($css, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException("Invalid CSS input");
}
Check Return Codes:
parse() returns a boolean. Validate before processing:
if (!$css->parse($rawCss)) {
Log::error('CSSTidy parse failed: ' . $css->error->message);
}
Log Configuration:
$css->set_cfg('debug', true);
Custom Rules:
$css->set_cfg('add_vendor_prefixes', ['-webkit-', '-moz-']);
Event Hooks:
Events):
event(new CssProcessed($cleanCss));
Cache Optimized CSS:
$cacheKey = 'css_'.md5($rawCss);
$cleanCss = Cache::remember($cacheKey, 3600, function () use ($css, $rawCss) {
return $css->parse($rawCss)->print->formatted();
});
How can I help you explore Laravel packages today?