ParagonIE\CSPBuilder\CSPBuilder::compile PHP Метод

compile() публичный Метод

Compile the current policies into a CSP header
public compile ( ) : string
Результат string
    public function compile() : string
    {
        $ruleKeys = \array_keys($this->policies);
        if (\in_array('report-only', $ruleKeys)) {
            $this->reportOnly = !!$this->policies['report-only'];
        } else {
            $this->reportOnly = false;
        }
        $compiled = [];
        foreach (self::$directives as $dir) {
            if (\in_array($dir, $ruleKeys)) {
                if (empty($ruleKeys)) {
                    if ($dir === 'base-uri') {
                        continue;
                    }
                }
                $compiled[] = $this->compileSubgroup($dir, $this->policies[$dir]);
            }
        }
        if (!empty($this->policies['report-uri'])) {
            $compiled[] = 'report-uri ' . $this->policies['report-uri'] . '; ';
        }
        if (!empty($this->policies['upgrade-insecure-requests'])) {
            $compiled[] = 'upgrade-insecure-requests';
        }
        $this->compiled = \implode('', $compiled);
        $this->needsCompile = false;
        return $this->compiled;
    }

Usage Example

Пример #1
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $this->csp->compile();
     $response = $this->csp->injectCSPHeader($response);
     return $next($request, $response);
 }