41f56a955f37b37ebfc8fa75822d99320e1bc258
[yaffs-website] / web / modules / contrib / advagg / src / Asset / AssetDumper.php
1 <?php
2
3 namespace Drupal\advagg\Asset;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Asset\AssetDumperInterface;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9
10 /**
11  * Dumps a CSS or JavaScript asset.
12  */
13 class AssetDumper implements AssetDumperInterface {
14
15   /**
16    * The path to use for saving the asset.
17    *
18    * @var string
19    */
20   protected $path;
21
22   /**
23    * The extension to use.
24    *
25    * @var string
26    */
27   protected $extension;
28
29   /**
30    * Hash of the AdvAgg settings.
31    *
32    * @var string
33    */
34   protected $hash;
35
36   /**
37    * A config object for the system performance configuration.
38    *
39    * @var \Drupal\Core\Config\Config
40    */
41   protected $config;
42
43   /**
44    * Module handler service.
45    *
46    * @var \Drupal\Core\Extension\ModuleHandlerInterface
47    */
48   protected $moduleHandler;
49
50   /**
51    * Construct the AssetDumper instance.
52    *
53    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
54    *   A config factory for retrieving required config objects.
55    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
56    *   The module handler.
57    */
58   public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
59     $this->config = $config_factory->get('system.performance');
60     $this->hash = '_' . advagg_get_current_hooks_hash() . '.';
61     $this->moduleHandler = $module_handler;
62   }
63
64   /**
65    * Set the folder path to save the data to.
66    *
67    * @param string $file_extension
68    *   The extension of the file.
69    */
70   public function preparePath($file_extension) {
71     $this->extension = $file_extension;
72     $this->path = 'public://' . $this->extension . '/';
73
74     // Allow other modules to alter the file path.
75     // Call hook_advagg_asset_path_alter().
76     $this->moduleHandler->alter('advagg_asset_path', $this->path, $this->extension);
77     file_prepare_directory($this->path, FILE_CREATE_DIRECTORY);
78     return $this->path;
79   }
80
81   /**
82    * {@inheritdoc}
83    *
84    * The file name for the CSS or JS cache file is generated from the hash of
85    * the aggregated contents of the files in $data. This forces proxies and
86    * browsers to download new CSS when the CSS changes.
87    */
88   public function dump($data, $file_extension) {
89     if (!isset($this->extension) || $this->extension != $file_extension) {
90       $this->preparePath($file_extension);
91     }
92
93     // Prefix filename to prevent blocking by firewalls which reject files
94     // starting with "ad*".
95     $filename = $this->extension . '_' . Crypt::hashBase64($data) . $this->hash . $this->extension;
96     $uri = $this->path . $filename;
97
98     // Create the CSS or JS file.
99     if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
100       return FALSE;
101     }
102
103     // If CSS/JS gzip compression is enabled and the zlib extension is available
104     // then create a gzipped version of this file. This file is served
105     // conditionally to browsers that accept gzip using .htaccess rules.
106     // It's possible that the rewrite rules in .htaccess aren't working on this
107     // server, but there's no harm (other than the time spent generating the
108     // file) in generating the file anyway. Sites on servers where rewrite rules
109     // aren't working can set css.gzip to FALSE in order to skip
110     // generating a file that won't be used.
111     if (extension_loaded('zlib') && $this->config->get($file_extension . '.gzip')) {
112       if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
113         return FALSE;
114       }
115     }
116     return [$uri, $filename];
117   }
118
119 }