Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Csstidy Laravel Package

alex198710/csstidy

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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).

  2. First Use Case:

    use Alex198710\Csstidy\Csstidy;
    
    $css = new Csstidy();
    $css->parse('a { color: black; }');
    echo $css->print->formatted(); // Outputs formatted CSS
    
  3. Where to Look First:

    • Class Reference: Focus on Csstidy class methods (e.g., parse(), print->formatted()).
    • Configuration: Review set_cfg() for options like remove_last_; (removes trailing semicolons).
    • Documentation: Check the CSSTidy source for advanced rules (e.g., reformat_selectors, remove_comments).

Implementation Patterns

Core Workflow

  1. 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
    
  2. Integration with Laravel:

    • Service Provider:
      // app/Providers/CsstidyServiceProvider.php
      public function register()
      {
          $this->app->singleton('csstidy', function () {
              return new \Alex198710\Csstidy\Csstidy();
          });
      }
      
    • Facade (Optional):
      // app/Facades/Csstidy.php
      public static function format($css) {
          return app('csstidy')->parse($css)->print->formatted();
      }
      
  3. 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());
    }
    
  4. 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);
    }
    

Gotchas and Tips

Pitfalls

  1. PHP Version Dependency:

    • The package requires PHP ≥5.3.2. Test on your target environment to avoid runtime errors.
  2. CSSTidy Configuration Conflicts:

    • Some set_cfg() options (e.g., remove_last_;) may not work as expected if the underlying CSSTidy library is outdated. Verify against CSSTidy docs.
  3. Memory Limits:

    • Processing large CSS files (e.g., 10MB+) may hit PHP’s memory_limit. Increase it temporarily:
      ini_set('memory_limit', '256M');
      
  4. Namespace Collisions:

    • The class name 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
      

Debugging Tips

  1. Validate Input:

    • CSSTidy may throw errors on malformed CSS. Sanitize input first:
      if (!filter_var($css, FILTER_VALIDATE_URL)) {
          throw new \InvalidArgumentException("Invalid CSS input");
      }
      
  2. Check Return Codes:

    • CSSTidy’s parse() returns a boolean. Validate before processing:
      if (!$css->parse($rawCss)) {
          Log::error('CSSTidy parse failed: ' . $css->error->message);
      }
      
  3. Log Configuration:

    • Enable CSSTidy’s debug mode to log configuration issues:
      $css->set_cfg('debug', true);
      

Extension Points

  1. Custom Rules:

    • Extend the wrapper to add Laravel-specific rules (e.g., auto-prefixing for Tailwind):
      $css->set_cfg('add_vendor_prefixes', ['-webkit-', '-moz-']);
      
  2. Event Hooks:

    • Trigger events before/after processing (e.g., using Laravel’s Events):
      event(new CssProcessed($cleanCss));
      
  3. Cache Optimized CSS:

    • Cache formatted CSS to avoid reprocessing:
      $cacheKey = 'css_'.md5($rawCss);
      $cleanCss = Cache::remember($cacheKey, 3600, function () use ($css, $rawCss) {
          return $css->parse($rawCss)->print->formatted();
      });
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo
stevenay/myanfont
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin