-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.py
More file actions
176 lines (129 loc) · 4.87 KB
/
bundle.py
File metadata and controls
176 lines (129 loc) · 4.87 KB
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
# -*- coding: utf-8 -*-
# This file is part of the pymfony package.
#
# (c) Alexandre Quercia <alquerci@email.com>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
from __future__ import absolute_import;
import re;
from os.path import dirname;
from pymfony.component.system.oop import interface;
from pymfony.component.system.oop import abstract;
from pymfony.component.system.oop import final;
from pymfony.component.system.exception import LogicException;
from pymfony.component.system.reflection import ReflectionObject;
from pymfony.component.system.reflection import ReflectionClass;
from pymfony.component.dependency import ContainerAwareInterface;
from pymfony.component.dependency import ContainerAware;
from pymfony.component.dependency import ContainerBuilder;
"""
"""
@interface
class BundleInterface(ContainerAwareInterface):
def boot(self):
"""Boots the Bundle.
"""
pass;
def shutdown(self):
"""Shutdowns the Bundle.
"""
pass;
def build(self, container):
"""Builds the bundle.
It is only ever called once when the cache is empty.
@param container: ContainerBuilder A ContainerBuilder instance
"""
pass;
def getContainerExtension(self):
"""Returns the container extension that should be implicitly loaded.
@return: ExtensionInterface|null The default extension or null
if there is none
"""
pass;
def getParent(self):
"""Returns the bundle name that this bundle overrides.
Despite its name, this method does not imply any parent/child
relationship between the bundles, just a way to extend and override
an existing Bundle.
@return: string The Bundle name it overrides or null if no parent
"""
pass;
def getName(self):
"""Returns the bundle name (the class short name).
@return: string The Bundle name
"""
pass;
def getNamespace(self):
"""Gets the Bundle namespace.
@return: string The Bundle namespace
"""
pass;
def getPath(self):
"""Gets the Bundle directory path.
The path should always be returned as a Unix path (with /).
@return: string The Bundle absolute path
"""
pass;
@abstract
class Bundle(ContainerAware, BundleInterface):
"""An implementation of BundleInterface that adds a few conventions
for DependencyInjection extensions.
"""
def __init__(self):
self._name = None;
self._reflected = None;
self._extension = None;
def boot(self):
pass;
def shutdown(self):
pass;
def build(self, container):
assert isinstance(container, ContainerBuilder);
def getContainerExtension(self):
"""Returns the bundle's container extension.
@return: ExtensionInterface|null The container extension
@raise LogicException: When alias not respect the naming convention
"""
if self._extension is None:
basename = re.sub(r"Bundle$", "", self.getName());
moduleName = "{0}.dependency".format(self.getNamespace());
className = "{0}.{1}Extension".format(moduleName, basename);
r = ReflectionClass(className);
if r.exists():
extension = r.newInstance();
# check naming convention
expectedAlias = ContainerBuilder.underscore(basename);
if expectedAlias != extension.getAlias():
raise LogicException(
'The extension alias for the default extension of a '
'bundle must be the underscored version of the '
'bundle name ("{0}" instead of "{1}")'
''.format(expectedAlias, extension.getAlias())
);
self._extension = extension;
else:
self._extension = False;
if self._extension:
return self._extension;
def getNamespace(self):
if self._reflected is None:
self._reflected = ReflectionObject(self);
return self._reflected.getNamespaceName();
def getPath(self):
if self._reflected is None:
self._reflected = ReflectionObject(self);
return dirname(self._reflected.getFileName());
@final
def getName(self):
if self._name is None:
self._name = str(type(self).__name__);
return self._name;
def getParent(self):
return None;
def registerCommands(self, collection):
"""Registers Commands.
It is only ever called once when the cache is empty.
This method can be overridden to register commands.
@param collection: pymfony.component.console_routing.RouteCollection
"""