인터페이스 레퍼런스
Contracts 모듈(TcpdfNext\Contracts)은 모든 TCPDF-Next 모듈이 프로그래밍하는 공유 인터페이스를 정의합니다. 구체 클래스 대신 계약에 의존함으로써 테스트 더블을 대체하고, 대안 구현을 만들고, 메이저 버전 간 안정적인 퍼블릭 API를 유지할 수 있습니다.
PdfDocumentInterface
네임스페이스: TcpdfNext\Contracts\PdfDocumentInterface
Document가 구현하는 기본 API 계약입니다. 이 인터페이스를 충족하는 모든 클래스는 내장 문서 모델의 드롭인 대체품으로 사용할 수 있습니다.
메서드
예제
use TcpdfNext\Contracts\PdfDocumentInterface;
use TcpdfNext\Contracts\OutputDestination;
function generateReport(PdfDocumentInterface $pdf): string
{
return $pdf
->addPage()
->font('Helvetica', size: 14)
->text('Quarterly Report')
->output(OutputDestination::String);
}FontManagerInterface
네임스페이스: TcpdfNext\Contracts\FontManagerInterface
폰트 로딩, 등록, 서브셋팅, 메트릭 조회를 정의합니다. 내장 FontManager가 이 인터페이스를 구현하지만, 특수한 폰트 소스를 위한 커스텀 구현을 제공할 수 있습니다.
메서드
예제
use TcpdfNext\Contracts\FontManagerInterface;
function addCustomFonts(FontManagerInterface $fonts): void
{
$fonts->registerFont('/fonts/NotoSans-Regular.ttf', 'NotoSans');
$fonts->registerFont('/fonts/NotoSans-Bold.ttf', 'NotoSans');
if ($fonts->hasFont('NotoSans', 'B')) {
$info = $fonts->getFont('NotoSans', 'B');
echo "Ascender: {$info->ascender}";
}
}SignerInterface
네임스페이스: TcpdfNext\Contracts\SignerInterface
모든 디지털 서명 제공자를 위한 계약을 정의합니다. 소프트웨어 인증서, 클라우드 기반 키 볼트, 하드웨어 보안 모듈(HSM)과 통합하려면 이 인터페이스를 구현합니다.
메서드
예제
use TcpdfNext\Contracts\SignerInterface;
class FileSigner implements SignerInterface
{
public function __construct(
private readonly string $certPath,
private readonly string $keyPath,
private readonly string $password,
) {}
public function sign(string $data): string
{
$cert = file_get_contents($this->certPath);
$key = openssl_pkey_get_private(
file_get_contents($this->keyPath),
$this->password,
);
openssl_pkcs7_sign(/* ... */);
// 원시 서명 바이트 반환
}
public function certificate(): string { /* ... */ }
public function chain(): array { return []; }
public function estimatedSize(): int { return 8192; }
public function algorithm(): string { return 'sha256WithRSAEncryption'; }
}HsmSignerInterface
네임스페이스: TcpdfNext\Contracts\HsmSignerInterface
개인 키를 절대 노출하지 않는 하드웨어 보안 모듈을 위해 SignerInterface를 확장합니다. 원시 데이터에 서명하는 대신 HSM이 사전 계산된 다이제스트에 서명합니다.
메서드
SignerInterface의 모든 메서드, 추가:
예제
use TcpdfNext\Contracts\HsmSignerInterface;
class AwsCloudHsmSigner implements HsmSignerInterface
{
public function __construct(
private readonly string $keyId,
private readonly AwsKmsClient $kms,
private readonly string $certPem,
) {}
public function sign(string $data): string
{
$digest = hash('sha256', $data, binary: true);
return $this->signDigest($digest, 'sha256');
}
public function signDigest(string $digest, string $algorithm): string
{
$result = $this->kms->sign([
'KeyId' => $this->keyId,
'Message' => $digest,
'MessageType' => 'DIGEST',
'SigningAlgorithm' => 'RSASSA_PKCS1_V1_5_SHA_256',
]);
return $result['Signature'];
}
public function certificate(): string { return $this->certPem; }
public function chain(): array { return []; }
public function estimatedSize(): int { return 16384; }
public function algorithm(): string { return 'sha256WithRSAEncryption'; }
}테스트를 위한 인터페이스 활용
네 가지 인터페이스 모두 PHPUnit이나 Mockery로 쉽게 모킹할 수 있도록 설계되었습니다.
use PHPUnit\Framework\TestCase;
use TcpdfNext\Contracts\FontManagerInterface;
class TextRendererTest extends TestCase
{
public function testCalculatesWidth(): void
{
$fonts = $this->createMock(FontManagerInterface::class);
$fonts->method('hasFont')->willReturn(true);
$fonts->method('stringWidth')
->with('Hello', 'Helvetica', 12.0, '')
->willReturn(26.64);
$renderer = new TextRenderer($fonts);
$this->assertEqualsWithDelta(26.64, $renderer->width('Hello'), 0.01);
}
}참고
- API 개요 -- 모든 패키지 한눈에 보기
- 열거형 레퍼런스 -- 이 인터페이스가 사용하는 열거형(Orientation, Alignment, OutputDestination)
- Document API -- PdfDocumentInterface를 구현하는 구체 클래스
- 보안 가이드 -- 암호화 및 디지털 서명 전체 가이드