Skip to content

Commit

Permalink
Replace underscores with DIRECTORY_SEPARATOR (PSR-O, PSR-4) (#71)
Browse files Browse the repository at this point in the history
* Replace underscores with DIRECTORY_SEPARATOR (PSR-O, PSR-4)

* Create dirs if needed
Fix Typo

* Added ensureDirectoryExists
Removed now unused directoryExists

* Used splFileInfo
Updated the spec test so that it also checks for the underscore replacements

* Use string in the Filesystem class
Update spec test

* Use SplFileInfo in the whole command
  • Loading branch information
janvernieuwe authored and veewee committed Sep 29, 2017
1 parent 39a6d26 commit f9a4526
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 39 deletions.
6 changes: 6 additions & 0 deletions spec/Phpro/SoapClient/CodeGenerator/Model/TypeSpec.php
Expand Up @@ -50,6 +50,12 @@ function it_has_a_path_name()
$this->getPathname('my/dir')->shouldReturn('my/dir/MyType.php');
}

function it_should_replace_underscores_in_paths()
{
$this->beConstructedWith('MyNamespace', 'myType_3_2', ['prop1' => 'string']);
$this->getFileInfo('my/dir')->getPathname()->shouldReturn('my/dir/MyType/3/2.php');
}

function it_has_properties()
{
$props = $this->getProperties();
Expand Down
15 changes: 12 additions & 3 deletions src/Phpro/SoapClient/CodeGenerator/Model/Type.php
Expand Up @@ -3,6 +3,7 @@
namespace Phpro\SoapClient\CodeGenerator\Model;

use Phpro\SoapClient\CodeGenerator\Util\Normalizer;
use SplFileInfo;

/**
* Class Type
Expand Down Expand Up @@ -75,14 +76,22 @@ public function getXsdName()
return $this->xsdName;
}

public function getFileInfo(string $destination) : SplFileInfo
{
$path = rtrim($destination, '/\\').DIRECTORY_SEPARATOR.$this->getName().'.php';
$path = str_replace('_', DIRECTORY_SEPARATOR, $path);

return new SplFileInfo($path);
}

/**
* @param $destination
*
* @param string $destination
* @deprecated please use getFileInfo instead
* @return string
*/
public function getPathname($destination)
{
return rtrim($destination, '/\\') . DIRECTORY_SEPARATOR . $this->getName() . '.php';
return $this->getFileInfo($destination)->getPathname();
}

/**
Expand Down
66 changes: 35 additions & 31 deletions src/Phpro/SoapClient/Console/Command/GenerateTypesCommand.php
Expand Up @@ -9,6 +9,7 @@
use Phpro\SoapClient\Exception\InvalidArgumentException;
use Phpro\SoapClient\Soap\SoapClient;
use Phpro\SoapClient\Util\Filesystem;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -89,11 +90,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$soapClient = new SoapClient($config->getWsdl(), $config->getSoapOptions());
$typeMap = TypeMap::fromSoapClient($config->getNamespace(), $soapClient);
$generator = new TypeGenerator($config->getRuleSet());

foreach ($typeMap->getTypes() as $type) {
$path = $type->getPathname($config->getDestination());
if ($this->handleType($generator, $type, $path)) {
$this->output->writeln(sprintf('Generated class %s to %s', $type->getFullName(), $path));
$fileInfo = $type->getFileInfo($config->getDestination());
if ($this->handleType($generator, $type, $fileInfo)) {
$this->output->writeln(
sprintf('Generated class %s to %s', $type->getFullName(), $fileInfo->getPathname())
);
}
}

Expand All @@ -106,17 +109,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
* If patching the old class does not wor: ask for an overwrite
* Create a class from an empty file
*
* @param TypeGenerator $generator
* @param Type $type
* @param $path
*
* @param TypeGenerator $generator
* @param Type $type
* @param SplFileInfo $fileInfo
* @return bool
*/
protected function handleType(TypeGenerator $generator, Type $type, $path)
protected function handleType(TypeGenerator $generator, Type $type, SplFileInfo $fileInfo)
{
// Generate type sub folders if needed
$this->filesystem->ensureDirectoryExists($fileInfo->getPath());
// Handle existing class:
if ($this->filesystem->fileExists($path)) {
if ($this->handleExistingFile($generator, $type, $path)) {
if ($fileInfo->isFile()) {
if ($this->handleExistingFile($generator, $type, $fileInfo)) {
return true;
}

Expand All @@ -130,9 +134,10 @@ protected function handleType(TypeGenerator $generator, Type $type, $path)
// Try to create a blanco class:
try {
$file = new FileGenerator();
$this->generateType($file, $generator, $type, $path);
$this->generateType($file, $generator, $type, $fileInfo);
} catch (\Exception $e) {
$this->output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
$this->output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');

return false;
}

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

if ($patched) {
$this->output->writeln('Patched!');
Expand All @@ -166,21 +171,20 @@ protected function handleExistingFile(TypeGenerator $generator, Type $type, $pat
/**
* This method tries to patch an existing type class.
*
* @param TypeGenerator $generator
* @param Type $type
* @param string $path
*
* @param TypeGenerator $generator
* @param Type $type
* @param SplFileInfo $fileInfo
* @return bool
*/
protected function patchExistingFile(TypeGenerator $generator, Type $type, $path)
protected function patchExistingFile(TypeGenerator $generator, Type $type, SplFileInfo $fileInfo)
{
try {
$this->filesystem->createBackup($path);
$file = FileGenerator::fromReflectedFileName($path);
$this->generateType($file, $generator, $type, $path);
$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($path);
$this->filesystem->removeBackup($fileInfo->getPathname());
return false;
}

Expand All @@ -193,12 +197,12 @@ protected function patchExistingFile(TypeGenerator $generator, Type $type, $path
* @param FileGenerator $file
* @param TypeGenerator $generator
* @param Type $type
* @param string $path
* @param SplFileInfo $fileInfo
*/
protected function generateType(FileGenerator $file, TypeGenerator $generator, Type $type, $path)
protected function generateType(FileGenerator $file, TypeGenerator $generator, Type $type, SplFileInfo $fileInfo)
{
$code = $generator->generate($file, $type);
$this->filesystem->putFileContents($path, $code);
$this->filesystem->putFileContents($fileInfo->getPathname(), $code);
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/Phpro/SoapClient/Util/Filesystem.php
Expand Up @@ -4,6 +4,7 @@

use Phpro\SoapClient\Exception\InvalidArgumentException;
use Phpro\SoapClient\Exception\RuntimeException;
use SplFileInfo;

/**
* Class Filesystem
Expand All @@ -14,13 +15,19 @@ class Filesystem
{

/**
* @param $directory
*
* @return bool
* @param string $directory
*/
public function dirextoryExists($directory)
public function ensureDirectoryExists($directory)
{
return is_dir($directory) && is_writable($directory);
if (is_dir($directory)) {
return;
}
if (file_exists($directory)) {
throw new RuntimeException($directory.' exists and is not a directory.');
}
if (!@mkdir($directory, 0777, true)) {
throw new RuntimeException($directory.' does not exist and could not be created.');
}
}

/**
Expand Down

0 comments on commit f9a4526

Please sign in to comment.