Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

ClosureCompilerService.php

Zuletzt modifiziert: 02.04.2025, 15:04 - Dateigröße: 3.38 KiB


001  <?php
002   
003  /**
004  * @package   s9e\TextFormatter
005  * @copyright Copyright (c) 2010-2022 The s9e authors
006  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
007  */
008  namespace s9e\TextFormatter\Configurator\JavaScript\Minifiers;
009   
010  use RuntimeException;
011  use s9e\TextFormatter\Configurator\JavaScript\OnlineMinifier;
012   
013  class ClosureCompilerService extends OnlineMinifier
014  {
015      /**
016      * @var string Closure Compiler's compilation level
017      */
018      public $compilationLevel = 'ADVANCED_OPTIMIZATIONS';
019   
020      /**
021      * @var bool Whether to exclude Closure Compiler's default externs
022      */
023      public $excludeDefaultExterns = true;
024   
025      /**
026      * @var string Externs used for compilation
027      */
028      public $externs;
029   
030      /**
031      * @var string Closure Compiler Service's URL
032      */
033      public $url = 'https://closure-compiler.appspot.com/compile';
034   
035      /**
036      * Constructor
037      */
038      public function __construct()
039      {
040          parent::__construct();
041          $this->externs = file_get_contents(__DIR__ . '/../externs.service.js');
042      }
043   
044      /**
045      * {@inheritdoc}
046      */
047      public function getCacheDifferentiator()
048      {
049          $key = [$this->compilationLevel, $this->excludeDefaultExterns];
050   
051          if ($this->excludeDefaultExterns)
052          {
053              $key[] = $this->externs;
054          }
055   
056          return $key;
057      }
058   
059      /**
060      * Compile given JavaScript source via the Closure Compiler Service
061      *
062      * @param  string $src JavaScript source
063      * @return string      Compiled source
064      */
065      public function minify($src)
066      {
067          $body     = $this->generateRequestBody($src);
068          $response = $this->query($body);
069          if ($response === false)
070          {
071              throw new RuntimeException('Could not contact the Closure Compiler service');
072          }
073   
074          return $this->decodeResponse($response);
075      }
076   
077      /**
078      * Decode the response returned by the Closure Compiler service
079      *
080      * @param  string $response Response body
081      * @return string           Minified code
082      */
083      protected function decodeResponse($response)
084      {
085          $response = json_decode($response, true);
086          if (is_null($response))
087          {
088              throw new RuntimeException('Closure Compiler service returned invalid JSON: ' . json_last_error_msg());
089          }
090   
091          if (isset($response['serverErrors'][0]))
092          {
093              $error = $response['serverErrors'][0];
094   
095              throw new RuntimeException('Server error ' . $error['code'] . ': ' . $error['error']);
096          }
097   
098          if (isset($response['errors'][0]))
099          {
100              $error = $response['errors'][0];
101   
102              throw new RuntimeException('Compilation error: ' . $error['error']);
103          }
104   
105          return $response['compiledCode'];
106      }
107   
108      /**
109      * Generate the request body for given code
110      *
111      * @param  string $src JavaScript source
112      * @return string      Compiled source
113      */
114      protected function generateRequestBody($src)
115      {
116          $params = [
117              'compilation_level' => $this->compilationLevel,
118              'js_code'           => $src,
119              'output_format'     => 'json',
120              'output_info'       => 'compiled_code'
121          ];
122   
123          // Add our custom externs if default externs are disabled
124          if ($this->excludeDefaultExterns && $this->compilationLevel === 'ADVANCED_OPTIMIZATIONS')
125          {
126              $params['exclude_default_externs'] = 'true';
127              $params['js_externs'] = $this->externs;
128          }
129   
130          // Add dupe variables by hand
131          $body = http_build_query($params) . '&output_info=errors';
132   
133          return $body;
134      }
135   
136      /**
137      * Query the Closure Compiler service
138      *
139      * @param  string       $body Request body
140      * @return string|false       Response body, or FALSE
141      */
142      protected function query($body)
143      {
144          return $this->httpClient->post(
145              $this->url,
146              ['headers' => ['Content-Type: application/x-www-form-urlencoded']],
147              $body
148          );
149      }
150  }