guiziweb/bookstack-sdk-php

A PHP SDK for BookStack API

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/guiziweb/bookstack-sdk-php

v1.0.0 2025-09-26 17:34 UTC

This package is auto-updated.

Last update: 2025-10-24 05:41:24 UTC


README

A PHP SDK for the BookStack API with DTOs, validation, and comprehensive documentation.

PHP Version License: MIT PHPStan Tests

๐Ÿš€ Installation

composer require guiziweb/bookstack-sdk-php

โšก Quick Usage

Create the client

<?php

use Guiziweb\BookStackSdk\BookStackClientFactory;

$client = BookStackClientFactory::create(
    'https://your-bookstack-instance.com',
    'your-api-token-id',
    'your-api-token-secret'
);

Basic examples

$booksResult = $client->books()->list();
foreach ($booksResult['data'] as $book) {
    echo $book->name;  
}

$book = $client->books()->create([
    'name' => 'My new book',
    'description' => 'Description'
]);
echo $book->id;

$results = $client->search()->search('my search term');
foreach ($results['data'] as $result) {
    echo $result->name; 
}

๐Ÿ“‹ Available Services

The client covers the BookStack API endpoints:

Service Description DTOs
books() Books (CRUD + export) Book, BookContent, Tag, Cover, PageSummary
pages() Pages (CRUD + export) Page
chapters() Chapters (CRUD + export) Chapter
shelves() Bookshelves (CRUD) Shelf
users() Users (CRUD) User, UserRole
roles() Roles and permissions Role, RoleUser
search() Global search SearchResult
images() Images Image, ImageUser
attachments() File attachments Attachment
recycleBin() Recycle bin RecycleBinItem
contentPermissions() Permissions ContentPermission, RolePermission, etc.
auditLogs() Audit logs AuditLogEntry, AuditUser

๐Ÿ“š Detailed Examples

Books management

// List with pagination and sorting
$books = $client->books()->list(50, 0, ['name' => 'asc']);

// Complete CRUD
$book = $client->books()->create(['name' => 'Test']);
$book = $client->books()->show(1);
$book = $client->books()->update(1, ['name' => 'New name']);
$client->books()->delete(1);

// Export PDF/HTML/etc.
$pdf = $client->books()->export(1, 'pdf');
$html = $client->books()->export(1, 'html');

Search

// General search
$results = $client->search()->search('term', 20);

// Specialized search
$books = $client->search()->searchBooks('guide');
$pages = $client->search()->searchPages('installation');

User management

// Create a user
$user = $client->users()->create([
    'name' => 'John Doe',
    'email' => 'john@example.com'
]);

// List users
$users = $client->users()->list();

Content permissions

// Get book permissions
$permissions = $client->contentPermissions()->getPermissions('book', 1);

// Update permissions
$updated = $client->contentPermissions()->set('book', 1, [
    'role_permissions' => [
        2 => ['view' => true, 'update' => false, 'delete' => false]
    ]
]);

๐Ÿ›ก๏ธ Error Handling

use Guiziweb\BookStackClient\Exception\BookStackClientException;
use Guiziweb\BookStackClient\Exception\ValidationException;

try {
    $book = $client->books()->show(999);
} catch (ValidationException $e) {
    // Invalid parameters
    echo "Validation error: " . $e->getMessage();
} catch (BookStackClientException $e) {
    // API error (404, 403, etc.)
    echo "API error: " . $e->getMessage();
}

โš™๏ธ Advanced Configuration

// With custom options
$client = BookStackClientFactory::createWithOptions(
    'https://your-bookstack-instance.com',
    'your-api-token-id',
    'your-api-token-secret',
    [
        'timeout' => 60,
        'max_redirects' => 5,
        'headers' => ['User-Agent' => 'My-App/1.0']
    ]
);

// With custom HTTP client
use Symfony\Component\HttpClient\HttpClient;

$httpClient = HttpClient::create(['timeout' => 30]);
$client = BookStackClientFactory::createWithHttpClient(
    $httpClient,
    'https://your-bookstack-instance.com',
    'your-api-token-id',
    'your-api-token-secret'
);

๐Ÿงช Tests

Unit tests (fast)

# Unit tests only
make test-unit

# With code coverage
make coverage

Integration tests (require BookStack)

# Start BookStack locally with automatic API token creation
make ci-setup

# Run integration tests
make ci-test

# Stop test BookStack
make ci-down

Complete tests

# All tests (unit + integration if BookStack available)
make test

๐Ÿ“‹ Requirements

  • PHP 8.2+
  • BookStack v24.05+ (tested with v25.07)
  • Valid API keys

Note: For ZIP exports with BookStack v25.05+, add 'zip' to the EXPORT_FORMATS array in src/Validator/ParameterValidator.php

๐Ÿ“„ License

MIT License. See LICENSE for more details.

๐Ÿ”— Links