-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[console] Add GenerateCommand Base class. (#334)
* [console] Add GenerateCommand Base class. * [console] Fix dockblock.
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Core\Command\GenerateCommand. | ||
*/ | ||
|
||
namespace Drupal\Console\Core\Command; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
use Webmozart\PathUtil\Path; | ||
|
||
/** | ||
* Class GenerateCommand | ||
* | ||
* @package Drupal\Console\Core\Command | ||
*/ | ||
abstract class GenerateCommand extends Command | ||
{ | ||
protected function validateFileExists( | ||
Filesystem $fs, | ||
$sourceFiles, | ||
$stopOnException = true | ||
) { | ||
$notFound = []; | ||
if (!is_array($sourceFiles)) { | ||
$sourceFiles = [$sourceFiles]; | ||
} | ||
foreach ($sourceFiles as $sourceFile) { | ||
if ($fs->exists($sourceFile)) { | ||
return $sourceFile; | ||
} | ||
|
||
$notFound[] = Path::makeRelative( | ||
$sourceFile, | ||
$this->drupalFinder->getComposerRoot() | ||
); | ||
} | ||
|
||
if ($stopOnException) { | ||
$this->createException( | ||
'File(s): ' . implode(', ', $notFound) . ' not found.' | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function backUpFile(Filesystem $fs, $fileName) | ||
{ | ||
$fileNameBackup = $fileName.'.original'; | ||
if ($fs->exists($fileName)) { | ||
if ($fs->exists($fileNameBackup)) { | ||
$fs->remove($fileName); | ||
return; | ||
} | ||
|
||
$fs->rename( | ||
$fileName, | ||
$fileNameBackup, | ||
TRUE | ||
); | ||
|
||
$fileNameBackup = Path::makeRelative( | ||
$fileNameBackup, | ||
$this->drupalFinder->getComposerRoot() | ||
); | ||
|
||
$this->getIo()->success( | ||
'File ' . $fileNameBackup . ' created.' | ||
); | ||
|
||
} | ||
} | ||
|
||
protected function showFileCreatedMessage($fileName) { | ||
$fileName = Path::makeRelative( | ||
$fileName, | ||
$this->drupalFinder->getComposerRoot() | ||
); | ||
|
||
$this->getIo()->success('File: ' . $fileName . ' created.'); | ||
} | ||
} |