PHP code example of shyim / sasso

1. Go to this page and download the library: Download shyim/sasso library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shyim / sasso example snippets



use Sasso\Compiler;
use Sasso\CompileException;

$css = (new Compiler())
    ->setStyle(Compiler::STYLE_COMPRESSED)
    ->addImportPath(__DIR__ . '/scss')   // load path for @import / @use / @forward
    ->setUrl('app.scss')                 // enables byte-exact error snippets
    ->compile('@use "base"; .x { color: base.$brand; }');

try {
    (new Compiler())->setUrl('input.scss')->compile('.a { color: ');
} catch (CompileException $e) {
    // Error: unexpected end of input in value
    //   ╷
    // 1 │ .a { color:
    //   │            ^
    //   ╵
    //   input.scss 1:13  root stylesheet
    echo $e->getMessage();
}


use Sasso\Compiler;
use Sasso\Importer;
use Sasso\ImporterResult;

class ArrayImporter implements Importer {
    public function __construct(private array $files) {}

    // Map the unquoted URL as written (e.g. "base" for @import "base") to a
    // stable canonical string, or null if this importer can't resolve it.
    // Must NOT load the file. Two URLs with the same canonical string are the
    // same partial (the module-cache key).
    public function canonicalize(string $url, bool $fromImport, ?string $containingUrl = null): ?string {
        return isset($this->files[$url]) ? "array:$url" : null;
    }

    // Fetch the source for a canonical string canonicalize() returned, or null
    // if it can no longer be found.
    public function load(string $canonicalUrl): ?ImporterResult {
        $key = substr($canonicalUrl, strlen('array:'));
        return new ImporterResult($this->files[$key]);
    }
}

$css = (new Compiler())
    ->setImporter(new ArrayImporter([
        'base'   => '$brand: #e91e63;',
        'mixins' => '@mixin big { font-size: 2rem; }',
    ]))
    ->compile('@use "base"; @import "mixins"; .btn { color: base.$brand; @

new ImporterResult('.a { color: red; }', Compiler::SYNTAX_SCSS);

class DbImporter implements Importer {
    public function canonicalize(string $url, bool $fromImport, ?string $containingUrl = null): ?string {
        return $this->db->exists($url) ? "db:$url" : null;   // may throw DbException
    }
    public function load(string $canonicalUrl): ?ImporterResult {
        $row = $this->db->find(substr($canonicalUrl, 3));    // may throw DbException
        return $row ? new ImporterResult($row->source) : null;
    }
}

try {
    (new Compiler())->setImporter(new DbImporter($db))->compile($scss);
} catch (DbException $e) {
    // the original DbException, not a CompileException
}

echo (new Compiler())
    ->setSyntax(Compiler::SYNTAX_SASS)
    ->compile(".a\n  color: red");
ini
extension=sasso
console
$ cargo build --release
$ php -dextension=target/release/libsasso.dylib  your_script.php   # macOS
$ php -dextension=target/release/libsasso.so     your_script.php   # Linux
console
$ php tests/run.php                                   # whole suite
$ php tests/run.php tests/030-importer-two-phase.phpt # a single test
$ composer test                                       # same, via composer