1: <?php
2:
3: namespace Guzzle\Service\Builder;
4:
5: use Guzzle\Service\AbstractConfigLoader;
6: use Guzzle\Service\Exception\ServiceNotFoundException;
7:
8: 9: 10:
11: class ServiceBuilderLoader extends AbstractConfigLoader
12: {
13: 14: 15:
16: protected function build($config, array $options)
17: {
18:
19: $class = !empty($config['class']) ? $config['class'] : __NAMESPACE__ . '\\ServiceBuilder';
20:
21:
22: $services = isset($config['services']) ? $config['services'] : $config;
23:
24:
25: foreach ($services as $name => &$service) {
26:
27: $service['params'] = isset($service['params']) ? $service['params'] : array();
28:
29:
30: if (!empty($service['extends'])) {
31:
32:
33: if (!isset($services[$service['extends']])) {
34: throw new ServiceNotFoundException(
35: "{$name} is trying to extend a non-existent service: {$service['extends']}"
36: );
37: }
38:
39: $extended = &$services[$service['extends']];
40:
41:
42: if (empty($service['class'])) {
43: $service['class'] = isset($extended['class']) ? $extended['class'] : '';
44: }
45: if ($extendsParams = isset($extended['params']) ? $extended['params'] : false) {
46: $service['params'] = $service['params'] + $extendsParams;
47: }
48: }
49:
50:
51: if (!empty($options)) {
52: $service['params'] = $options + $service['params'];
53: }
54:
55: $service['class'] = isset($service['class']) ? $service['class'] : '';
56: }
57:
58: return new $class($services);
59: }
60:
61: 62: 63:
64: protected function mergeData(array $a, array $b)
65: {
66: $result = $b + $a;
67:
68:
69: if (isset($a['services']) && $b['services']) {
70:
71:
72: $result['services'] = $b['services'] + $a['services'];
73:
74:
75: foreach ($result['services'] as $name => &$service) {
76:
77:
78: if (isset($a['services'][$name]['extends'])
79: && isset($b['services'][$name]['extends'])
80: && $b['services'][$name]['extends'] == $name
81: ) {
82: $service += $a['services'][$name];
83:
84: $service['extends'] = $a['services'][$name]['extends'];
85:
86: if (isset($a['services'][$name]['params'])) {
87: $service['params'] += $a['services'][$name]['params'];
88: }
89: }
90: }
91: }
92:
93: return $result;
94: }
95: }
96: