function ( $downloadTotal, $downloadedBytes, $uploadTotal, $uploadedBytes ) use ($progressBar) { if ($downloadTotal != 0) { $progressBar->update(intval(floor($downloadedBytes / $downloadTotal * 1000))); } }, )); $request = new Request('get', $url); $promise = $client->sendAsync($request, array( 'sink' => $filePath, )); $promise->then(function (Response $resp) { echo 'download completed' . PHP_EOL; }, function (RequestException $e) { throw $e; }); $promise->wait(); echo 'extracting' . PHP_EOL; $pharData = new PharData($filePath); $pharData->decompress(); $pharData = new PharData(substr($filePath, 0, -3)); $pharData->extractTo(self::$tmpPath); } /** * 安装Typecho * * @return void */ public static function installTypecho() { exec(sprintf( "echo 'DROP DATABASE IF EXISTS `%s`; CREATE DATABASE `%s`;' | mysql -u %s --password=%s", getenv('MYSQL_DB'), getenv('MYSQL_DB'), getenv('MYSQL_USER'), getenv('MYSQL_PWD') )); exec(sprintf( 'mysql -u %s --password=%s %s < %s', getenv('MYSQL_USER'), getenv('MYSQL_PWD'), getenv('MYSQL_DB'), __DIR__ . '/typecho.sql' )); $configFileContent = sprintf('addServer(array ( "host" => "%s", "user" => "%s", "password" => "%s", "charset" => "utf8mb4", "port" => "3306", "database" => "%s", "engine" => "InnoDB", ), Typecho_Db::READ | Typecho_Db::WRITE); Typecho_Db::set($db); define("WEB_SERVER_PORT", "%s"); define("FORKED_WEB_SERVER_PORT", "%s"); define("IN_PHPUNIT_SERVER", true); ', getenv('MYSQL_HOST'), getenv('MYSQL_USER'), getenv('MYSQL_PWD'), getenv('MYSQL_DB'), getenv('WEB_SERVER_PORT'), getenv('FORKED_WEB_SERVER_PORT')); file_put_contents(self::$typechoDir . '/config.inc.php', $configFileContent); $pluginDir = self::$typechoDir . '/usr/plugins/Restful'; try { self::deleteDir($pluginDir); } catch (Exception $e) {} self::mkdirs($pluginDir); copy(__DIR__ . '/../Plugin.php', $pluginDir . '/Plugin.php'); copy(__DIR__ . '/../Action.php', $pluginDir . '/Action.php'); file_put_contents(self::$typechoDir . '/restful.php', "get('action')], ['Restful']); "); $actionUrl = 'http://' . getenv('WEB_SERVER_HOST') . ':' . getenv('WEB_SERVER_PORT') . '/restful.php'; $client = new Client(array( 'allow_redirects' => false, )); $client->get($actionUrl . '?action=deactivate'); $client->get($actionUrl . '?action=activate'); } /** * 递归删除目录 * * @param string $dirPath 目录路径 * @param boolean $removeOnlyChildren * @return void */ private static function deleteDir($dirPath, $removeOnlyChildren = false) { if (empty($dirPath) || file_exists($dirPath) === false) { return false; } if (is_file($dirPath) || is_link($dirPath)) { return unlink($dirPath); } $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); // $fileinfo as SplFileInfo foreach ($files as $fileinfo) { if ($fileinfo->isDir()) { if (self::deleteDir($fileinfo->getRealPath()) === false) { return false; } } else { if (unlink($fileinfo->getRealPath()) === false) { return false; } } } if ($removeOnlyChildren === false) { return rmdir($dirPath); } return true; } /** * 创建目录 * * @param string $pathname 目录路径 * @param integer $mode 目录权限 * @return boolean */ private static function mkdirs($pathname, $mode = 0755) { is_dir(dirname($pathname)) || self::mkdirs(dirname($pathname), $mode); return is_dir($pathname) || @mkdir($pathname, $mode); // @codingStandardsIgnoreLine } }