1 | <?php |
||
51 | class Service |
||
52 | { |
||
53 | /** |
||
54 | * Registered repositories |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $registry = []; |
||
59 | /** |
||
60 | * Repository auto-connector service |
||
61 | * |
||
62 | * @var AutoConnectorInterface |
||
63 | */ |
||
64 | protected static $_autoConnector = null; |
||
65 | /** |
||
66 | * Adapter strategy factory |
||
67 | * |
||
68 | * @var AdapterStrategyFactoryInterface |
||
69 | */ |
||
70 | protected static $_adapterStrategyFactory = null; |
||
71 | /** |
||
72 | * Object manager |
||
73 | * |
||
74 | * @var ManagerInterface |
||
75 | */ |
||
76 | protected static $_objectManager = null; |
||
77 | /** |
||
78 | * Auto-connect to repositories |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | protected static $autoConnectEnabled = true; |
||
83 | |||
84 | /******************************************************************************* |
||
85 | * PUBLIC METHODS |
||
86 | *******************************************************************************/ |
||
87 | |||
88 | /** |
||
89 | * Pre-register a repository |
||
90 | * |
||
91 | * The purpose of repository pre-registration is to provide custom arguments (like a base |
||
92 | * directory or basic authentication credentials. |
||
93 | * The repository URL may be local or remote, relative or absolute, with Apparat or HTTP scheme. |
||
94 | * |
||
95 | * @param string|Url $url Repository URL |
||
96 | * @param RepositoryInterface $repository Repository |
||
97 | */ |
||
98 | public static function register($url, RepositoryInterface $repository) |
||
104 | |||
105 | /** |
||
106 | * Return an object repository by URL |
||
107 | * |
||
108 | * If a repository URL hasn't been pre-registered, the method tries to perform an adhoc registration |
||
109 | * based on the URL given. |
||
110 | * The repository URL may be local or remote, relative or absolute, with Apparat or HTTP scheme. |
||
111 | * |
||
112 | * @param string|Url $url Repository URL |
||
113 | * @return \Apparat\Object\Domain\Repository\Repository Object repository |
||
114 | * @throws InvalidArgumentException If the repository URL is invalid |
||
115 | * @throws InvalidArgumentException If the repository URL is unknown |
||
116 | */ |
||
117 | public static function get($url) |
||
118 | { |
||
119 | // Ensure that the service is properly configured |
||
120 | self::isConfigured(); |
||
121 | |||
122 | $url = self::normalizeRepositoryUrl($url); |
||
123 | |||
124 | // If the repository URL is unknown |
||
125 | if (!self::connected($url)) { |
||
126 | throw new InvalidArgumentException( |
||
127 | sprintf('Unknown repository URL "%s"', $url), |
||
128 | InvalidArgumentException::UNKNOWN_REPOSITORY_URL |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | // Return the repository instance |
||
133 | return self::$registry[$url]; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Test whether a repository URL is registered |
||
138 | * |
||
139 | * @param string $url Repository URL |
||
140 | * @return bool Repository URL is registered |
||
141 | */ |
||
142 | public static function isRegistered($url) |
||
147 | |||
148 | /** |
||
149 | * Configure the repository service |
||
150 | * |
||
151 | * @param AutoConnectorInterface|null $autoConnector Auto-connector |
||
152 | * @param AdapterStrategyFactoryInterface|null $adapterStrategyFactory Adapter strategy factory |
||
153 | * @param ManagerInterface|null $objectManager Object manager |
||
154 | */ |
||
155 | public static function configure( |
||
164 | |||
165 | /** |
||
166 | * Check whether the service is configured for auto-connecting to repositories |
||
167 | * |
||
168 | * @return bool Service is configured for auto-connecting |
||
169 | * @throws RuntimeException If the service is not properly configured |
||
170 | */ |
||
171 | public static function isConfigured() |
||
186 | |||
187 | /** |
||
188 | * Return the auto-connector service |
||
189 | * |
||
190 | * @return AutoConnectorInterface Auto-connector servie |
||
191 | */ |
||
192 | public static function getAutoConnector() |
||
196 | |||
197 | /** |
||
198 | * Return the adapter strategy factory |
||
199 | * |
||
200 | * @return AdapterStrategyFactoryInterface Adapter strategy factory |
||
201 | */ |
||
202 | public static function getAdapterStrategyFactory() |
||
206 | |||
207 | /** |
||
208 | * Return the object manager |
||
209 | * |
||
210 | * @return ManagerInterface Object manager |
||
211 | */ |
||
212 | public static function getObjectManager() |
||
216 | |||
217 | /** |
||
218 | * Normalize a repository URL |
||
219 | * |
||
220 | * @param string|Url $url Repository URL |
||
221 | * @return string Normalized repository URL |
||
222 | * @throws InvalidArgumentException If the repository URL is invalid |
||
223 | */ |
||
224 | public static function normalizeRepositoryUrl($url) |
||
264 | |||
265 | /** |
||
266 | * Enable repository auto-connections |
||
267 | * |
||
268 | * If the method is called without any arguments it will just return the current auto-connection state. |
||
269 | * |
||
270 | * @param null|boolean $autoConnect Enable / disable auto-connections |
||
271 | * @return bool Status of repository auto-connection |
||
272 | */ |
||
273 | public static function useAutoConnect($autoConnect = null) |
||
280 | |||
281 | /******************************************************************************* |
||
282 | * PRIVATE METHODS |
||
283 | *******************************************************************************/ |
||
284 | |||
285 | /** |
||
286 | * Test whether a repository URL registered or can be auto-connected |
||
287 | * |
||
288 | * @param string $url Repository URL |
||
289 | * @return bool Repository is connected |
||
290 | */ |
||
291 | protected static function connected($url) |
||
301 | } |
||
302 |