Skip to content

Commit

Permalink
Merge pull request #684 from stof/compile_file
Browse files Browse the repository at this point in the history
Add a compileFile method to compile the code of a file
  • Loading branch information
stof committed Nov 2, 2023
2 parents ace2503 + bbe476d commit 918894b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ parameters:

-
message: "#^PHPDoc tag @throws with type ScssPhp\\\\ScssPhp\\\\Exception\\\\SassException is not subtype of Throwable$#"
count: 2
count: 3
path: src/Compiler.php

-
Expand Down
27 changes: 25 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,33 @@ public function compile($code, $path = null)
}

/**
* Compile scss
* Compiles the provided scss file into CSS.
*
* @param string $path
*
* @return CompilationResult
*
* @throws SassException when the source fails to compile
*/
public function compileFile($path)
{
$source = file_get_contents($path);

if ($source === false) {
throw new \RuntimeException('Could not read the file content');
}

return $this->compileString($source, $path);
}

/**
* Compiles the provided scss source code into CSS.
*
* If provided, the path is considered to be the path from which the source code comes
* from, which will be used to resolve relative imports.
*
* @param string $source
* @param string|null $path
* @param string|null $path The path for the source, used to resolve relative imports
*
* @return CompilationResult
*
Expand Down
8 changes: 4 additions & 4 deletions tests/FrameworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testBootstrap()

$entrypoint = dirname(__DIR__) . '/vendor/twbs/bootstrap/scss/bootstrap.scss';

$result = $compiler->compileString(file_get_contents($entrypoint), $entrypoint);
$result = $compiler->compileFile($entrypoint);

$this->assertNotEmpty($result->getCss());
}
Expand All @@ -42,7 +42,7 @@ public function testBootstrap4()

$entrypoint = dirname(__DIR__) . '/vendor/twbs/bootstrap4/scss/bootstrap.scss';

$result = $compiler->compileString(file_get_contents($entrypoint), $entrypoint);
$result = $compiler->compileFile($entrypoint);

$this->assertNotEmpty($result->getCss());
}
Expand Down Expand Up @@ -74,7 +74,7 @@ public function testFoundation()

$entrypoint = dirname(__DIR__) . '/vendor/zurb/foundation/assets/foundation.scss';

$result = $compiler->compileString(file_get_contents($entrypoint), $entrypoint);
$result = $compiler->compileFile($entrypoint);

$this->assertNotEmpty($result->getCss());
}
Expand All @@ -90,7 +90,7 @@ public function testBourbon($entrypoint)
$compiler->addImportPath(dirname(__DIR__) . '/vendor/thoughtbot/bourbon');
$compiler->addImportPath(dirname(__DIR__) . '/vendor/thoughtbot/bourbon/spec/fixtures');

$result = $compiler->compileString(file_get_contents($entrypoint), $entrypoint);
$result = $compiler->compileFile($entrypoint);

$this->assertNotEmpty($result->getCss());
}
Expand Down
5 changes: 2 additions & 3 deletions tests/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ public function testInputFile($inFname, $outFname)
$this->fail("$outFname is missing, consider building tests with \"make rebuild-outputs\".");
}

$input = file_get_contents($inFname);
$output = file_get_contents($outFname);

$css = $this->scss->compileString($input, $inFname)->getCss();
$css = $this->scss->compileFile($inFname)->getCss();
$this->assertEquals($output, $css);
}

Expand All @@ -79,7 +78,7 @@ function ($a) {
*/
private function buildInput($inFname, $outFname)
{
$css = $this->scss->compileString(file_get_contents($inFname), $inFname)->getCss();
$css = $this->scss->compileFile($inFname)->getCss();

file_put_contents($outFname, $css);
}
Expand Down

0 comments on commit 918894b

Please sign in to comment.