This commit is contained in:
2019-09-27 00:29:18 +02:00
parent 40af43681e
commit b857814072
470 changed files with 37562 additions and 10060 deletions
+59 -49
View File
@@ -1,8 +1,9 @@
<?php
/**
* @package Grav.Common.GPM
* @package Grav\Common\GPM
*
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
@@ -14,23 +15,23 @@ use Grav\Common\Grav;
class Installer
{
/** @const No error */
const OK = 0;
public const OK = 0;
/** @const Target already exists */
const EXISTS = 1;
public const EXISTS = 1;
/** @const Target is a symbolic link */
const IS_LINK = 2;
public const IS_LINK = 2;
/** @const Target doesn't exist */
const NOT_FOUND = 4;
public const NOT_FOUND = 4;
/** @const Target is not a directory */
const NOT_DIRECTORY = 8;
public const NOT_DIRECTORY = 8;
/** @const Target is not a Grav instance */
const NOT_GRAV_ROOT = 16;
public const NOT_GRAV_ROOT = 16;
/** @const Error while trying to open the ZIP package */
const ZIP_OPEN_ERROR = 32;
public const ZIP_OPEN_ERROR = 32;
/** @const Error while trying to extract the ZIP package */
const ZIP_EXTRACT_ERROR = 64;
public const ZIP_EXTRACT_ERROR = 64;
/** @const Invalid source file */
const INVALID_SOURCE = 128;
public const INVALID_SOURCE = 128;
/**
* Destination folder on which validation checks are applied
@@ -39,12 +40,12 @@ class Installer
protected static $target;
/**
* @var integer Error Code
* @var int Error Code
*/
protected static $error = 0;
/**
* @var integer Zip Error Code
* @var int Zip Error Code
*/
protected static $error_zip = 0;
@@ -74,9 +75,10 @@ class Installer
* @param string $destination The local path to the Grav Instance
* @param array $options Options to use for installing. ie, ['install_path' => 'user/themes/antimatter']
* @param string $extracted The local path to the extacted ZIP package
* @param bool $keepExtracted True if you want to keep the original files
* @return bool True if everything went fine, False otherwise.
*/
public static function install($zip, $destination, $options = [], $extracted = null)
public static function install($zip, $destination, $options = [], $extracted = null, $keepExtracted = false)
{
$destination = rtrim($destination, DS);
$options = array_merge(self::$options, $options);
@@ -88,15 +90,15 @@ class Installer
return false;
}
if (self::lastErrorCode() == self::IS_LINK && $options['ignore_symlinks'] ||
self::lastErrorCode() == self::EXISTS && !$options['overwrite']
if ((self::lastErrorCode() === self::IS_LINK && $options['ignore_symlinks']) ||
(self::lastErrorCode() === self::EXISTS && !$options['overwrite'])
) {
return false;
}
// Create a tmp location
$tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
$tmp = $tmp_dir . '/Grav-' . uniqid();
$tmp = $tmp_dir . '/Grav-' . uniqid('', false);
if (!$extracted) {
$extracted = self::unZip($zip, $tmp);
@@ -111,7 +113,6 @@ class Installer
return false;
}
$is_install = true;
$installer = self::loadInstaller($extracted, $is_install);
@@ -140,7 +141,7 @@ class Installer
self::moveInstall($extracted, $install_path);
}
} else {
self::sophisticatedInstall($extracted, $install_path, $options['ignores']);
self::sophisticatedInstall($extracted, $install_path, $options['ignores'], $keepExtracted);
}
Folder::delete($tmp);
@@ -165,8 +166,8 @@ class Installer
/**
* Unzip a file to somewhere
*
* @param $zip_file
* @param $destination
* @param string $zip_file
* @param string $destination
* @return bool|string
*/
public static function unZip($zip_file, $destination)
@@ -175,7 +176,7 @@ class Installer
$archive = $zip->open($zip_file);
if ($archive === true) {
Folder::mkdir($destination);
Folder::create($destination);
$unzip = $zip->extractTo($destination);
@@ -189,9 +190,8 @@ class Installer
$package_folder_name = preg_replace('#\./$#', '', $zip->getNameIndex(0));
$zip->close();
$extracted_folder = $destination . '/' . $package_folder_name;
return $extracted_folder;
return $destination . '/' . $package_folder_name;
}
self::$error = self::ZIP_EXTRACT_ERROR;
@@ -216,7 +216,7 @@ class Installer
$install_file = $installer_file_folder . DS . 'install.php';
if (file_exists($install_file)) {
require_once($install_file);
require_once $install_file;
} else {
return null;
}
@@ -253,8 +253,8 @@ class Installer
}
/**
* @param $source_path
* @param $install_path
* @param string $source_path
* @param string $install_path
*
* @return bool
*/
@@ -270,8 +270,8 @@ class Installer
}
/**
* @param $source_path
* @param $install_path
* @param string $source_path
* @param string $install_path
*
* @return bool
*/
@@ -279,24 +279,26 @@ class Installer
{
if (empty($source_path)) {
throw new \RuntimeException("Directory $source_path is missing");
} else {
Folder::rcopy($source_path, $install_path);
}
Folder::rcopy($source_path, $install_path);
return true;
}
/**
* @param $source_path
* @param $install_path
* @param string $source_path
* @param string $install_path
* @param array $ignores
* @param bool $keep_source
*
* @return bool
*/
public static function sophisticatedInstall($source_path, $install_path, $ignores = [])
public static function sophisticatedInstall($source_path, $install_path, $ignores = [], $keep_source = false)
{
foreach (new \DirectoryIterator($source_path) as $file) {
if ($file->isLink() || $file->isDot() || in_array($file->getFilename(), $ignores)) {
if ($file->isLink() || $file->isDot() || \in_array($file->getFilename(), $ignores, true)) {
continue;
}
@@ -304,7 +306,11 @@ class Installer
if ($file->isDir()) {
Folder::delete($path);
Folder::move($file->getPathname(), $path);
if ($keep_source) {
Folder::copy($file->getPathname(), $path);
} else {
Folder::move($file->getPathname(), $path);
}
if ($file->getFilename() === 'bin') {
foreach (glob($path . DS . '*') as $bin_file) {
@@ -326,7 +332,7 @@ class Installer
* @param string $path The slug of the package(s)
* @param array $options Options to use for uninstalling
*
* @return boolean True if everything went fine, False otherwise.
* @return bool True if everything went fine, False otherwise.
*/
public static function uninstall($path, $options = [])
{
@@ -368,7 +374,7 @@ class Installer
* @param string $destination The directory to run validations at
* @param array $exclude An array of constants to exclude from the validation
*
* @return boolean True if validation passed. False otherwise
* @return bool True if validation passed. False otherwise
*/
public static function isValidDestination($destination, $exclude = [])
{
@@ -385,11 +391,11 @@ class Installer
self::$error = self::NOT_DIRECTORY;
}
if (count($exclude) && in_array(self::$error, $exclude)) {
if (\count($exclude) && \in_array(self::$error, $exclude, true)) {
return true;
}
return !(self::$error);
return !self::$error;
}
/**
@@ -397,7 +403,7 @@ class Installer
*
* @param string $target The local path to the Grav Instance
*
* @return boolean True if is a Grav Instance. False otherwise
* @return bool True if is a Grav Instance. False otherwise
*/
public static function isGravInstance($target)
{
@@ -469,23 +475,23 @@ class Installer
if (self::$error_zip) {
switch(self::$error_zip) {
case \ZipArchive::ER_EXISTS:
$msg .= "File already exists.";
$msg .= 'File already exists.';
break;
case \ZipArchive::ER_INCONS:
$msg .= "Zip archive inconsistent.";
$msg .= 'Zip archive inconsistent.';
break;
case \ZipArchive::ER_MEMORY:
$msg .= "Malloc failure.";
$msg .= 'Memory allocation failure.';
break;
case \ZipArchive::ER_NOENT:
$msg .= "No such file.";
$msg .= 'No such file.';
break;
case \ZipArchive::ER_NOZIP:
$msg .= "Not a zip archive.";
$msg .= 'Not a zip archive.';
break;
case \ZipArchive::ER_OPEN:
@@ -493,16 +499,20 @@ class Installer
break;
case \ZipArchive::ER_READ:
$msg .= "Read error.";
$msg .= 'Read error.';
break;
case \ZipArchive::ER_SEEK:
$msg .= "Seek error.";
$msg .= 'Seek error.';
break;
}
}
break;
case self::INVALID_SOURCE:
$msg = 'Invalid source file';
break;
default:
$msg = 'Unknown Error';
break;
@@ -513,7 +523,7 @@ class Installer
/**
* Returns the last error code of the occurred error
* @return integer The code of the last error
* @return int|string The code of the last error
*/
public static function lastErrorCode()
{