|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Databases\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Filesystem\IFileFinder; |
|
8
|
|
|
use DateTime; |
|
9
|
|
|
use League\Flysystem\FileNotFoundException; |
|
10
|
|
|
use Opulence\Databases\IConnection; |
|
11
|
|
|
use Opulence\Databases\Migrations\Migration; |
|
12
|
|
|
|
|
13
|
|
|
class BaseMigration extends Migration |
|
14
|
|
|
{ |
|
15
|
|
|
protected const FILENAME = 'foo-bar'; |
|
16
|
|
|
|
|
17
|
|
|
protected const UP = 'up'; |
|
18
|
|
|
protected const DOWN = 'down'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var IFileFinder */ |
|
21
|
|
|
protected $fileFinder; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Init constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param IConnection $connection |
|
27
|
|
|
* @param IFileFinder $fileFinder |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(IConnection $connection, IFileFinder $fileFinder) |
|
30
|
|
|
{ |
|
31
|
|
|
parent::__construct($connection); |
|
32
|
|
|
|
|
33
|
|
|
$this->fileFinder = $fileFinder; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return DateTime |
|
38
|
|
|
* @throws Exception |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getCreationDate(): DateTime |
|
41
|
|
|
{ |
|
42
|
|
|
return new DateTime(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Executes the query that rolls back the migration |
|
47
|
|
|
* |
|
48
|
|
|
* @throws FileNotFoundException |
|
49
|
|
|
* @throws Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function down(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$filename = sprintf('%s/%s', static::DOWN, static::FILENAME); |
|
54
|
|
|
|
|
55
|
|
|
$query = $this->execute($filename); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Executes the query that commits the migration |
|
60
|
|
|
* |
|
61
|
|
|
* @throws FileNotFoundException |
|
62
|
|
|
* @throws Exception |
|
63
|
|
|
*/ |
|
64
|
|
|
public function up(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$filename = sprintf('%s/%s', static::UP, static::FILENAME); |
|
67
|
|
|
|
|
68
|
|
|
$query = $this->execute($filename); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $filename |
|
74
|
|
|
* |
|
75
|
|
|
* @throws FileNotFoundException |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function execute(string $filename) |
|
78
|
|
|
{ |
|
79
|
|
|
$content = $this->fileFinder->read($filename); |
|
80
|
|
|
|
|
81
|
|
|
$this->connection->beginTransaction(); |
|
82
|
|
|
|
|
83
|
|
|
$statement = $this->connection->prepare($content); |
|
84
|
|
|
try { |
|
85
|
|
|
if (!$statement->execute()) { |
|
86
|
|
|
$this->connection->rollBack(); |
|
87
|
|
|
|
|
88
|
|
|
throw new Exception($statement->errorInfo(), $content, get_class($this), $filename); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
} catch (\PDOException $e) { |
|
91
|
|
|
$this->connection->rollBack(); |
|
92
|
|
|
|
|
93
|
|
|
throw new Exception($statement->errorInfo(), $content, get_class($this), $filename, '', 0, $e); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->connection->commit(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.