Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--- cf.orig.php 2011-08-30 14:24:16.420428911 +1000
+++ cf.php      2011-08-30 14:34:23.471334631 +1000
@@ -57,6 +57,19 @@
      *      We should really force the timestamp to improve caching.
      *      Trun on the option in core.php
      *
+     *  - remoteCompressedFiles
+     *
+     *      If Cloudfront origin is s3 then we can supply the user
+     *      with compressed files if we add them to S3 first and then
+     *      change the file that is requested.
+     *      To make this work we need a gzip version of each css and
+     *      js file in the format cake.gz.css.
+     *      If this is turned on the app will check that the agent
+     *      accepts gzip encoded files and will server the gz version
+     *      instead.
+     *      This feature is not required if the origin of cloudfront
+     *      is your website.
+     *
      */
     public $configuration = array(
         'assetHost' => 'assets%d.example.com',
@@ -68,21 +81,35 @@
         'jsDir' => 'js',
         'cssDir' => 'css',
         'assetDir' => null,
-        'forceTimestamp' => false
+        'forceTimestamp' => false,
+        'remoteCompressedFiles' => false
     );
 
     public function __construct($configuration) {
 
-        $this->configuration = array_merge($this->configuration, $configuration);
+        if (!empty($configuration)) {
+            $this->configuration = array_merge($this->configuration, $configuration);
+        }
 
         $this->configuration['modulo'] = $this->configuration['numHostsMax'];
         if ($this->configuration['numHostsMin'] == 0) {
             $this->configuration['modulo'] = $this->configuration['numHostsMax'] + 1;
         }
 
+        // Using remote compressed files, i.e. s3 hosted 'website.gz.css' type
+        // of files is dependant on whether it has been turned on, whether
+        // the user agent accepts it, and we are in production mode
+        $useCompressedFiles = $this->configuration['remoteCompressedFiles'] &&
+                                                (strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false) &&
+                                                         Configure::read('debug') == 0;
+
+        $this->configuration['remoteCompressedFiles'] = $useCompressedFiles;
+
         if (Configure::read('debug') > 0) {
             $this->configuration['assetHost'] = rtrim(env('HTTP_HOST') . Router::url('/'), '/');
+            $this->configuration['sslHost'] = rtrim(env('HTTP_HOST') . Router::url('/'), '/');
         }
+
     }
 
     /**
@@ -92,7 +119,7 @@
      */
     public function beforeRender() {
 
-        if ((Configure::read('Asset.timestamp') == true && Configure::read('debug') > 0) || Configure::read('Asset.timestamp') === 'force') {
+        if ((Configure::read('Asset.timestamp') == true && Configure::read('debug') == 0) || Configure::read('Asset.timestamp') === 'force') {
 
             $this->configuration['forceTimestamp'] = true;
         }
@@ -123,22 +150,22 @@
      * Return JS link path/URL either remote or local based on the debug level
      *
      */
-    public function script($assets, $inline = true) {
+    public function script($assets, $options = array()) {
 
         $this->setAssetDir($this->configuration['jsDir']);
 
-        return $this->Html->script($this->setAssetPath($assets), $inline);
+        return $this->Html->script($this->setAssetPath($assets, true), $options);
     }
 
     /**
      * Return CSS link path/URL either remote or local based on the debug level
      *
      */
-    public function css($assets, $rel = null, $htmlAttributes = array(), $inline = true) {
+    public function css($assets, $rel = null, $options = array()) {
 
         $this->setAssetDir($this->configuration['cssDir']);
 
-        return $this->Html->css($this->setAssetPath($assets), $rel, $htmlAttributes, $inline);
+        return $this->Html->css($this->setAssetPath($assets, true), $rel, $options);
     }
 
     /**
@@ -146,25 +173,39 @@
      * Works for arrays of assets (like with JS or CSS) or single files
      *
      */
-    private function setAssetPath($assets = null) {
+    private function setAssetPath($assets = null, $tryGzVersion = false) {
 
         if ($assets) {
             if (is_array($assets)) {
 
                 $size = count($assets);
                 for ($i = 0; $i < $size; $i++) {
-                    $assets[$i] = $this->pathPrep($assets[$i]) . $assets[$i] . $this->getAssetTimestamp();
+                    $assets[$i] = $this->pathPrep($assets[$i]) . $this->gzName($assets[$i], $tryGzVersion) . $this->getAssetTimestamp();
                 }
             }
             else {
 
-                return $this->pathPrep($assets) . $assets . $this->getAssetTimestamp();
+                return $this->pathPrep($assets) .  $this->gzName($assets, $tryGzVersion) . $this->getAssetTimestamp();
             }
         }
 
         return $assets;
     }
 
+    /**
+     * Return the gzip filename of a file
+     * myapp.css -> myapp.gz.css
+     *
+     */
+    private function gzName($assetFileName, $tryGzVersion = false) {
+        if ($tryGzVersion && $this->configuration['remoteCompressedFiles']) {
+            $assetParts = pathinfo($assetFileName);
+            return $assetParts['filename'] . '.gz.' . $assetParts['extension'];
+        }
+
+        return $assetFileName;
+    }
+
    /**
      * Build asset URL
      *