Skip to content

Commit

Permalink
[console] Add GenerateCommand Base class. (#334)
Browse files Browse the repository at this point in the history
* [console] Add  GenerateCommand Base class.

* [console] Fix dockblock.
  • Loading branch information
jmolivas authored Mar 21, 2018
1 parent 18992a8 commit 957dda4
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Command/GenerateCommand.php
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.');
}
}

0 comments on commit 957dda4

Please sign in to comment.