'object', 'properties' => [ 'path' => [ 'type' => 'string', 'description' => 'Каталог относительно корня. По умолчанию корень.', ], ], 'required' => [], 'additionalProperties' => false, ]; } public function handle(array $args): string { $path = (string) ($args['path'] ?? ''); try { $dir = $this->sandbox->resolve($path); } catch (\RuntimeException $e) { return $e->getMessage(); } if (!is_dir($dir)) { return 'Это не каталог: ' . $path; } $entries = @scandir($dir); if ($entries === false) { return 'Не удалось прочитать каталог.'; } $entries = array_values(array_filter( $entries, static fn (string $name): bool => $name !== '.' && $name !== '..' )); sort($entries); $dirs = []; $files = []; foreach ($entries as $name) { $full = $dir . DIRECTORY_SEPARATOR . $name; if (is_dir($full)) { $dirs[] = $name . '/'; continue; } $size = @filesize($full); $files[] = $name . ' (' . $this->human($size === false ? 0 : $size) . ')'; } $all = array_merge($dirs, $files); $shown = array_slice($all, 0, self::MAX_ENTRIES); $title = $this->sandbox->relative($dir); $text = 'Каталог ' . $title . ': ' . count($all) . " объектов\n" . implode("\n", $shown); if (count($all) > count($shown)) { $text .= "\n…и ещё " . (count($all) - count($shown)); } return $text; } private function human(int $bytes): string { if ($bytes >= 1048576) { return round($bytes / 1048576, 1) . ' МБ'; } if ($bytes >= 1024) { return round($bytes / 1024, 1) . ' КБ'; } return $bytes . ' Б'; } }