src/CSPBuilder.php
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, ' '),
                    '" always;',
                    "\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;
    }
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, ' '),
                    '" always;',
                    "\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;
    }
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 , ' ' ) , '" always;' , "\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 ; }
Save CSP to a snippet file

@param string $outputFile Output file name
@param string $format Which format are we saving in?
@return bool
@throws \Exception
Save CSP to a snippet file