Skip to content

[RFC] Stateless API for the compiler #222

@stof

Description

@stof

Currently, the Compiler is a stateless class due to its getParsedFiles getter, which allows to get some details about the previous compilation.
This stateful getter means that the Compiler has to keep that info around. It also means that some weird things might happen if the same Compiler instance is shared between multiple part of a project, and that one of them expects to be able to call this getter until the next time it triggers a compilation, while giving back the execution to other code (which could then potentially trigger a different compilation).

I suggest an alternate signature (using PHP types for brevity here, but that would be implemented in phpdoc instead due to the support for PHP 5.6):

<?php

class Compiler
{
    public function compile(string $code, ?string $path = null): CompilationResult
    {
        // ...
    }
}

class CompilationResult
{
    public function getCss(): string
    {
        // ...
    }

    // name matching the JS API
    public function getIncludedFiles(): array
    {
        // ...
    }

    public function __toString(): string
    {
        return $this->getCss(); // To reduce the impact of the BC break
    }

    // The sourceMap content, if it was generated
    public function getMap(): ?string
    {
        // ...
    }
}

This result object is similar to what the JS API does (except they have a included files in a nested stats object, which I decided to skip as relevant).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions