google-api-php-client
google-api-php-client copied to clipboard
Call to undefined function GuzzleHttp\Promise\settle()
Although I installed the latest version of "google-api-php-client", we can't fix the "Promise" error. I'm updating with "Composer" but we can't fix the "Promise" error. What am I doing wrong?
The following code is the code to create a folder tree. `include DIR . '/google_drive_setup.php'; use GuzzleHttp\Client; use GuzzleHttp\Promise;
class GoogleDriveTreeView { private $service; private $folderId; private $client;
public function __construct($service, $folderId = 'root') {
$this->service = $service;
$this->folderId = $folderId;
$this->client = new Client();
ob_start();
ini_set('memory_limit', '-1');
ignore_user_abort(true);
set_time_limit(3600); // 1 saat
}
public function showSize($size_in_bytes) {
if ($size_in_bytes >= 1073741824) {
return number_format($size_in_bytes / 1073741824, 2) . ' GB';
} elseif ($size_in_bytes >= 1048576) {
return number_format($size_in_bytes / 1048576, 2) . ' MB';
} elseif ($size_in_bytes >= 1024) {
return number_format($size_in_bytes / 1024, 2) . ' KB';
} elseif ($size_in_bytes >= 1) {
return $size_in_bytes . ' Bayt';
} else {
return '0 Bayt';
}
}
private function fetchFiles($query) {
$pageToken = null;
$files = [];
do {
$response = $this->service->files->listFiles([
'q' => $query,
'pageToken' => $pageToken,
'fields' => 'nextPageToken, files(id, name, mimeType, size)',
'orderBy' => 'name',
]);
$files = array_merge($files, $response->files);
$pageToken = $response->nextPageToken;
} while ($pageToken);
return $files;
}
private function fetchFileDetailsParallel($fileIds) {
$promises = [];
foreach ($fileIds as $fileId) {
$promises[$fileId] = $this->client->getAsync("https://www.googleapis.com/drive/v3/files/{$fileId}?fields=size");
}
$results = Promise\settle($promises)->wait();
$sizes = [];
foreach ($results as $fileId => $result) {
if ($result['state'] === 'fulfilled') {
$sizes[$fileId] = json_decode($result['value']->getBody(), true)['size'] ?? 0;
} else {
$sizes[$fileId] = 0;
}
}
return $sizes;
}
public function getFilesAndFolders() {
$query = "'{$this->folderId}' in parents";
$files = $this->fetchFiles($query);
$drive_dizinler_arr = [];
$drive_dosyalar_arr = [];
$fileIds = array_map(function($file) { return $file->id; }, $files);
$sizes = $this->fetchFileDetailsParallel($fileIds);
foreach ($files as $file) {
if ($file->mimeType == 'application/vnd.google-apps.folder') {
$drive_dizinler_arr[$file->id][$this->showSize($sizes[$file->id])] = $file->name;
} else {
$drive_dosyalar_arr[$file->id][$this->showSize($sizes[$file->id])] = $file->name;
}
}
return [$drive_dizinler_arr, $drive_dosyalar_arr];
}
public function emptyDir($dirid) {
$query = "'$dirid' in parents";
$files = $this->fetchFiles($query);
return count($files);
}
public function generateList() {
list($drive_dizinler_arr, $drive_dosyalar_arr) = $this->getFilesAndFolders();
$list = '<ul id="uzak" class="filetree" style="display: none;">';
foreach ($drive_dizinler_arr as $id => $arr_devam) {
foreach ($arr_devam as $boyutu => $dizin_adi) {
if ($this->emptyDir($id) == '0') {
$list .= '<li class="folder collapsed"><a href="#" rel="' . $id . '" adi="' . $dizin_adi . '">' . $dizin_adi . '<span style="float: right;color: black;padding-right: 10px;">4 KB</span></a></li>';
} else {
$list .= '<li class="folder_plus collapsed"><a href="#" rel="' . $id . '" adi="' . $dizin_adi . '">' . $dizin_adi . '<span style="float: right;color: black;padding-right: 10px;">4 KB</span></a></li>';
}
}
}
foreach ($drive_dosyalar_arr as $id => $devam_arr) {
foreach ($devam_arr as $boyutu => $dosya_adi) {
$ext = preg_replace('/^.*\./', '', $dosya_adi);
$list .= '<li class="file ext_' . $ext . '"><a href="#" rel="' . $id . '" adi="' . $dosya_adi . '">' . $dosya_adi . '<span style="float: right;color: black;padding-right: 10px;">' . $boyutu . '</span></a></li>';
}
}
$list .= '</ul>';
return $list;
}
}
$folderId = isset($_POST['dir']) ? $_POST['dir'] : 'root'; $driveManager = new GoogleDriveTreeView($service, $folderId); echo $driveManager->generateList(); `