jbzoo/less

PHP wrapper for any less-compilers

7.0.1 2024-01-28 11:01 UTC

This package is auto-updated.

Last update: 2024-11-16 19:51:58 UTC


README

CI Coverage Status Psalm Coverage Psalm Level CodeFactor
Stable Version Total Downloads Dependents GitHub License

PHP wrapper for wikimedia/less.php.

Install

composer require jbzoo/less

Usage

use JBZoo\Less\Less;

try { // Any error handling

    // There is not option required
    $less = new Less([
        'force'        => false,                    // Can forced compile on each compile() calling
        'debug'        => false,                    // On/Off Source map for browser debug console

        'root_url'     => 'http://site.com/',       // Root URL for all CSS files and debug mode
                                                    // For example - background:url('http://site.com/image.png')

        'root_path'    => '/full/path/to/site',     // Full path to root of web directory

        'global_vars'  => [                         // Some vars that will be in all less files
            'color'  => '#f00',                     // @color: #f00;
            'media' => 'print',                     // @media: print;
        ],

        'autoload'     => [                         // Autoload before eash compiling
            '/full/path/to/my_mixins.less',         // See the best of collection here
        ],                                          // https://github.com/JBZoo/JBlank/tree/master/less/misc

        'import_paths' => [                         // Import paths
            '/full/path/to/assets/less/' => 'http://site.com/assets/less/',
            './or/relative/path/to/dir/' => './or/relative/path/to/dir/',
        ],

        'cache_path'   => './cache',                // Where JBZoo/Less will save compiled CSS-files
        'cache_ttl'    => 2592000,                  // How often rebuild css files (in seconds)

        'functions' => [                            // Custom functions for less (only for gpeasy!)
            'str-revert' => function ($arg) {       // Register name `str-revert()`
                $arg->value = strrev($arg->value);  // Just revert argument
                return $arg;                        // Result: str-revert('1234567890'); => '0987654321';
            },
        ],
    ]);

    $less->setImportPath(
        '/full/path/to/other/import/directory/',    // Full or relative path
        'http://site.com/other/import/directory/'   // Not required
    );

    $fullCSSpath_1 = $less->compile('/full/path/to/styles.less');       // Basepath from config
    $fullCSSpath_2 = $less->compile('./relative/path/to/styles.less');  // OR relative path
    $fullCSSpath_3 = $less->compile(
        './relative/path/to/styles.less',
        'http://site.com/relative/path/to/'                             // Force base path for any URLs
    );

} catch (JBZoo\Less\Exception $e) {
    echo 'JBZoo/Less: ' . $e->getMessage();
}

Unit tests and check code style

make update
make test-all

License

MIT