Skip to content

Commit

Permalink
Merge pull request #364 from simivar/feature/remove-patch-existing
Browse files Browse the repository at this point in the history
Remove patching from Generate commands
  • Loading branch information
veewee authored Aug 27, 2021
2 parents b8b413e + 9d2c1df commit 8dc4768
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 247 deletions.
74 changes: 0 additions & 74 deletions src/Phpro/SoapClient/Console/Command/GenerateClassmapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,6 @@ protected function generateClassmap(
*/
protected function handleClassmap(ClassMapGenerator $generator, TypeMap $typeMap, string $path): bool
{
// Handle existing class:
if ($this->filesystem->fileExists($path)) {
if ($this->handleExistingFile($generator, $typeMap, $path)) {
return true;
}

// Ask if a class can be overwritten if it contains errors
if (!$this->askForOverwrite()) {
$this->output->writeln(sprintf('Skipping %s', $path));

return false;
}
}

// Try to create a new class:
try {
$file = new FileGenerator();
Expand All @@ -156,66 +142,6 @@ protected function handleClassmap(ClassMapGenerator $generator, TypeMap $typeMap
return true;
}

/**
* An existing file was found. Try to patch or ask if it can be overwritten.
*
* @param ClassMapGenerator $generator
* @param TypeMap $typeMap
* @param string $path
* @return bool
*/
protected function handleExistingFile(ClassMapGenerator $generator, TypeMap $typeMap, $path): bool
{
$this->output->write(sprintf('Type %s exists. Trying to patch ...', $path));
$patched = $this->patchExistingFile($generator, $typeMap, $path);

if ($patched) {
$this->output->writeln('Patched!');

return true;
}

$this->output->writeln('Could not patch.');

return false;
}

/**
* This method tries to patch an existing type class.
*
* @param ClassMapGenerator $generator
* @param TypeMap $typeMap
* @param string $path
* @return bool
* @internal param Type $type
*/
protected function patchExistingFile(ClassMapGenerator $generator, TypeMap $typeMap, $path): bool
{
try {
$this->filesystem->createBackup($path);
$file = FileGenerator::fromReflectedFileName($path);
$this->generateClassmap($file, $generator, $typeMap, $path);
} catch (\Exception $e) {
$this->output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
$this->filesystem->removeBackup($path);

return false;
}

return true;
}

/**
* @return bool
*/
protected function askForOverwrite(): bool
{
$overwriteByDefault = $this->input->getOption('overwrite');
$question = new ConfirmationQuestion('Do you want to overwrite it?', $overwriteByDefault);

return $this->getHelper('question')->ask($this->input, $this->output, $question);
}

/**
* Function for added type hint
* @return ConfigHelper
Expand Down
74 changes: 0 additions & 74 deletions src/Phpro/SoapClient/Console/Command/GenerateClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,6 @@ protected function generateClient(FileGenerator $file, GeneratorInterface $gener
*/
protected function handleClient(ClientGenerator $generator, Client $client, string $path): bool
{
// Handle existing class:
if ($this->filesystem->fileExists($path)) {
if ($this->handleExistingFile($generator, $client, $path)) {
return true;
}

// Ask if a class can be overwritten if it contains errors
if (!$this->askForOverwrite()) {
$this->output->writeln(sprintf('Skipping %s', $client->getName()));

return false;
}
}

// Try to create a blanco class:
try {
$file = new FileGenerator();
Expand All @@ -154,66 +140,6 @@ protected function handleClient(ClientGenerator $generator, Client $client, stri
return true;
}

/**
* An existing file was found. Try to patch or ask if it can be overwritten.
*
* @param GeneratorInterface $generator
* @param Client $client
* @param string $path
* @return bool
*/
protected function handleExistingFile(GeneratorInterface $generator, Client $client, $path): bool
{
$this->output->write(sprintf('Class %s exists. Trying to patch ...', $client->getName()));
$patched = $this->patchExistingFile($generator, $client, $path);

if ($patched) {
$this->output->writeln('Patched!');

return true;
}

$this->output->writeln('Could not patch.');

return false;
}

/**
* This method tries to patch an existing type class.
*
* @param GeneratorInterface $generator
* @param Client $client
* @param string $path
* @return bool
* @internal param Type $type
*/
protected function patchExistingFile(GeneratorInterface $generator, Client $client, $path): bool
{
try {
$this->filesystem->createBackup($path);
$file = FileGenerator::fromReflectedFileName($path);
$this->generateClient($file, $generator, $client, $path);
} catch (\Exception $e) {
$this->output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
$this->filesystem->removeBackup($path);

return false;
}

return true;
}

/**
* @return bool
*/
protected function askForOverwrite(): bool
{
$overwriteByDefault = $this->input->getOption('overwrite');
$question = new ConfirmationQuestion('Do you want to overwrite it?', $overwriteByDefault);

return $this->getHelper('question')->ask($this->input, $this->output, $question);
}

/**
* Function for added type hint
* @return ConfigHelper
Expand Down
73 changes: 0 additions & 73 deletions src/Phpro/SoapClient/Console/Command/GenerateTypesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,6 @@ protected function handleType(TypeGenerator $generator, Type $type, SplFileInfo
{
// Generate type sub folders if needed
$this->filesystem->ensureDirectoryExists($fileInfo->getPath());
// Handle existing class:
if ($fileInfo->isFile()) {
if ($this->handleExistingFile($generator, $type, $fileInfo)) {
return true;
}

// Ask if a class can be overwritten if it contains errors
if (!$this->askForOverwrite()) {
$this->output->writeln(sprintf('Skipping %s', $type->getName()));

return false;
}
}

// Try to create a blanco class:
try {
Expand All @@ -139,55 +126,6 @@ protected function handleType(TypeGenerator $generator, Type $type, SplFileInfo
return true;
}

/**
* An existing file was found. Try to patch or ask if it can be overwritten.
*
* @param TypeGenerator $generator
* @param Type $type
* @param SplFileInfo $fileInfo
* @return bool
*
*/
protected function handleExistingFile(TypeGenerator $generator, Type $type, SplFileInfo $fileInfo): bool
{
$this->output->write(sprintf('Type %s exists. Trying to patch ...', $type->getName()));
$patched = $this->patchExistingFile($generator, $type, $fileInfo);

if ($patched) {
$this->output->writeln('Patched!');

return true;
}

$this->output->writeln('Could not patch.');

return false;
}

/**
* This method tries to patch an existing type class.
*
* @param TypeGenerator $generator
* @param Type $type
* @param SplFileInfo $fileInfo
* @return bool
*/
protected function patchExistingFile(TypeGenerator $generator, Type $type, SplFileInfo $fileInfo): bool
{
try {
$this->filesystem->createBackup($fileInfo->getPathname());
$file = FileGenerator::fromReflectedFileName($fileInfo->getPathname());
$this->generateType($file, $generator, $type, $fileInfo);
} catch (\Exception $e) {
$this->output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
$this->filesystem->removeBackup($fileInfo->getPathname());

return false;
}

return true;
}

/**
* Generates one type class
*
Expand All @@ -202,17 +140,6 @@ protected function generateType(FileGenerator $file, TypeGenerator $generator, T
$this->filesystem->putFileContents($fileInfo->getPathname(), $code);
}

/**
* @return bool
*/
protected function askForOverwrite(): bool
{
$overwriteByDefault = $this->input->getOption('overwrite');
$question = new ConfirmationQuestion('Do you want to overwrite it?', $overwriteByDefault);

return (bool)$this->getHelper('question')->ask($this->input, $this->output, $question);
}

/**
* Function for added type hint
* @return ConfigHelper
Expand Down
26 changes: 0 additions & 26 deletions src/Phpro/SoapClient/Util/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,4 @@ public function putFileContents(string $path, $content)
$this->ensureDirectoryExists(\dirname($path));
file_put_contents($path, $content);
}

/**
* @param string $file
*/
public function createBackup(string $file)
{
if (!$this->fileExists($file)) {
throw new RuntimeException('Could not create a backup from a non existing file: '.$file);
}

$backupFile = preg_replace('{\.backup$}', '', $file).'.backup';
copy($file, $backupFile);
}

/**
* @param string $file
*/
public function removeBackup(string $file)
{
$backupFile = preg_replace('{\.backup$}', '', $file).'.backup';
if (!$this->fileExists($backupFile)) {
return;
}

unlink($backupFile);
}
}

0 comments on commit 8dc4768

Please sign in to comment.