drupal-module-generator/src/Command/GenerateDrupal7Command.php

136 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace Opdavies\DrupalModuleGenerator\Command;
use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException;
2020-02-09 19:16:57 +00:00
use Opdavies\DrupalModuleGenerator\Service\ModuleNameConverter;
2020-02-09 18:40:26 +00:00
use Opdavies\DrupalModuleGenerator\Service\TestNameConverter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2020-02-09 13:32:21 +00:00
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
2020-02-09 14:27:12 +00:00
use Tightenco\Collect\Support\Collection;
2020-02-09 21:27:30 +00:00
final class GenerateDrupal7Command extends Command
{
2020-02-09 18:40:26 +00:00
private $moduleName;
2020-02-09 19:16:57 +00:00
private $machineName;
2020-02-09 18:40:26 +00:00
private $testName;
2020-02-09 13:32:21 +00:00
private $finder;
private $io;
2020-02-09 19:16:57 +00:00
private $moduleNameConverter;
2020-02-09 18:40:26 +00:00
private $testNameConverter;
2020-02-09 18:40:26 +00:00
public function __construct(
Finder $finder,
2020-02-09 19:16:57 +00:00
ModuleNameConverter $moduleNameConverter,
2020-02-09 18:40:26 +00:00
TestNameConverter $testNameConverter,
string $name = null
) {
2020-02-09 13:32:21 +00:00
parent::__construct($name);
$this->finder = $finder;
2020-02-09 19:16:57 +00:00
$this->moduleNameConverter = $moduleNameConverter;
2020-02-09 18:40:26 +00:00
$this->testNameConverter = $testNameConverter;
2020-02-09 13:32:21 +00:00
}
/**
* {@inheritdoc}
*/
2020-02-09 21:46:33 +00:00
protected static $defaultName = 'generate:drupal-7-module';
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
2020-02-09 21:49:34 +00:00
->setDescription('Generate a new Drupal 7 module')
->addArgument('module-name', InputArgument::REQUIRED, 'The name of the module to create')
->setAliases(['d7', 'drupal7'])
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
2020-02-09 13:32:21 +00:00
$this->io = new SymfonyStyle($input, $output);
2020-02-10 08:48:18 +00:00
$this->io->title("Drupal Module Generator (D7)");
2020-02-09 21:46:24 +00:00
2020-02-09 19:16:57 +00:00
$this->machineName = $input->getArgument('module-name');
$this->moduleName = $this->moduleNameConverter->__invoke($this->machineName);
$this->testName = $this->testNameConverter->__invoke($this->machineName);
2020-02-09 13:32:21 +00:00
$this
->ensureDirectoryDoesNotExist()
->createModuleDirectory()
->createFiles();
2020-02-09 12:24:28 +00:00
return 0;
}
/**
* Ensure that the directory name for the module doesn't already exist.
*/
private function ensureDirectoryDoesNotExist()
{
2020-02-09 19:16:57 +00:00
if (is_dir($this->machineName)) {
throw CannotCreateModuleException::directoryAlreadyExists();
}
2020-02-09 13:32:21 +00:00
return $this;
}
private function createModuleDirectory()
{
2020-02-09 19:16:57 +00:00
mkdir($this->machineName);
2020-02-09 13:32:21 +00:00
return $this;
}
private function createFiles()
{
2020-02-09 14:27:12 +00:00
$createdFiles = new Collection();
2020-02-09 13:32:21 +00:00
/** @var SplFileInfo $file */
2020-02-09 21:27:08 +00:00
foreach ($this->finder->in(__DIR__.'/../../fixtures/drupal7_module')->files() as $file) {
2020-02-09 19:16:57 +00:00
$filename = "{$this->machineName}.{$file->getExtension()}";
2020-02-09 18:40:26 +00:00
if ($file->getRelativePath()) {
2020-02-09 19:16:57 +00:00
mkdir("{$this->machineName}/{$file->getRelativePath()}", 0777, $recursive = true);
2020-02-09 18:40:26 +00:00
$filename = "{$this->testName}.php";
$filename = "{$file->getRelativePath()}/{$filename}";
}
2020-02-09 13:32:21 +00:00
$contents = $this->updateFileContents($file->getContents());
2020-02-09 19:16:57 +00:00
file_put_contents("{$this->machineName}/{$filename}", $contents);
2020-02-09 13:32:21 +00:00
2020-02-09 18:40:26 +00:00
$createdFiles->push($filename);
2020-02-09 13:32:21 +00:00
}
2020-02-09 21:46:24 +00:00
if ($createdFiles->isNotEmpty()) {
$this->io->block('Files generated:');
$this->io->listing($createdFiles->sort()->toArray());
}
2020-02-09 13:32:21 +00:00
}
private function updateFileContents($contents)
{
2020-02-09 21:28:46 +00:00
$contents = str_replace('{{ machine_name }}', $this->machineName, $contents);
2020-02-09 13:32:21 +00:00
$contents = str_replace('{{ name }}', $this->moduleName, $contents);
2020-02-09 18:40:26 +00:00
$contents = str_replace('{{ test_name }}', $this->testName, $contents);
2020-02-09 13:32:21 +00:00
return $contents;
}
}