Skip to content

Commit

Permalink
Make Autopimple PSR-11 compatible (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
suysdavid authored and SuRaMoN committed Feb 1, 2019
1 parent 267348d commit f447f90
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 43 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"name": "suramon/auto-pimple",
"description": "Pimple with automated service loading",
"require": {
"pimple/pimple": "1.1.*"
"pimple/pimple": "1.1.*",
"psr/container": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
Expand Down
98 changes: 70 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 37 additions & 13 deletions src/AutoPimple/AutoPimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace AutoPimple;

use Exception;
use InvalidArgumentException;
use Pimple;
use Psr\Container\ContainerInterface;
use ReflectionClass;

/**
* Extension for pimple allowing auto-wiring
*/
class AutoPimple extends Pimple
class AutoPimple extends Pimple implements ContainerInterface
{
/** @var string */
private $cacheFolder;
Expand Down Expand Up @@ -53,7 +52,7 @@ public function extend($id, $callable): ExtendedService
public static function share($callable)
{
if (! is_object($callable) || ! method_exists($callable, '__invoke')) {
throw new InvalidArgumentException('Service definition is not a Closure or invokable object.');
throw new InvalidDefinitionException('Service definition is not a Closure or invokable object.');
}

return function ($c) use ($callable) {
Expand All @@ -67,18 +66,21 @@ public static function share($callable)
};
}

public function get(string $className)
/**
* {@inheritdoc}
*/
public function get($id)
{
$serviceName = StringUtil::underscore($className);
$serviceName = StringUtil::underscore($id);
foreach ($this->prefixMap as $prefix => $newPrefix) {
if ('' != $prefix && strpos($serviceName, $prefix) === 0) {
$serviceName = $newPrefix . substr($serviceName, strlen($prefix));
break;
}
}
$service = $this->offsetGet($serviceName);
if (!$service instanceof $className) {
throw new Exception('Expected service of class "' . $className . '"');
if (!$service instanceof $id) {
throw new NotFoundException('Expected service of class "' . $id . '"');
}
return $service;
}
Expand All @@ -89,10 +91,9 @@ public function get(string $className)
public function autoFactory($className)
{
$serviceReflector = new ReflectionClass($className);
$underscoreName = StringUtil::underscore($className);
$factoryCallback = $this->serviceFactoryFromReflector($serviceReflector);
if (null === $factoryCallback) {
throw new InvalidArgumentException('Unable to create factory for this class');
throw new InvalidFactoryException('Unable to create factory for this class ' . $className);
}
return new Factory($factoryCallback);
}
Expand Down Expand Up @@ -140,7 +141,6 @@ public function alias($from, $to)
) {
return;
}
$self = $this;
$this->values[$from] = $this->aliases[$pairKey] = new AliasedService($to);
}

Expand All @@ -160,12 +160,14 @@ public function serviceMethod($serviceId, $methodName)
* the parameters b and c. If you want non default parameters you can specify them like this:
* getModified('a', array('c' => $otherC));
* The c parameter will be injected with the $otherC variable and b will be auto-injected like always
*
* @throws \AutoPimple\NotFoundException
*/
public function getModified($id, array $modifiedInjectables = [])
{
list($prefixedId, $service) = $this->serviceFactoryAndNameFromPartialServiceId($id, $modifiedInjectables);
if (null === $prefixedId) {
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
throw new NotFoundException(sprintf('Identifier "%s" is not defined.', $id));
}
$isFactory = is_object($service) && method_exists($service, '__invoke');
return $isFactory ? $service($this) : $service;
Expand Down Expand Up @@ -198,7 +200,13 @@ public function offsetGet($id)
} elseif (array_key_exists($id, $this->values) && $this->values[$id] instanceof AliasedService) {
return $this->offsetGet($this->values[$id]->getTarget());
} else {
return parent::offsetGet($id);
try {
return parent::offsetGet($id);
} catch (\InvalidArgumentException $e) {
throw new NotFoundException($e->getMessage(), null, $e);
} catch (\Exception $e) {
throw new ContainerException($e->getMessage(), null, $e);
}
}
}

Expand Down Expand Up @@ -337,4 +345,20 @@ protected function serviceFactoryFromReflector(ReflectionClass $serviceReflector
return $serviceReflector->newInstanceArgs($dependencies);
};
}

/**
* {@inheritdoc}
*/
public function has($id)
{
$serviceName = StringUtil::underscore($id);
foreach ($this->prefixMap as $prefix => $newPrefix) {
if ('' != $prefix && strpos($serviceName, $prefix) === 0) {
$serviceName = $newPrefix . substr($serviceName, strlen($prefix));
break;
}
}

return $this->offsetExists($serviceName);
}
}
9 changes: 9 additions & 0 deletions src/AutoPimple/ContainerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AutoPimple;

use Psr\Container\ContainerExceptionInterface;

final class ContainerException extends \Exception implements ContainerExceptionInterface
{
}
9 changes: 9 additions & 0 deletions src/AutoPimple/InvalidDefinitionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AutoPimple;

use Psr\Container\ContainerExceptionInterface;

final class InvalidDefinitionException extends \InvalidArgumentException implements ContainerExceptionInterface
{
}
9 changes: 9 additions & 0 deletions src/AutoPimple/InvalidFactoryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AutoPimple;

use Psr\Container\ContainerExceptionInterface;

final class InvalidFactoryException extends \InvalidArgumentException implements ContainerExceptionInterface
{
}
9 changes: 9 additions & 0 deletions src/AutoPimple/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AutoPimple;

use Psr\Container\NotFoundExceptionInterface;

final class NotFoundException extends \InvalidArgumentException implements NotFoundExceptionInterface
{
}
2 changes: 1 addition & 1 deletion tests/AutoPimple/AutoPimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testAlias()

try {
$c['true'];
} catch(InvalidArgumentException $e) {
} catch(NotFoundException $e) {
$hasFailed = true;
}
$this->assertTrue($hasFailed);
Expand Down

0 comments on commit f447f90

Please sign in to comment.