pax_global_header00006660000000000000000000000064122436130450014512gustar00rootroot0000000000000052 comment=2019c145fe393923f3441b23f29bbdfaa5c58c4d Pimple-1.1.1/000077500000000000000000000000001224361304500127405ustar00rootroot00000000000000Pimple-1.1.1/.gitignore000066400000000000000000000000141224361304500147230ustar00rootroot00000000000000phpunit.xml Pimple-1.1.1/.travis.yml000066400000000000000000000000541224361304500150500ustar00rootroot00000000000000language: php php: - 5.3 - 5.4 - 5.5 Pimple-1.1.1/LICENSE000066400000000000000000000020511224361304500137430ustar00rootroot00000000000000Copyright (c) 2009-2013 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. Pimple-1.1.1/README.rst000066400000000000000000000121531224361304500144310ustar00rootroot00000000000000Pimple ====== Pimple is a small Dependency Injection Container for PHP 5.3 that consists of just one file and one class (about 80 lines of code). `Download it`_, require it in your code, and you're good to go:: require_once '/path/to/Pimple.php'; Creating a container is a matter of instating the ``Pimple`` class:: $container = new Pimple(); As many other dependency injection containers, Pimple is able to manage two different kind of data: *services* and *parameters*. Defining Parameters ------------------- Defining a parameter is as simple as using the Pimple instance as an array:: // define some parameters $container['cookie_name'] = 'SESSION_ID'; $container['session_storage_class'] = 'SessionStorage'; Defining Services ----------------- A service is an object that does something as part of a larger system. Examples of services: Database connection, templating engine, mailer. Almost any object could be a service. Services are defined by anonymous functions that return an instance of an object:: // define some services $container['session_storage'] = function ($c) { return new $c['session_storage_class']($c['cookie_name']); }; $container['session'] = function ($c) { return new Session($c['session_storage']); }; Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters. As objects are only created when you get them, the order of the definitions does not matter, and there is no performance penalty. Using the defined services is also very easy:: // get the session object $session = $container['session']; // the above call is roughly equivalent to the following code: // $storage = new SessionStorage('SESSION_ID'); // $session = new Session($storage); Defining Shared Services ------------------------ By default, each time you get a service, Pimple returns a new instance of it. If you want the same instance to be returned for all calls, wrap your anonymous function with the ``share()`` method:: $container['session'] = $container->share(function ($c) { return new Session($c['session_storage']); }); Protecting Parameters --------------------- Because Pimple sees anonymous functions as service definitions, you need to wrap anonymous functions with the ``protect()`` method to store them as parameter:: $container['random'] = $container->protect(function () { return rand(); }); Modifying services after creation --------------------------------- In some cases you may want to modify a service definition after it has been defined. You can use the ``extend()`` method to define additional code to be run on your service just after it is created:: $container['mail'] = function ($c) { return new \Zend_Mail(); }; $container['mail'] = $container->extend('mail', function($mail, $c) { $mail->setFrom($c['mail.default_from']); return $mail; }); The first argument is the name of the object, the second is a function that gets access to the object instance and the container. The return value is a service definition, so you need to re-assign it on the container. If the service you plan to extend is already shared, it's recommended that you re-wrap your extended service with the ``shared`` method, otherwise your extension code will be called every time you access the service:: $container['twig'] = $container->share(function ($c) { return new Twig_Environment($c['twig.loader'], $c['twig.options']); }); $container['twig'] = $container->share($container->extend('twig', function ($twig, $c) { $twig->addExtension(new MyTwigExtension()); return $twig; })); Fetching the service creation function -------------------------------------- When you access an object, Pimple automatically calls the anonymous function that you defined, which creates the service object for you. If you want to get raw access to this function, you can use the ``raw()`` method:: $container['session'] = $container->share(function ($c) { return new Session($c['session_storage']); }); $sessionFunction = $container->raw('session'); Packaging a Container for reusability ------------------------------------- If you use the same libraries over and over, you might want to create reusable containers. Creating a reusable container is as simple as creating a class that extends ``Pimple``, and configuring it in the constructor:: class SomeContainer extends Pimple { public function __construct() { $this['parameter'] = 'foo'; $this['object'] = function () { return stdClass(); }; } } Using this container from your own is as easy as it can get:: $container = new Pimple(); // define your project parameters and services // ... // embed the SomeContainer container $container['embedded'] = $container->share(function () { return new SomeContainer(); }); // configure it $container['embedded']['parameter'] = 'bar'; // use it $container['embedded']['object']->...; .. _Download it: https://github.com/fabpot/Pimple Pimple-1.1.1/composer.json000066400000000000000000000011251224361304500154610ustar00rootroot00000000000000{ "name": "pimple/pimple", "type": "library", "description": "Pimple is a simple Dependency Injection Container for PHP 5.3", "keywords": ["dependency injection", "container"], "homepage": "http://pimple.sensiolabs.org", "license": "MIT", "authors": [ { "name": "Fabien Potencier", "email": "fabien@symfony.com" } ], "require": { "php": ">=5.3.0" }, "autoload": { "psr-0": { "Pimple": "lib/" } }, "extra": { "branch-alias": { "dev-master": "1.1.x-dev" } } }Pimple-1.1.1/lib/000077500000000000000000000000001224361304500135065ustar00rootroot00000000000000Pimple-1.1.1/lib/Pimple.php000066400000000000000000000152731224361304500154550ustar00rootroot00000000000000values = $values; } /** * Sets a parameter or an object. * * Objects must be defined as Closures. * * Allowing any PHP callable leads to difficult to debug problems * as function names (strings) are callable (creating a function with * the same a name as an existing parameter would break your container). * * @param string $id The unique identifier for the parameter or object * @param mixed $value The value of the parameter or a closure to defined an object */ public function offsetSet($id, $value) { $this->values[$id] = $value; } /** * Gets a parameter or an object. * * @param string $id The unique identifier for the parameter or object * * @return mixed The value of the parameter or an object * * @throws InvalidArgumentException if the identifier is not defined */ public function offsetGet($id) { if (!array_key_exists($id, $this->values)) { throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); } $isFactory = is_object($this->values[$id]) && method_exists($this->values[$id], '__invoke'); return $isFactory ? $this->values[$id]($this) : $this->values[$id]; } /** * Checks if a parameter or an object is set. * * @param string $id The unique identifier for the parameter or object * * @return Boolean */ public function offsetExists($id) { return array_key_exists($id, $this->values); } /** * Unsets a parameter or an object. * * @param string $id The unique identifier for the parameter or object */ public function offsetUnset($id) { unset($this->values[$id]); } /** * Returns a closure that stores the result of the given service definition * for uniqueness in the scope of this instance of Pimple. * * @param callable $callable A service definition to wrap for uniqueness * * @return Closure The wrapped closure */ 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.'); } return function ($c) use ($callable) { static $object; if (null === $object) { $object = $callable($c); } return $object; }; } /** * Protects a callable from being interpreted as a service. * * This is useful when you want to store a callable as a parameter. * * @param callable $callable A callable to protect from being evaluated * * @return Closure The protected closure */ public static function protect($callable) { if (!is_object($callable) || !method_exists($callable, '__invoke')) { throw new InvalidArgumentException('Callable is not a Closure or invokable object.'); } return function ($c) use ($callable) { return $callable; }; } /** * Gets a parameter or the closure defining an object. * * @param string $id The unique identifier for the parameter or object * * @return mixed The value of the parameter or the closure defining an object * * @throws InvalidArgumentException if the identifier is not defined */ public function raw($id) { if (!array_key_exists($id, $this->values)) { throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); } return $this->values[$id]; } /** * Extends an object definition. * * Useful when you want to extend an existing object definition, * without necessarily loading that object. * * @param string $id The unique identifier for the object * @param callable $callable A service definition to extend the original * * @return Closure The wrapped closure * * @throws InvalidArgumentException if the identifier is not defined or not a service definition */ public function extend($id, $callable) { if (!array_key_exists($id, $this->values)) { throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $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 (!is_object($callable) || !method_exists($callable, '__invoke')) { throw new InvalidArgumentException('Extension service definition is not a Closure or invokable object.'); } $factory = $this->values[$id]; return $this->values[$id] = function ($c) use ($callable, $factory) { return $callable($factory($c), $c); }; } /** * Returns all defined value names. * * @return array An array of value names */ public function keys() { return array_keys($this->values); } } Pimple-1.1.1/phpunit.xml.dist000066400000000000000000000010631224361304500161130ustar00rootroot00000000000000 ./tests/Pimple/ Pimple-1.1.1/tests/000077500000000000000000000000001224361304500141025ustar00rootroot00000000000000Pimple-1.1.1/tests/Pimple/000077500000000000000000000000001224361304500153305ustar00rootroot00000000000000Pimple-1.1.1/tests/Pimple/Tests/000077500000000000000000000000001224361304500164325ustar00rootroot00000000000000Pimple-1.1.1/tests/Pimple/Tests/Invokable.php000066400000000000000000000025171224361304500210620ustar00rootroot00000000000000value = $value; return $service; } } Pimple-1.1.1/tests/Pimple/Tests/NonInvokable.php000066400000000000000000000023531224361304500215330ustar00rootroot00000000000000 */ class PimpleTest extends \PHPUnit_Framework_TestCase { public function testWithString() { $pimple = new Pimple(); $pimple['param'] = 'value'; $this->assertEquals('value', $pimple['param']); } public function testWithClosure() { $pimple = new Pimple(); $pimple['service'] = function () { return new Service(); }; $this->assertInstanceOf('Pimple\Tests\Service', $pimple['service']); } public function testServicesShouldBeDifferent() { $pimple = new Pimple(); $pimple['service'] = function () { return new Service(); }; $serviceOne = $pimple['service']; $this->assertInstanceOf('Pimple\Tests\Service', $serviceOne); $serviceTwo = $pimple['service']; $this->assertInstanceOf('Pimple\Tests\Service', $serviceTwo); $this->assertNotSame($serviceOne, $serviceTwo); } public function testShouldPassContainerAsParameter() { $pimple = new Pimple(); $pimple['service'] = function () { return new Service(); }; $pimple['container'] = function ($container) { return $container; }; $this->assertNotSame($pimple, $pimple['service']); $this->assertSame($pimple, $pimple['container']); } public function testIsset() { $pimple = new Pimple(); $pimple['param'] = 'value'; $pimple['service'] = function () { return new Service(); }; $pimple['null'] = null; $this->assertTrue(isset($pimple['param'])); $this->assertTrue(isset($pimple['service'])); $this->assertTrue(isset($pimple['null'])); $this->assertFalse(isset($pimple['non_existent'])); } public function testConstructorInjection() { $params = array("param" => "value"); $pimple = new Pimple($params); $this->assertSame($params['param'], $pimple['param']); } /** * @expectedException InvalidArgumentException * @expectedExceptionMessage Identifier "foo" is not defined. */ public function testOffsetGetValidatesKeyIsPresent() { $pimple = new Pimple(); echo $pimple['foo']; } public function testOffsetGetHonorsNullValues() { $pimple = new Pimple(); $pimple['foo'] = null; $this->assertNull($pimple['foo']); } public function testUnset() { $pimple = new Pimple(); $pimple['param'] = 'value'; $pimple['service'] = function () { return new Service(); }; unset($pimple['param'], $pimple['service']); $this->assertFalse(isset($pimple['param'])); $this->assertFalse(isset($pimple['service'])); } /** * @dataProvider serviceDefinitionProvider */ public function testShare($service) { $pimple = new Pimple(); $pimple['shared_service'] = $pimple->share($service); $serviceOne = $pimple['shared_service']; $this->assertInstanceOf('Pimple\Tests\Service', $serviceOne); $serviceTwo = $pimple['shared_service']; $this->assertInstanceOf('Pimple\Tests\Service', $serviceTwo); $this->assertSame($serviceOne, $serviceTwo); } /** * @dataProvider serviceDefinitionProvider */ public function testProtect($service) { $pimple = new Pimple(); $pimple['protected'] = $pimple->protect($service); $this->assertSame($service, $pimple['protected']); } public function testGlobalFunctionNameAsParameterValue() { $pimple = new Pimple(); $pimple['global_function'] = 'strlen'; $this->assertSame('strlen', $pimple['global_function']); } public function testRaw() { $pimple = new Pimple(); $pimple['service'] = $definition = function () { return 'foo'; }; $this->assertSame($definition, $pimple->raw('service')); } public function testRawHonorsNullValues() { $pimple = new Pimple(); $pimple['foo'] = null; $this->assertNull($pimple->raw('foo')); } /** * @expectedException InvalidArgumentException * @expectedExceptionMessage Identifier "foo" is not defined. */ public function testRawValidatesKeyIsPresent() { $pimple = new Pimple(); $pimple->raw('foo'); } /** * @dataProvider serviceDefinitionProvider */ public function testExtend($service) { $pimple = new Pimple(); $pimple['shared_service'] = $pimple->share(function () { return new Service(); }); $pimple->extend('shared_service', $service); $serviceOne = $pimple['shared_service']; $this->assertInstanceOf('Pimple\Tests\Service', $serviceOne); $serviceTwo = $pimple['shared_service']; $this->assertInstanceOf('Pimple\Tests\Service', $serviceTwo); $this->assertNotSame($serviceOne, $serviceTwo); $this->assertSame($serviceOne->value, $serviceTwo->value); } /** * @expectedException InvalidArgumentException * @expectedExceptionMessage Identifier "foo" is not defined. */ public function testExtendValidatesKeyIsPresent() { $pimple = new Pimple(); $pimple->extend('foo', function () {}); } public function testKeys() { $pimple = new Pimple(); $pimple['foo'] = 123; $pimple['bar'] = 123; $this->assertEquals(array('foo', 'bar'), $pimple->keys()); } /** @test */ public function settingAnInvokableObjectShouldTreatItAsFactory() { $pimple = new Pimple(); $pimple['invokable'] = new Invokable(); $this->assertInstanceOf('Pimple\Tests\Service', $pimple['invokable']); } /** @test */ public function settingNonInvokableObjectShouldTreatItAsParameter() { $pimple = new Pimple(); $pimple['non_invokable'] = new NonInvokable(); $this->assertInstanceOf('Pimple\Tests\NonInvokable', $pimple['non_invokable']); } /** * @dataProvider badServiceDefinitionProvider * @expectedException InvalidArgumentException * @expectedExceptionMessage Service definition is not a Closure or invokable object. */ public function testShareFailsForInvalidServiceDefinitions($service) { $pimple = new Pimple(); $pimple->share($service); } /** * @dataProvider badServiceDefinitionProvider * @expectedException InvalidArgumentException * @expectedExceptionMessage Callable is not a Closure or invokable object. */ public function testProtectFailsForInvalidServiceDefinitions($service) { $pimple = new Pimple(); $pimple->protect($service); } /** * @dataProvider badServiceDefinitionProvider * @expectedException InvalidArgumentException * @expectedExceptionMessage Identifier "foo" does not contain an object definition. */ public function testExtendFailsForKeysNotContainingServiceDefinitions($service) { $pimple = new Pimple(); $pimple['foo'] = $service; $pimple->extend('foo', function () {}); } /** * @dataProvider badServiceDefinitionProvider * @expectedException InvalidArgumentException * @expectedExceptionMessage Extension service definition is not a Closure or invokable object. */ public function testExtendFailsForInvalidServiceDefinitions($service) { $pimple = new Pimple(); $pimple['foo'] = function () {}; $pimple->extend('foo', $service); } /** * Provider for invalid service definitions */ public function badServiceDefinitionProvider() { return array( array(123), array(new NonInvokable()) ); } /** * Provider for service definitions */ public function serviceDefinitionProvider() { return array( array(function ($value) { $service = new Service(); $service->value = $value; return $service; }), array(new Invokable()) ); } } Pimple-1.1.1/tests/Pimple/Tests/Service.php000066400000000000000000000024271224361304500205500ustar00rootroot00000000000000 */ class Service { } Pimple-1.1.1/tests/bootstrap.php000066400000000000000000000006661224361304500166400ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once __DIR__.'/../lib/Pimple.php'; require_once __DIR__.'/Pimple/Tests/Service.php'; require_once __DIR__.'/Pimple/Tests/Invokable.php'; require_once __DIR__.'/Pimple/Tests/NonInvokable.php';