3 namespace Drupal\hacked;
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 * Base class for downloading remote versions of projects.
10 class hackedProjectWebDownloader {
11 use StringTranslationTrait;
16 * Constructor, pass in the project this downloaded is expected to download.
18 function __construct(&$project) {
19 $this->project = $project;
23 * Returns a temp directory to work in.
25 * @param null $namespace
26 * The optional namespace of the temp directory, defaults to the classname.
29 function get_temp_directory($namespace = NULL) {
30 if (is_null($namespace)) {
31 $reflect = new \ReflectionClass($this);
32 $namespace = $reflect->getShortName();
35 file_directory_temp(),
36 'hacked-cache-' . get_current_user(),
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);
49 * Returns a directory to save the downloaded project into.
51 function get_destination() {
52 $type = $this->project->project_type;
53 $name = $this->project->name;
54 $version = $this->project->existing_version;
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);
63 return "$dir/$version";
67 * Returns the final destination of the unpacked project.
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";
79 $module_dir = $dir . '/' . $name . '-' . $version;
85 * Download the remote files to the local filesystem.
92 * Recursively delete all files and folders in the specified filepath, then
93 * delete the containing folder.
95 * Note that this only deletes visible files with write permission.
98 * A filepath relative to file_directory_path.
100 function remove_dir($path) {
101 if (is_file($path) || is_link($path)) {
104 elseif (is_dir($path)) {
106 while (($entry = $d->read()) !== FALSE) {
107 if ($entry == '.' || $entry == '..') {
110 $entry_path = $path . '/' . $entry;
111 $this->remove_dir($entry_path);
117 $message = $this->t('Unknown file type(%path) stat: %stat ', [
119 '%stat' => print_r(stat($path), 1)
121 \Drupal::logger('hacked')->error($message);