Skip to content

Extending

Muhammet Şafak edited this page Jun 11, 2026 · 1 revision

Extending

Barbarian is deliberately small, but its pieces are separated so you can reuse or replace them.

The building blocks

Class / interface Responsibility
QueryInterface Contract handed to migrations to run statements.
QueryRunner Default QueryInterface: a thin PDO wrapper.
Migrations Discovers migrations, keeps the version table in sync.
MigrationStatus Enum: Down = 0, Up = 1.
Schema\Grammar Driver-specific version table DDL and identifier quoting.
Console\Config Immutable, validated barbarian.json representation.
Console\ConfigLoader Parses a config file/string into a Config.
Console\ManagerFactory Builds a Migrations (and its PDO) from a Config.
Console\MigrationTemplate Renders the source of a new migration class.
Console\*Command The init, create, status, up and down commands.

Using the query runner directly

QueryRunner is a standalone PDO helper you can use anywhere:

use InitPHP\Barbarian\QueryRunner;

$runner = new QueryRunner($pdo);   // forces PDO::ERRMODE_EXCEPTION
$runner->driver();                  // 'mysql' | 'sqlite' | 'pgsql' | ...
$statement = $runner->query('SELECT * FROM users WHERE id = :id', [':id' => 1]);
$user = $statement->fetch(PDO::FETCH_ASSOC);

Because the manager type-hints QueryInterface, you can also provide your own implementation (for logging, metrics, dry-runs, …) and pass it to your migrations when invoking them yourself.

Loading configuration in your own tools

use InitPHP\Barbarian\Console\ConfigLoader;
use InitPHP\Barbarian\Console\ManagerFactory;

// From a file:
$config = ConfigLoader::fromFile('barbarian.json');

// ...or from a JSON string (e.g. an env var):
$config = ConfigLoader::fromJson(getenv('BARBARIAN_CONFIG') ?: '{}', 'env');

$migrations = ManagerFactory::fromConfig($config);

ConfigLoader validates the required keys (dsn, username, password, folder) and throws a MigrationException on malformed input.

Registering the commands in your own CLI

The console commands are ordinary InitPHP\Console\Command subclasses, so you can register them in your own console application:

use InitPHP\Console\Application;
use InitPHP\Barbarian\Console\CreateCommand;
use InitPHP\Barbarian\Console\DownCommand;
use InitPHP\Barbarian\Console\InitCommand;
use InitPHP\Barbarian\Console\StatusCommand;
use InitPHP\Barbarian\Console\UpCommand;

$app = new Application('My Tool', '1.0.0');
$app->register(InitCommand::class);
$app->register(CreateCommand::class);
$app->register(StatusCommand::class);
$app->register(UpCommand::class);
$app->register(DownCommand::class);
$app->run();

This is exactly what the bundled barbarian binary does.

Generating migration source

use InitPHP\Barbarian\Console\MigrationTemplate;

$code = MigrationTemplate::render('Migration_20240101000000', 'App\\Migrations');
file_put_contents('migrations/Migration_20240101000000.php', $code);

Framework integration

Because the whole flow is just “build a Migrations, then call upMigration()/downMigration(), integrating with a framework is mostly a matter of constructing the manager from the framework's PDO connection and wiring a few console commands. See Recipes for concrete patterns.

See also

Clone this wiki locally