5 * Contains \Drupal\hacked\hackedProjectWebDownloader.
8 namespace Drupal\hacked;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
13 * Base class for downloading remote versions of projects.
15 class hackedProjectWebDownloader {
16 use StringTranslationTrait;
21 * Constructor, pass in the project this downloaded is expected to download.
23 function __construct(&$project) {
24 $this->project = $project;
28 * Returns a temp directory to work in.
30 * @param null $namespace
31 * The optional namespace of the temp directory, defaults to the classname.
34 function get_temp_directory($namespace = NULL) {
35 if (is_null($namespace)) {
36 $reflect = new \ReflectionClass($this);
37 $namespace = $reflect->getShortName();
40 file_directory_temp(),
41 'hacked-cache-' . get_current_user(),
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);
54 * Returns a directory to save the downloaded project into.
56 function get_destination() {
57 $type = $this->project->project_type;
58 $name = $this->project->name;
59 $version = $this->project->existing_version;
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);
68 return "$dir/$version";
72 * Returns the final destination of the unpacked project.
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";
84 $module_dir = $dir . '/' . $name . '-' . $version;
90 * Download the remote files to the local filesystem.
97 * Recursively delete all files and folders in the specified filepath, then
98 * delete the containing folder.
100 * Note that this only deletes visible files with write permission.
102 * @param string $path
103 * A filepath relative to file_directory_path.
105 function remove_dir($path) {
106 if (is_file($path) || is_link($path)) {
109 elseif (is_dir($path)) {
111 while (($entry = $d->read()) !== FALSE) {
112 if ($entry == '.' || $entry == '..') {
115 $entry_path = $path . '/' . $entry;
116 $this->remove_dir($entry_path);
122 $message = $this->t('Unknown file type(%path) stat: %stat ', [
124 '%stat' => print_r(stat($path), 1)
126 \Drupal::logger('hacked')->error($message);