Skip to content

API Reference

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

API Reference

Every public class lives under the InitPHP\Barbarian namespace. This page lists each type and its members with exact signatures.

Class Kind Summary
Migrations final class Discovers migrations and runs them.
MigrationInterface interface The migration contract.
MigrationAbstract abstract class Base class implementing getName().
QueryInterface interface The query gateway.
QueryRunner final class Default QueryInterface (PDO wrapper).
MigrationStatus enum Down = 0, Up = 1.
MigrationException class Library-level failure.
Schema\Grammar final class Driver-specific DDL & quoting.
Console layer Config, factory, template, commands.

Migrations

final class InitPHP\Barbarian\Migrations

Discovers migration classes on disk, keeps the version table in sync and runs the up()/down() side of a migration on demand.

Constructor

public function __construct(PDO $pdo, string $folder, array $options = [])
Parameter Type Description
$pdo PDO Connection; its error mode is forced to ERRMODE_EXCEPTION.
$folder string Directory holding the Migration_*.php files.
$options array namespace (?string) and migrationTable (string). Unknown keys ignored.

Throws InvalidArgumentException when the folder does not exist or the table name is invalid; MigrationException when the version table cannot be created or the folder cannot be read.

Constructing the instance ensures the version table exists and scans the folder.

Members

Method Returns Description
getMigrations() array<string, class-string> Discovered migrations, keyed by version name.
getErrors() array<int, string> Problems found while scanning.
getFolder() string Normalised folder (trailing separator).
getNamespace() ?string Configured namespace, or null.
getMigrationTable() string Version table name.
upMigration() bool Apply a migration.
downMigration() bool Revert a migration.
status() ?MigrationStatus Recorded status of one migration.
getStatuses() array<string, ?MigrationStatus> Status of every discovered migration.

getMigrations()

public function getMigrations(): array

Returns the discovered migrations as array<string, class-string<MigrationInterface>> — keyed by version name (the unqualified class name) and mapped to the fully-qualified class name. Only classes implementing MigrationInterface are included.

getErrors()

public function getErrors(): array

Returns array<int, string> of human-readable problems found while scanning the folder — for example a Migration_*.php file that does not declare the expected class, or one whose class does not implement MigrationInterface.

upMigration()

public function upMigration(MigrationInterface $migration, bool $force = false): bool

Applies a migration's up() side and records it as MigrationStatus::Up. A migration already at Up is skipped unless $force is true. When up() returns false the state is left untouched. Returns true when up() ran and the state was recorded. Throws \PDOException on a database error.

downMigration()

public function downMigration(MigrationInterface $migration, bool $force = false): bool

Reverts a migration's down() side and records it as MigrationStatus::Down. A migration that was never applied, or already at Down (unless $force), is skipped. When down() returns false the state is left untouched. Returns true when down() ran and the state was recorded. Throws \PDOException on a database error.

status() / getStatuses()

public function status(MigrationInterface|string $migration): ?MigrationStatus
public function getStatuses(): array

status() returns the recorded MigrationStatus of a migration (by instance or version name), or null when it has never been applied. getStatuses() returns array<string, MigrationStatus|null> for every discovered migration, keyed by version name.


MigrationInterface

interface InitPHP\Barbarian\MigrationInterface

public function up(QueryInterface $query): bool;
public function down(QueryInterface $query): bool;
public function getName(): string;
Method Description
up() Applies the migration. Return true on success; false (or throw) to abort recording.
down() Reverts the migration. Same return semantics as up().
getName() The unique version name the migration is tracked under.

MigrationAbstract

abstract class InitPHP\Barbarian\MigrationAbstract implements MigrationInterface

Implements getName() for you; subclasses implement only up() and down().

abstract public function up(QueryInterface $query): bool;
abstract public function down(QueryInterface $query): bool;
final public function getName(): string;

getName() returns the class name without its namespace prefix, so App\Migrations\Migration_20240101000000 is tracked as Migration_20240101000000.


QueryInterface

interface InitPHP\Barbarian\QueryInterface

The query gateway handed to a migration's up()/down().

public function query(string $sql, ?array $arguments = null): \PDOStatement;
Parameter Type Description
$sql string The statement to run.
$arguments array<int|string, mixed>|null Bound parameters, or null.

Returns the executed PDOStatement. Throws \PDOException when the statement cannot be prepared or executed.


QueryRunner

final class InitPHP\Barbarian\QueryRunner implements QueryInterface

The default QueryInterface: a thin wrapper around a PDO connection.

public function __construct(PDO $pdo)
public function pdo(): PDO
public function driver(): string
public function query(string $sql, ?array $arguments = null): \PDOStatement
Member Description
__construct() Wraps a connection and forces PDO::ERRMODE_EXCEPTION.
pdo() The underlying PDO connection.
driver() The PDO driver name (mysql, sqlite, pgsql, …).
query() See QueryInterface::query().

MigrationStatus

enum InitPHP\Barbarian\MigrationStatus: int

The persisted state of a migration in the version table.

Case Value Meaning
Down 0 down() was the last method applied.
Up 1 up() was the last method applied.

The backing integer is exactly what is stored in (and read back from) the status column.


MigrationException

class InitPHP\Barbarian\MigrationException extends \Exception

Raised for failures originating in Barbarian itself — an unreadable migration folder, an invalid configuration file, a failed connection — as opposed to \PDOExceptions raised while executing migration statements. See Exceptions.


Grammar

final class InitPHP\Barbarian\Schema\Grammar

Produces the small amount of driver-specific SQL the version table needs. MySQL/MariaDB, SQLite and PostgreSQL are handled explicitly; any other driver falls back to the MySQL grammar.

public function __construct(string $driver)
public function quoteIdentifier(string $identifier): string
public function createVersionTable(string $table): string
Member Description
__construct() $driver is a PDO driver name such as mysql, sqlite or pgsql.
quoteIdentifier() Wraps an identifier in the driver's quoting characters.
createVersionTable() The CREATE TABLE IF NOT EXISTS statement for the version table.

See Database Drivers for the generated DDL.


Console layer

namespace InitPHP\Barbarian\Console

These types power the CLI and can be reused in your own tooling (see Extending).

Config

final class Config

An immutable, validated representation of a barbarian.json file.

public function __construct(
    public readonly string $dsn,
    public readonly string $username,
    public readonly string $password,
    public readonly string $folder,
    public readonly string $migrationTable,
    public readonly ?string $namespace
)

ConfigLoader

final class ConfigLoader

Reads and validates configuration into a Config. Both table_name and migrationTable are accepted for the version table name.

public static function fromFile(string $path): Config
public static function fromJson(string $json, string $source = 'configuration'): Config

Required keys: dsn, username, password, folder. Throws MigrationException when the input cannot be read, is not valid JSON, or is missing a required key.

ManagerFactory

final class ManagerFactory

Builds a configured Migrations (and the PDO connection it needs) from a Config.

public static function fromConfig(Config $config): Migrations

Throws MigrationException when the connection cannot be opened.

MigrationTemplate

final class MigrationTemplate

Renders the PHP source for a new, empty migration class.

public static function render(string $className, ?string $namespace): string

Commands

Class-based InitPHP Console commands. Each sets a $command name and implements execute().

Class Command Arguments
InitCommand init --dir
CreateCommand create --config
StatusCommand status --config
UpCommand up --version, --config
DownCommand down --version, --config

The configuration-aware commands extend BarbarianCommand, which provides the shared --config argument and the loadConfig() helper. See CLI Commands for usage and Extending for registering them in your own application.


See also

Clone this wiki locally