Skip to content

Commit

Permalink
Add Migrator service
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed May 25, 2022
1 parent bc9276c commit 8185f95
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Expand Up @@ -22,6 +22,7 @@
'locator',
'logger',
'mailer',
'migrator',
'request',
'response',
'router',
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -48,6 +48,7 @@
"aplus/cli": "^2.0",
"aplus/config": "^3.0",
"aplus/database": "^3.0",
"aplus/database-extra": "^3.0",
"aplus/date": "^1.4",
"aplus/debug": "^3.1",
"aplus/email": "^3.0",
Expand Down
43 changes: 43 additions & 0 deletions src/App.php
Expand Up @@ -19,6 +19,7 @@
use Framework\Config\Config;
use Framework\Database\Database;
use Framework\Database\Debug\DatabaseCollector;
use Framework\Database\Extra\Migrator;
use Framework\Debug\Debugger;
use Framework\Debug\ExceptionHandler;
use Framework\Email\Debug\EmailCollector;
Expand Down Expand Up @@ -620,6 +621,48 @@ protected static function setMailer(string $instance) : Mailer
);
}

/**
* Get a Migrator service.
*
* @param string $instance
*
* @return Migrator
*/
public static function migrator(string $instance = 'default') : Migrator
{
$service = static::getService('migrator', $instance);
if ($service) {
return $service;
}
if (isset(static::$debugCollector)) {
$start = \microtime(true);
$service = static::setMigrator($instance);
$end = \microtime(true);
static::$debugCollector->addData([
'service' => 'migrator',
'instance' => $instance,
'start' => $start,
'end' => $end,
]);
return $service;
}
return static::setMigrator($instance);
}

protected static function setMigrator(string $instance) : Migrator
{
$config = static::config()->get('migrator', $instance);
return static::setService(
'migrator',
new Migrator(
static::database($config['database_instance'] ?? 'default'),
$config['directory'],
$config['table'] ?? 'Migrations',
),
$instance
);
}

/**
* Get the Language service.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/AppTest.php
Expand Up @@ -17,6 +17,7 @@
use Framework\Config\Config;
use Framework\Database\Database;
use Framework\Database\Definition\Table\TableDefinition;
use Framework\Database\Extra\Migrator;
use Framework\Debug\Debugger;
use Framework\Debug\ExceptionHandler;
use Framework\Email\Mailer;
Expand Down Expand Up @@ -110,6 +111,8 @@ public function testServicesInstances() : void
self::assertInstanceOf(Logger::class, App::logger());
self::assertInstanceOf(Mailer::class, App::mailer());
self::assertInstanceOf(Mailer::class, App::mailer());
self::assertInstanceOf(Migrator::class, App::migrator());
self::assertInstanceOf(Migrator::class, App::migrator());
self::assertInstanceOf(Request::class, App::request());
self::assertInstanceOf(Request::class, App::request());
self::assertInstanceOf(Response::class, App::response());
Expand Down Expand Up @@ -324,6 +327,7 @@ public function testDebug() : void
App::locator();
App::logger();
App::mailer();
App::migrator();
App::request();
App::response();
App::router();
Expand Down
20 changes: 20 additions & 0 deletions tests/configs/migrator.config.php
@@ -0,0 +1,20 @@
<?php
/*
* This file is part of Aplus Framework MVC Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @see App::migrator()
*/

return [
'default' => [
'database_instance' => 'default',
'directory' => __DIR__,
'table' => 'Migrations',
],
];

0 comments on commit 8185f95

Please sign in to comment.