Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / hacked / src / hackedFileHasher.php
1 <?php
2
3 namespace Drupal\hacked;
4
5 /**
6  * Base class for the different ways that files can be hashed.
7  */
8 abstract class hackedFileHasher {
9   /**
10    * Returns a hash of the given filename.
11    *
12    * Ignores file line endings
13    */
14   function hash($filename) {
15     if (file_exists($filename)) {
16       if ($hash = $this->cache_get($filename)) {
17         return $hash;
18       }
19       else {
20         $hash = $this->perform_hash($filename);
21         $this->cache_set($filename, $hash);
22         return $hash;
23       }
24     }
25   }
26
27   function cache_set($filename, $hash) {
28     \Drupal::cache(HACKED_CACHE_TABLE)->set($this->cache_key($filename), $hash, strtotime('+7 days'));
29   }
30
31   function cache_get($filename) {
32     $cache = \Drupal::cache(HACKED_CACHE_TABLE)->get($this->cache_key($filename));
33     if (!empty($cache->data)) {
34       return $cache->data;
35     }
36   }
37
38   function cache_key($filename) {
39     $key = array(
40       'filename' => $filename,
41       'mtime' => filemtime($filename),
42       'class_name' => get_class($this),
43     );
44     return sha1(serialize($key));
45   }
46
47   /**
48    * Compute and return the hash of the given file.
49    *
50    * @param $filename
51    *   A fully-qualified filename to hash.
52    *
53    * @return string
54    *   The computed hash of the given file.
55    */
56   abstract function perform_hash($filename);
57
58   /**
59    * Compute and return the lines of the given file.
60    *
61    * @param $filename
62    *   A fully-qualified filename to return.
63    *
64    * @return array|bool
65    *   The lines of the given filename or FALSE on failure.
66    */
67   abstract function fetch_lines($filename);
68 }