1 | <?php |
||
9 | class Migrations |
||
10 | { |
||
11 | private $manipulatorFactory; |
||
12 | private $config; |
||
13 | private $io; |
||
14 | |||
15 | 41 | public function __construct(Io $io, DatabaseManipulatorFactory $manipulatorFactory, array $config) |
|
16 | { |
||
17 | 41 | $this->manipulatorFactory = $manipulatorFactory; |
|
18 | 41 | $this->config = $config; |
|
19 | 41 | $this->io = $io; |
|
20 | 41 | } |
|
21 | |||
22 | 9 | public function getAllPaths() |
|
23 | { |
||
24 | 9 | return array_merge( |
|
25 | array( |
||
26 | array( |
||
27 | 9 | 'home' => $this->getPath('migrations'), |
|
28 | 9 | 'variables' => $this->config['variables'] |
|
29 | ) |
||
30 | 9 | ), $this->config['other_migrations'] |
|
31 | ); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Return an array of all migrations available. |
||
36 | * |
||
37 | * @param string $path |
||
38 | * @return array |
||
39 | */ |
||
40 | 9 | public function getMigrationFiles($path) |
|
41 | { |
||
42 | 9 | if (!file_exists($path)) |
|
43 | return []; |
||
44 | 9 | $migrationFiles = scandir($path, 0); |
|
45 | 9 | $migrations = array(); |
|
46 | 9 | foreach ($migrationFiles as $migration) { |
|
47 | 9 | $details = $this->getMigrationDetails($migration); |
|
48 | 9 | if ($details === false) |
|
49 | 9 | continue; |
|
50 | 9 | $migrations[$details['timestamp']] = $details; |
|
51 | 9 | unset($migrations[$details['timestamp']][0]); |
|
52 | 9 | unset($migrations[$details['timestamp']][1]); |
|
53 | 9 | unset($migrations[$details['timestamp']][2]); |
|
54 | } |
||
55 | |||
56 | 9 | return $migrations; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Return the details of a migration extracted from the file name. |
||
61 | * This method uses a regular expression to extract the timestamp and |
||
62 | * migration name from the migration script. |
||
63 | * |
||
64 | * @param string $migration |
||
65 | * @return array|bool |
||
66 | */ |
||
67 | 9 | private function getMigrationDetails($migration) |
|
68 | { |
||
69 | 9 | if (preg_match("/^(?<timestamp>[0-9]{14})\_(?<migration>[a-z][a-z0-9\_]*)\.php$/", $migration, $details)) { |
|
70 | 9 | $details['file'] = $migration; |
|
71 | } else { |
||
72 | 9 | $details = false; |
|
73 | } |
||
74 | 9 | return $details; |
|
75 | } |
||
76 | /** |
||
77 | * Returns an array of all migrations, in all configured migrations |
||
78 | * directories. |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getAllMigrations() |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Returns an array of all migrations that have been run on the database. |
||
93 | * The information returned includes the timestamp, the name of the migration |
||
94 | * and the default schema on which it was run. |
||
95 | * @return array |
||
96 | * @throws exceptions\DatabaseManipulatorException |
||
97 | */ |
||
98 | public function getRunMirations() |
||
113 | |||
114 | /** |
||
115 | * Returns a path relative to the current yentu home. |
||
116 | * @param string $path |
||
117 | * @return string |
||
118 | */ |
||
119 | 41 | public function getPath($path) |
|
123 | |||
124 | |||
125 | /** |
||
126 | * Announce a migration based on the command and the arguments called for |
||
127 | * the migration. |
||
128 | * |
||
129 | * @param string $command The action being performed |
||
130 | * @param string $itemType The type of item |
||
131 | * @param array $arguments The arguments of the |
||
132 | */ |
||
133 | 9 | public function announce($command, $itemType, $arguments) |
|
142 | |||
143 | /** |
||
144 | * Convert the arguments of a migration event to a string description. |
||
145 | * |
||
146 | * @param string $command |
||
147 | * @param array $arguments |
||
148 | * @return string |
||
149 | */ |
||
150 | 9 | private function getMigrationEventDescription($command, $arguments) |
|
182 | } |