Pārlūkot izejas kodu

enable precompression of certain files at highest level

Rob Bast 9 gadi atpakaļ
vecāks
revīzija
4633a19787

+ 1 - 0
app/config/defaults.yml

@@ -1,6 +1,7 @@
 parameters:
     packagist_host: ~
     packagist_metadata_dir: "%kernel.cache_dir%/composer-packages-build"
+    packagist_dumper_compress: 0
     session_save_path: %kernel.cache_dir%/sessions
     database_name_test: packagist_test
     redis_dsn_test: redis://127.0.0.1/14

+ 23 - 3
src/Packagist/WebBundle/Package/SymlinkDumper.php

@@ -85,6 +85,12 @@ class SymlinkDumper
      */
     private $writeLog = array();
 
+    /**
+     * Generate compressed files.
+     * @var int 0 disabled, 9 maximum.
+     */
+    private $compress;
+
     /**
      * Constructor
      *
@@ -93,8 +99,9 @@ class SymlinkDumper
      * @param UrlGeneratorInterface $router
      * @param string                $webDir     web root
      * @param string                $targetDir
+     * @param int                   $compress
      */
-    public function __construct(RegistryInterface $doctrine, Filesystem $filesystem, UrlGeneratorInterface $router, $webDir, $targetDir)
+    public function __construct(RegistryInterface $doctrine, Filesystem $filesystem, UrlGeneratorInterface $router, $webDir, $targetDir, $compress)
     {
         $this->doctrine = $doctrine;
         $this->fs = $filesystem;
@@ -102,6 +109,7 @@ class SymlinkDumper
         $this->router = $router;
         $this->webDir = realpath($webDir);
         $this->buildDir = $targetDir;
+        $this->compress = $compress;
     }
 
     /**
@@ -467,7 +475,14 @@ class SymlinkDumper
         if (file_exists($file)) {
             rename($file, $file.'-'.time());
         }
-        $this->writeFile($file, json_encode($this->rootFile));
+
+        $json = json_encode($this->rootFile);
+        $time = time();
+
+        $this->writeFile($file, $json, $time);
+        if ($this->compress) {
+            $this->writeFile($file . '.gz', gzencode($json, $this->compress), $time);
+        }
     }
 
     private function dumpListing($path)
@@ -480,8 +495,13 @@ class SymlinkDumper
         $json = json_encode($this->listings[$key]);
         $hash = hash('sha256', $json);
         $path = substr($path, 0, -5) . '$' . $hash . '.json';
+        $time = time();
+
         if (!file_exists($path)) {
-            $this->writeFile($path, $json);
+            $this->writeFile($path, $json, $time);
+            if ($this->compress) {
+                $this->writeFile($path . '.gz', gzencode($json, $this->compress), $time);
+            }
         }
 
         return array($path, $hash);

+ 1 - 1
src/Packagist/WebBundle/Resources/config/services.yml

@@ -17,7 +17,7 @@ services:
 
     packagist.package_dumper:
         class: Packagist\WebBundle\Package\SymlinkDumper
-        arguments: [ @doctrine, @filesystem, @router, "%kernel.root_dir%/../web/", "%packagist_metadata_dir%" ]
+        arguments: [ @doctrine, @filesystem, @router, "%kernel.root_dir%/../web/", "%packagist_metadata_dir%", "%packagist_dumper_compress%" ]
 
     packagist.user_provider:
         class: Packagist\WebBundle\Security\Provider\UserProvider