README.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. Pimple
  2. ======
  3. .. caution::
  4. This is the documentation for Pimple 3.x. If you are using Pimple 1.x, read
  5. the `Pimple 1.x documentation`_. Reading the Pimple 1.x code is also a good
  6. way to learn more about how to create a simple Dependency Injection
  7. Container (recent versions of Pimple are more focused on performance).
  8. Pimple is a small Dependency Injection Container for PHP.
  9. Installation
  10. ------------
  11. Before using Pimple in your project, add it to your ``composer.json`` file:
  12. .. code-block:: bash
  13. $ ./composer.phar require pimple/pimple ~3.0
  14. Alternatively, Pimple is also available as a PHP C extension:
  15. .. code-block:: bash
  16. $ git clone https://github.com/silexphp/Pimple
  17. $ cd Pimple/ext/pimple
  18. $ phpize
  19. $ ./configure
  20. $ make
  21. $ make install
  22. Usage
  23. -----
  24. Creating a container is a matter of creating a ``Container`` instance:
  25. .. code-block:: php
  26. use Pimple\Container;
  27. $container = new Container();
  28. As many other dependency injection containers, Pimple manages two different
  29. kind of data: **services** and **parameters**.
  30. Defining Services
  31. ~~~~~~~~~~~~~~~~~
  32. A service is an object that does something as part of a larger system. Examples
  33. of services: a database connection, a templating engine, or a mailer. Almost
  34. any **global** object can be a service.
  35. Services are defined by **anonymous functions** that return an instance of an
  36. object:
  37. .. code-block:: php
  38. // define some services
  39. $container['session_storage'] = function ($c) {
  40. return new SessionStorage('SESSION_ID');
  41. };
  42. $container['session'] = function ($c) {
  43. return new Session($c['session_storage']);
  44. };
  45. Notice that the anonymous function has access to the current container
  46. instance, allowing references to other services or parameters.
  47. As objects are only created when you get them, the order of the definitions
  48. does not matter.
  49. Using the defined services is also very easy:
  50. .. code-block:: php
  51. // get the session object
  52. $session = $container['session'];
  53. // the above call is roughly equivalent to the following code:
  54. // $storage = new SessionStorage('SESSION_ID');
  55. // $session = new Session($storage);
  56. Defining Factory Services
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~
  58. By default, each time you get a service, Pimple returns the **same instance**
  59. of it. If you want a different instance to be returned for all calls, wrap your
  60. anonymous function with the ``factory()`` method
  61. .. code-block:: php
  62. $container['session'] = $container->factory(function ($c) {
  63. return new Session($c['session_storage']);
  64. });
  65. Now, each call to ``$container['session']`` returns a new instance of the
  66. session.
  67. Defining Parameters
  68. ~~~~~~~~~~~~~~~~~~~
  69. Defining a parameter allows to ease the configuration of your container from
  70. the outside and to store global values:
  71. .. code-block:: php
  72. // define some parameters
  73. $container['cookie_name'] = 'SESSION_ID';
  74. $container['session_storage_class'] = 'SessionStorage';
  75. If you change the ``session_storage`` service definition like below:
  76. .. code-block:: php
  77. $container['session_storage'] = function ($c) {
  78. return new $c['session_storage_class']($c['cookie_name']);
  79. };
  80. You can now easily change the cookie name by overriding the
  81. ``session_storage_class`` parameter instead of redefining the service
  82. definition.
  83. Protecting Parameters
  84. ~~~~~~~~~~~~~~~~~~~~~
  85. Because Pimple sees anonymous functions as service definitions, you need to
  86. wrap anonymous functions with the ``protect()`` method to store them as
  87. parameters:
  88. .. code-block:: php
  89. $container['random_func'] = $container->protect(function () {
  90. return rand();
  91. });
  92. Modifying Services after Definition
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. In some cases you may want to modify a service definition after it has been
  95. defined. You can use the ``extend()`` method to define additional code to be
  96. run on your service just after it is created:
  97. .. code-block:: php
  98. $container['session_storage'] = function ($c) {
  99. return new $c['session_storage_class']($c['cookie_name']);
  100. };
  101. $container->extend('session_storage', function ($storage, $c) {
  102. $storage->...();
  103. return $storage;
  104. });
  105. The first argument is the name of the service to extend, the second a function
  106. that gets access to the object instance and the container.
  107. Extending a Container
  108. ~~~~~~~~~~~~~~~~~~~~~
  109. If you use the same libraries over and over, you might want to reuse some
  110. services from one project to the next one; package your services into a
  111. **provider** by implementing ``Pimple\ServiceProviderInterface``:
  112. .. code-block:: php
  113. use Pimple\Container;
  114. class FooProvider implements Pimple\ServiceProviderInterface
  115. {
  116. public function register(Container $pimple)
  117. {
  118. // register some services and parameters
  119. // on $pimple
  120. }
  121. }
  122. Then, register the provider on a Container:
  123. .. code-block:: php
  124. $pimple->register(new FooProvider());
  125. Fetching the Service Creation Function
  126. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127. When you access an object, Pimple automatically calls the anonymous function
  128. that you defined, which creates the service object for you. If you want to get
  129. raw access to this function, you can use the ``raw()`` method:
  130. .. code-block:: php
  131. $container['session'] = function ($c) {
  132. return new Session($c['session_storage']);
  133. };
  134. $sessionFunction = $container->raw('session');
  135. .. _Pimple 1.x documentation: https://github.com/silexphp/Pimple/tree/1.1