2017 © Pedro Peláez
 

symfony-bundle cookies-bundle

Cookie model for using cookies as Symfony services

image

ongr/cookies-bundle

Cookie model for using cookies as Symfony services

  • Wednesday, June 15, 2016
  • by saimaz
  • Repository
  • 19 Watchers
  • 4 Stars
  • 18,394 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 11 Forks
  • 1 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

CookiesBundle

Cookies bundle provides Symfony way to handle cookies defining them as services. This allows changing the values of the service and the bundle handles the actual creation and updates of cookies., (*1)

Build Status Coverage Status Latest Stable Version Scrutinizer Code Quality, (*2)

Install bundle

To install this bundle add it to composer., (*3)


composer require ongr/cookies-bundle

Then register it in AppKernel.php, (*4)


class AppKernel extends Kernel { public function registerBundles() { return [ // ... new ONGR\CookiesBundle\ONGRCookiesBundle(), ]; } // ... }

That's it - the bundle is ready for work., (*5)

Working with cookies

ONGR provides cookie model abstraction for working with cookie values in the request and response., (*6)

One can define a service:, (*7)


parameters: project.cookie.foo.name: cookie_foo project.cookie.foo.defaults: # Defaults section is optional http_only: false expires_interval: P5DT4H # 5 days and 4 hours services: project.cookie.foo: class: %ongr_cookie.json.class% arguments: [ %project.cookie.foo.name% ] calls: - [setDefaults, [%project.cookie.foo.defaults%]] # Optional tags: - { name: ongr_cookie.cookie }

Notice Cookie models' names of a cookie service should not contain dot symbol '.' and must be the same as cookie names that need to be modeled., (*8)

Such injected service allows accessing cookie value. If the value has been modified by your code, it will send new value back to the client browser. Manipulating cookie values with ONGR Cookies Bundle is very easy: you need to get the cookie model service and from it you can get the cookie value with getValue() method and set the value with setValue(mixed $value) method., (*9)


class CookieController { use ContainerAwareTrait; public function updateAction() { /** @var JsonCookie $cookie */ $cookie = $this->container->get('project.cookie.foo'); $cookie->setValue(['bar']); // Cookie has been marked as dirty and will be updated in the response. $cookie->setExpiresTime(2000000000); return new JsonResponse(); } }

Default values

Possible setDefaults keys (default values if unspecified):, (*10)

  • domain - string (null), (*11)

  • path - string ('/'), (*12)

  • http_only - boolean (true), (*13)

  • secure - boolean (false), (*14)

  • expires_time - integer (0), (*15)

  • expires_interval - DateInterval string (null), (*16)

These values are used to initialize the cookie model if cookie does not exist in client's browser., (*17)

Model types

Currently, there are these pre-configured classes one can use:, (*18)

  • %ongr_cookie.json.class% - one can work with it's value as it was a PHP array. In the background, value is encoded and decoded back using JSON format., (*19)

  • %ongr_cookie.generic.class% works with plain string data. Other cookie formats can be created by extending this class., (*20)

If a cookie with the same name, path and domain is added to the response object, it's value is not overwritten with the changed cookie model data., (*21)

To remove a cookie from the client browser, use $cookie->setClear(true). All other model values will be ignored., (*22)

Components

  1. CookieModelListener - event listener responsible for listening for kernel.request and kernel.response events;, (*23)

  2. CookieInjector - service doing the heavy lifting (gets and sets cookies)., (*24)

  3. Cookie Models all implementing CookieInterface via GenericCookie class. Basic cookie fields (domain, expires, etc.) are placed in CookieFieldsTrait trait. Cookie Models are responsible for loading the raw mixed data into an nice PHP object (implementation is customizable and may differ per-model) and returning cookie-izable raw data when the need arises., (*25)

How it works?

  1. Symfony receives a request. kernel.request event is fired. CookieModelListener is listening., (*26)

  2. CookieModelListener's onKernelRequest method is called, GetResponseEvent is passed to it. onKernelRequest calls CookieInjector's inject method., (*27)

  3. CookieInjector iterates through registered Cookie Models, gets raw data for each one and calls a Cookie Model's load method to load the data from the cookie., (*28)

  4. Now we have a nice cookie-based object available!, (*29)

  5. We do whatever we need to do,, (*30)

  6. Symfony prepares to return a response. kernel.response event is fired. Once again, CookieModelListener is listening., (*31)

  7. CookieModelListener 's onKernelResponse method is called, FilterResponseEvent is passed to it. onKernelResponse calls CookieInjector's update method., (*32)

  8. CookieInjector iterates through cookies to be sent to the client, "flattens" them, and then iterates through registered Cookie Models, calling toCookie method to get the data to be stored in the cookie. If Cookie Model's clear property is set to true, the cookie is cleared, otherwise it is saved., (*33)

  9. Our cookie is either saved and sent to the users' browser or cleared from it., (*34)

  10. Everyone is happy., (*35)

Cookie Models are described as any other Symfony service, with one significant difference: tag ongr_cookie.cookie is used to denote that the service is a Cookie Model. All services tagged with this tag are collected in a separate compiler pass and added to the ongr_cookie.injector service by appending addCookieModel call to its' definition., (*36)

Cookie models' names of a cookie service should not contain dot symbol '.' and must be the same as cookie names that need to be modeled., (*37)

License

This bundle is under the MIT license. Please, see the complete license in the bundle LICENSE file., (*38)

The Versions

15/06 2016

1.0.x-dev

1.0.9999999.9999999-dev http://ongr.io

Cookie model for using cookies as Symfony services

  Sources   Download

MIT

The Requires

 

The Development Requires

service cookie model symfony cookies ongr

06/06 2016

dev-master

9999999-dev http://ongr.io

Cookie model for using cookies as Symfony services

  Sources   Download

MIT

The Requires

 

The Development Requires

service cookie model symfony cookies ongr

06/06 2016

v1.0.0

1.0.0.0 http://ongr.io

Cookie model for using cookies as Symfony services

  Sources   Download

MIT

The Requires

 

The Development Requires

service cookie model symfony cookies ongr