Skip to content
Snippets Groups Projects
Verified Commit f75263bc authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

build: prepare tools and basic build

parent 16880789
No related branches found
No related tags found
No related merge requests found
Pipeline #8880 failed
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace WPDesk\Migrations\Version; namespace WPDesk\Migrations\Version;
......
...@@ -4,7 +4,7 @@ namespace WPDesk\Migrations\Version; ...@@ -4,7 +4,7 @@ namespace WPDesk\Migrations\Version;
use WPDesk\Migrations\AbstractMigration; use WPDesk\Migrations\AbstractMigration;
interface MigrationFactory interface MigrationFactory {
{
public function create_version( string $migration_class): AbstractMigration; public function create_version( string $migration_class): AbstractMigration;
} }
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace WPDesk\Migrations\Version; namespace WPDesk\Migrations\Version;
final class Version { final class Version {
/** @var string */
private $version; private $version;
public function __construct( string $version ) { public function __construct( string $version ) {
......
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace WPDesk\Migrations\Version; namespace WPDesk\Migrations\Version;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WPDesk\Migrations\AbstractMigration; use WPDesk\Migrations\AbstractMigration;
class WpdbMigrationFactory implements MigrationFactory class WpdbMigrationFactory implements MigrationFactory {
{
/** @var \wpdb */
protected $wpdb; protected $wpdb;
/** @var LoggerInterface */
protected $logger; protected $logger;
public function __construct( \wpdb $wpdb, LoggerInterface $logger ) { public function __construct( \wpdb $wpdb, LoggerInterface $logger ) {
......
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace WPDesk\Migrations; namespace WPDesk\Migrations;
...@@ -10,16 +11,17 @@ class WpdbLogger implements \Psr\Log\LoggerInterface { ...@@ -10,16 +11,17 @@ class WpdbLogger implements \Psr\Log\LoggerInterface {
private const DB_LOG = 'omnibus_db_log'; private const DB_LOG = 'omnibus_db_log';
private const MAX_LOG_SIZE = 30; private const MAX_LOG_SIZE = 30;
/** @var string[] */
private $log; private $log;
public function __construct() { public function __construct() {
$this->log = json_decode( get_option( self::DB_LOG, '[]' ), true ); $this->log = json_decode( get_option( self::DB_LOG, '[]' ) );
if ( ! \is_array( $this->log ) ) { if ( ! \is_array( $this->log ) ) {
$this->log = []; $this->log = [];
} }
} }
public function log($level, $message, array $context = array()) { public function log( $level, $message, array $context = [] ) {
$this->log[] = date( 'Y-m-d G:i:s' ) . sprintf( ': %s', $message ); $this->log[] = date( 'Y-m-d G:i:s' ) . sprintf( ': %s', $message );
if ( \count( $this->log ) > self::MAX_LOG_SIZE ) { if ( \count( $this->log ) > self::MAX_LOG_SIZE ) {
array_shift( $this->log ); array_shift( $this->log );
......
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace WPDesk\Migrations; namespace WPDesk\Migrations;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WPDesk\Migrations\Finder\GlobFinder; use WPDesk\Migrations\Finder\GlobFinder;
use WPDesk\Migrations\Version\AlphabeticalComparator; use WPDesk\Migrations\Version\AlphabeticalComparator;
use WPDesk\Migrations\Version\Comparator;
use WPDesk\Migrations\Version\Version; use WPDesk\Migrations\Version\Version;
use WPDesk\Migrations\Version\WpdbMigrationFactory; use WPDesk\Migrations\Version\WpdbMigrationFactory;
...@@ -14,11 +16,18 @@ class WpdbMigrator implements Migrator { ...@@ -14,11 +16,18 @@ class WpdbMigrator implements Migrator {
/** @var \wpdb */ /** @var \wpdb */
private $wpdb; private $wpdb;
/** @var MigrationsRepository */
private $migrations_repository; private $migrations_repository;
/** @var Comparator */
private $comparator;
/** @var LoggerInterface */
private $logger; private $logger;
public static function fromWpdb(\wpdb $wpdb, array $migration_directories): self { /** @param string[] $migration_directories */
public static function fromDirectories( array $migration_directories ): self {
global $wpdb;
$logger = new WpdbLogger(); $logger = new WpdbLogger();
return new self( return new self(
...@@ -29,8 +38,30 @@ class WpdbMigrator implements Migrator { ...@@ -29,8 +38,30 @@ class WpdbMigrator implements Migrator {
new WpdbMigrationFactory( new WpdbMigrationFactory(
$wpdb, $wpdb,
$logger $logger
)
), ),
new AlphabeticalComparator()
),
new AlphabeticalComparator(),
$logger
);
}
/** @param class-string<AbstractMigration>[] $migration_class_names */
public static function fromClasses( array $migration_class_names ): self {
global $wpdb;
$logger = new WpdbLogger();
return new self(
$wpdb,
new ArrayMigrationsRepository(
$migration_class_names,
new WpdbMigrationFactory(
$wpdb,
$logger
),
new AlphabeticalComparator()
),
new AlphabeticalComparator(),
$logger $logger
); );
} }
...@@ -38,10 +69,12 @@ class WpdbMigrator implements Migrator { ...@@ -38,10 +69,12 @@ class WpdbMigrator implements Migrator {
public function __construct( public function __construct(
\wpdb $wpdb, \wpdb $wpdb,
MigrationsRepository $migrations_repository, MigrationsRepository $migrations_repository,
Comparator $comparator,
LoggerInterface $logger LoggerInterface $logger
) { ) {
$this->wpdb = $wpdb; $this->wpdb = $wpdb;
$this->migrations_repository = $migrations_repository; $this->migrations_repository = $migrations_repository;
$this->comparator = $comparator;
$this->logger = $logger; $this->logger = $logger;
} }
...@@ -55,9 +88,8 @@ class WpdbMigrator implements Migrator { ...@@ -55,9 +88,8 @@ class WpdbMigrator implements Migrator {
$this->logger->info( 'DB update start' ); $this->logger->info( 'DB update start' );
$current_version = $this->get_current_version(); $current_version = $this->get_current_version();
$comparator = new AlphabeticalComparator();
foreach ( $this->migrations_repository->get_migrations() as $migration ) { foreach ( $this->migrations_repository->get_migrations() as $migration ) {
if ( $comparator->compare($migration->get_version(), $this->get_current_version()) ) { if ( $this->comparator->compare( $migration->get_version(), $this->get_current_version() ) ) {
$this->logger->info( sprintf( 'DB update %s:%s', $current_version, $migration->get_version() ) ); $this->logger->info( sprintf( 'DB update %s:%s', $current_version, $migration->get_version() ) );
try { try {
$migration->get_migration()->up(); $migration->get_migration()->up();
......
File added
File added
File added
File added
tools/phpunit 0 → 100755
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment