Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / hacked / src / hackedProjectWebDownloader.php
1 <?php
2
3 namespace Drupal\hacked;
4
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
6
7 /**
8  * Base class for downloading remote versions of projects.
9  */
10 class hackedProjectWebDownloader {
11   use StringTranslationTrait;
12
13   var $project;
14
15   /**
16    * Constructor, pass in the project this downloaded is expected to download.
17    */
18   function __construct(&$project) {
19     $this->project = $project;
20   }
21
22   /**
23    * Returns a temp directory to work in.
24    *
25    * @param null $namespace
26    *   The optional namespace of the temp directory, defaults to the classname.
27    * @return bool|string
28    */
29   function get_temp_directory($namespace = NULL) {
30     if (is_null($namespace)) {
31       $reflect = new \ReflectionClass($this);
32       $namespace = $reflect->getShortName();
33     }
34     $segments = [
35       file_directory_temp(),
36       'hacked-cache-' . get_current_user(),
37       $namespace,
38     ];
39     $dir = implode('/', array_filter($segments));
40     if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
41       $message = $this->t('Failed to create temp directory: %dir', array('%dir' => $dir));
42       \Drupal::logger('hacked')->error($message);
43       return FALSE;
44     }
45     return $dir;
46   }
47
48   /**
49    * Returns a directory to save the downloaded project into.
50    */
51   function get_destination() {
52     $type = $this->project->project_type;
53     $name = $this->project->name;
54     $version = $this->project->existing_version;
55
56     $dir = $this->get_temp_directory() . "/$type/$name";
57     // Build the destination folder tree if it doesn't already exists.
58     if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
59       $message = $this->t('Failed to create temp directory: %dir', ['%dir' => $dir]);
60       \Drupal::logger('hacked')->error($message);
61       return FALSE;
62     }
63     return "$dir/$version";
64   }
65
66   /**
67    * Returns the final destination of the unpacked project.
68    */
69   function get_final_destination() {
70     $dir = $this->get_destination();
71     $name = $this->project->name;
72     $version = $this->project->existing_version;
73     $type = $this->project->project_type;
74     // More special handling for core:
75     if ($type != 'core') {
76       $module_dir = $dir . "/$name";
77     }
78     else {
79       $module_dir = $dir . '/' . $name . '-' . $version;
80     }
81     return $module_dir;
82   }
83
84   /**
85    * Download the remote files to the local filesystem.
86    */
87   function download() {
88
89   }
90
91   /**
92    * Recursively delete all files and folders in the specified filepath, then
93    * delete the containing folder.
94    *
95    * Note that this only deletes visible files with write permission.
96    *
97    * @param string $path
98    *   A filepath relative to file_directory_path.
99    */
100   function remove_dir($path) {
101     if (is_file($path) || is_link($path)) {
102       unlink($path);
103     }
104     elseif (is_dir($path)) {
105       $d = dir($path);
106       while (($entry = $d->read()) !== FALSE) {
107         if ($entry == '.' || $entry == '..') {
108           continue;
109         }
110         $entry_path = $path . '/' . $entry;
111         $this->remove_dir($entry_path);
112       }
113       $d->close();
114       rmdir($path);
115     }
116     else {
117       $message = $this->t('Unknown file type(%path) stat: %stat ', [
118         '%path' => $path,
119         '%stat' => print_r(stat($path), 1)
120       ]);
121       \Drupal::logger('hacked')->error($message);
122     }
123   }
124
125 }