-
Notifications
You must be signed in to change notification settings - Fork 1
Writing a Custom Adapter
vendeeglobe edited this page Jul 8, 2026
·
1 revision
As of Bad Behaviour 2.3.0, the library relies on Dependency Injection to communicate with host platforms. To integrate Bad Behaviour into a new or unsupported framework, you need to write a custom Adapter by implementing the BadBehaviour\Core\HostAdapterInterface.
Create a class that implements the interface. The interface dictates how Bad Behaviour interacts with your host environment (database queries, escaping, getting configuration, etc).
<?php
namespace MyFramework\Integration;
use BadBehaviour\Core\HostAdapterInterface;
class MyFrameworkAdapter implements HostAdapterInterface
{
private $db;
private array $settings;
public function __construct($db)
{
$this->db = $db;
}
public function get_date_string(): string
{
return gmdate('Y-m-d H:i:s');
}
public function get_affected_rows($result): int
{
return $this->db->affectedRows();
}
public function escape_string(string $string): string
{
return $this->db->real_escape_string($string);
}
public function get_num_rows($result): int
{
return $result->numRows();
}
public function query(string $query)
{
return $this->db->query($query);
}
public function get_rows($result): array
{
$rows = [];
while ($row = $result->fetchAssoc()) {
$rows[] = $row;
}
return $rows;
}
public function get_insert_sql(array $settings, array $package, string $key): string
{
// Return a formatted INSERT SQL string for your specific database driver
// using the $package data and the $settings['log_table'] name.
}
public function get_email(): string
{
return 'admin@myframework.com';
}
public function read_whitelist(): array
{
return @parse_ini_file('config/bb_whitelist.ini') ?: [];
}
public function read_settings(): array
{
return [
'log_table' => 'bad_behaviour',
'display_stats' => false,
'strict' => false,
'verbose' => false,
'logging' => true,
// ... other defaults
];
}
public function write_settings(array $settings): bool
{
return false; // Implement if your framework saves settings dynamically
}
public function install(): void
{
// Run table creation logic here if not already handled by the framework
}
public function get_relative_path(): string
{
return '/';
}
public function get_table_structure(string $name)
{
// Return the CREATE TABLE string or array of strings for your DB engine
}
}Pass your newly created adapter to the main BadBehaviour class:
use BadBehaviour\Core\BadBehaviour;
use MyFramework\Integration\MyFrameworkAdapter;
$adapter = new MyFrameworkAdapter($frameworkDb);
$bb = new BadBehaviour($adapter);
// Execute screening
$bb->run();That's it! The core library will now use your adapter to log blocks, check whitelists, and format database interactions transparently.