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