hackedProjectWebDownloader.php in Hacked! 8.2
File
src/hackedProjectWebDownloader.php
View source
<?php
namespace Drupal\hacked;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class hackedProjectWebDownloader {
use StringTranslationTrait;
var $project;
function __construct(&$project) {
$this->project = $project;
}
function get_temp_directory($namespace = NULL) {
if (is_null($namespace)) {
$reflect = new \ReflectionClass($this);
$namespace = $reflect
->getShortName();
}
$segments = [
\Drupal::service('file_system')
->getTempDirectory(),
'hacked-cache-' . get_current_user(),
$namespace,
];
$dir = implode('/', array_filter($segments));
if (!\Drupal::service('file_system')
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
$message = $this
->t('Failed to create temp directory: %dir', array(
'%dir' => $dir,
));
\Drupal::logger('hacked')
->error($message);
return FALSE;
}
return $dir;
}
function get_destination() {
$type = $this->project->project_type;
$name = $this->project->name;
$version = $this->project->existing_version;
$dir = $this
->get_temp_directory() . "/{$type}/{$name}";
if (!\Drupal::service('file_system')
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
$message = $this
->t('Failed to create temp directory: %dir', [
'%dir' => $dir,
]);
\Drupal::logger('hacked')
->error($message);
return FALSE;
}
return "{$dir}/{$version}";
}
function get_final_destination() {
$dir = $this
->get_destination();
$name = $this->project->name;
$version = $this->project->existing_version;
$type = $this->project->project_type;
if ($type != 'core') {
$module_dir = $dir . "/{$name}";
}
else {
$module_dir = $dir . '/' . $name . '-' . $version;
}
return $module_dir;
}
function download() {
}
function remove_dir($path) {
if (is_file($path) || is_link($path)) {
unlink($path);
}
elseif (is_dir($path)) {
$d = dir($path);
while (($entry = $d
->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry_path = $path . '/' . $entry;
$this
->remove_dir($entry_path);
}
$d
->close();
rmdir($path);
}
else {
$message = $this
->t('Unknown file type(%path) stat: %stat ', [
'%path' => $path,
'%stat' => print_r(stat($path), 1),
]);
\Drupal::logger('hacked')
->error($message);
}
}
}