diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2018-01-22 11:14:53 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2018-01-22 11:14:53 +0300 |
commit | 63bb8fa1ddc79124d6635378a7d85288cd171b57 (patch) | |
tree | e49e66ee2fb919843a7de46b978118d1199eeb67 /pimple | |
parent | dd2cb3094cbe27e995ac256fc149d2fce2fd4112 (diff) |
Bump pimple to 3.2.3
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'pimple')
8 files changed, 413 insertions, 25 deletions
diff --git a/pimple/pimple/src/Pimple/Container.php b/pimple/pimple/src/Pimple/Container.php index c976431e..707b92b8 100644 --- a/pimple/pimple/src/Pimple/Container.php +++ b/pimple/pimple/src/Pimple/Container.php @@ -26,10 +26,15 @@ namespace Pimple; +use Pimple\Exception\ExpectedInvokableException; +use Pimple\Exception\FrozenServiceException; +use Pimple\Exception\InvalidServiceIdentifierException; +use Pimple\Exception\UnknownIdentifierException; + /** * Container main class. * - * @author Fabien Potencier + * @author Fabien Potencier */ class Container implements \ArrayAccess { @@ -41,11 +46,11 @@ class Container implements \ArrayAccess private $keys = array(); /** - * Instantiate the container. + * Instantiates the container. * * Objects and parameters can be passed as argument to the constructor. * - * @param array $values The parameters or objects. + * @param array $values The parameters or objects */ public function __construct(array $values = array()) { @@ -69,12 +74,12 @@ class Container implements \ArrayAccess * @param string $id The unique identifier for the parameter or object * @param mixed $value The value of the parameter or a closure to define an object * - * @throws \RuntimeException Prevent override of a frozen service + * @throws FrozenServiceException Prevent override of a frozen service */ public function offsetSet($id, $value) { if (isset($this->frozen[$id])) { - throw new \RuntimeException(sprintf('Cannot override frozen service "%s".', $id)); + throw new FrozenServiceException($id); } $this->values[$id] = $value; @@ -88,19 +93,19 @@ class Container implements \ArrayAccess * * @return mixed The value of the parameter or an object * - * @throws \InvalidArgumentException if the identifier is not defined + * @throws UnknownIdentifierException If the identifier is not defined */ public function offsetGet($id) { if (!isset($this->keys[$id])) { - throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); + throw new UnknownIdentifierException($id); } if ( isset($this->raw[$id]) - || !is_object($this->values[$id]) + || !\is_object($this->values[$id]) || isset($this->protected[$this->values[$id]]) - || !method_exists($this->values[$id], '__invoke') + || !\method_exists($this->values[$id], '__invoke') ) { return $this->values[$id]; } @@ -138,7 +143,7 @@ class Container implements \ArrayAccess public function offsetUnset($id) { if (isset($this->keys[$id])) { - if (is_object($this->values[$id])) { + if (\is_object($this->values[$id])) { unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]); } @@ -153,12 +158,12 @@ class Container implements \ArrayAccess * * @return callable The passed callable * - * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object + * @throws ExpectedInvokableException Service definition has to be a closure or an invokable object */ public function factory($callable) { - if (!method_exists($callable, '__invoke')) { - throw new \InvalidArgumentException('Service definition is not a Closure or invokable object.'); + if (!\method_exists($callable, '__invoke')) { + throw new ExpectedInvokableException('Service definition is not a Closure or invokable object.'); } $this->factories->attach($callable); @@ -175,12 +180,12 @@ class Container implements \ArrayAccess * * @return callable The passed callable * - * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object + * @throws ExpectedInvokableException Service definition has to be a closure or an invokable object */ public function protect($callable) { - if (!method_exists($callable, '__invoke')) { - throw new \InvalidArgumentException('Callable is not a Closure or invokable object.'); + if (!\method_exists($callable, '__invoke')) { + throw new ExpectedInvokableException('Callable is not a Closure or invokable object.'); } $this->protected->attach($callable); @@ -195,12 +200,12 @@ class Container implements \ArrayAccess * * @return mixed The value of the parameter or the closure defining an object * - * @throws \InvalidArgumentException if the identifier is not defined + * @throws UnknownIdentifierException If the identifier is not defined */ public function raw($id) { if (!isset($this->keys[$id])) { - throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); + throw new UnknownIdentifierException($id); } if (isset($this->raw[$id])) { @@ -221,20 +226,31 @@ class Container implements \ArrayAccess * * @return callable The wrapped callable * - * @throws \InvalidArgumentException if the identifier is not defined or not a service definition + * @throws UnknownIdentifierException If the identifier is not defined + * @throws FrozenServiceException If the service is frozen + * @throws InvalidServiceIdentifierException If the identifier belongs to a parameter + * @throws ExpectedInvokableException If the extension callable is not a closure or an invokable object */ public function extend($id, $callable) { if (!isset($this->keys[$id])) { - throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); + throw new UnknownIdentifierException($id); + } + + if (isset($this->frozen[$id])) { + throw new FrozenServiceException($id); + } + + if (!\is_object($this->values[$id]) || !\method_exists($this->values[$id], '__invoke')) { + throw new InvalidServiceIdentifierException($id); } - if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) { - throw new \InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id)); + if (isset($this->protected[$this->values[$id]])) { + @\trigger_error(\sprintf('How Pimple behaves when extending protected closures will be fixed in Pimple 4. Are you sure "%s" should be protected?', $id), \E_USER_DEPRECATED); } - if (!is_object($callable) || !method_exists($callable, '__invoke')) { - throw new \InvalidArgumentException('Extension service definition is not a Closure or invokable object.'); + if (!\is_object($callable) || !\method_exists($callable, '__invoke')) { + throw new ExpectedInvokableException('Extension service definition is not a Closure or invokable object.'); } $factory = $this->values[$id]; @@ -258,7 +274,7 @@ class Container implements \ArrayAccess */ public function keys() { - return array_keys($this->values); + return \array_keys($this->values); } /** diff --git a/pimple/pimple/src/Pimple/Exception/ExpectedInvokableException.php b/pimple/pimple/src/Pimple/Exception/ExpectedInvokableException.php new file mode 100644 index 00000000..7228421b --- /dev/null +++ b/pimple/pimple/src/Pimple/Exception/ExpectedInvokableException.php @@ -0,0 +1,38 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace Pimple\Exception; + +use Psr\Container\ContainerExceptionInterface; + +/** + * A closure or invokable object was expected. + * + * @author Pascal Luna <skalpa@zetareticuli.org> + */ +class ExpectedInvokableException extends \InvalidArgumentException implements ContainerExceptionInterface +{ +} diff --git a/pimple/pimple/src/Pimple/Exception/FrozenServiceException.php b/pimple/pimple/src/Pimple/Exception/FrozenServiceException.php new file mode 100644 index 00000000..e4d2f6d3 --- /dev/null +++ b/pimple/pimple/src/Pimple/Exception/FrozenServiceException.php @@ -0,0 +1,45 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace Pimple\Exception; + +use Psr\Container\ContainerExceptionInterface; + +/** + * An attempt to modify a frozen service was made. + * + * @author Pascal Luna <skalpa@zetareticuli.org> + */ +class FrozenServiceException extends \RuntimeException implements ContainerExceptionInterface +{ + /** + * @param string $id Identifier of the frozen service + */ + public function __construct($id) + { + parent::__construct(\sprintf('Cannot override frozen service "%s".', $id)); + } +} diff --git a/pimple/pimple/src/Pimple/Exception/InvalidServiceIdentifierException.php b/pimple/pimple/src/Pimple/Exception/InvalidServiceIdentifierException.php new file mode 100644 index 00000000..91e82f98 --- /dev/null +++ b/pimple/pimple/src/Pimple/Exception/InvalidServiceIdentifierException.php @@ -0,0 +1,45 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace Pimple\Exception; + +use Psr\Container\NotFoundExceptionInterface; + +/** + * An attempt to perform an operation that requires a service identifier was made. + * + * @author Pascal Luna <skalpa@zetareticuli.org> + */ +class InvalidServiceIdentifierException extends \InvalidArgumentException implements NotFoundExceptionInterface +{ + /** + * @param string $id The invalid identifier + */ + public function __construct($id) + { + parent::__construct(\sprintf('Identifier "%s" does not contain an object definition.', $id)); + } +} diff --git a/pimple/pimple/src/Pimple/Exception/UnknownIdentifierException.php b/pimple/pimple/src/Pimple/Exception/UnknownIdentifierException.php new file mode 100644 index 00000000..fb6b626e --- /dev/null +++ b/pimple/pimple/src/Pimple/Exception/UnknownIdentifierException.php @@ -0,0 +1,45 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace Pimple\Exception; + +use Psr\Container\NotFoundExceptionInterface; + +/** + * The identifier of a valid service or parameter was expected. + * + * @author Pascal Luna <skalpa@zetareticuli.org> + */ +class UnknownIdentifierException extends \InvalidArgumentException implements NotFoundExceptionInterface +{ + /** + * @param string $id The unknown identifier + */ + public function __construct($id) + { + parent::__construct(\sprintf('Identifier "%s" is not defined.', $id)); + } +} diff --git a/pimple/pimple/src/Pimple/Psr11/Container.php b/pimple/pimple/src/Pimple/Psr11/Container.php new file mode 100644 index 00000000..cadbfffa --- /dev/null +++ b/pimple/pimple/src/Pimple/Psr11/Container.php @@ -0,0 +1,55 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009-2017 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace Pimple\Psr11; + +use Pimple\Container as PimpleContainer; +use Psr\Container\ContainerInterface; + +/** + * PSR-11 compliant wrapper. + * + * @author Pascal Luna <skalpa@zetareticuli.org> + */ +final class Container implements ContainerInterface +{ + private $pimple; + + public function __construct(PimpleContainer $pimple) + { + $this->pimple = $pimple; + } + + public function get($id) + { + return $this->pimple[$id]; + } + + public function has($id) + { + return isset($this->pimple[$id]); + } +} diff --git a/pimple/pimple/src/Pimple/Psr11/ServiceLocator.php b/pimple/pimple/src/Pimple/Psr11/ServiceLocator.php new file mode 100644 index 00000000..3361c6f1 --- /dev/null +++ b/pimple/pimple/src/Pimple/Psr11/ServiceLocator.php @@ -0,0 +1,75 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace Pimple\Psr11; + +use Pimple\Container as PimpleContainer; +use Pimple\Exception\UnknownIdentifierException; +use Psr\Container\ContainerInterface; + +/** + * Pimple PSR-11 service locator. + * + * @author Pascal Luna <skalpa@zetareticuli.org> + */ +class ServiceLocator implements ContainerInterface +{ + private $container; + private $aliases = array(); + + /** + * @param PimpleContainer $container The Container instance used to locate services + * @param array $ids Array of service ids that can be located. String keys can be used to define aliases + */ + public function __construct(PimpleContainer $container, array $ids) + { + $this->container = $container; + + foreach ($ids as $key => $id) { + $this->aliases[\is_int($key) ? $id : $key] = $id; + } + } + + /** + * {@inheritdoc} + */ + public function get($id) + { + if (!isset($this->aliases[$id])) { + throw new UnknownIdentifierException($id); + } + + return $this->container[$this->aliases[$id]]; + } + + /** + * {@inheritdoc} + */ + public function has($id) + { + return isset($this->aliases[$id]) && isset($this->container[$this->aliases[$id]]); + } +} diff --git a/pimple/pimple/src/Pimple/ServiceIterator.php b/pimple/pimple/src/Pimple/ServiceIterator.php new file mode 100644 index 00000000..5cde5188 --- /dev/null +++ b/pimple/pimple/src/Pimple/ServiceIterator.php @@ -0,0 +1,69 @@ +<?php + +/* + * This file is part of Pimple. + * + * Copyright (c) 2009 Fabien Potencier + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +namespace Pimple; + +/** + * Lazy service iterator. + * + * @author Pascal Luna <skalpa@zetareticuli.org> + */ +final class ServiceIterator implements \Iterator +{ + private $container; + private $ids; + + public function __construct(Container $container, array $ids) + { + $this->container = $container; + $this->ids = $ids; + } + + public function rewind() + { + \reset($this->ids); + } + + public function current() + { + return $this->container[\current($this->ids)]; + } + + public function key() + { + return \current($this->ids); + } + + public function next() + { + \next($this->ids); + } + + public function valid() + { + return null !== \key($this->ids); + } +} |