diff --git a/composer.json b/composer.json index ddff774..11e426a 100644 --- a/composer.json +++ b/composer.json @@ -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.*" diff --git a/composer.lock b/composer.lock index 2b3860b..4ade43b 100644 --- a/composer.lock +++ b/composer.lock @@ -1,21 +1,22 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" ], - "hash": "f90056242c8158089980d06922b36afd", + "content-hash": "0b5723c70486e8f54e4229b8bd040349", "packages": [ { "name": "pimple/pimple", "version": "v1.1.1", "source": { "type": "git", - "url": "https://github.com/fabpot/Pimple.git", + "url": "https://github.com/silexphp/Pimple.git", "reference": "2019c145fe393923f3441b23f29bbdfaa5c58c4d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fabpot/Pimple/zipball/2019c145fe393923f3441b23f29bbdfaa5c58c4d", + "url": "https://api.github.com/repos/silexphp/Pimple/zipball/2019c145fe393923f3441b23f29bbdfaa5c58c4d", "reference": "2019c145fe393923f3441b23f29bbdfaa5c58c4d", "shasum": "" }, @@ -40,9 +41,7 @@ "authors": [ { "name": "Fabien Potencier", - "email": "fabien@symfony.com", - "homepage": "http://fabien.potencier.org", - "role": "Lead Developer" + "email": "fabien@symfony.com" } ], "description": "Pimple is a simple Dependency Injection Container for PHP 5.3", @@ -51,7 +50,56 @@ "container", "dependency injection" ], - "time": "2013-11-22 08:30:29" + "time": "2013-11-22T08:30:29+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14T16:28:37+00:00" } ], "packages-dev": [ @@ -114,7 +162,7 @@ "testing", "xunit" ], - "time": "2014-03-28 10:53:45" + "time": "2014-03-28T10:53:45+00:00" }, { "name": "phpunit/php-file-iterator", @@ -159,7 +207,7 @@ "filesystem", "iterator" ], - "time": "2013-10-10 15:34:57" + "time": "2013-10-10T15:34:57+00:00" }, { "name": "phpunit/php-text-template", @@ -203,7 +251,7 @@ "keywords": [ "template" ], - "time": "2014-01-30 17:20:04" + "time": "2014-01-30T17:20:04+00:00" }, { "name": "phpunit/php-timer", @@ -247,7 +295,7 @@ "keywords": [ "timer" ], - "time": "2013-08-02 07:42:54" + "time": "2013-08-02T07:42:54+00:00" }, { "name": "phpunit/php-token-stream", @@ -297,7 +345,7 @@ "keywords": [ "tokenizer" ], - "time": "2014-03-03 05:10:30" + "time": "2014-03-03T05:10:30+00:00" }, { "name": "phpunit/phpunit", @@ -371,7 +419,7 @@ "testing", "xunit" ], - "time": "2014-04-21 06:25:54" + "time": "2014-04-21T06:25:54+00:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -420,7 +468,7 @@ "mock", "xunit" ], - "time": "2013-01-13 10:24:48" + "time": "2013-01-13T10:24:48+00:00" }, { "name": "symfony/yaml", @@ -469,20 +517,14 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-03-12 18:29:58" + "time": "2014-03-12T18:29:58+00:00" } ], - "aliases": [ - - ], + "aliases": [], "minimum-stability": "stable", - "stability-flags": [ - - ], - "platform": [ - - ], - "platform-dev": [ - - ] + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] } diff --git a/src/AutoPimple/AutoPimple.php b/src/AutoPimple/AutoPimple.php index b0229eb..c78e966 100644 --- a/src/AutoPimple/AutoPimple.php +++ b/src/AutoPimple/AutoPimple.php @@ -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; @@ -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) { @@ -67,9 +66,12 @@ 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)); @@ -77,8 +79,8 @@ public function get(string $className) } } $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; } @@ -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); } @@ -140,7 +141,6 @@ public function alias($from, $to) ) { return; } - $self = $this; $this->values[$from] = $this->aliases[$pairKey] = new AliasedService($to); } @@ -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; @@ -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); + } } } @@ -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); + } } diff --git a/src/AutoPimple/ContainerException.php b/src/AutoPimple/ContainerException.php new file mode 100644 index 0000000..4023ae2 --- /dev/null +++ b/src/AutoPimple/ContainerException.php @@ -0,0 +1,9 @@ +assertTrue($hasFailed);