|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the SVN-Buddy 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/svn-buddy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Repository\RevisionLog; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use Aura\Sql\ExtendedPdo; |
|
15
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
|
16
|
|
|
use ConsoleHelpers\ConsoleKit\ConsoleIO; |
|
17
|
|
|
use ConsoleHelpers\DatabaseMigration\MigrationManager; |
|
18
|
|
|
use ConsoleHelpers\DatabaseMigration\MigrationContext; |
|
|
|
|
|
|
19
|
|
|
use ConsoleHelpers\SVNBuddy\Database\StatementProfiler; |
|
20
|
|
|
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector; |
|
21
|
|
|
|
|
22
|
|
|
class DatabaseManager |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Working directory. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $_workingDirectory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Migration manager. |
|
34
|
|
|
* |
|
35
|
|
|
* @var MigrationManager |
|
36
|
|
|
*/ |
|
37
|
|
|
private $_migrationManager; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Statement profiler. |
|
41
|
|
|
* |
|
42
|
|
|
* @var StatementProfiler |
|
43
|
|
|
*/ |
|
44
|
|
|
private $_statementProfiler; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Database manager constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $working_directory Working directory. |
|
50
|
|
|
* @param MigrationManager $migration_manager Migration manager. |
|
51
|
|
|
* @param StatementProfiler $statement_profiler Statement profiler. |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function __construct( |
|
54
|
|
|
$working_directory, |
|
55
|
|
|
MigrationManager $migration_manager, |
|
56
|
|
|
StatementProfiler $statement_profiler |
|
57
|
|
|
) { |
|
58
|
2 |
|
$this->_workingDirectory = $working_directory; |
|
59
|
2 |
|
$this->_migrationManager = $migration_manager; |
|
60
|
2 |
|
$this->_statementProfiler = $statement_profiler; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns db for given repository. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $repository_url Repository url. |
|
67
|
|
|
* @param ConsoleIO $io Console IO. |
|
68
|
|
|
* |
|
69
|
|
|
* @return ExtendedPdoInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getDatabase($repository_url, ConsoleIO $io = null) |
|
72
|
|
|
{ |
|
73
|
|
|
if ( preg_match(Connector::URL_REGEXP, $repository_url, $regs) ) { |
|
74
|
|
|
$sub_folder = $regs[2] . $regs[3] . $regs[4]; |
|
75
|
|
|
} |
|
76
|
|
|
else { |
|
77
|
|
|
$sub_folder = 'misc'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$parent_path = $this->_workingDirectory . '/' . $sub_folder; |
|
81
|
|
|
|
|
82
|
|
|
if ( !file_exists($parent_path) ) { |
|
83
|
|
|
mkdir($parent_path, 0777, true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$db = new ExtendedPdo('sqlite:' . $parent_path . '/log_' . crc32($repository_url) . '.sqlite'); |
|
87
|
|
|
|
|
88
|
|
|
$profiler = clone $this->_statementProfiler; |
|
89
|
|
|
$profiler->setIO($io); |
|
90
|
|
|
|
|
91
|
|
|
$db->setProfiler($profiler); |
|
92
|
|
|
|
|
93
|
|
|
return $db; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Runs outstanding migrations on the database. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \ConsoleHelpers\DatabaseMigration\MigrationContext $context Context. |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function runMigrations(MigrationContext $context) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->_migrationManager->run($context); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: