public function saveSnippet(string $outputFile, string $format = self::FORMAT_NGINX) : bool
{
if ($this->needsCompile) {
$this->compile();
}
// Are we doing a report-only header?
$which = $this->reportOnly ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy';
switch ($format) {
case self::FORMAT_NGINX:
// In PHP < 7, implode() is faster than concatenation
$output = \implode('', ['add_header ', $which, ' "', \rtrim($this->compiled, ' '), '";', "\n"]);
break;
case self::FORMAT_APACHE:
$output = \implode('', ['Header add ', $which, ' "', \rtrim($this->compiled, ' '), '"', "\n"]);
break;
default:
throw new \Exception('Unknown format: ' . $format);
}
return \file_put_contents($outputFile, $output) !== false;
}