Migrations: make it ready for use by other modules

fixes #746
This commit is contained in:
Thomas Gelf 2017-01-24 15:17:59 +01:00
parent e1b420d4c4
commit 0e2a056f49
1 changed files with 40 additions and 11 deletions

View File

@ -4,7 +4,8 @@ namespace Icinga\Module\Director\Db;
use DirectoryIterator; use DirectoryIterator;
use Exception; use Exception;
use Icinga\Module\Director\Db; use Icinga\Application\Icinga;
use Icinga\Module\Director\Data\Db\DbConnection;
class Migrations class Migrations
{ {
@ -14,13 +15,13 @@ class Migrations
protected $db; protected $db;
/** /**
* @var Db * @var DbConnection
*/ */
protected $connection; protected $connection;
protected $migrationsDir; protected $migrationsDir;
public function __construct(Db $connection) public function __construct(DbConnection $connection)
{ {
$this->connection = $connection; $this->connection = $connection;
$this->db = $connection->getDbAdapter(); $this->db = $connection->getDbAdapter();
@ -142,10 +143,9 @@ class Migrations
protected function getMigrationsDir() protected function getMigrationsDir()
{ {
if ($this->migrationsDir === null) { if ($this->migrationsDir === null) {
$this->migrationsDir = dirname(dirname(dirname(__DIR__))) $this->migrationsDir = $this->getSchemaDir(
. '/schema/' $this->connection->getDbType() . '-migrations'
. $this->connection->getDbType() );
. '-migrations';
} }
return $this->migrationsDir; return $this->migrationsDir;
@ -153,9 +153,38 @@ class Migrations
protected function getFullSchemaFile() protected function getFullSchemaFile()
{ {
return dirname(dirname(dirname(__DIR__))) return $this->getSchemaDir(
. '/schema/' $this->connection->getDbType() . '.sql'
. $this->connection->getDbType() );
. '.sql'; }
protected function getSchemaDir($sub = null)
{
$dir = $this->getModuleDir('/schema');
if ($sub === null) {
return $dir;
} else {
return $dir . '/' . ltrim($sub, '/');
}
}
protected function getModuleDir($sub = '')
{
return Icinga::app()->getModuleManager()->getModuleDir(
$this->getModuleName(),
$sub
);
}
protected function getModuleName()
{
return $this->getModuleNameForObject($this);
}
protected function getModuleNameForObject($object)
{
$class = get_class($object);
// Hint: Icinga\Module\ -> 14 chars
return lcfirst(substr($class, 14, strpos($class, '\\', 15) - 14));
} }
} }