Skip to content

Commit aba44eb

Browse files
committed
issue #262 - implement namespace-directories option
The goal is to be able to match or not the namespace path as the directory structure for the generated classes
1 parent b58e351 commit aba44eb

File tree

51 files changed

+138
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+138
-22
lines changed

src/Command/GeneratePackageCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ protected function configure(): void
144144
InputOption::VALUE_OPTIONAL,
145145
'Package classes\' namespace'
146146
)
147+
->addOption(
148+
'namespace-directories',
149+
null,
150+
InputOption::VALUE_OPTIONAL,
151+
'Should the directories match the namespace path or not? True by default'
152+
)
147153
->addOption(
148154
'category',
149155
null,
@@ -302,6 +308,7 @@ protected function getPackageGenerationCommandLineOptions(): array
302308
'gentutorial' => GeneratorOptions::GENERATE_TUTORIAL_FILE,
303309
'login' => GeneratorOptions::BASIC_LOGIN,
304310
'namespace' => GeneratorOptions::NAMESPACE_PREFIX,
311+
'namespace-directories' => GeneratorOptions::NAMESPACE_DICTATES_DIRECTORIES,
305312
'password' => GeneratorOptions::BASIC_PASSWORD,
306313
'prefix' => GeneratorOptions::PREFIX,
307314
'proxy-host' => GeneratorOptions::PROXY_HOST,
@@ -339,7 +346,7 @@ protected function initGeneratorOptions(): self
339346
return $this;
340347
}
341348

342-
protected function formatOptionValue($optionValue): bool
349+
protected function formatOptionValue($optionValue)
343350
{
344351
if ('true' === $optionValue || (is_numeric($optionValue) && 1 === (int) $optionValue)) {
345352
return true;

src/ConfigurationReader/GeneratorOptions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* @method self setGenerateTutorialFile(bool $generateTutorialFile)
1818
* @method bool getGenericConstantsNames()
1919
* @method self setGenericConstantsNames(bool $genericConstantsNames)
20+
* @method bool getNamespaceDictatesDirectories()
21+
* @method self setNamespaceDictatesDirectories(bool $namespaceDictatesDirectories)
2022
* @method bool getStandalone()
2123
* @method self setStandalone(bool $standalone)
2224
* @method bool getValidation()
@@ -101,6 +103,7 @@ final class GeneratorOptions extends AbstractYamlReader implements JsonSerializa
101103
public const GENERATE_TUTORIAL_FILE = 'generate_tutorial_file';
102104
public const GENERIC_CONSTANTS_NAME = 'generic_constants_names';
103105
public const NAMESPACE_PREFIX = 'namespace_prefix';
106+
public const NAMESPACE_DICTATES_DIRECTORIES = 'namespace_dictates_directories';
104107
public const ORIGIN = 'origin';
105108
public const PREFIX = 'prefix';
106109
public const PROXY_HOST = 'proxy_host';
@@ -136,6 +139,7 @@ final class GeneratorOptions extends AbstractYamlReader implements JsonSerializa
136139
self::GENERATE_TUTORIAL_FILE,
137140
self::GENERIC_CONSTANTS_NAME,
138141
self::NAMESPACE_PREFIX,
142+
self::NAMESPACE_DICTATES_DIRECTORIES,
139143
self::ORIGIN,
140144
self::PREFIX,
141145
self::PROXY_HOST,

src/File/AbstractModelFile.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public function getDestinationFolder(bool $withSrc = true): string
6161
return sprintf(
6262
'%s%s%s%s',
6363
$this->getGenerator()->getOptionDestination(),
64-
(bool) $withSrc && !empty($src) ? $src.DIRECTORY_SEPARATOR : '',
65-
str_replace('\\', DIRECTORY_SEPARATOR, $this->getGenerator()->getOptionNamespacePrefix()),
66-
$this->getGenerator()->getOptionNamespacePrefix() ? DIRECTORY_SEPARATOR : ''
64+
$withSrc && !empty($src) ? $src.DIRECTORY_SEPARATOR : '',
65+
$this->getGenerator()->getOptionNamespaceDictatesDirectories() ? str_replace('\\', DIRECTORY_SEPARATOR, $this->getGenerator()->getOptionNamespacePrefix()) : '',
66+
$this->getGenerator()->getOptionNamespacePrefix() && $this->getGenerator()->getOptionNamespaceDictatesDirectories() ? DIRECTORY_SEPARATOR : ''
6767
);
6868
}
6969

@@ -134,7 +134,7 @@ public function getStructAttributeType(StructAttributeModel $attribute = null, b
134134
$attribute = $this->getStructAttribute($attribute);
135135

136136
if (!$attribute instanceof StructAttributeModel) {
137-
throw new InvalidArgumentException(sprintf('Couldn\'t not find any valid StructAttribute'));
137+
throw new InvalidArgumentException('Could not find any valid StructAttribute');
138138
}
139139

140140
if ($returnArrayType && ($attribute->isArray())) {
@@ -174,7 +174,7 @@ public function getStructAttributeTypeAsPhpType(StructAttributeModel $fromAttrib
174174
$attribute = $this->getStructAttribute($fromAttribute);
175175

176176
if (!$attribute instanceof StructAttributeModel) {
177-
throw new InvalidArgumentException(sprintf('Couldn\'t not find any valid StructAttribute'));
177+
throw new InvalidArgumentException('Could not find any valid StructAttribute');
178178
}
179179

180180
$attributeType = $this->getStructAttributeType($attribute, true, $returnArrayType);
@@ -421,11 +421,4 @@ protected function useBrackets(StructAttributeModel $attribute, bool $returnArra
421421
{
422422
return $returnArrayType && $attribute->isArray();
423423
}
424-
425-
protected function getStructAttributeTypeHint(StructAttributeModel $attribute = null, bool $returnArrayType = true): string
426-
{
427-
$attribute = $this->getStructAttribute($attribute);
428-
429-
return ($returnArrayType && ($attribute->isArray() || $attribute->isList())) ? self::TYPE_ARRAY : $this->getStructAttributeType($attribute, true);
430-
}
431424
}

src/Generator/Generator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* @method self setOptionGenerateTutorialFile(bool $generateTutorialFile)
2929
* @method string getOptionNamespace()
3030
* @method self setOptionNamespace(string $namespace)
31+
* @method bool getOptionNamespaceDictatesDirectories()
32+
* @method self setOptionNamespaceDictatesDirectories(bool $namespaceDictatesDirectories)
3133
* @method array getOptionAddComments()
3234
* @method self setOptionAddComments(array $addComments)
3335
* @method bool getOptionStandalone()

src/resources/config/generator_options.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ add_comments:
2020
namespace_prefix:
2121
default: ''
2222
values: ''
23+
namespace_dictates_directories:
24+
default: true
25+
values: [ true, false ]
2326
standalone:
2427
default: true
2528
values: [ true, false ]

tests/ConfigurationReader/GeneratorOptionsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ public function testSetNamespace()
305305
$this->assertSame('\My\Project', $instance->getNamespace());
306306
}
307307

308+
public function testGetNamespaceDictatesDirectories()
309+
{
310+
$this->assertSame(true, self::optionsInstance()->getNamespaceDictatesDirectories());
311+
}
312+
313+
public function testSetNamespaceDictatesDirectories()
314+
{
315+
$instance = self::optionsInstance();
316+
$instance->setNamespaceDictatesDirectories(false);
317+
318+
$this->assertSame(false, $instance->getNamespaceDictatesDirectories());
319+
}
320+
308321
public function testGetStandalone()
309322
{
310323
$this->assertTrue(self::optionsInstance()->getStandalone());
@@ -549,6 +562,7 @@ public function testToArray()
549562
'generate_tutorial_file' => true,
550563
'add_comments' => [],
551564
'namespace_prefix' => '',
565+
'namespace_dictates_directories' => true,
552566
'standalone' => true,
553567
'validation' => true,
554568
'struct_class' => AbstractStructBase::class,
@@ -588,6 +602,7 @@ public function testJsonSerialize()
588602
'generate_tutorial_file' => true,
589603
'add_comments' => [],
590604
'namespace_prefix' => '',
605+
'namespace_dictates_directories' => true,
591606
'standalone' => true,
592607
'validation' => true,
593608
'struct_class' => AbstractStructBase::class,

tests/File/StructTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ public function testGetFileName()
4242
$this->assertSame(sprintf('%s%s%s/%s.php', self::getTestDirectory(), $model->getGenerator()->getOptionSrcDirname().DIRECTORY_SEPARATOR, $model->getContextualPart(), $model->getPackagedName(false)), $file->getFileName());
4343
}
4444

45+
public function testGetDestinationFolderMatchesNamespace()
46+
{
47+
$generator = self::bingGeneratorInstance()->setOptionNamespacePrefix($ns = 'Bing\Sdk');
48+
$model = new StructModel($generator, 'Foo');
49+
$file = new StructFile($generator, 'foo');
50+
$file->setModel($model);
51+
52+
$this->assertSame(sprintf(
53+
'%s%s%s',
54+
self::getTestDirectory(),
55+
$model->getGenerator()->getOptionSrcDirname().DIRECTORY_SEPARATOR,
56+
str_replace('\\', DIRECTORY_SEPARATOR, $ns).DIRECTORY_SEPARATOR
57+
), $file->getDestinationFolder(true));
58+
}
59+
60+
public function testGetDestinationFolderDoesNotMatchNamespace()
61+
{
62+
$generator = self::bingGeneratorInstance()->setOptionNamespacePrefix('Bing\Sdk')->setOptionNamespaceDictatesDirectories(false);
63+
$model = new StructModel($generator, 'Foo');
64+
$file = new StructFile($generator, 'foo');
65+
$file->setModel($model);
66+
67+
$this->assertSame(sprintf(
68+
'%s%s',
69+
self::getTestDirectory(),
70+
$model->getGenerator()->getOptionSrcDirname().DIRECTORY_SEPARATOR,
71+
), $file->getDestinationFolder(true));
72+
}
73+
4574
public function testWriteBingSearchStructQuery()
4675
{
4776
$generator = self::bingGeneratorInstance();

tests/Generator/GeneratorTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,19 @@ public function testSetOptionNamespacePrefix()
265265
$this->assertSame('My\Project', $instance->getOptionNamespacePrefix());
266266
}
267267

268+
public function testGetOptionNamespaceDictatesDirectories()
269+
{
270+
$this->assertTrue(self::localInstance()->getOptionNamespaceDictatesDirectories());
271+
}
272+
273+
public function testSetOptionNamespaceDictatesDirectories()
274+
{
275+
$instance = self::getBingGeneratorInstance();
276+
$instance->setOptionNamespaceDictatesDirectories(false);
277+
278+
$this->assertSame(false, $instance->getOptionNamespaceDictatesDirectories());
279+
}
280+
268281
public function testGetOptionSoapClientClass()
269282
{
270283
$this->assertSame(AbstractSoapClientBase::class, self::localInstance()->getOptionSoapClientClass());
@@ -642,13 +655,14 @@ private function generate($dir, $wsdl, $standalone = true)
642655
{
643656
Utils::createDirectory($destination = self::getTestDirectory().$dir);
644657

658+
/** @var GeneratorOptions $options */
645659
$options = GeneratorOptions::instance();
646660
$options
647661
->setGenerateTutorialFile(false)
648662
->setAddComments([])
649663
->setArraysFolder('ArrayType')
650-
->setBasicLogin(null)
651-
->setBasicPassword(null)
664+
->setBasicLogin('')
665+
->setBasicPassword('')
652666
->setCategory(GeneratorOptions::VALUE_CAT)
653667
->setComposerName($standalone ? 'wsdltophp/'.$dir : '')
654668
->setComposerSettings($standalone ? [
@@ -659,14 +673,15 @@ private function generate($dir, $wsdl, $standalone = true)
659673
->setEnumsFolder('EnumType')
660674
->setGatherMethods(GeneratorOptions::VALUE_START)
661675
->setGenerateTutorialFile(true)
662-
->setGenericConstantsName(false)
676+
->setGenericConstantsNames(false)
663677
->setNamespace('')
678+
->setNamespaceDictatesDirectories(true)
664679
->setOrigin($wsdl)
665680
->setPrefix('')
666-
->setProxyHost(null)
667-
->setProxyLogin(null)
668-
->setProxyPassword(null)
669-
->setProxyPort(null)
681+
->setProxyHost('')
682+
->setProxyLogin('')
683+
->setProxyPassword('')
684+
->setProxyPort('')
670685
->setServicesFolder('ServiceType')
671686
->setSchemasSave(false)
672687
->setSchemasFolder('wsdl')

tests/resources/generated/json_serialized.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,6 +3347,7 @@
33473347
"generate_tutorial_file": true,
33483348
"add_comments": [],
33493349
"namespace_prefix": "",
3350+
"namespace_dictates_directories": true,
33503351
"standalone": false,
33513352
"validation": true,
33523353
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_actonservice2_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,6 +3583,7 @@
35833583
"release": "1.1.0"
35843584
},
35853585
"namespace_prefix": "",
3586+
"namespace_dictates_directories": true,
35863587
"standalone": true,
35873588
"validation": true,
35883589
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_actonservice2_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,6 +3583,7 @@
35833583
"release": "1.1.0"
35843584
},
35853585
"namespace_prefix": "",
3586+
"namespace_dictates_directories": true,
35863587
"standalone": true,
35873588
"validation": true,
35883589
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_bingsearch_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,6 +3349,7 @@
33493349
"release": "1.1.0"
33503350
},
33513351
"namespace_prefix": "",
3352+
"namespace_dictates_directories": true,
33523353
"standalone": true,
33533354
"validation": true,
33543355
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_bingsearch_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,6 +3349,7 @@
33493349
"release": "1.1.0"
33503350
},
33513351
"namespace_prefix": "",
3352+
"namespace_dictates_directories": true,
33523353
"standalone": true,
33533354
"validation": true,
33543355
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_deliveryservice_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12356,6 +12356,7 @@
1235612356
"release": "1.1.0"
1235712357
},
1235812358
"namespace_prefix": "",
12359+
"namespace_dictates_directories": true,
1235912360
"standalone": true,
1236012361
"validation": true,
1236112362
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_deliveryservice_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12113,6 +12113,7 @@
1211312113
"release": "1.1.0"
1211412114
},
1211512115
"namespace_prefix": "",
12116+
"namespace_dictates_directories": true,
1211612117
"standalone": true,
1211712118
"validation": true,
1211812119
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_docdatapayments_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10037,6 +10037,7 @@
1003710037
"release": "1.1.0"
1003810038
},
1003910039
"namespace_prefix": "",
10040+
"namespace_dictates_directories": true,
1004010041
"standalone": true,
1004110042
"validation": true,
1004210043
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_docdatapayments_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10028,6 +10028,7 @@
1002810028
"release": "1.1.0"
1002910029
},
1003010030
"namespace_prefix": "",
10031+
"namespace_dictates_directories": true,
1003110032
"standalone": true,
1003210033
"validation": true,
1003310034
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_ews_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92579,6 +92579,7 @@
9257992579
"release": "1.1.0"
9258092580
},
9258192581
"namespace_prefix": "",
92582+
"namespace_dictates_directories": true,
9258292583
"standalone": true,
9258392584
"validation": true,
9258492585
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_ews_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91949,6 +91949,7 @@
9194991949
"release": "1.1.0"
9195091950
},
9195191951
"namespace_prefix": "",
91952+
"namespace_dictates_directories": true,
9195291953
"standalone": true,
9195391954
"validation": true,
9195491955
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_myboard_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11534,6 +11534,7 @@
1153411534
"release": "1.1.0"
1153511535
},
1153611536
"namespace_prefix": "",
11537+
"namespace_dictates_directories": true,
1153711538
"standalone": true,
1153811539
"validation": true,
1153911540
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_myboard_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10652,6 +10652,7 @@
1065210652
"release": "1.1.0"
1065310653
},
1065410654
"namespace_prefix": "",
10655+
"namespace_dictates_directories": true,
1065510656
"standalone": true,
1065610657
"validation": true,
1065710658
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_odigeo_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,7 @@
14551455
"release": "1.1.0"
14561456
},
14571457
"namespace_prefix": "",
1458+
"namespace_dictates_directories": true,
14581459
"standalone": true,
14591460
"validation": true,
14601461
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_odigeo_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,7 @@
14551455
"release": "1.1.0"
14561456
},
14571457
"namespace_prefix": "",
1458+
"namespace_dictates_directories": true,
14581459
"standalone": true,
14591460
"validation": true,
14601461
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_omnitureadminservices_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26757,6 +26757,7 @@
2675726757
"release": "1.1.0"
2675826758
},
2675926759
"namespace_prefix": "",
26760+
"namespace_dictates_directories": true,
2676026761
"standalone": true,
2676126762
"validation": true,
2676226763
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_omnitureadminservices_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25101,6 +25101,7 @@
2510125101
"release": "1.1.0"
2510225102
},
2510325103
"namespace_prefix": "",
25104+
"namespace_dictates_directories": true,
2510425105
"standalone": true,
2510525106
"validation": true,
2510625107
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_ordercontract_none.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5721,6 +5721,7 @@
57215721
"release": "1.1.0"
57225722
},
57235723
"namespace_prefix": "",
5724+
"namespace_dictates_directories": true,
57245725
"standalone": true,
57255726
"validation": true,
57265727
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

tests/resources/generated/parsed_ordercontract_start.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5703,6 +5703,7 @@
57035703
"release": "1.1.0"
57045704
},
57055705
"namespace_prefix": "",
5706+
"namespace_dictates_directories": true,
57065707
"standalone": true,
57075708
"validation": true,
57085709
"struct_class": "WsdlToPhp\\PackageBase\\AbstractStructBase",

0 commit comments

Comments
 (0)