CSSLoL

An standalone CSS optimizer in PHP


Do not use it in production! :D

This is a CSS optimizer that parse CSS code into an set of associatives arrays, allowing to manipulate the CSS with PHP and execute the magic, outputing as a text or into a file.

Optimize

Minify


Installing

Prerequisites: PHP 5.4+

Just download the file CSSLoL.class.php and use it, there is no others external dependencies to the CSSLoL class. Of course you can download the folder /tests/ if you want some examples to how use and to test the funcionalities.

Usage

<?php
$CSSLoL = new CSSLoL();
// Set initial CSS 
$CSSLoL->set('body{color:#333333;}');
// Add rule to the end of the CSS
$CSSLoL->append(array(
    'p' => array('color'=>'#222222'),
));
echo $CSS->get('string');

Will output:

body{color:#333}p{color:#222}



<?php
$CSSLoL = new CSSLoL();
// Set initial CSS 
$CSSLoL->set('body{color:#333333;}');
// Add rule to the beginning of the CSS
$CSSLoL->prepend(array(
    'p' => array('color'=>'#222222'),
));
echo $CSS->get('string');

Will output:

p{color:#222}body{color:#333}



Loading a CSS file from a local path

<?php
$CSSLoL = new CSSLoL();
// Load CSS from a file
$CSSLoL->load('tests/example.css');
echo $CSS->get('string');

Will output:

body{color:#333}

Loading a CSS file from an URL

<?php
$CSSLoL = new CSSLoL();
// Load CSS from a file
$CSSLoL->load('https://localhost/tests/example.css');
echo $CSS->get('string');

Will output:

body{color:#333}



The default value of the get() parameter is ‘array’, so the return will be an set of associatives arrays

<?php
$CSSLoL = new CSSLoL();
$CSSLoL->load('tests/example.css');
// Output as an array
echo $CSS->get();

Will output:

array(1) {
  [0]=>
  array(1) {
    ["body"]=>
    array(1) {
      ["color"]=>
      string(4) "#333"
    }
  }



If you need the CSS in a string you have to indicate that with the string value ‘string’

<?php
$CSSLoL = new CSSLoL();
$CSSLoL->load('tests/example.css');
// Output as a string
echo $CSS->get('string');

Will output:

body{color:#333}



<?php
$CSSLoL = new CSSLoL();
$CSSLoL->set('body{color:#333}');
// Create a minified file
if($CSS->save('tests/css/example-min.css')){
  echo 'File created with success';
} else {
  echo 'Something wrong...';
}

Will output an file minified in the indicated path. For more complexes paths, you could indicate the final name of the file and the destination using the second parameter.

<?php
$CSSLoL = new CSSLoL();
$CSSLoL->set('body{color:#333}');
// Create a minified file
if($CSS->save('example.css','tests/css')){
  echo 'File created with success';
} else {
  echo 'Something wrong...';
}

The third parameter indicate if the final CSS will be minified or idented. The default value is true;

<?php
$CSSLoL = new CSSLoL();
$CSSLoL->set('body{color:#333}');
// Create an idented file
if($CSS->save('example.css','tests/css',false)){
  echo 'File with CSS idented created with success';
} else {
  echo 'Something went wrong with idented version...';
}
// Create a minified file
if($CSS->save('example.min.css','tests/css',true)){
  echo 'File with CSS minified created with success';
} else {
  echo 'Something went wrong with minified version...';
}



You can use the set() method with an string, this string could came from a webform or from loaded from a file for example

<?php
$CSSLoL = new CSSLoL();
// Set an initial CSS from string
$CSSLoL->set('body{color:#333}');
echo $CSS->get('string');

Will output:

body{color:#333}

Or if you have an set of rules structured in an associative array, you can use that too:

<?php
$CSSLoL = new CSSLoL();
// Set an initial CSS from array
$rule = array( 
    'body' => array('color' => '#333')
);
$CSSLoL->set($rule);
echo $CSS->get('string');

Will output:

body{color:#333}



Configurations

<?php
// An array with configs
$configs = array(
  // The default value is already true :D
  'autoprefixer' => true,
); 
// Pass it through constructor
$CSSLoL = new CSSLoL($configs);
$CSSLoL->set('.example{transform: rotate(30deg);}');
echo $CSSLoL->get('string',false);

Will output

.example {
    -webkit-transform: rotate(30deg);
    -ms-transform: rotate(30deg);
    transform: rotate(30deg);
}



Running the tests

The tests are in the /tests/ folder, just run them into your browser to see the results. They are not that good as a test but I think they do their job. Basically the tests test the methods in the class, to see if they output what is desired.

Built With

Google Search, Visual Code Editor, PHP 7, Apache (on Windows), Git and Github.

Versioning

I use my imagination for versioning. For the versions available, see the releases on this repository.

Author

There is no other contributors until now, but if someone show up will be in the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details