|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Contains \Drupal\Console\Core\Command\GenerateCommand. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Drupal\Console\Core\Command; |
| 9 | + |
| 10 | +use Symfony\Component\Filesystem\Filesystem; |
| 11 | +use Webmozart\PathUtil\Path; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class GenerateCommand |
| 15 | + * |
| 16 | + * @package Drupal\Console\Core\Command |
| 17 | + */ |
| 18 | +abstract class GenerateCommand extends Command |
| 19 | +{ |
| 20 | + protected function validateFileExists( |
| 21 | + Filesystem $fs, |
| 22 | + $sourceFiles, |
| 23 | + $stopOnException = true |
| 24 | + ) { |
| 25 | + $notFound = []; |
| 26 | + if (!is_array($sourceFiles)) { |
| 27 | + $sourceFiles = [$sourceFiles]; |
| 28 | + } |
| 29 | + foreach ($sourceFiles as $sourceFile) { |
| 30 | + if ($fs->exists($sourceFile)) { |
| 31 | + return $sourceFile; |
| 32 | + } |
| 33 | + |
| 34 | + $notFound[] = Path::makeRelative( |
| 35 | + $sourceFile, |
| 36 | + $this->drupalFinder->getComposerRoot() |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + if ($stopOnException) { |
| 41 | + $this->createException( |
| 42 | + 'File(s): ' . implode(', ', $notFound) . ' not found.' |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + protected function backUpFile(Filesystem $fs, $fileName) |
| 50 | + { |
| 51 | + $fileNameBackup = $fileName.'.original'; |
| 52 | + if ($fs->exists($fileName)) { |
| 53 | + if ($fs->exists($fileNameBackup)) { |
| 54 | + $fs->remove($fileName); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $fs->rename( |
| 59 | + $fileName, |
| 60 | + $fileNameBackup, |
| 61 | + TRUE |
| 62 | + ); |
| 63 | + |
| 64 | + $fileNameBackup = Path::makeRelative( |
| 65 | + $fileNameBackup, |
| 66 | + $this->drupalFinder->getComposerRoot() |
| 67 | + ); |
| 68 | + |
| 69 | + $this->getIo()->success( |
| 70 | + 'File ' . $fileNameBackup . ' created.' |
| 71 | + ); |
| 72 | + |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + protected function showFileCreatedMessage($fileName) { |
| 77 | + $fileName = Path::makeRelative( |
| 78 | + $fileName, |
| 79 | + $this->drupalFinder->getComposerRoot() |
| 80 | + ); |
| 81 | + |
| 82 | + $this->getIo()->success('File: ' . $fileName . ' created.'); |
| 83 | + } |
| 84 | +} |
0 commit comments