README.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Pimple
  2. ======
  3. Pimple is a small Dependency Injection Container for PHP 5.3 that consists
  4. of just one file and one class (about 80 lines of code).
  5. `Download it`_, require it in your code, and you're good to go::
  6. require_once '/path/to/Pimple.php';
  7. Creating a container is a matter of instating the ``Pimple`` class::
  8. $container = new Pimple();
  9. As many other dependency injection containers, Pimple is able to manage two
  10. different kind of data: *services* and *parameters*.
  11. Defining Parameters
  12. -------------------
  13. Defining a parameter is as simple as using the Pimple instance as an array::
  14. // define some parameters
  15. $container['cookie_name'] = 'SESSION_ID';
  16. $container['session_storage_class'] = 'SessionStorage';
  17. Defining Services
  18. -----------------
  19. A service is an object that does something as part of a larger system.
  20. Examples of services: Database connection, templating engine, mailer. Almost
  21. any object could be a service.
  22. Services are defined by anonymous functions that return an instance of an
  23. object::
  24. // define some services
  25. $container['session_storage'] = function ($c) {
  26. return new $c['session_storage_class']($c['cookie_name']);
  27. };
  28. $container['session'] = function ($c) {
  29. return new Session($c['session_storage']);
  30. };
  31. Notice that the anonymous function has access to the current container
  32. instance, allowing references to other services or parameters.
  33. As objects are only created when you get them, the order of the definitions
  34. does not matter, and there is no performance penalty.
  35. Using the defined services is also very easy::
  36. // get the session object
  37. $session = $container['session'];
  38. // the above call is roughly equivalent to the following code:
  39. // $storage = new SessionStorage('SESSION_ID');
  40. // $session = new Session($storage);
  41. Defining Shared Services
  42. ------------------------
  43. By default, each time you get a service, Pimple returns a new instance of it.
  44. If you want the same instance to be returned for all calls, wrap your
  45. anonymous function with the ``share()`` method::
  46. $container['session'] = $container->share(function ($c) {
  47. return new Session($c['session_storage']);
  48. });
  49. Protecting Parameters
  50. ---------------------
  51. Because Pimple sees anonymous functions as service definitions, you need to
  52. wrap anonymous functions with the ``protect()`` method to store them as
  53. parameter::
  54. $container['random'] = $container->protect(function () { return rand(); });
  55. Modifying services after creation
  56. ---------------------------------
  57. In some cases you may want to modify a service definition after it has been
  58. defined. You can use the ``extend()`` method to define additional code to
  59. be run on your service just after it is created::
  60. $container['mail'] = function ($c) {
  61. return new \Zend_Mail();
  62. };
  63. $container['mail'] = $container->extend('mail', function($mail, $c) {
  64. $mail->setFrom($c['mail.default_from']);
  65. return $mail;
  66. });
  67. The first argument is the name of the object, the second is a function that
  68. gets access to the object instance and the container. The return value is
  69. a service definition, so you need to re-assign it on the container.
  70. If the service you plan to extend is already shared, it's recommended that you
  71. re-wrap your extended service with the ``shared`` method, otherwise your extension
  72. code will be called every time you access the service::
  73. $container['twig'] = $container->share(function ($c) {
  74. return new Twig_Environment($c['twig.loader'], $c['twig.options']);
  75. });
  76. $container['twig'] = $container->share($container->extend('twig', function ($twig, $c) {
  77. $twig->addExtension(new MyTwigExtension());
  78. return $twig;
  79. }));
  80. Fetching the service creation function
  81. --------------------------------------
  82. When you access an object, Pimple automatically calls the anonymous function
  83. that you defined, which creates the service object for you. If you want to get
  84. raw access to this function, you can use the ``raw()`` method::
  85. $container['session'] = $container->share(function ($c) {
  86. return new Session($c['session_storage']);
  87. });
  88. $sessionFunction = $container->raw('session');
  89. Packaging a Container for reusability
  90. -------------------------------------
  91. If you use the same libraries over and over, you might want to create reusable
  92. containers. Creating a reusable container is as simple as creating a class
  93. that extends ``Pimple``, and configuring it in the constructor::
  94. class SomeContainer extends Pimple
  95. {
  96. public function __construct()
  97. {
  98. $this['parameter'] = 'foo';
  99. $this['object'] = function () { return stdClass(); };
  100. }
  101. }
  102. Using this container from your own is as easy as it can get::
  103. $container = new Pimple();
  104. // define your project parameters and services
  105. // ...
  106. // embed the SomeContainer container
  107. $container['embedded'] = $container->share(function () { return new SomeContainer(); });
  108. // configure it
  109. $container['embedded']['parameter'] = 'bar';
  110. // use it
  111. $container['embedded']['object']->...;
  112. .. _Download it: https://github.com/fabpot/Pimple