1 | <?php |
||
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( |
|
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) |
||
107 | |||
108 | } |
||
109 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: