forked from mikaelcom/WsdlToPhp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WsdlToPhp.php
2361 lines (2361 loc) · 83.5 KB
/
WsdlToPhp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File for class WsdlToPhp
* @package WsdlToPhp
* @deprecated In favor to WsdlToPhpGenerator file
* @date 25/06/2012
*/
/**
* Class WsdlToPhp
* @package WsdlToPhp
* @deprecated In favor to WsdlToPhpGenerator class
* @date 25/06/2012
*/
class WsdlToPhp extends SoapClient
{
/**
* Set categorization of classes based on the end of the name of the struct or the function
* The catagory set the tree folders
* @var int
*/
const OPT_CAT_END_NAME = 0;
/**
* Set categorization of classes based on the start of the name of the struct or the function
* The catagory set the tree folders
* @var int
*/
const OPT_CAT_START_NAME = 1;
/**
* Set uncategorization of classes
* All files are put in the same folder
* @var int
*/
const OPT_CAT_NONE_NAME = 2;
/**
* Index to set categorization when calling the constructor
* @var string
*/
const OPT_CAT_KEY = 'option_category_key';
/**
* Set subcategorization of classes based on the end of the name of the struct or the function
* The catagory set the tree folders
* @var int
*/
const OPT_SUB_CAT_END_NAME = 0;
/**
* Set subcategorization of classes based on the start of the name of the struct or the function
* The catagory set the tree folders
* @var int
*/
const OPT_SUB_CAT_START_NAME = 1;
/**
* Set uncategorization of classes
* All files are put in the same folder
* @var int
*/
const OPT_SUB_CAT_NONE_NAME = 2;
/**
* Index to set subcategorization when calling the constructor
* @var string
*/
const OPT_SUB_CAT_KEY = 'option_sub_category_key';
/**
* Set gathering mode of mtehod per class based on the end of the name of the operation
* @var int
*/
const OPT_GATH_METH_END_NAME = 0;
/**
* Set gathering mode of mtehod per class based on the start of the name of the operation
* @var int
*/
const OPT_GATH_METH_START_NAME = 1;
/**
* Index to set gathering methods when calling the constructor
* @var string
*/
const OPT_GATH_METH_KEY = 'option_gather_methods_key';
/**
* Index to set gathering methods when calling the constructor
* @var string
*/
const OPT_SEND_PARAM_AS_ARRAY_KEY = 'option_send_param_as_array_key';
/**
* Index to enable/disable autoload file generation
* @var string
*/
const OPT_GEN_AUTOLOAD_KEY = 'option_generate_autaload_file_key';
/**
* Index to enable/disable autoload file generation
* @var string
*/
const OPT_GEN_WSDL_CLASS_KEY = 'option_generate_wsdl_class_key';
/**
* Index to enable/disable encapsulation of response or not in the response object
* @var string
*/
const OPT_RESPONSE_AS_WSDL_OBJECT_KEY = 'option_response_as_wsdl_object_key';
/**
* Index to enable/disable encapsulation of request in array with 'parameters' as main index
* @var string
*/
const OPT_SEND_PARAMETERS_AS_ARRAY_KEY = 'option_send_parameters_as_array_key';
/**
* Index to set string that points bases classes from which some classes inherits
* @var string
*/
const OPT_INHERITS_FROM_IDENTIFIER_KEY = 'option_inherits_from_identifier_key';
/**
* Index to set the generation of contants names based on the enumeration name with an incremental value
* @var string
*/
const OPT_GENERIC_CONSTANTS_NAMES_KEY = 'option_generic_constants_names_key';
/**
* Index to enable/disable tutorial file generation
* @var string
*/
const OPT_GEN_TUTORIAL_KEY = 'option_generate_tutorial_file_key';
/**
* Structs array
* @var array
*/
private $structs;
/**
* Functions arrays
* @var array
*/
private $functions;
/**
* Wsdl lists
* @var array
*/
private $wsdls;
/**
* Option to categorize classes
* @var int
*/
private $optionCategory;
/**
* Option to subcategorize classes
* @var int
*/
private $optionSubCategory;
/**
* Option to define how to gather methods by classes
* @var int
*/
private $optionGatherMethods;
/**
* Option to set that parameters to soap call must be contained by an array where indexex are the parameters name
* @var bool
*/
private $optionSendArrayAsParameter;
/**
* Option to enabled/disable autoload file generation
* @var bool
*/
private $optionGenerateAutoloadFile;
/**
* Option to enabled/disable wsdl class file generation
* @var bool
*/
private $optionGenerateWsdlClassFile;
/**
* Option to enable/disable encapsulation of response or not in the response class
* @var bool
*/
private $optionResponseAsWsdlObject;
/**
* Option to enable/disable encapsulation of request in array with 'parameters' as main index
* @var bool
*/
private $optionSendParametersAsArray;
/**
* Option to set string that points bases classes from which some classes inherits
* @var string
*/
private $optionInheritsClassIdentifier;
/**
* Option to set set the generation of contants names based on the enumeration name with an incremental value
* @var string
*/
private $optionGenericConstantsNames;
/**
* Option to enabled/disable tutorial file generation
* @var bool
*/
private $optionGenerateTutorialFile;
/**
* Constructor
* @uses SoapClient::__construct()
* @uses WsdlToPhp::setStructs()
* @uses WsdlToPhp::setFunctions()
* @uses WsdlToPhp::setWsdls()
* @uses WsdlToPhp::addWsdl()
* @uses WsdlToPhp::setOptionCategory()
* @uses WsdlToPhp::setOptionGenerateAutoloadFile()
* @uses WsdlToPhp::setOptionGenerateTutorialFile()
* @uses WsdlToPhp::setOptionSubCategory()
* @uses WsdlToPhp::setOptionGenerateWsdlClassFile()
* @uses WsdlToPhp::setOptionGatherMethods()
* @uses WsdlToPhp::setOptionSendArrayAsParameter()
* @uses WsdlToPhp::setOptionResponseAsWsdlObject()
* @uses WsdlToPhp::setOptionGenericConstantsNames()
* @uses WsdlToPhp::setOptionInheritsClassIdentifier()
* @uses WsdlToPhp::setOptionSendParametersAsArray()
* @uses WsdlToPhp::OPT_CAT_KEY
* @uses WsdlToPhp::OPT_CAT_START_NAME
* @uses WsdlToPhp::OPT_GEN_AUTOLOAD_KEY
* @uses WsdlToPhp::OPT_GEN_TUTORIAL_KEY
* @uses WsdlToPhp::OPT_SUB_CAT_KEY
* @uses WsdlToPhp::OPT_SUB_CAT_START_NAME
* @uses WsdlToPhp::OPT_GEN_WSDL_CLASS_KEY
* @uses WsdlToPhp::OPT_GATH_METH_KEY
* @uses WsdlToPhp::OPT_GATH_METH_START_NAME
* @uses WsdlToPhp::OPT_SEND_PARAM_AS_ARRAY_KEY
* @uses WsdlToPhp::OPT_RESPONSE_AS_WSDL_OBJECT_KEY
* @uses WsdlToPhp::OPT_GENERIC_CONSTANTS_NAMES_KEY
* @uses WsdlToPhp::OPT_INHERITS_FROM_IDENTIFIER_KEY
* @uses WsdlToPhp::OPT_SEND_PARAMETERS_AS_ARRAY_KEY
* @param string $_pathToWsdl WSDL url or path
* @param string $_login login to get access to WSDL
* @param string $_password password to get access to WSDL
* @param array $_options associative array between WsdlToPhp options keys and values
* @param array $_wsdlOptions options to get access to WSDL
* @return WsdlToPhp
*/
public function __construct($_pathToWsdl,$_login = false,$_password = false,array $_options = array(),array $_wsdlOptions = array())
{
$pathToWsdl = trim($_pathToWsdl);
/**
* Options for WSDL
*/
$options = $_wsdlOptions;
$options['trace'] = true;
$options['exceptions'] = true;
$options['cache_wsdl'] = WSDL_CACHE_NONE;
if(!empty($_login) && !empty($_password))
{
$options['login'] = $_login;
$options['password'] = $_password;
}
$this->setStructs(array());
$this->setFunctions(array());
$this->setWsdls(array());
/**
* Construct
*/
try
{
parent::__construct($pathToWsdl,$options);
}
catch(SoapFault $fault)
{
print_r($fault);
}
$this->addWsdl($pathToWsdl);
/**
* Set attributes
*/
$this->setOptionCategory(array_key_exists(self::OPT_CAT_KEY,$_options)?$_options[self::OPT_CAT_KEY]:self::OPT_CAT_START_NAME);
$this->setOptionGenerateAutoloadFile(array_key_exists(self::OPT_GEN_AUTOLOAD_KEY,$_options)?$_options[self::OPT_GEN_AUTOLOAD_KEY]:false);
$this->setOptionGenerateTutorialFile(array_key_exists(self::OPT_GEN_TUTORIAL_KEY,$_options)?$_options[self::OPT_GEN_TUTORIAL_KEY]:false);
$this->setOptionSubCategory(array_key_exists(self::OPT_SUB_CAT_KEY,$_options)?$_options[self::OPT_SUB_CAT_KEY]:self::OPT_SUB_CAT_START_NAME);
$this->setOptionGenerateWsdlClassFile(array_key_exists(self::OPT_GEN_WSDL_CLASS_KEY,$_options)?$_options[self::OPT_GEN_WSDL_CLASS_KEY]:false);
$this->setOptionGatherMethods(array_key_exists(self::OPT_GATH_METH_KEY,$_options)?$_options[self::OPT_GATH_METH_KEY]:self::OPT_GATH_METH_START_NAME);
$this->setOptionSendArrayAsParameter(array_key_exists(self::OPT_SEND_PARAM_AS_ARRAY_KEY,$_options)?$_options[self::OPT_SEND_PARAM_AS_ARRAY_KEY]:false);
$this->setOptionResponseAsWsdlObject(array_key_exists(self::OPT_RESPONSE_AS_WSDL_OBJECT_KEY,$_options)?$_options[self::OPT_RESPONSE_AS_WSDL_OBJECT_KEY]:false);
$this->setOptionGenericConstantsNames(array_key_exists(self::OPT_GENERIC_CONSTANTS_NAMES_KEY,$_options)?$_options[self::OPT_GENERIC_CONSTANTS_NAMES_KEY]:false);
$this->setOptionInheritsClassIdentifier(array_key_exists(self::OPT_INHERITS_FROM_IDENTIFIER_KEY,$_options)?$_options[self::OPT_INHERITS_FROM_IDENTIFIER_KEY]:'');
$this->setOptionSendParametersAsArray(array_key_exists(self::OPT_SEND_PARAMETERS_AS_ARRAY_KEY,$_options)?$_options[self::OPT_SEND_PARAMETERS_AS_ARRAY_KEY]:false);
}
/**
* Generate all classes based on options
* @uses WsdlToPhp::getStructs()
* @uses WsdlToPhp::initStructs()
* @uses WsdlToPhp::getFunctions()
* @uses WsdlToPhp::initFunctions()
* @uses WsdlToPhp::loadWsdls()
* @uses WsdlToPhp::getOptionGenerateWsdlClassFile()
* @uses WsdlToPhp::generateWsdlClassFile()
* @uses WsdlToPhp::setOptionGenerateWsdlClassFile()
* @uses WsdlToPhp::generateStructsClasses()
* @uses WsdlToPhp::generateFunctionsClasses()
* @uses WsdlToPhp::generateClassMap()
* @uses WsdlToPhp::getOptionGenerateAutoloadFile()
* @uses WsdlToPhp::generateAutoloadFile()
* @uses WsdlToPhp::getOptionGenerateTutorialFile()
* @uses WsdlToPhp::generateTutorialFile()
* @param string $_packageName the string used to prefix all generate classes
* @param string $_rootDirectory path where classes should be generated
* @param int $_rootDirectoryRights system rights to apply on folder
* @param bool $_createRootDirectory create root directory if not exist
* @return bool true|false depending on the well creation fot the root directory
*/
public function generateClasses($_packageName,$_rootDirectory,$_rootDirectoryRights = 0775,$_createRootDirectory = true)
{
$rootDirectory = $_rootDirectory . (substr($_rootDirectory,-1) != '/'?'/':'');
/**
* Root directory
*/
if(!is_dir($rootDirectory) && !$_createRootDirectory)
return false;
elseif($_createRootDirectory)
@mkdir($rootDirectory,$_rootDirectoryRights);
/**
* Begin process
*/
if(is_dir($rootDirectory))
{
/**
* Initialize elements
*/
$init = false;
if(!count($this->getStructs()))
$this->initStructs();
else
$init = true;
if(!count($this->getFunctions()))
$this->initFunctions();
if(!$init && count($this->wsdls))
$this->loadWsdls(implode('',array_slice(array_keys($this->wsdls),0,1)));
/**
* Generate Wsdl Class ?
*/
if($this->getOptionGenerateWsdlClassFile())
$wsdlClassFile = $this->generateWsdlClassFile($_packageName,$rootDirectory,$_rootDirectoryRights);
else
$wsdlClassFile = array();
if(!count($wsdlClassFile))
$this->setOptionGenerateWsdlClassFile(false);
/**
* Generate classes files
*/
$structsClassesFiles = $this->generateStructsClasses($_packageName,$rootDirectory,$_rootDirectoryRights);
$functionsClassesFiles = $this->generateFunctionsClasses($_packageName,$rootDirectory,$_rootDirectoryRights);
$classMapFile = $this->generateClassMap($_packageName,$rootDirectory,$_rootDirectoryRights);
/**
* Generate autoload ?
*/
if($this->getOptionGenerateAutoloadFile())
$this->generateAutoloadFile($_packageName,$rootDirectory,$_rootDirectoryRights,array_merge($wsdlClassFile,$structsClassesFiles,$functionsClassesFiles,$classMapFile));
/**
* Generate tutorial ?
*/
if($this->getOptionGenerateTutorialFile())
$this->generateTutorialFile($_packageName,$rootDirectory,$_rootDirectoryRights,$functionsClassesFiles);
return true;
}
else
return false;
}
/**
* Initialize structs defined in WSDL :
* - Get structs defined
* - Parse each struct definition
* - Analyze each struct paramaters
* @uses SoapClient::__getTypes()
* @uses WsdlToPhp::addStruct()
* @uses WsdlToPhp::cleanName()
* @tutorial restriction aren't get with structs, see loadWsdls :
* <xsd:simpleType name="SearchOption">
* --<xsd:restriction base="xsd:string">
* ----<xsd:enumeration value="DisableLocationDetection"/>
* ----<xsd:enumeration value="EnableHighlighting"/>
* --</xsd:restriction>
* </xsd:simpleType>
* Example on how to send them : http://msdn.microsoft.com/en-us/library/dd250961
* @return bool true|false depending on the well types catching from the WSDL
*/
private function initStructs()
{
$types = $this->__getTypes();
if(is_array($types) && count($types))
{
$structsDefined = array();
$structsParams = array();
foreach($types as $type)
{
/**
* Remove useless break line, tabs
*/
$type = str_replace("\r",'',$type);
$type = str_replace("\n",'',$type);
$type = str_replace("\t",'',$type);
/**
* Remove curly braces
*/
$type = str_replace("{",'',$type);
$type = str_replace("}",'',$type);
/**
* Remove brackets
*/
$type = str_replace("[",'',$type);
$type = str_replace("]",'',$type);
/**
* Add space to parse it
*/
$type = str_replace(';',' ;',$type);
/**
* Remove duplicate spaces
*/
$type = preg_replace('/[\s]+/',' ',$type);
/**
* Explode definition base on format :
* struct {struct_name} {paramName} {paramValue} ;[{paramName} {paramValue} ;]+
*/
$typeDef = explode(' ',$type);
/**
* Get struct definition start
*/
$struct = $typeDef[0];
if($struct != 'struct' && !array_key_exists(self::cleanName($struct),$this->getStructs()))
continue;
/**
* Replace some uppercase words in struct name
*/
$structName = $typeDef[1];
/**
* Struct already known ?
*/
if(in_array($structName,$structsDefined))
continue;
/**
* Collect struct params
*/
$start = false;
$then = false;
$end = false;
$structParamName = '';
$structParamType = '';
$typeDefCount = count($typeDef);
if($typeDefCount > 3)
{
for($i = 2;$i < $typeDefCount;$i++)
{
$typeVal = $typeDef[$i];
if($typeVal != '{' && is_string($typeVal) && !empty($typeVal) && !$start)
{
$end = false;
$then = false;
$start = true;
}
if($typeVal === ';')
{
$end = true;
$then = false;
$start = false;
}
if($then)
{
$structParamName = $typeVal;
if(!empty($structParamType) && !empty($structParamName) && !empty($structName))
{
$this->addStruct($structParamType,$structParamName,$structName);
$structsDefined[] = $structName;
$structParamName = '';
$structParamType = '';
}
}
if($start && !$then)
{
/**
* Replace some weird definition to known valid type
*/
$typeVal = str_replace('<anyXML>','DOMDocument',$typeVal);
$structParamType = $typeVal;
$then = true;
}
}
}
else
$this->addStruct($structParamType,$structParamName,$structName);
}
return true;
}
else
return false;
}
/**
* Generate structs classes based on structs collected
* @uses WsdlToPhp::getStructs()
* @uses WsdlToPhp::getDirectory()
* @uses WsdlToPhp::getOptionInheritsClassIdentifier()
* @uses WsdlToPhp::getOptionCategory()
* @uses WsdlToPhp::setOptionCategory()
* @uses WsdlToPhp::cleanClassName()
* @uses WsdlToPhp::cleanPropertyName()
* @uses WsdlToPhp::getOptionGenericConstantsNames()
* @uses WsdlToPhp::cleanConstantName()
* @uses WsdlToPhp::structIsKnown()
* @uses WsdlToPhp::structName()
* @uses ezcPhpGenerator::appendCustomCode()
* @uses ezcPhpGenerator::finish()
* @param string $_packageName
* @param string $_rootDirectory
* @param bool $_rootDirectoryRights
*/
private function generateStructsClasses($_packageName,$_rootDirectory,$_rootDirectoryRights)
{
$structs = $this->getStructs();
$structsClassesFiles = array();
if(count($structs))
{
$ClassType = ucfirst($_packageName);
$classType = $_packageName;
/**
* Ordering structs in order to generate mother class first and to put them on top in the autoload file
*/
$structsToGenerateDone = array();
foreach($structs as $structName=>$structParams)
{
if(!array_key_exists($structName,$structsToGenerateDone))
$structsToGenerateDone[$structName] = 0;
if(is_array($structParams) && array_key_exists('inherits',$structParams) && !empty($structParams['inherits']) && array_key_exists($structParams['inherits'],$structs))
{
$inherits = $structParams['inherits'];
while(!empty($inherits) && array_key_exists($inherits,$structs))
{
if(!array_key_exists($inherits,$structsToGenerateDone))
$structsToGenerateDone[$inherits] = 1;
else
$structsToGenerateDone[$inherits]++;
$inherits = (array_key_exists($inherits,$structs) && is_array($structs[$inherits]) && array_key_exists('inherits',$structs[$inherits]))?$structs[$inherits]['inherits']:'';
}
}
}
/**
* Order by priority desc
*/
arsort($structsToGenerateDone);
$structTmp = $structs;
$structs = array();
foreach($structsToGenerateDone as $structName=>$structPriority)
$structs[$structName] = $structTmp[$structName];
unset($structTmp);
unset($structsToGenerateDone);
foreach($structs as $structName=>$structParams)
{
$elementFolder = $this->getDirectory($_rootDirectory,$_rootDirectoryRights,$structName);
/**
* Extends class name
*/
$extend = '';
$inherits = false;
if($this->getOptionInheritsClassIdentifier() != '')
{
$currentOptionValue = $this->getOptionCategory();
$this->setOptionCategory(self::OPT_CAT_END_NAME);
$structType = $this->getPart($structName,self::OPT_CAT_KEY);
if(self::structIsKnown($structType . $this->getOptionInheritsClassIdentifier()))
{
$extend = self::structName($structType . $this->getOptionInheritsClassIdentifier(),$_packageName,true);
$inherits = true;
}
$this->setOptionCategory($currentOptionValue);
}
elseif(array_key_exists('inherits',$structParams) && self::structIsKnown($structParams['inherits']))
{
$extend = self::structName($structParams['inherits'],$_packageName,true);
$inherits = true;
unset($structParams['inherits']);
}
if(empty($extend) && $this->getOptionGenerateWsdlClassFile())
$extend = $ClassType . 'WsdlClass';
/**
* Class definition
*/
$className = self::structName($structName,$_packageName);
$classMap[$structName] = $className;
/**
* Define struct class file name and initialize struct class generator
*/
$documentation = '';
if(array_key_exists('documentation',$structParams))
{
$documentation = "\r\n * Documentation : " . $structParams['documentation'];
unset($structParams['documentation']);
}
$structsClassesFiles[] = $structClassFileName = $elementFolder . $className . '.php';
$php = new ezcPhpGenerator($structClassFileName,true,true);
$php->indentString = " ";
$php->appendCustomCode("/**\r\n * Class file for $className\r\n * @date " . date('d/m/Y') . "\r\n */\r\n/**\r\n * Class $className$documentation\r\n * @date " . date('d/m/Y') . "\r\n */");
$php->appendCustomCode("class $className" . (!empty($extend)?' extends ' . self::cleanClassName($extend):''));
$php->appendCustomCode("{");
/**
* Attributes
*/
$php->indentLevel++;
$parameters = array();
$parametersSring = "";
$usesSring = "";
$isRestriction = false;
$parametersList = array();
$parametersForParent = array();
$parametersType = array();
if(count($structParams))
{
foreach($structParams as $elementIndex=>$element)
{
/**
* Current index is not valid
*/
if(!is_numeric($elementIndex))
continue;
/**
* Get informations and sanitize them
*/
$type = $element['type'];
$Type = self::structName($type,$_packageName);
$name = $element['name'];
$cleanName = self::cleanPropertyName($name);
$meta = $element['meta'];
$isRestriction = (array_key_exists('isRestriction',$element) && $element['isRestriction'] == true);
/**
* Is it's not a restriction, aka an enumeration in mot case, we generate the attributes
*/
if(!$isRestriction)
{
/**
* List of attributes for which we generate setters, getters, general methods
*/
$parameters[] = array(
'type'=>$type,
'name'=>$name);
/**
* Is this attribute a know type ?
*/
$parametersSring .= "\r\n * @param " . (self::structIsKnown($type)?$Type:$type) . ' $_' . lcfirst($cleanName) . " $name";
/**
* Uses documentation part
*/
$usesSring .= "\r\n * @uses $className::set" . ucfirst($cleanName) . "()";
/**
* Parameters used for methods assigned to classes matching ArrayOf
*/
$parametersType[] = $Type;
/**
* Attribute has a default value ? then use it
*/
if(array_key_exists('default',$meta))
{
$defaultValue = $meta['default'];
if(is_numeric($defaultValue))
$defaultValue = floatval($defaultValue);
elseif(is_bool($defaultValue) || $defaultValue === 'true' || $defaultValue === 'false')
$defaultValue = ($defaultValue === true || $defaultValue == 'true')?true:false;
$parametersList[] = '$_' . lcfirst($cleanName) . ' = ' . var_export($defaultValue,true);
}
/**
* Attribute is required, then the value si required !
*/
elseif((array_key_exists('minOccurs',$meta) && $meta['minOccurs'] >= 1) || (array_key_exists('minoccurs',$meta) && $meta['minoccurs'] >= 1))
$parametersList[] = '$_' . lcfirst($cleanName);
/**
* Default value assignement
*/
else
$parametersList[] = '$_' . lcfirst($cleanName) . ' = null';
/**
* If attribute is type of ArrayOf, then we assign the returned value to the ArrayOf class
*/
if(strpos($type,'ArrayOf') !== false && self::structIsKnown($type))
$parametersForParent[] = "'$cleanName'=>new $Type(" . '$_' . lcfirst($cleanName) . ")";
else
$parametersForParent[] = "'$cleanName'=>\$_" . lcfirst($cleanName);
/**
* Informations extracted from the XML/WSDL tag attributes of the current attribute
*/
$metas = array();
foreach($meta as $metaName=>$metaValue)
$metas[] = "\t- " . $metaName . ' : ' . $metaValue;
if(count($metas))
array_unshift($metas,"\r\n * " . 'Meta informations :');
$php->appendCustomCode("/**\r\n * The $name" . implode("\r\n * ",$metas) . "\r\n * @var " . (self::structIsKnown($type)?$Type:$type) . "\r\n */");
$php->appendCustomCode("public \$$cleanName;");
}
}
}
/**
* Restriction class, set constant and values allowed
*/
if($isRestriction && array_key_exists('values',$element) && count($element['values']))
{
$valuesDone = array();
$inArray = array();
$constantsUsed = "";
$valuesCount = count($element['values']);
$valuesCountLength = strlen($valuesCount);
foreach($element['values'] as $index=>$value)
{
$meta = '';
if(array_key_exists($value,$element['meta']))
{
foreach($element['meta'][$value] as $metaName=>$metaValue)
$meta = "\r\n * \t- $metaName : $metaValue";
}
$meta = !empty($meta)?"\r\n * " . 'Meta informations :' . $meta:$meta;
$php->appendCustomCode("/**\r\n * Constant for value " . var_export($value,true) . "$meta\r\n * @return " . gettype($value) . " " . var_export($value,true) . "\r\n */");
/**
* Generic name avoiding naming problems from custom values
*/
if($this->getOptionGenericConstantsNames())
$constantValueName = 'ENUM_VALUE_' . str_repeat('0',$valuesCountLength - strlen($index)) . $index;
/**
* Constant name based on the value contained by the constant
*/
else
{
/**
* Avoid multiple constant with same name for different case value
*/
$cleanValueName = self::cleanConstantName($value);
$constantValueName = strtoupper($cleanValueName);
if(!array_key_exists($constantValueName,$valuesDone))
$valuesDone[$constantValueName] = 0;
else
$valuesDone[$constantValueName]++;
$constantValueName .= ((array_key_exists($constantValueName,$valuesDone) && $valuesDone[$constantValueName])?'_' . $valuesDone[$constantValueName]:'');
$constantValueName = 'VALUE_' . $constantValueName;
}
$inArray[] = "self::$constantValueName";
$constantsUsed .= "\r\n * @uses $className" . "::$constantValueName";
$php->appendCustomCode('const ' . $constantValueName . ' = ' . var_export($value,true) . ';');
}
}
/**
* Constructor
*/
$php->appendCustomCode("/**\r\n * Constructor$parametersSring\r\n * @return $className\r\n */");
$php->appendCustomCode("public function __construct(" . implode(',',$parametersList) . ")");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode(($inherits && $this->getOptionGenerateWsdlClassFile()?$ClassType . 'WsdlClass':'parent') . "::__construct(array(" . implode(',',$parametersForParent) . "));");
$php->indentLevel--;
$php->appendCustomCode("}");
/**
* Add method per attributes
*/
if(count($parameters))
{
foreach($parameters as $parameter)
{
$cleanParameter = self::cleanPropertyName($parameter['name']);
$cleanType = self::structName($parameter['type'],$_packageName);
/**
* Set
*/
$php->appendCustomCode("/**\r\n * Set " . $cleanParameter . "\r\n * @param $cleanType \$_" . lcfirst($cleanParameter) . " $cleanParameter\r\n * @return " . $cleanType . "\r\n */");
$php->appendCustomCode("public function set" . ucfirst($cleanParameter) . "(\$_" . lcfirst($cleanParameter) . ")");
$php->appendCustomCode("{");
$php->indentLevel++;
if(strpos($className,'ArrayOf') === false && array_key_exists($parameter['type'],$this->getStructs()) && array_key_exists(0,$this->structs[$parameter['type']]) && array_key_exists('isRestriction',$this->structs[$parameter['type']][0]) && $this->structs[$parameter['type']][0]['isRestriction'] == true && array_key_exists('values',$this->structs[$parameter['type']][0]) && count($this->structs[$parameter['type']][0]['values']))
$php->appendCustomCode('return ($this->' . $cleanParameter . ' = ' . $cleanType . '::valueIsValid($_' . lcfirst($cleanParameter) . ')?$_' . lcfirst($cleanParameter) . ':null);');
else
$php->appendCustomCode("return (\$this->" . $cleanParameter . ' = $_' . lcfirst($cleanParameter) . ');');
$php->indentLevel--;
$php->appendCustomCode("}");
/**
* Get
*/
$php->appendCustomCode("/**\r\n * Get " . $cleanParameter . "\r\n * @return $cleanType\r\n */");
$php->appendCustomCode("public function get" . ucfirst($cleanParameter) . "()");
$php->appendCustomCode("{");
$php->indentLevel++;
if(strpos($parameter['type'],'XML') !== false || strpos($parameter['type'],'DOMDocument') !== false)
{
$php->appendCustomCode("if(!(\$this->" . $cleanParameter . " instanceof DOMDocument))");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("\$dom = new DOMDocument('1.0','UTF-8');");
$php->appendCustomCode("\$dom->formatOutput = true;");
$php->appendCustomCode("\$dom->loadXML(\$this->" . $cleanParameter . ");");
$php->appendCustomCode("\$this->set" . ucfirst($cleanParameter) . "(\$dom);");
$php->indentLevel--;
$php->appendCustomCode("}");
}
$php->appendCustomCode("return \$this->" . $cleanParameter . ';');
$php->indentLevel--;
$php->appendCustomCode("}");
}
/**
* Add specifics methods for classes like "*ArrayOf*" in order to give type to value returned by specifics methods
*/
if(strpos($className,'ArrayOf') !== false && count($parametersType) == 1)
{
/**
* current method
*/
$php->appendCustomCode("/**\r\n * Returns the current element\r\n * @see " . $ClassType . "WsdlClass::current()\r\n * @return " . $parametersType[0] . "\r\n */");
$php->appendCustomCode("public function current()");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return parent::current();");
$php->indentLevel--;
$php->appendCustomCode("}");
/**
* item method
*/
$php->appendCustomCode("/**\r\n * Returns the element\r\n * @see " . $ClassType . "WsdlClass::item()\r\n * @param int \$_index\r\n * @return " . $parametersType[0] . "\r\n */");
$php->appendCustomCode("public function item(\$_index)");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return parent::item(\$_index);");
$php->indentLevel--;
$php->appendCustomCode("}");
/**
* first method
*/
$php->appendCustomCode("/**\r\n * Returns the first element\r\n * @see " . $ClassType . "WsdlClass::first()\r\n * @return " . $parametersType[0] . "\r\n */");
$php->appendCustomCode("public function first()");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return parent::first();");
$php->indentLevel--;
$php->appendCustomCode("}");
/**
* last method
*/
$php->appendCustomCode("/**\r\n * Returns the first element\r\n * @see " . $ClassType . "WsdlClass::last()\r\n * @return " . $parametersType[0] . "\r\n */");
$php->appendCustomCode("public function last()");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return parent::last();");
$php->indentLevel--;
$php->appendCustomCode("}");
/**
* offsetGet method
*/
$php->appendCustomCode("/**\r\n * Returns the first element\r\n * @see " . $ClassType . "WsdlClass::offsetGet()\r\n * @param int \$_offset\r\n * @return " . $parametersType[0] . "\r\n */");
$php->appendCustomCode("public function offsetGet(\$_offset)");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return parent::offsetGet(\$_offset);");
$php->indentLevel--;
$php->appendCustomCode("}");
/**
* Add method
*/
if(is_array($parameter) && array_key_exists($parameter['type'],$this->getStructs()) && is_array($this->structs[$parameter['type']]) && count($this->structs[$parameter['type']]) && array_key_exists(0,$this->structs[$parameter['type']]) && array_key_exists('isRestriction',$this->structs[$parameter['type']][0]) && $this->structs[$parameter['type']][0]['isRestriction'] == true && array_key_exists('values',$this->structs[$parameter['type']][0]) && count($this->structs[$parameter['type']][0]['values']))
{
$php->appendCustomCode("/**\r\n * Add element to array\r\n * @see " . $ClassType . "WsdlClass::add()\r\n * @uses " . $cleanType . "::valueIsValid()\r\n * @param " . $parametersType[0] . " \$_item\r\n * @return bool true|false\r\n */");
$php->appendCustomCode("public function add(\$_item)");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return " . $cleanType . '::valueIsValid($_item)?parent::add($_item):false;');
$php->indentLevel--;
$php->appendCustomCode("}");
}
/**
* Return alone attribute name
*/
$php->appendCustomCode("/**\r\n * Returns the attribute name\r\n * @return string '$cleanParameter'\r\n */");
$php->appendCustomCode("public function getAttributeName()");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return '" . str_replace($ClassType . 'Type','',$cleanParameter) . "';");
$php->indentLevel--;
$php->appendCustomCode("}");
}
}
/**
* Restriction class, set constant and values allowed
*/
if($isRestriction && array_key_exists('values',$element) && count($element['values']))
{
/**
* Return true if value is allowed
*/
$php->appendCustomCode("/**\r\n * Return true if value is allowed$constantsUsed\r\n * @param mixed \$_value value\r\n * @return bool true|false\r\n */");
$php->appendCustomCode("public static function valueIsValid(\$_value)");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode("return in_array(\$_value,array(" . implode(',',$inArray) . "));");
$php->indentLevel--;
$php->appendCustomCode("}");
}
/**
* Class name
*/
$php->appendCustomCode("/**\r\n * Method returning the class name\r\n * @return string __CLASS__\r\n */");
$php->appendCustomCode("public function __toString()");
$php->appendCustomCode("{");
$php->indentLevel++;
$php->appendCustomCode('return __CLASS__;');
$php->indentLevel--;
$php->appendCustomCode("}");
$php->indentLevel--;
/**
* End
*/
$php->appendCustomCode("}");
$php->finish();
}
}
return $structsClassesFiles;
}
/**
* Initialize functions :
* - Get structs defined
* - Parse each struct definition
* @uses SoapClient::__getFunctions()
* @uses WsdlToPhp::addFunction()
* @return bool true|false depending on the well functions catching from the WSDL
*/
private function initFunctions()
{
$functions = $this->__getFunctions();
if(is_array($functions) && count($functions))
{
foreach($functions as $function)
{
$infos = array();
preg_match('/([A-Za-z\d_]+) ([A-Za-z\d_]+)\(([A-Za-z\d_]+) \$.+\)/',$function,$infos);
if(count($infos) == 4)
$this->addFunction($infos[2],$infos[3],$infos[1]);
}
return true;
}
else
return false;
}
/**
* Generate methods by class
* @uses WsdlToPhp::getFunctions()
* @uses WsdlToPhp::getStructs()
* @uses WsdlToPhp::getDirectory()
* @uses WsdlToPhp::getOptionGenerateWsdlClassFile()
* @uses WsdlToPhp::getOptionSendArrayAsParameter()
* @uses WsdlToPhp::getOptionSendParametersAsArray()
* @uses WsdlToPhp::getOptionResponseAsWsdlObject()
* @uses WsdlToPhp::getOptionGenericConstantsNames()
* @uses WsdlToPhp::cleanClassName()
* @uses WsdlToPhp::structIsKnown()
* @uses WsdlToPhp::structName()
* @uses ezcPhpGenerator::appendCustomCode()
* @uses ezcPhpGenerator::finish()
* @param string $_packageName
* @param string $_rootDirectory
* @param bool $_rootDirectoryRights
* @return array the absolute paths to the generated files
*/
private function generateFunctionsClasses($_packageName,$_rootDirectory,$_rootDirectoryRights)
{
$functions = $this->getFunctions();
$functionsClassesFiles = array();
if(count($functions))
{
$PackageName = ucfirst($_packageName);
$structs = $this->getStructs();
foreach($functions as $className=>$methods)
{
$elementFolder = $this->getDirectory($_rootDirectory,$_rootDirectoryRights,$className);
$ClassName = $PackageName . 'Service' . ucfirst(self::cleanClassName($className));
$functionsClassesFiles[] = $functionClassFileName = $elementFolder . $ClassName . '.php';
$php = new ezcPhpGenerator($functionClassFileName,true,true);
$php->indentString = " ";
/**
* Start
*/
if($this->getOptionGenerateWsdlClassFile())
$extend = $PackageName . 'WsdlClass';
else
$extend = '';
$php->appendCustomCode("/**\r\n * Class file for $ClassName\r\n * @date " . date('d/m/Y') . "\r\n */\r\n/**\r\n * Class $ClassName\r\n * @date " . date('d/m/Y') . "\r\n */");
$php->appendCustomCode("class $ClassName" . (empty($extend)?'':' extends ' . $extend));
$php->appendCustomCode("{");
$php->indentLevel++;
/**
* Returns type list
*/
$methodReturns = array();
/**
* Methods
*/
foreach($methods as $index=>$methodInfos)
{
$methodName = $methodInfos['method'];
$meta = $methodInfos['meta'];
/**
* Parameter name
*/
$methodParam = ucfirst($methodInfos['parameter']);
$lMethodParam = lcfirst($methodInfos['parameter']);
$uMethodParam = ucfirst($methodInfos['parameter']);
$cleanParameterName = self::structName($methodInfos['parameter'],$_packageName);
/**
* Get parameter infos
*/
$methodsToCall = array();
$methodsUsed = array();
if(self::structIsKnown($methodInfos['parameter']))
{
$methodParamAttributes = array_key_exists($lMethodParam,$structs)?$structs[$lMethodParam]:$structs[$uMethodParam];
foreach($methodParamAttributes as $structInfoIndex=>$structInfos)
{
if(is_numeric($structInfoIndex))
{
$methodsToCall[] = ($this->getOptionSendArrayAsParameter()?'\'' . $structInfos['name'] . '\'=>':'') . '$_' . lcfirst($cleanParameterName) . '->get' . ucfirst($structInfos['name']) . '()';
$methodsUsed[] = $cleanParameterName . '::get' . ucfirst($structInfos['name']) . '()';
}
}