コンテンツにスキップ

NextPDF CodeIgniter インテグレーション

パッケージをインストールすると、CodeIgniter 4 に自動的に組み込まれます。このページは、その組み込みに関するリファレンスです。検出、バインディングモデル、構成の公開、そして統合が動作することを確認する単一メソッドのスモークテストについて説明します。

Terminal window
composer require nextpdf/codeigniter

サービスプロバイダーを編集したり、ブートストラップファイルを変更したりする必要はありません。検証済みのバージョン制約については、/integrations/codeigniter/install/ を参照してください。

CodeIgniter 4 は、Composer のパッケージ検出を通じてパッケージを見つけます。これは、フレームワークのデフォルトである Config\Modules::$discoverInComposertrue の場合に行われます。PSR-4 プレフィックス NextPDF\CodeIgniter\src/CodeIgniter/ にマッピングされます。このマッピングにより、フレームワークは NextPDF\CodeIgniter\Config\Services を該当ファイルに resolve(解決)します(PSR-4 §x1.x3)。/integrations/codeigniter/boot-and-discovery/ では、完全なシーケンスと、それを制御する Config\Modules のプロパティについて説明しています。

CodeIgniter 4 は PSR-11 コンテナーを同梱していません。PSR-11 §1.3 では、サービスロケーターパターンを推奨していません(SHOULD NOT の助動詞)。このパッケージはフレームワークのロケーター規約に従いながら、その使用を最小限に抑えています。各バインディングは、bool $getShared パラメーターを持つ名前付きの静的ファクトリー 1 つです。

サービス名戻り値デフォルトの有効期間
fontRegistryFontRegistryInterface共有
imageRegistryImageRegistry共有
documentFactoryDocumentFactoryInterface共有
pdfDocumentDocument新規
pdfPdf新規
tsaClient?TsaClient共有
pdfSigner?SignerInterface新規

どちらのエントリーポイントから解決しても同じです。

<?php
declare(strict_types=1);
use NextPDF\CodeIgniter\Config\Services;
$a = Services::pdf(false); // direct
$b = \service('pdf'); // helper → Services::pdf()

グローバルヘルパー pdf() および pdf_document() は、Services::pdf(false)Services::pdfDocument(false) をラップする薄いラッパーにすぎません。

パッケージの構成は NextPDF\CodeIgniter\Config\NextPdf に定義されています。これは non-final な BaseConfig です。これをオーバーライドする方法は、サポートされているものが 2 つあります。

1. クラスを拡張する(型付きで、バージョン管理対象です)。app/Config/NextPdf.php を作成します。

<?php
declare(strict_types=1);
namespace Config;
use NextPDF\CodeIgniter\Config\NextPdf as BaseNextPdf;
final class NextPdf extends BaseNextPdf
{
public int $imageCacheMb = 100;
}

CodeIgniter は、パッケージのデフォルトの代わりにアプリケーションのクラスを読み込みます。

2. .env でオーバーライドする(環境ごとに設定できます)。プレフィックスは、小文字の短いクラス名である nextpdf です。

nextpdf.imageCacheMb = 100
nextpdf.signature.enabled = true
nextpdf.signature.certificate = /etc/nextpdf/cert.pem

/integrations/codeigniter/configuration/ では、すべてのキーと配列のオーバーライドルールを説明しています。

CodeIgniter には、テスト対象となるサービスプロバイダーやバンドルクラスはありません。代わりに、同等のスモークテストでは、検出によりサービスが解決されること、および有効期間が指定どおりに動作することの 2 点を確認します。以下のテストは、パッケージ自身の機能テストのアサーションを反映した、実行可能なコントローラーアクションです。

<?php
declare(strict_types=1);
namespace App\Controllers;
use CodeIgniter\HTTP\ResponseInterface;
use NextPDF\CodeIgniter\Config\Services;
use NextPDF\CodeIgniter\Libraries\Pdf;
use NextPDF\Core\Document;
final class NextPdfSmokeController extends BaseController
{
public function check(): ResponseInterface
{
// Discovery resolved the services.
$document = Services::pdfDocument(false);
$library = Services::pdf(false);
// Documents are fresh per call (no cross-request leakage).
$freshIsolated = Services::pdfDocument(false) !== $document;
// Registries are shared singletons.
$registrySingleton = Services::fontRegistry() === Services::fontRegistry();
// Optional services degrade to null without Premium / TSA config.
$signerOptional = Services::pdfSigner(false) === null;
$ok = $document instanceof Document
&& $library instanceof Pdf
&& $freshIsolated
&& $registrySingleton
&& $signerOptional;
return $this->response
->setStatusCode($ok ? 200 : 500)
->setJSON([
'discovery' => $document instanceof Document,
'document_fresh_per_call' => $freshIsolated,
'registry_shared' => $registrySingleton,
'signer_optional_null' => $signerOptional,
]);
}
}

このアクションにルートをマッピングしてリクエストします。ステータスコード 200 のレスポンスで、すべてのフラグが true に設定されていれば、統合が正しく機能していることを確認できます。ここでの各アサーションは、パッケージの機能テストスイートで検証済みの動作に対応しています。

  • ホストアプリケーションが Composer の検出を無効にしている場合は、nextpdf/codeigniterConfig\Modules::$composerPackages['only'] に追加してください。
  • Services::pdfDocument(true) は、共有のドキュメントを返します。これはテスト時のリセット専用です。リクエストコードやジョブコードでは、共有のドキュメントを決してリクエストしないでください。
  • Services::pdfSigner()Services::tsaClient() は、署名または TSA URL が構成されるまでは null を返します。これは意図された正常な機能低下であり、障害ではありません。
  • モジュール検出のためのクラスパスのマッピング(PSR-4 Autoloader §x1.x3)。
  • バインディングモデルに関するサービスロケーターのガイダンス(PSR-11 Container §1.3)。

NextPDF のコアは Apache-2.0 です。Pro および Enterprise のサービスは、インストールされると同じ Services インターフェイス上で利用できます。CodeIgniter パッケージは、対応するサービスメソッドを公開します。それぞれのメソッドは、対応する Premium パッケージがインストールされるまで null を返します。</get-license/?intent=codeigniter> を参照してください。

  • /integrations/codeigniter/boot-and-discovery/ — 検出の内部動作。
  • /integrations/codeigniter/install/ — バージョン制約と検証。
  • /integrations/codeigniter/quickstart/ — 最初の PDF。
  • /integrations/codeigniter/production-usage/ — DI で構成されたコントローラーとキュージョブ。
  • /integrations/codeigniter/configuration/ — すべての構成キー。