Tree @lintian-fixes/main (Download .tar.gz)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | FIG Cookies =========== Managing Cookies for PSR-7 Requests and Responses. [![Latest Stable Version](https://poser.pugx.org/dflydev/fig-cookies/v/stable)](https://packagist.org/packages/dflydev/fig-cookies) [![Total Downloads](https://poser.pugx.org/dflydev/fig-cookies/downloads)](https://packagist.org/packages/dflydev/fig-cookies) [![Latest Unstable Version](https://poser.pugx.org/dflydev/fig-cookies/v/unstable)](https://packagist.org/packages/dflydev/fig-cookies) [![License](https://poser.pugx.org/dflydev/fig-cookies/license)](https://packagist.org/packages/dflydev/fig-cookies) <br> [![Build Status](https://travis-ci.org/dflydev/dflydev-fig-cookies.svg?branch=master)](https://travis-ci.org/dflydev/dflydev-fig-cookies) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/dflydev/dflydev-fig-cookies/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/dflydev/dflydev-fig-cookies/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/dflydev/dflydev-fig-cookies/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/dflydev/dflydev-fig-cookies/?branch=master) [![Code Climate](https://codeclimate.com/github/dflydev/dflydev-fig-cookies/badges/gpa.svg)](https://codeclimate.com/github/dflydev/dflydev-fig-cookies) <br> [![Join the chat at https://gitter.im/dflydev/dflydev-fig-cookies](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dflydev/dflydev-fig-cookies?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) Installation ------------ ```bash $> composer require dflydev/fig-cookies ``` Concepts -------- FIG Cookies tackles two problems, managing **Cookie** *Request* headers and managing **Set-Cookie** *Response* headers. It does this by way of introducing a `Cookies` class to manage collections of `Cookie` instances and a `SetCookies` class to manage collections of `SetCookie` instances. Instantiating these collections looks like this: ```php // Get a collection representing the cookies in the Cookie headers // of a PSR-7 Request. $cookies = Dflydev\FigCookies\Cookies::fromRequest($request); // Get a collection representing the cookies in the Set-Cookie headers // of a PSR-7 Response $setCookies = Dflydev\FigCookies\SetCookies::fromResponse($response); ``` After modifying these collections in some way, they are rendered into a PSR-7 Request or PSR-7 Response like this: ```php // Render the Cookie headers and add them to the headers of a // PSR-7 Request. $request = $cookies->renderIntoCookieHeader($request); // Render the Set-Cookie headers and add them to the headers of a // PSR-7 Response. $response = $setCookies->renderIntoSetCookieHeader($response); ``` Like PSR-7 Messages, `Cookie`, `Cookies`, `SetCookie`, and `SetCookies` are all represented as immutable value objects and all mutators will return new instances of the original with the requested changes. While this style of design has many benefits it can become fairly verbose very quickly. In order to get around that, FIG Cookies provides two facades in an attempt to help simply things and make the whole process less verbose. Basic Usage ----------- The easiest way to start working with FIG Cookies is by using the `FigRequestCookies` and `FigResponseCookies` classes. They are facades to the primitive FIG Cookies classes. Their jobs are to make common cookie related tasks easier and less verbose than working with the primitive classes directly. There is overhead on creating `Cookies` and `SetCookies` and rebuilding requests and responses. Each of the `FigCookies` methods will go through this process so be wary of using too many of these calls in the same section of code. In some cases it may be better to work with the primitive FIG Cookies classes directly rather than using the facades. ### Request Cookies Requests include cookie information in the **Cookie** request header. The cookies in this header are represented by the `Cookie` class. ```php use Dflydev\FigCookies\Cookie; $cookie = Cookie::create('theme', 'blue'); ``` To easily work with request cookies, use the `FigRequestCookies` facade. #### Get a Request Cookie The `get` method will return a `Cookie` instance. If no cookie by the specified name exists, the returned `Cookie` instance will have a `null` value. The optional third parameter to `get` sets the value that should be used if a cookie does not exist. ```php use Dflydev\FigCookies\FigRequestCookies; $cookie = FigRequestCookies::get($request, 'theme'); $cookie = FigRequestCookies::get($request, 'theme', 'default-theme'); ``` #### Set a Request Cookie The `set` method will either add a cookie or replace an existing cookie. The `Cookie` primitive is used as the second argument. ```php use Dflydev\FigCookies\FigRequestCookies; $request = FigRequestCookies::set($request, Cookie::create('theme', 'blue')); ``` #### Modify a Request Cookie The `modify` method allows for replacing the contents of a cookie based on the current cookie with the specified name. The third argument is a `callable` that takes a `Cookie` instance as its first argument and is expected to return a `Cookie` instance. If no cookie by the specified name exists, a new `Cookie` instance with a `null` value will be passed to the callable. ```php use Dflydev\FigCookies\FigRequestCookies; $modify = function (Cookie $cookie) { $value = $cookie->getValue(); // ... inspect current $value and determine if $value should // change or if it can stay the same. in all cases, a cookie // should be returned from this callback... return $cookie->withValue($value); } $request = FigRequestCookies::modify($request, 'theme', $modify); ``` #### Remove a Request Cookie The `remove` method removes a cookie if it exists. ```php use Dflydev\FigCookies\FigRequestCookies; $request = FigRequestCookies::remove($request, 'theme'); ``` Note that this does not cause the client to remove the cookie. Take a look at `FigResponseCookies::expire` to do that. ### Response Cookies Responses include cookie information in the **Set-Cookie** response header. The cookies in these headers are represented by the `SetCookie` class. ```php use Dflydev\FigCookies\Modifier\SameSite; use Dflydev\FigCookies\SetCookie; $setCookie = SetCookie::create('lu') ->withValue('Rg3vHJZnehYLjVg7qi3bZjzg') ->withExpires('Tue, 15-Jan-2013 21:47:38 GMT') ->withMaxAge(500) ->rememberForever() ->withPath('/') ->withDomain('.example.com') ->withSecure(true) ->withHttpOnly(true) ->withSameSite(SameSite::lax()) ; ``` To easily work with response cookies, use the `FigResponseCookies` facade. #### Get a Response Cookie The `get` method will return a `SetCookie` instance. If no cookie by the specified name exists, the returned `SetCookie` instance will have a `null` value. The optional third parameter to `get` sets the value that should be used if a cookie does not exist. ```php use Dflydev\FigCookies\FigResponseCookies; $setCookie = FigResponseCookies::get($response, 'theme'); $setCookie = FigResponseCookies::get($response, 'theme', 'simple'); ``` #### Set a Response Cookie The `set` method will either add a cookie or replace an existing cookie. The `SetCookie` primitive is used as the second argument. ```php use Dflydev\FigCookies\FigResponseCookies; $response = FigResponseCookies::set($response, SetCookie::create('token') ->withValue('a9s87dfz978a9') ->withDomain('example.com') ->withPath('/firewall') ); ``` #### Modify a Response Cookie The `modify` method allows for replacing the contents of a cookie based on the current cookie with the specified name. The third argument is a `callable` that takes a `SetCookie` instance as its first argument and is expected to return a `SetCookie` instance. If no cookie by the specified name exists, a new `SetCookie` instance with a `null` value will be passed to the callable. ```php use Dflydev\FigCookies\FigResponseCookies; $modify = function (SetCookie $setCookie) { $value = $setCookie->getValue(); // ... inspect current $value and determine if $value should // change or if it can stay the same. in all cases, a cookie // should be returned from this callback... return $setCookie ->withValue($newValue) ->withExpires($newExpires) ; } $response = FigResponseCookies::modify($response, 'theme', $modify); ``` #### Remove a Response Cookie The `remove` method removes a cookie from the response if it exists. ```php use Dflydev\FigCookies\FigResponseCookies; $response = FigResponseCookies::remove($response, 'theme'); ``` #### Expire a Response Cookie The `expire` method sets a cookie with an expiry date in the far past. This causes the client to remove the cookie. ```php use Dflydev\FigCookies\FigResponseCookies; $response = FigResponseCookies::expire($response, 'session_cookie'); ``` FAQ --- ### Do you call `setcookies`? No. Delivery of the rendered `SetCookie` instances is the responsibility of the PSR-7 client implementation. ### Do you do anything with sessions? No. It would be possible to build session handling using cookies on top of FIG Cookies but it is out of scope for this package. ### Do you read from `$_COOKIES`? No. FIG Cookies only pays attention to the `Cookie` headers on [PSR-7](https://packagist.org/packages/psr/http-message) Request instances. In the case of `ServerRequestInterface` instances, PSR-7 implementations should be including `$_COOKIES` values in the headers so in that case FIG Cookies may be interacting with `$_COOKIES` indirectly. License ------- MIT, see LICENSE. Community --------- Want to get involved? Here are a few ways: * Find us in the #dflydev IRC channel on irc.freenode.org. * Mention [@dflydev](https://twitter.com/dflydev) on Twitter. * [![Join the chat at https://gitter.im/dflydev/dflydev-fig-cookies](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dflydev/dflydev-fig-cookies?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
Commit History @lintian-fixes/main
0
»»
0
»»