跳到內容

安裝 NextPDF Cloudflare 橋接器

使用 Composer 安裝此套件,接著加入一個 PSR-18 HTTP 用戶端與一個 PSR-17 工廠實作。這個橋接器會宣告這些介面;本身並不隨附任何具體的 HTTP 用戶端。

這個橋接器會從 composer.jsonrequire 區段讀取以下資訊:

相依套件版本限制
PHP>=8.4 <9.0
nextpdf/core^3.0
psr/http-client (PSR-18)^1.0
psr/http-factory (PSR-17)^1.1
psr/log (PSR-3)^3.0

此套件會以 src/Cloudflare/ 作為來源,自動載入 NextPDF\Cloudflare\(PSR-4)。它通過 PHPStan Level 10 靜態分析,且不隨附任何分析器基準檔。

Terminal window
composer require nextpdf/cloudflare

這個橋接器的建構式需要一個 PSR-18 ClientInterface、一個 PSR-17 RequestFactoryInterface,以及一個 PSR-17 StreamFactoryInterface。如果你有提供,固定傳輸路徑也會使用一個 PSR-17 ResponseFactoryInterface。任何符合規範的實作都能運作。README 以 Guzzle 作為其中一個範例:

Terminal window
composer require guzzlehttp/guzzle

Guzzle 7 提供全部四個 PSR-17 工廠介面以及 PSR-18 用戶端。Symfony HttpClient、Buzz 以及其他符合規範的技術堆疊也同樣適用。此套件的設計與供應商無關。tests/Unit/Architecture/PsrConformanceTest.php 測試會斷言此套件僅相依於 PSR 契約,而不相依於任何具體的用戶端。

當 Worker 無法連線且已啟用後援時,這個橋接器可以委派給本機算繪器。你需要透過 NextPDF\Cloudflare\Contract\LocalRendererFactoryInterface 提供該算繪器。composer.jsonsuggest 區塊指出第一方實作:

Terminal window
composer require nextpdf/artisan

僅安裝 nextpdf/artisan 本身並不足夠。你還必須將一個 LocalRendererFactoryInterface 傳入算繪器的建構式。這個橋接器會偵測 Artisan 已安裝但尚未接上任何工廠的情況,接著引發一個 CloudflareNotAvailableException,其訊息會明確說明這一點。接線方式請參閱 /integrations/cloudflare/production-usage/。

<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use NextPDF\Cloudflare\CloudflareRendererConfig;
$config = new CloudflareRendererConfig(
workerUrl: 'https://pdf-renderer.example.workers.dev/render',
apiToken: 'placeholder',
);
echo $config->isValid() ? "ready\n" : "incomplete\n";

isValid() 會在 workerUrlapiToken 兩者皆非空白時傳回 true。它不會連線至網路;這純粹是一項組態完整性檢查,適合用於部署時的冒煙測試。

  • /integrations/cloudflare/configuration/ — 完整的組態介面。
  • /integrations/cloudflare/quickstart/ — 你的第一次算繪。
  • /integrations/cloudflare/boot-and-discovery/ — 將這個橋接器接入框架容器。