2
2
3
3
namespace AutoPimple ;
4
4
5
- use Exception ;
6
- use InvalidArgumentException ;
7
5
use Pimple ;
6
+ use Psr \Container \ContainerInterface ;
8
7
use ReflectionClass ;
9
8
10
9
/**
11
10
* Extension for pimple allowing auto-wiring
12
11
*/
13
- class AutoPimple extends Pimple
12
+ class AutoPimple extends Pimple implements ContainerInterface
14
13
{
15
14
/** @var string */
16
15
private $ cacheFolder ;
@@ -53,7 +52,7 @@ public function extend($id, $callable): ExtendedService
53
52
public static function share ($ callable )
54
53
{
55
54
if (! is_object ($ callable ) || ! method_exists ($ callable , '__invoke ' )) {
56
- throw new InvalidArgumentException ('Service definition is not a Closure or invokable object. ' );
55
+ throw new InvalidDefinitionException ('Service definition is not a Closure or invokable object. ' );
57
56
}
58
57
59
58
return function ($ c ) use ($ callable ) {
@@ -67,18 +66,21 @@ public static function share($callable)
67
66
};
68
67
}
69
68
70
- public function get (string $ className )
69
+ /**
70
+ * {@inheritdoc}
71
+ */
72
+ public function get ($ id )
71
73
{
72
- $ serviceName = StringUtil::underscore ($ className );
74
+ $ serviceName = StringUtil::underscore ($ id );
73
75
foreach ($ this ->prefixMap as $ prefix => $ newPrefix ) {
74
76
if ('' != $ prefix && strpos ($ serviceName , $ prefix ) === 0 ) {
75
77
$ serviceName = $ newPrefix . substr ($ serviceName , strlen ($ prefix ));
76
78
break ;
77
79
}
78
80
}
79
81
$ service = $ this ->offsetGet ($ serviceName );
80
- if (!$ service instanceof $ className ) {
81
- throw new Exception ('Expected service of class " ' . $ className . '" ' );
82
+ if (!$ service instanceof $ id ) {
83
+ throw new NotFoundException ('Expected service of class " ' . $ id . '" ' );
82
84
}
83
85
return $ service ;
84
86
}
@@ -89,10 +91,9 @@ public function get(string $className)
89
91
public function autoFactory ($ className )
90
92
{
91
93
$ serviceReflector = new ReflectionClass ($ className );
92
- $ underscoreName = StringUtil::underscore ($ className );
93
94
$ factoryCallback = $ this ->serviceFactoryFromReflector ($ serviceReflector );
94
95
if (null === $ factoryCallback ) {
95
- throw new InvalidArgumentException ('Unable to create factory for this class ' );
96
+ throw new InvalidFactoryException ('Unable to create factory for this class ' . $ className );
96
97
}
97
98
return new Factory ($ factoryCallback );
98
99
}
@@ -140,7 +141,6 @@ public function alias($from, $to)
140
141
) {
141
142
return ;
142
143
}
143
- $ self = $ this ;
144
144
$ this ->values [$ from ] = $ this ->aliases [$ pairKey ] = new AliasedService ($ to );
145
145
}
146
146
@@ -160,12 +160,14 @@ public function serviceMethod($serviceId, $methodName)
160
160
* the parameters b and c. If you want non default parameters you can specify them like this:
161
161
* getModified('a', array('c' => $otherC));
162
162
* The c parameter will be injected with the $otherC variable and b will be auto-injected like always
163
+ *
164
+ * @throws \AutoPimple\NotFoundException
163
165
*/
164
166
public function getModified ($ id , array $ modifiedInjectables = [])
165
167
{
166
168
list ($ prefixedId , $ service ) = $ this ->serviceFactoryAndNameFromPartialServiceId ($ id , $ modifiedInjectables );
167
169
if (null === $ prefixedId ) {
168
- throw new InvalidArgumentException (sprintf ('Identifier "%s" is not defined. ' , $ id ));
170
+ throw new NotFoundException (sprintf ('Identifier "%s" is not defined. ' , $ id ));
169
171
}
170
172
$ isFactory = is_object ($ service ) && method_exists ($ service , '__invoke ' );
171
173
return $ isFactory ? $ service ($ this ) : $ service ;
@@ -198,7 +200,13 @@ public function offsetGet($id)
198
200
} elseif (array_key_exists ($ id , $ this ->values ) && $ this ->values [$ id ] instanceof AliasedService) {
199
201
return $ this ->offsetGet ($ this ->values [$ id ]->getTarget ());
200
202
} else {
201
- return parent ::offsetGet ($ id );
203
+ try {
204
+ return parent ::offsetGet ($ id );
205
+ } catch (\InvalidArgumentException $ e ) {
206
+ throw new NotFoundException ($ e ->getMessage (), null , $ e );
207
+ } catch (\Exception $ e ) {
208
+ throw new ContainerException ($ e ->getMessage (), null , $ e );
209
+ }
202
210
}
203
211
}
204
212
@@ -337,4 +345,20 @@ protected function serviceFactoryFromReflector(ReflectionClass $serviceReflector
337
345
return $ serviceReflector ->newInstanceArgs ($ dependencies );
338
346
};
339
347
}
348
+
349
+ /**
350
+ * {@inheritdoc}
351
+ */
352
+ public function has ($ id )
353
+ {
354
+ $ serviceName = StringUtil::underscore ($ id );
355
+ foreach ($ this ->prefixMap as $ prefix => $ newPrefix ) {
356
+ if ('' != $ prefix && strpos ($ serviceName , $ prefix ) === 0 ) {
357
+ $ serviceName = $ newPrefix . substr ($ serviceName , strlen ($ prefix ));
358
+ break ;
359
+ }
360
+ }
361
+
362
+ return $ this ->offsetExists ($ serviceName );
363
+ }
340
364
}
0 commit comments