Skip to content

Commit f447f90

Browse files
suysdavidSuRaMoN
authored andcommitted
Make Autopimple PSR-11 compatible (#1)
1 parent 267348d commit f447f90

File tree

8 files changed

+146
-43
lines changed

8 files changed

+146
-43
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"name": "suramon/auto-pimple",
66
"description": "Pimple with automated service loading",
77
"require": {
8-
"pimple/pimple": "1.1.*"
8+
"pimple/pimple": "1.1.*",
9+
"psr/container": "^1.0"
910
},
1011
"require-dev": {
1112
"phpunit/phpunit": "3.7.*"

composer.lock

Lines changed: 70 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AutoPimple/AutoPimple.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace AutoPimple;
44

5-
use Exception;
6-
use InvalidArgumentException;
75
use Pimple;
6+
use Psr\Container\ContainerInterface;
87
use ReflectionClass;
98

109
/**
1110
* Extension for pimple allowing auto-wiring
1211
*/
13-
class AutoPimple extends Pimple
12+
class AutoPimple extends Pimple implements ContainerInterface
1413
{
1514
/** @var string */
1615
private $cacheFolder;
@@ -53,7 +52,7 @@ public function extend($id, $callable): ExtendedService
5352
public static function share($callable)
5453
{
5554
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.');
5756
}
5857

5958
return function ($c) use ($callable) {
@@ -67,18 +66,21 @@ public static function share($callable)
6766
};
6867
}
6968

70-
public function get(string $className)
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function get($id)
7173
{
72-
$serviceName = StringUtil::underscore($className);
74+
$serviceName = StringUtil::underscore($id);
7375
foreach ($this->prefixMap as $prefix => $newPrefix) {
7476
if ('' != $prefix && strpos($serviceName, $prefix) === 0) {
7577
$serviceName = $newPrefix . substr($serviceName, strlen($prefix));
7678
break;
7779
}
7880
}
7981
$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 . '"');
8284
}
8385
return $service;
8486
}
@@ -89,10 +91,9 @@ public function get(string $className)
8991
public function autoFactory($className)
9092
{
9193
$serviceReflector = new ReflectionClass($className);
92-
$underscoreName = StringUtil::underscore($className);
9394
$factoryCallback = $this->serviceFactoryFromReflector($serviceReflector);
9495
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);
9697
}
9798
return new Factory($factoryCallback);
9899
}
@@ -140,7 +141,6 @@ public function alias($from, $to)
140141
) {
141142
return;
142143
}
143-
$self = $this;
144144
$this->values[$from] = $this->aliases[$pairKey] = new AliasedService($to);
145145
}
146146

@@ -160,12 +160,14 @@ public function serviceMethod($serviceId, $methodName)
160160
* the parameters b and c. If you want non default parameters you can specify them like this:
161161
* getModified('a', array('c' => $otherC));
162162
* The c parameter will be injected with the $otherC variable and b will be auto-injected like always
163+
*
164+
* @throws \AutoPimple\NotFoundException
163165
*/
164166
public function getModified($id, array $modifiedInjectables = [])
165167
{
166168
list($prefixedId, $service) = $this->serviceFactoryAndNameFromPartialServiceId($id, $modifiedInjectables);
167169
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));
169171
}
170172
$isFactory = is_object($service) && method_exists($service, '__invoke');
171173
return $isFactory ? $service($this) : $service;
@@ -198,7 +200,13 @@ public function offsetGet($id)
198200
} elseif (array_key_exists($id, $this->values) && $this->values[$id] instanceof AliasedService) {
199201
return $this->offsetGet($this->values[$id]->getTarget());
200202
} 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+
}
202210
}
203211
}
204212

@@ -337,4 +345,20 @@ protected function serviceFactoryFromReflector(ReflectionClass $serviceReflector
337345
return $serviceReflector->newInstanceArgs($dependencies);
338346
};
339347
}
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+
}
340364
}

src/AutoPimple/ContainerException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace AutoPimple;
4+
5+
use Psr\Container\ContainerExceptionInterface;
6+
7+
final class ContainerException extends \Exception implements ContainerExceptionInterface
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace AutoPimple;
4+
5+
use Psr\Container\ContainerExceptionInterface;
6+
7+
final class InvalidDefinitionException extends \InvalidArgumentException implements ContainerExceptionInterface
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace AutoPimple;
4+
5+
use Psr\Container\ContainerExceptionInterface;
6+
7+
final class InvalidFactoryException extends \InvalidArgumentException implements ContainerExceptionInterface
8+
{
9+
}

src/AutoPimple/NotFoundException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace AutoPimple;
4+
5+
use Psr\Container\NotFoundExceptionInterface;
6+
7+
final class NotFoundException extends \InvalidArgumentException implements NotFoundExceptionInterface
8+
{
9+
}

tests/AutoPimple/AutoPimpleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testAlias()
4343

4444
try {
4545
$c['true'];
46-
} catch(InvalidArgumentException $e) {
46+
} catch(NotFoundException $e) {
4747
$hasFailed = true;
4848
}
4949
$this->assertTrue($hasFailed);

0 commit comments

Comments
 (0)