mirror of
https://github.com/processwire/processwire.git
synced 2025-08-17 04:04:13 +02:00
Minor code adjustments in ProcessModuleInstall
This commit is contained in:
@@ -11,8 +11,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class ProcessModuleInstall extends Wire {
|
class ProcessModuleInstall extends Wire {
|
||||||
|
|
||||||
protected $tempDir = '';
|
/**
|
||||||
|
* @var WireTempDir
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private $tempDir = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a temporary directory (path) for use by this object
|
* Returns a temporary directory (path) for use by this object
|
||||||
@@ -114,7 +118,8 @@ class ProcessModuleInstall extends Wire {
|
|||||||
static $level = 0;
|
static $level = 0;
|
||||||
$level++;
|
$level++;
|
||||||
$files = array();
|
$files = array();
|
||||||
if(!$path) $path = $this->wire('config')->paths->siteModules;
|
|
||||||
|
if(!$path) $path = $this->wire()->config->paths->siteModules;
|
||||||
|
|
||||||
// find the names of all existing module files, so we can defer to their dirs
|
// find the names of all existing module files, so we can defer to their dirs
|
||||||
// if a module is being installed that already exists
|
// if a module is being installed that already exists
|
||||||
@@ -158,7 +163,8 @@ class ProcessModuleInstall extends Wire {
|
|||||||
$moduleFiles1 = array(); // level1 module files (those in closest dir or subdir)
|
$moduleFiles1 = array(); // level1 module files (those in closest dir or subdir)
|
||||||
$moduleDirs = array(); // all module dirs found
|
$moduleDirs = array(); // all module dirs found
|
||||||
$moduleDir = ''; // recommended name for module dir (returned by this method)
|
$moduleDir = ''; // recommended name for module dir (returned by this method)
|
||||||
if(!$modulePath) $modulePath = $this->wire('config')->paths->siteModules;
|
|
||||||
|
if(!$modulePath) $modulePath = $this->wire()->config->paths->siteModules;
|
||||||
$tempDir = $this->getTempDir();
|
$tempDir = $this->getTempDir();
|
||||||
|
|
||||||
foreach($files as $key => $f) {
|
foreach($files as $key => $f) {
|
||||||
@@ -272,26 +278,36 @@ class ProcessModuleInstall extends Wire {
|
|||||||
/**
|
/**
|
||||||
* Unzip the module file to tempDir and then copy to destination directory
|
* Unzip the module file to tempDir and then copy to destination directory
|
||||||
*
|
*
|
||||||
* @param string $file File to unzip
|
* @param string $zipFile File to unzip
|
||||||
* @param string $destinationDir Directory to copy completed files into. Optionally omit to determine automatically.
|
* @param string $destinationDir Directory to copy completed files into. Optionally omit to determine automatically.
|
||||||
* @return bool|string Returns destinationDir on success, false on failure
|
* @return bool|string Returns destinationDir on success, false on failure
|
||||||
* @throws WireException
|
* @throws WireException
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function unzipModule($file, $destinationDir = '') {
|
public function unzipModule($zipFile, $destinationDir = '') {
|
||||||
|
|
||||||
|
$config = $this->wire()->config;
|
||||||
|
|
||||||
$success = false;
|
$success = false;
|
||||||
$tempDir = $this->getTempDir();
|
$tempDir = $this->getTempDir();
|
||||||
$mkdirDestination = false;
|
$mkdirDestination = false;
|
||||||
|
$fileTools = $this->wire()->files;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$files = $this->wire('files')->unzip($file, $tempDir);
|
$files = $fileTools->unzip($zipFile, $tempDir);
|
||||||
if(is_file($file)) $this->wire('files')->unlink($file, true);
|
if(is_file($zipFile)) $fileTools->unlink($zipFile, true);
|
||||||
foreach($files as $f) $this->message("Extracted: $f", Notice::debug);
|
$qty = count($files);
|
||||||
|
if($qty < 100 && $config->debug) {
|
||||||
|
foreach($files as $f) {
|
||||||
|
$this->message(sprintf($this->_('Extracted: %s'), $f));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->message(sprintf($this->_n('Extracted %d file', 'Extracted %d files', $qty)));
|
||||||
|
}
|
||||||
|
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
$this->error($e->getMessage());
|
$this->error($e->getMessage());
|
||||||
if(is_file($file)) $this->wire('files')->unlink($file, true);
|
if(is_file($zipFile)) $fileTools->unlink($zipFile, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,20 +325,20 @@ class ProcessModuleInstall extends Wire {
|
|||||||
// destination dir already there, perhaps an older version of same module?
|
// destination dir already there, perhaps an older version of same module?
|
||||||
// create a backup of it
|
// create a backup of it
|
||||||
$hasBackup = $this->backupDir($destinationDir);
|
$hasBackup = $this->backupDir($destinationDir);
|
||||||
if($hasBackup) $this->wire('files')->mkdir($destinationDir, true);
|
if($hasBackup) $fileTools->mkdir($destinationDir, true);
|
||||||
} else {
|
} else {
|
||||||
if($this->wire('files')->mkdir($destinationDir, true)) $mkdirDestination = true;
|
if($fileTools->mkdir($destinationDir, true)) $mkdirDestination = true;
|
||||||
$hasBackup = false;
|
$hasBackup = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// label to identify destinationDir in messages and errors
|
// label to identify destinationDir in messages and errors
|
||||||
$dirLabel = str_replace($this->config->paths->root, '/', $destinationDir);
|
$dirLabel = str_replace($config->paths->root, '/', $destinationDir);
|
||||||
|
|
||||||
if(is_dir($destinationDir)) {
|
if(is_dir($destinationDir)) {
|
||||||
$from = $tempDir . $extractedDir;
|
$from = $tempDir . $extractedDir;
|
||||||
if($this->wire('files')->copy($from, $destinationDir)) {
|
if($fileTools->copy($from, $destinationDir)) {
|
||||||
$this->message($this->_('Successfully copied files to new directory:') . ' ' . $dirLabel);
|
$this->message($this->_('Successfully copied files to new directory:') . ' ' . $dirLabel);
|
||||||
$this->wire('files')->chmod($destinationDir, true);
|
$fileTools->chmod($destinationDir, true);
|
||||||
$success = true;
|
$success = true;
|
||||||
} else {
|
} else {
|
||||||
$this->error($this->_('Unable to copy files to new directory:') . ' ' . $dirLabel);
|
$this->error($this->_('Unable to copy files to new directory:') . ' ' . $dirLabel);
|
||||||
@@ -334,53 +350,72 @@ class ProcessModuleInstall extends Wire {
|
|||||||
|
|
||||||
if(!$success) {
|
if(!$success) {
|
||||||
$this->error($this->_('Unable to copy module files:') . ' ' . $dirLabel);
|
$this->error($this->_('Unable to copy module files:') . ' ' . $dirLabel);
|
||||||
if($mkdirDestination && !$this->wire('files')->rmdir($destinationDir, true)) {
|
if($mkdirDestination && !$fileTools->rmdir($destinationDir, true)) {
|
||||||
$this->error($this->_('Could not delete failed module dir:') . ' ' . $destinationDir, Notice::log);
|
$this->error($this->_('Could not delete failed module dir:') . ' ' . $destinationDir, Notice::log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $success ? $destinationDir : false;
|
return $success ? $destinationDir : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a backup of a module directory
|
||||||
|
*
|
||||||
|
* @param string $moduleDir
|
||||||
|
* @return bool
|
||||||
|
* @throws WireException
|
||||||
|
*
|
||||||
|
*/
|
||||||
protected function backupDir($moduleDir) {
|
protected function backupDir($moduleDir) {
|
||||||
|
$files = $this->wire()->files;
|
||||||
|
$config = $this->wire()->config;
|
||||||
|
|
||||||
$dir = rtrim($moduleDir, "/");
|
$dir = rtrim($moduleDir, "/");
|
||||||
$name = basename($dir);
|
$name = basename($dir);
|
||||||
$parentDir = dirname($dir);
|
$parentDir = dirname($dir);
|
||||||
$backupDir = "$parentDir/.$name/";
|
$backupDir = "$parentDir/.$name/";
|
||||||
if(is_dir($backupDir)) wireRmdir($backupDir, true); // if there's already an old backup copy, remove it
|
if(is_dir($backupDir)) $files->rmdir($backupDir, true); // if there's already an old backup copy, remove it
|
||||||
$success = false;
|
$success = false;
|
||||||
|
|
||||||
if(is_link(rtrim($moduleDir, '/'))) {
|
if(is_link(rtrim($moduleDir, '/'))) {
|
||||||
// module directory is a symbolic link
|
// module directory is a symbolic link
|
||||||
// copy files from symlink dir to real backup dir
|
// copy files from symlink dir to real backup dir
|
||||||
$success = $this->wire('files')->copy($moduleDir, $backupDir);
|
$success = $files->copy($moduleDir, $backupDir);
|
||||||
// remove symbolic link
|
// remove symbolic link
|
||||||
unlink(rtrim($moduleDir, '/'));
|
unlink(rtrim($moduleDir, '/'));
|
||||||
$dir = str_replace($this->wire('config')->paths->root, '/', $moduleDir);
|
$dir = str_replace($config->paths->root, '/', $moduleDir);
|
||||||
$this->warning(sprintf(
|
$this->warning(sprintf(
|
||||||
$this->_('Please note that %s was a symbolic link and has been converted to a regular directory'), $dir
|
$this->_('Please note that %s was a symbolic link and has been converted to a regular directory'), $dir
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
// module is a regular directory
|
// module is a regular directory
|
||||||
// just rename it to become the new backup dir
|
// just rename it to become the new backup dir
|
||||||
if($this->wire('files')->rename($moduleDir, $backupDir)) $success = true;
|
if($files->rename($moduleDir, $backupDir)) $success = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($success) {
|
if($success) {
|
||||||
$this->message(sprintf($this->_('Backed up existing %s'), $name) . " => " . str_replace($this->wire('config')->paths->root, '/', $backupDir));
|
$this->message(sprintf($this->_('Backed up existing %s'), $name) . " => " . str_replace($config->paths->root, '/', $backupDir));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restore a module directory
|
||||||
|
*
|
||||||
|
* @param string $moduleDir
|
||||||
|
* @return bool
|
||||||
|
* @throws WireException
|
||||||
|
*
|
||||||
|
*/
|
||||||
protected function restoreDir($moduleDir) {
|
protected function restoreDir($moduleDir) {
|
||||||
$dir = rtrim($moduleDir, "/");
|
$dir = rtrim($moduleDir, "/");
|
||||||
$name = basename($dir);
|
$name = basename($dir);
|
||||||
$parentDir = dirname($dir);
|
$parentDir = dirname($dir);
|
||||||
$backupDir = "$parentDir/.$name/";
|
$backupDir = "$parentDir/.$name/";
|
||||||
if(is_dir($backupDir)) {
|
if(is_dir($backupDir)) {
|
||||||
wireRmdir($moduleDir, true); // if there's already an old backup copy, remove it
|
$this->wire()->files->rmdir($moduleDir, true); // if there's already an old backup copy, remove it
|
||||||
if(rename($backupDir, $moduleDir)) {
|
if(rename($backupDir, $moduleDir)) {
|
||||||
$this->message(sprintf($this->_('Restored backup of %s'), $name) . " => $moduleDir");
|
$this->message(sprintf($this->_('Restored backup of %s'), $name) . " => $moduleDir");
|
||||||
}
|
}
|
||||||
@@ -459,7 +494,7 @@ class ProcessModuleInstall extends Wire {
|
|||||||
|
|
||||||
// download the zip file and save it in assets directory
|
// download the zip file and save it in assets directory
|
||||||
$success = false;
|
$success = false;
|
||||||
$http = $this->wire(new WireHttp());
|
$http = $this->wire(new WireHttp()); /** @var WireHttp $http */
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$file = $http->download($url, $tempZIP); // throws exceptions on any error
|
$file = $http->download($url, $tempZIP); // throws exceptions on any error
|
||||||
@@ -472,7 +507,7 @@ class ProcessModuleInstall extends Wire {
|
|||||||
|
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
$this->error($e->getMessage());
|
$this->error($e->getMessage());
|
||||||
$this->wire('files')->unlink($tempZIP);
|
$this->wire()->files->unlink($tempZIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $success ? $destinationDir : false;
|
return $success ? $destinationDir : false;
|
||||||
|
Reference in New Issue
Block a user