Compilation scss code by php function

Hi,

I wanted to get a php function that will compile the scss file to css when you click the button, the function itself works ok, but it has a problem with exec(). Because the file is not compiled and the function returns an error that is predicted in the function code. But it doesn’t get the desired result.

<div class="ccm-dashboard-form-actions-wrapper">
    <div class="ccm-dashboard-form-actions">
		<button class="btn btn-secondary float-start" id="compSass" name="compSass"><?= t('Compile SCSS') ?></button>
        <button class="pull-right btn btn-primary float-end" type="submit"><?= t('Save') ?></button>
    </div>
</div>
	<input type="hidden" name="clickedButtonId" id="clickedButtonId" value="">
</form>    
 document.addEventListener('DOMContentLoaded', function() {
        var compSassButton = document.getElementById('SassComp');

        // Nasłuchujemy kliknięcia na przycisku kompilacji SCSS
        compSassButton.addEventListener('click', function() {
            document.getElementById('clickedButtonId').value = compSassButton.id;
        });
    });
       if ($this->request->request->has('compSass')) {
            // Uruchom kompilację SCSS
            $result = $this->compile_scss();

            // Sprawdzamy, czy kompilacja zakończyła się sukcesem
            if ($result['success']) {
                // Kompilacja zakończona sukcesem, wyświetlamy odpowiedni komunikat
                $this->flash('success', t('SCSS compiled successfully.'));
                return Redirect::to('/dashboard/cookie_consent')->send();
            } else {
                // Kompilacja zakończona niepowodzeniem, wyświetlamy odpowiedni komunikat
                $this->flash('success', t($result['message']));
                return Redirect::to('/dashboard/cookie_consent')->send();
            }
        } else {
            // Jeżeli kliknięto inny przycisk, wyświetlamy informację o zapisaniu ustawień
            $this->flash('success', t('Changes saved successfully.'));
            return Redirect::to('/dashboard/cookie_consent')->send();
        }
    }
}



public function compile_scss() {
    // Ścieżka do pliku wejściowego SCSS
    $inputFilePath = 'sass/main.scss';

    // Ścieżka do pliku wyjściowego CSS
    $outputFilePath = 'css/cookieconsent.css';

    // Komenda do kompilacji SCSS do CSS za pomocą Dart Sass
    $command = 'sass "' . $inputFilePath . '" "' . $outputFilePath . '"';

    // Wywołanie komendy w systemie
    exec($command, $output, $returnCode);

    // Sprawdzenie kodu zwróconego przez wywołanie
    if ($returnCode === 0) {
        // Kompilacja zakończona sukcesem
        return ['success' => true];
    } else {
        // Kompilacja zakończona niepowodzeniem
        return ['success' => false, 'message' => 'Error compiling SCSS: ' . $command ];
    }
}

Do you know how to use Exec(), or implement other code to work it? Or is the any better way to solve it?

Web server security is often configured to block exec and similar host commands.

In case I work with Xampp, on the local host. I didn’t find out any semicolon lines in php.ini. Ok, do you know any other way to get te same css result?

regards

You might take a look at how some open source PHP SASS compiler projects do it to get ideas as to how they perform the compilation in a way that doesn’t upset the web server.