From bbe476dd2ac0f7fdc6a2ecf48d9aa19d3654a499 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 2 Nov 2023 22:46:49 +0100 Subject: [PATCH] Add a compileFile method to compile the code of a file --- phpstan-baseline.neon | 2 +- src/Compiler.php | 27 +++++++++++++++++++++++++-- tests/FrameworkTest.php | 8 ++++---- tests/InputTest.php | 5 ++--- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 96841e6c..f2cda5ab 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 - diff --git a/src/Compiler.php b/src/Compiler.php index c534c0a9..86c3a8ab 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -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 * diff --git a/tests/FrameworkTest.php b/tests/FrameworkTest.php index c5108f02..1ebb3fba 100644 --- a/tests/FrameworkTest.php +++ b/tests/FrameworkTest.php @@ -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()); } @@ -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()); } @@ -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()); } @@ -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()); } diff --git a/tests/InputTest.php b/tests/InputTest.php index 0b5a84af..efffc5ff 100644 --- a/tests/InputTest.php +++ b/tests/InputTest.php @@ -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); } @@ -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); }