local_fire_departmentHoneystax
search⌘K
loginLog Inperson_addSign Up
layers
HONEYSTAX TERMINAL v1.0
HomeNewsSavedSubmit
Back to the live board
C

claude-hooks-sdk

Plugin

PHP SDK for building Claude Code hooks.

Copy the install, test the workflow, then decide if it earns a permanent slot.

63
Why nowMoving now

Fresh repo activity plus visible builder pull. This is the kind of tool people test before it turns obvious.

DecisionWorth testing this week

Copy the install, test the workflow, then decide if it earns a permanent slot.

Trial costFast eval

You can test this quickly and remove it cleanly if it misses.

Risk43/100

GitHub health 37/100. no security policy. 8 open issues make this testable, but not something to trust blind.

What You Are Adopting

AI Agent

Claude Code

Model

Claude

Build Time

Minutes

Test This In Your Stack

One command inClean rollbackLow commitment
folderLocalClones to current directory. Delete the folder to remove.

Fastest way to find out if claude-hooks-sdk belongs in your setup.

Copy the install command, run a real test, and back it out cleanly if it slows you down.

Try now
git clone https://github.com/beyondcode/claude-hooks-sdk && cd claude-hooks-sdk && cat README.md

Run this first. You will know quickly if the workflow earns a permanent slot.

Back out
rm -rf claude-hooks-sdk

No messy cleanup loop. If it misses, remove it and keep moving.

Install Location

./  └─ claude-hooks-sdk/ ← clones here

About

PHP SDK for building Claude Code hooks.

README

Claude Hooks SDK

Latest Version on Packagist Tests Total Downloads

A Laravel-inspired PHP SDK for building Claude Code hook responses with a clean, fluent API. This SDK makes it easy to create structured JSON responses for Claude Code hooks using an expressive, chainable interface.

Claude Code hooks are user-defined shell commands that execute at specific points in Claude Code's lifecycle, providing deterministic control over its behavior. For more details, see the Claude Code Hooks documentation.

Installation

You can install the package via composer:

composer require beyondcode/claude-hooks-sdk

Usage

Creating a Claude Hook

Here's how to create a PHP script that Claude Code can use as a hook:

Step 1: Create your PHP hook script

Create a new PHP file (e.g., validate-code.php) using the SDK:

<?php

require 'vendor/autoload.php';

use BeyondCode\ClaudeHooks\ClaudeHook;

// Read the hook data from stdin. 
// This will automatically return the correct Hook instance (for example PreToolUse)
$hook = ClaudeHook::create();

// Example: Validate bash commands
if ($hook->toolName() === 'Bash') {
    $command = $hook->toolInput('command', '');
    
    // Check for potentially dangerous commands
    if (str_contains($command, 'rm -rf')) {
        // Block the tool call with feedback
        $hook->response()->block('Dangerous command detected. Use caution with rm -rf commands.');
    }
}

// Allow other tool calls to proceed
$hook->success();

Step 2: Register your hook in Claude Code

  1. Run the /hooks command in Claude Code
  2. Select the PreToolUse hook event (runs before tool execution)
  3. Add a matcher (e.g., Bash to match shell commands)
  4. Add your hook command: php /path/to/your/validate-code.php
  5. Save to user or project settings

Your hook is now active and will validate commands before Claude Code executes them!

Hook Types and Methods

The SDK automatically creates the appropriate hook type based on the input:

use BeyondCode\ClaudeHooks\ClaudeHook;
use BeyondCode\ClaudeHooks\Hooks\{PreToolUse, PostToolUse, Notification, Stop, SubagentStop};

$hook = ClaudeHook::create();

if ($hook instanceof PreToolUse) {
    $toolName = $hook->toolName();           // e.g., "Bash", "Write", "Edit"
    $toolInput = $hook->toolInput();         // Full input array
    $filePath = $hook->toolInput('file_path'); // Specific input value
}

if ($hook instanceof PostToolUse) {
    $toolResponse = $hook->toolResponse();   // Full response array
    $success = $hook->toolResponse('success', true); // With default value
}

if ($hook instanceof Notification) {
    $message = $hook->message();
    $title = $hook->title();
}

if ($hook instanceof Stop || $hook instanceof SubagentStop) {
    $isActive = $hook->stopHookActive();
}

Response Methods

All hooks provide a fluent response API:

// Continue processing (default behavior)
$hook->response()->continue();

// Stop Claude from continuing with a reason
$hook->response()->stop('Reason for stopping');

// For PreToolUse: approve or block tool calls
$hook->response()->approve('Optional approval message')->continue();
$hook->response()->block('Required reason for blocking')->continue();

// Suppress output from transcript mode
$hook->response()->suppressOutput()->continue();

Example Hooks

Code Formatter Hook

Automatically format PHP files after edits:

<?php

require 'vendor/autoload.php';

use BeyondCode\ClaudeHooks\ClaudeHook;
use BeyondCode\ClaudeHooks\Hooks\PostToolUse;

$hook = ClaudeHook::create();

$filePath = $hook->toolInput('file_path', '');

if (str_ends_with($filePath, '.php')) {
    exec("php-cs-fixer fix $filePath", $output, $exitCode);
    
    if ($exitCode !== 0) {
        $hook->response()
            ->suppressOutput()
            ->merge(['error' => 'Formatting failed'])
            ->continue();
    }
}

Security Validator Hook

Prevent modifications to sensitive files:

#!/usr/bin/env php
<?php

require 'vendor/autoload.php';

use BeyondCode\ClaudeHooks\ClaudeHook;
use BeyondCode\ClaudeHooks\Hooks\PreToolUse;

$hook = ClaudeHook::fromStdin(file_get_contents('php://stdin'));

if ($hook instanceof PreToolUse) {
    // Check file-modifying tools
    if (in_array($hook->toolName(), ['Write', 'Edit', 'MultiEdit'])) {
        $filePath = $hook->toolInput('file_path', '');
        
        $sensitivePatterns = [
            '.env',
            'config/database.php',
            'storage/oauth-private.key',
        ];
        
        foreach ($sensitivePatterns as $pattern) {
            if (str_contains($filePath, $pattern)) {
                $hook->response()->block("Cannot modify sensitive file: $filePath");
            }
        }
    }
}

// Allow all other operations
$hook->response()->continue();

Notification Handler Hook

Custom notification handling:

<?php

require 'vendor/autoload.php';

use BeyondCode\ClaudeHooks\ClaudeHook;
use BeyondCode\ClaudeHooks\Hooks\Notification;

$hook = ClaudeHook::create();

// Send to custom notification system
$notificationData = [
    'title' => $hook->title(),
    'message' => $hook->message(),
    'session' => $hook->sessionId(),
    'timestamp' => time()
];
    
// Send notification to Slack, Discord, etc.

$hook->success();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

  • Marcel Pociot
  • All Contributors

License

The MIT License (MIT). Please see License File for more information.

Tech Stack

ClaudePHPLaravel
Open Live ProjectAudit Repo

Reviews0

Log in to write a review.

ActiveLast commit 9d ago
bug_report8open issues
Submitted July 3, 2025

auto_awesomeYour strongest next moves after claude-hooks-sdk