-
Notifications
You must be signed in to change notification settings - Fork 1
/
definition.py
865 lines (537 loc) · 18.6 KB
/
definition.py
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
# -*- coding: utf-8 -*-
# This file is part of the pymfony package.
#
# (c) Alexandre Quercia <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
from __future__ import absolute_import;
from pymfony.component.system import Object;
from pymfony.component.dependency.exception import OutOfBoundsException;
from pymfony.component.dependency.exception import InvalidArgumentException;
from pymfony.component.dependency.interface import ContainerInterface;
"""
"""
class Alias(Object):
def __init__(self, identifier, public=True):
self.__id = str(identifier).lower();
self.__public = bool(public);
def isPublic(self):
return self.__public;
def setPublic(self, boolean):
self.__public = bool(boolean);
def __str__(self):
return self.__id;
class Reference(Object):
"""Reference represents a service reference.
@author: Fabien Potencier <[email protected]>
@api
"""
def __init__(self, identifier, invalidBehavior = ContainerInterface.EXCEPTION_ON_INVALID_REFERENCE, strict = True):
"""Constructor.
@param: string id The service identifier:
@param int invalidBehavior The behavior when the service does not exist
@param Boolean strict Sets how this reference is validated
@see Container
"""
self.__id = None;
self.__invalidBehavior = None;
self.__strict = None;
self.__id = str(identifier).lower();
self.__invalidBehavior = invalidBehavior;
self.__strict = strict;
def __str__(self):
"""__str__.
@return: string The service identifier:
"""
return str(self.__id);
def getInvalidBehavior(self):
"""Returns the behavior to be used when the service does not exist.
@return: int
"""
return self.__invalidBehavior;
def isStrict(self):
"""Returns True when this Reference is strict
@return: Boolean
"""
return self.__strict;
class Definition(Object):
"""Definition represents a service definition.
@author: Fabien Potencier <[email protected]>
@api
"""
def __init__(self, className=None, arguments=None):
"""Constructor.
@param: string class The service class):
@param array arguments An array of arguments to pass to the service constructor
@api
"""
if arguments is None:
arguments = list();
assert isinstance(arguments, list);
self._arguments = arguments;
self.__class = className;
self.__file = None;
self.__factoryClass = None;
self.__factoryMethod = None;
self.__factoryService = None;
self.__configurator = None;
self.__properties = dict();
self.__calls = list();
self.__tags = dict();
self.__public = True;
self.__synthetic = False;
self.__abstract = False;
self.__scope = ContainerInterface.SCOPE_CONTAINER;
def setFactoryClass(self, factoryClass):
"""Sets the name of the class that(, acts as a factory using the factory method,):
which will be invoked statically.
@param: string factoryClass The factory class name(Object):
@return Definition The current instance
@api
"""
self.__factoryClass = factoryClass;
return self;
def getFactoryClass(self):
"""Gets the factory class.
@return: string The factory class name(Object):
@api
"""
return self.__factoryClass;
def setFactoryMethod(self, factoryMethod):
"""Sets the factory method able to create an instance of this class.
@param: string factoryMethod The factory method name
@return Definition The current instance
@api
"""
self.__factoryMethod = factoryMethod;
return self;
def getFactoryMethod(self):
"""Gets the factory method.
@return: string The factory method name
@api
"""
return self.__factoryMethod;
def setFactoryService(self, factoryService):
"""Sets the name of the service that acts as a factory using the factory method.
@param: string factoryService The factory service id
@return Definition The current instance
@api
"""
self.__factoryService = factoryService;
return self;
def getFactoryService(self):
"""Gets the factory service id.
@return: string The factory service id
@api
"""
return self.__factoryService;
def setClass(self, className):
"""Sets the service class.
@param: string class The(, service class):
@return Definition The current instance
@api
"""
self.__class = className;
return self;
def getClass(self):
"""Gets the service class.
@return: string The service class
@api
"""
return self.__class;
def setArguments(self, arguments):
"""Sets the arguments to pass to the service constructor/factory method.
@param: list arguments An array of arguments
@return Definition The current instance
@api
"""
assert isinstance(arguments, list);
self._arguments = arguments;
return self;
def setProperties(self, properties):
"""
@api:
@param: dict properties An array of properties
"""
assert isinstance(properties, dict)
self.__properties = properties;
return self;
def getProperties(self):
"""
@api:
@return dict
"""
return self.__properties;
def setProperty(self, key, value):
"""
@api:
"""
self.__properties[key] = value;
return self;
def addArgument(self, argument):
"""Adds an argument to pass to the service constructor/factory method.
@param: mixed argument An argument
@return Definition The current instance
@api
"""
self._arguments.append(argument);
return self;
def replaceArgument(self, index, argument):
"""Sets a specific argument:
@param: integer index
@param mixed argument
@return Definition The current instance
@raise OutOfBoundsException When the replaced argument does not exist
@api
"""
if index < 0 or index > len(self._arguments) - 1:
raise OutOfBoundsException(
'The index "{!d}" is not in the range [0, {!d}].'
''.format(index, len(self._arguments) - 1)
);
self._arguments[index] = argument;
return self;
def getArguments(self):
"""Gets the arguments to pass to the service constructor/factory method.
@return: array The array of arguments
@api
"""
return self._arguments;
def getArgument(self, index):
"""Gets an argument to pass to the service constructor/factory method.
@param: integer index
@return mixed The argument value
@raise OutOfBoundsException When the argument does not exist
@api
"""
if index < 0 or index > len(self._arguments) - 1:
raise OutOfBoundsException(
'The index "{!d}" is not in the range [0, {!d}].'
''.format(index, len(self._arguments) - 1)
);
return self._arguments[index];
def setMethodCalls(self, calls):
"""Sets the methods to call after service initialization.
@param: list calls An array of method calls
list of [methodName, [arg1, ...]]
@return Definition The current instance
@api
"""
assert isinstance(calls, list);
self.__calls = list();
for call in calls:
assert isinstance(call, list);
self.addMethodCall(call[0], call[1]);
return self;
def addMethodCall(self, method, arguments = None):
"""Adds a method to call after service initialization.
@param: string method The method name to call
@param array arguments An array of arguments to pass to the method call
@return Definition The current instance
@raise InvalidArgumentException on empty method param
@api
"""
if arguments is None:
arguments = list();
arguments = list(arguments);
method = str(method);
if not method:
raise InvalidArgumentException('Method name cannot be empty.');
self.__calls.append([method, arguments]);
return self;
def removeMethodCall(self, method):
"""Removes a method to call after service initialization.
@param: string method The method name to remove
@return Definition The current instance
@api
"""
i = -1;
for call in self.__calls:
i += 1;
if call[0] == method:
del self.__calls[i];
break;
return self;
def hasMethodCall(self, method):
"""Check if the current definition has a given method to call after service initialization.:
@param: string method The method name to search for
@return Boolean
@api
"""
for call in self.__calls:
if call[0] == method:
return True;
return False;
def getMethodCalls(self):
"""Gets the methods to call after service initialization.
@return: list An array of method calls
@api
"""
return self.__calls;
def setTags(self, tags):
"""Sets tags for this definition
@param: dict tags
@return Definition the current instance
@api
"""
assert isinstance(tags, dict);
self.__tags = tags;
return self;
def getTags(self):
"""Returns all tags.
@return: dict An array of tags
@api
"""
return self.__tags;
def getTag(self, name):
"""Gets a tag by name.
@param: string name The tag name
@return list An array of attributes
@api
"""
if name in self.__tags:
return self.__tags[name];
else:
return list();
def addTag(self, name, attributes = None):
"""Adds a tag for this definition.
@param: string name The tag name
@param dict attributes An array of attributes
@return Definition The current instance
@api
"""
if attributes is None:
attributes = dict();
assert isinstance(attributes, dict);
if name not in self.__tags:
self.__tags[name] = list();
self.__tags[name].append(attributes);
return self;
def hasTag(self, name):
"""Whether this definition has a tag with the given name
@param: string name
@return Boolean
@api
"""
return name in self.__tags;
def clearTag(self, name):
"""Clears all tags for a given name.
@param: string name The tag name
@return Definition
"""
if self.hasTag(name):
del self.__tags[name];
return self;
def clearTags(self):
"""Clears the tags for this definition.
@return: Definition The current instance
@api
"""
self.__tags = dict();
return self;
def setFile(self, filename):
"""Sets a file to require before creating the service.
@param: string file A full pathname to include
@return Definition The current instance
@api
"""
self.__file = filename;
return self;
def getFile(self):
"""Gets the file to require before creating the service.
@return: string The full pathname to include
@api
"""
return self.__file;
def setScope(self, scope):
"""Sets the scope of the service
@param: string scope Whether the service must be shared or not
@return Definition The current instance
@api
"""
self.__scope = scope;
return self;
def getScope(self):
"""Returns the scope of the service
@return: string
@api
"""
return self.__scope;
def setPublic(self, boolean):
"""Sets the visibility of this service.
@param: Boolean boolean
@return Definition The current instance
@api
"""
self.__public = bool(boolean);
return self;
def isPublic(self):
"""Whether this service is public facing
@return: Boolean
@api
"""
return self.__public;
def setSynthetic(self, boolean):
"""Sets whether this definition is synthetic, that is not constructed by the
container, but dynamically injected.
@param: Boolean boolean
@return Definition the current instance
@api
"""
self.__synthetic = bool(boolean);
return self;
def isSynthetic(self):
"""Whether this definition is synthetic, that is not constructed by the
container, but dynamically injected.
@return: Boolean
@api
"""
return self.__synthetic;
def setAbstract(self, boolean):
"""Whether this definition is abstract, that means it merely serves as a
template for other definitions.
@param: Boolean boolean
@return Definition the current instance
@api
"""
self.__abstract = bool(boolean);
return self;
def isAbstract(self):
"""Whether this definition is abstract, that means it merely serves as a
template for other definitions.
@return: Boolean
@api
"""
return self.__abstract;
def setConfigurator(self, closure):
"""Sets a configurator to call after the service is fully initialized.
@param: callable callable A PYTHON callable
@return Definition The current instance
@api
"""
self.__configurator = closure;
return self;
def getConfigurator(self):
"""Gets the configurator to call after the service is fully initialized.
@return: callable The PHP callable to call
@api
"""
return self.__configurator;
class DefinitionDecorator(Definition):
"""This definition decorates another definition.
@author Johannes M. Schmitt <[email protected]>
@api
"""
def __init__(self, parent):
"""Constructor.
@param string parent The id of Definition instance to decorate.
@api
"""
Definition.__init__(self);
self.__parent = None;
self.__changes = None;
self.__overwriteArguments = None;
self.__parent = parent;
self.__changes = dict();
self.__overwriteArguments = dict();
def getParent(self):
"""Returns the Definition being decorated.
@return string
@api
"""
return self.__parent;
def getChanges(self):
"""Returns all changes tracked for the Definition object.
@return array An array of changes for this Definition
@api
"""
return self.__changes;
def getOverwriteArguments(self):
"""Returns all overwrite arguments for the Definition object.
@return dict A dict of overwrite arguments for the Definition object.
@api
"""
return self.__overwriteArguments;
def setClass(self, className):
"""
@api
"""
self.__changes['class'] = True;
return Definition.setClass(self, className);
def setFactoryClass(self, className):
"""
@api
"""
self.__changes['factory_class'] = True;
return Definition.setFactoryClass(self, className);
def setFactoryMethod(self, method):
"""
@api
"""
self.__changes['factory_method'] = True;
return Definition.setFactoryMethod(self, method);
def setFactoryService(self, service):
"""
@api
"""
self.__changes['factory_service'] = True;
return Definition.setFactoryService(self, service);
def setConfigurator(self, closure):
"""
@api
"""
self.__changes['configurator'] = True;
return Definition.setConfigurator(self, closure);
def setFile(self, filename):
"""
@api
"""
self.__changes['file'] = True;
return Definition.setFile(self, filename);
def setPublic(self, boolean):
"""
@api
"""
self.__changes['public'] = True;
return Definition.setPublic(self, boolean);
def getArgument(self, index):
"""Gets an argument to pass to the service constructor/factory method.
If replaceArgument() has been used to replace an argument, this method
will return the replacement value.
@param integer index
@return mixed The argument value
@raise OutOfBoundsException When the argument does not exist
@api
"""
index = int(index);
# UPDATED
try:
return self.__overwriteArguments[index];
except IndexError:
pass;
lastIndex = len(self._arguments) - 1;
if (index < 0 or index > lastIndex) :
raise OutOfBoundsException(
'The index "{0!d}" is not in the range [0, {1!d}].'.format(
index,
len(self._arguments) - 1
));
return self._arguments[index];
def replaceArgument(self, index, value):
"""You should always use this method when overwriting existing arguments
of the parent definition.
If you directly call setArguments() keep in mind that you must follow
certain conventions when you want to overwrite the arguments of the
parent definition, otherwise your arguments will only be appended.
@param integer index
@param mixed value
@return DefinitionDecorator the current instance
@raise InvalidArgumentException when index isn't an integer
@api
"""
if not isinstance(index, int) or isinstance(index, bool):
raise InvalidArgumentException('index must be an integer.');
# UPDATED
self.__overwriteArguments[index] = value;
return self;