|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Code-Insight library. |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
|
8
|
|
|
* @link https://github.com/console-helpers/code-insight |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\CodeInsight\KnowledgeBase; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use Aura\Sql\ExtendedPdo; |
|
15
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
|
16
|
|
|
use ConsoleHelpers\DatabaseMigration\MigrationManager; |
|
17
|
|
|
use ConsoleHelpers\DatabaseMigration\MigrationContext; |
|
18
|
|
|
|
|
19
|
|
|
class DatabaseManager |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Migration manager. |
|
24
|
|
|
* |
|
25
|
|
|
* @var MigrationManager |
|
26
|
|
|
*/ |
|
27
|
|
|
private $_migrationManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Database directory. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $_databaseDirectory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Database manager constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param MigrationManager $migration_manager Migration manager. |
|
40
|
|
|
* @param string $working_directory Working directory. |
|
41
|
|
|
*/ |
|
42
|
6 |
|
public function __construct(MigrationManager $migration_manager, $working_directory) |
|
43
|
|
|
{ |
|
44
|
6 |
|
$this->_migrationManager = $migration_manager; |
|
45
|
6 |
|
$this->_databaseDirectory = $working_directory . '/databases'; |
|
46
|
|
|
|
|
47
|
6 |
|
if ( !file_exists($this->_databaseDirectory) ) { |
|
48
|
6 |
|
mkdir($this->_databaseDirectory); |
|
49
|
|
|
} |
|
50
|
6 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns db for given project. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $project_path Project path. |
|
56
|
|
|
* @param string|null $fork Fork name. |
|
57
|
|
|
* |
|
58
|
|
|
* @return ExtendedPdoInterface |
|
59
|
|
|
* @throws \InvalidArgumentException When relative project path is given. |
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function getDatabase($project_path, $fork = null) |
|
62
|
|
|
{ |
|
63
|
4 |
|
if ( substr($project_path, 0, 1) !== '/' ) { |
|
64
|
1 |
|
throw new \InvalidArgumentException('The "$project_path" argument must contain absolute path.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
$project_path = $this->_databaseDirectory . $project_path; |
|
68
|
|
|
|
|
69
|
3 |
|
if ( !file_exists($project_path) ) { |
|
70
|
3 |
|
mkdir($project_path, 0777, true); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
$db_file = $project_path . '/code_insight.sqlite'; |
|
74
|
|
|
|
|
75
|
3 |
|
if ( !strlen($fork) ) { |
|
|
|
|
|
|
76
|
2 |
|
return new ExtendedPdo('sqlite:' . $db_file); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
$fork_db_file = $project_path . '/code_insight-' . $fork . '.sqlite'; |
|
80
|
|
|
|
|
81
|
2 |
|
if ( !file_exists($fork_db_file) && file_exists($db_file) ) { |
|
82
|
1 |
|
copy($db_file, $fork_db_file); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
return new ExtendedPdo('sqlite:' . $fork_db_file); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Runs outstanding migrations on the database. |
|
90
|
|
|
* |
|
91
|
|
|
* @param MigrationContext $context Context. |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function runMigrations(MigrationContext $context) |
|
96
|
|
|
{ |
|
97
|
1 |
|
$this->_migrationManager->run($context); |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|