@@ -6,167 +6,167 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Migrator extends M |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Fully qualified path to the application's migration directory |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - private $path; |
|
| 9 | + /** |
|
| 10 | + * Fully qualified path to the application's migration directory |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + private $path; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Get all of the migration files in a given path. |
|
| 18 | - * |
|
| 19 | - * @param string $path |
|
| 20 | - * @param bool $recursive |
|
| 21 | - * |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function getMigrationFiles($path, $recursive = true) |
|
| 25 | - { |
|
| 26 | - $this->setPath($path); |
|
| 16 | + /** |
|
| 17 | + * Get all of the migration files in a given path. |
|
| 18 | + * |
|
| 19 | + * @param string $path |
|
| 20 | + * @param bool $recursive |
|
| 21 | + * |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function getMigrationFiles($path, $recursive = true) |
|
| 25 | + { |
|
| 26 | + $this->setPath($path); |
|
| 27 | 27 | |
| 28 | - if ($recursive === true) { |
|
| 29 | - $files = $this->rglob($this->path.'/*_*.php', 0, true); |
|
| 30 | - } else { |
|
| 31 | - $files = $this->files->glob($this->path.'/*_*.php'); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - // Once we have the array of files in the directory we will just remove the |
|
| 35 | - // extension and take the basename of the file which is all we need when |
|
| 36 | - // finding the migrations that haven't been run against the databases. |
|
| 37 | - if ($files === false) { |
|
| 38 | - return []; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - $files = array_map(function ($file) { |
|
| 42 | - return str_replace('.php', '', basename($file)); |
|
| 43 | - |
|
| 44 | - }, $files); |
|
| 45 | - |
|
| 46 | - // Once we have all of the formatted file names we will sort them and since |
|
| 47 | - // they all start with a timestamp this should give us the migrations in |
|
| 48 | - // the order they were actually created by the application developers. |
|
| 49 | - sort($files); |
|
| 50 | - |
|
| 51 | - return $files; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Require in all the migration files in a given path. |
|
| 56 | - * |
|
| 57 | - * @param array $files |
|
| 58 | - * |
|
| 59 | - * @return void |
|
| 60 | - */ |
|
| 61 | - public function requireFiles(array $files) |
|
| 62 | - { |
|
| 63 | - foreach ($files as $file) { |
|
| 64 | - $newPath = $this->getFilePathWithFolders($file).'.php'; |
|
| 65 | - $this->files->requireOnce($newPath); |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Run "up" a migration instance. |
|
| 71 | - * |
|
| 72 | - * @param string $file |
|
| 73 | - * @param int $batch |
|
| 74 | - * @param bool $pretend |
|
| 75 | - * |
|
| 76 | - * @return void |
|
| 77 | - */ |
|
| 78 | - protected function runUp($file, $batch, $pretend) |
|
| 79 | - { |
|
| 80 | - // First we will resolve a "real" instance of the migration class from this |
|
| 81 | - // migration file name. Once we have the instances we can run the actual |
|
| 82 | - // command such as "up" or "down", or we can just simulate the action. |
|
| 83 | - $migration = $this->resolve($file); |
|
| 84 | - |
|
| 85 | - if ($pretend) { |
|
| 86 | - return $this->pretendToRun($migration, 'up'); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $migration->up(); |
|
| 90 | - |
|
| 91 | - // Once we have run a migrations class, we will log that it was run in this |
|
| 92 | - // repository so that we don't try to run it next time we do a migration |
|
| 93 | - // in the application. A migration repository keeps the migrate order. |
|
| 94 | - $this->repository->log($this->getFilePathWithoutFolders($file), $batch); |
|
| 95 | - |
|
| 96 | - $this->note("<info>Migrated:</info> $file"); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Recursive glob. |
|
| 101 | - * |
|
| 102 | - * @param string $pattern |
|
| 103 | - * @param int $flags |
|
| 104 | - * @param bool $ignore |
|
| 105 | - * |
|
| 106 | - * @return array |
|
| 107 | - */ |
|
| 108 | - public function rglob($pattern, $flags = 0, $ignore = false) |
|
| 109 | - { |
|
| 110 | - if ($ignore === false) { |
|
| 111 | - $files = glob($pattern, $flags); |
|
| 112 | - } else { |
|
| 113 | - $files = []; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { |
|
| 117 | - $files = array_merge($files, $this->rglob($dir.'/'.basename($pattern), $flags)); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $files; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Get the migration file path with our injected date folder. |
|
| 125 | - * |
|
| 126 | - * @param string $file |
|
| 127 | - * |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function getFilePathWithFolders($file) |
|
| 131 | - { |
|
| 132 | - $datePath = $this->getDateFolderStructure($file); |
|
| 133 | - |
|
| 134 | - return $this->path . '/' . $datePath . $file; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Remove folders from file path. |
|
| 139 | - * |
|
| 140 | - * @param string $file |
|
| 141 | - * |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function getFilePathWithoutFolders($file) |
|
| 145 | - { |
|
| 146 | - return basename($file); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Add date folders to migrations path. |
|
| 151 | - * |
|
| 152 | - * @param string $file |
|
| 153 | - * |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - public function getDateFolderStructure($file) |
|
| 157 | - { |
|
| 158 | - $parts = explode('_', $file); |
|
| 159 | - |
|
| 160 | - return $parts[0].'/'.$parts[1].'/'; |
|
| 161 | - } |
|
| 28 | + if ($recursive === true) { |
|
| 29 | + $files = $this->rglob($this->path.'/*_*.php', 0, true); |
|
| 30 | + } else { |
|
| 31 | + $files = $this->files->glob($this->path.'/*_*.php'); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + // Once we have the array of files in the directory we will just remove the |
|
| 35 | + // extension and take the basename of the file which is all we need when |
|
| 36 | + // finding the migrations that haven't been run against the databases. |
|
| 37 | + if ($files === false) { |
|
| 38 | + return []; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + $files = array_map(function ($file) { |
|
| 42 | + return str_replace('.php', '', basename($file)); |
|
| 43 | + |
|
| 44 | + }, $files); |
|
| 45 | + |
|
| 46 | + // Once we have all of the formatted file names we will sort them and since |
|
| 47 | + // they all start with a timestamp this should give us the migrations in |
|
| 48 | + // the order they were actually created by the application developers. |
|
| 49 | + sort($files); |
|
| 50 | + |
|
| 51 | + return $files; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Require in all the migration files in a given path. |
|
| 56 | + * |
|
| 57 | + * @param array $files |
|
| 58 | + * |
|
| 59 | + * @return void |
|
| 60 | + */ |
|
| 61 | + public function requireFiles(array $files) |
|
| 62 | + { |
|
| 63 | + foreach ($files as $file) { |
|
| 64 | + $newPath = $this->getFilePathWithFolders($file).'.php'; |
|
| 65 | + $this->files->requireOnce($newPath); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Run "up" a migration instance. |
|
| 71 | + * |
|
| 72 | + * @param string $file |
|
| 73 | + * @param int $batch |
|
| 74 | + * @param bool $pretend |
|
| 75 | + * |
|
| 76 | + * @return void |
|
| 77 | + */ |
|
| 78 | + protected function runUp($file, $batch, $pretend) |
|
| 79 | + { |
|
| 80 | + // First we will resolve a "real" instance of the migration class from this |
|
| 81 | + // migration file name. Once we have the instances we can run the actual |
|
| 82 | + // command such as "up" or "down", or we can just simulate the action. |
|
| 83 | + $migration = $this->resolve($file); |
|
| 84 | + |
|
| 85 | + if ($pretend) { |
|
| 86 | + return $this->pretendToRun($migration, 'up'); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $migration->up(); |
|
| 90 | + |
|
| 91 | + // Once we have run a migrations class, we will log that it was run in this |
|
| 92 | + // repository so that we don't try to run it next time we do a migration |
|
| 93 | + // in the application. A migration repository keeps the migrate order. |
|
| 94 | + $this->repository->log($this->getFilePathWithoutFolders($file), $batch); |
|
| 95 | + |
|
| 96 | + $this->note("<info>Migrated:</info> $file"); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Recursive glob. |
|
| 101 | + * |
|
| 102 | + * @param string $pattern |
|
| 103 | + * @param int $flags |
|
| 104 | + * @param bool $ignore |
|
| 105 | + * |
|
| 106 | + * @return array |
|
| 107 | + */ |
|
| 108 | + public function rglob($pattern, $flags = 0, $ignore = false) |
|
| 109 | + { |
|
| 110 | + if ($ignore === false) { |
|
| 111 | + $files = glob($pattern, $flags); |
|
| 112 | + } else { |
|
| 113 | + $files = []; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { |
|
| 117 | + $files = array_merge($files, $this->rglob($dir.'/'.basename($pattern), $flags)); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $files; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Get the migration file path with our injected date folder. |
|
| 125 | + * |
|
| 126 | + * @param string $file |
|
| 127 | + * |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function getFilePathWithFolders($file) |
|
| 131 | + { |
|
| 132 | + $datePath = $this->getDateFolderStructure($file); |
|
| 133 | + |
|
| 134 | + return $this->path . '/' . $datePath . $file; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Remove folders from file path. |
|
| 139 | + * |
|
| 140 | + * @param string $file |
|
| 141 | + * |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function getFilePathWithoutFolders($file) |
|
| 145 | + { |
|
| 146 | + return basename($file); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Add date folders to migrations path. |
|
| 151 | + * |
|
| 152 | + * @param string $file |
|
| 153 | + * |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + public function getDateFolderStructure($file) |
|
| 157 | + { |
|
| 158 | + $parts = explode('_', $file); |
|
| 159 | + |
|
| 160 | + return $parts[0].'/'.$parts[1].'/'; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Set the path |
|
| 165 | - * |
|
| 166 | - * @param array|string $path |
|
| 167 | - */ |
|
| 168 | - private function setPath($path) |
|
| 169 | - { |
|
| 170 | - $this->path = is_array($path) ? $path[0] : $path; |
|
| 171 | - } |
|
| 163 | + /** |
|
| 164 | + * Set the path |
|
| 165 | + * |
|
| 166 | + * @param array|string $path |
|
| 167 | + */ |
|
| 168 | + private function setPath($path) |
|
| 169 | + { |
|
| 170 | + $this->path = is_array($path) ? $path[0] : $path; |
|
| 171 | + } |
|
| 172 | 172 | } |