30
EVENT MANAGER

Making the most out of CakePHP 2.2

Embed Size (px)

Citation preview

Page 1: Making the most out of CakePHP 2.2

EVENT MANAGER

Page 2: Making the most out of CakePHP 2.2

PUBLISHER - SUBSCRIBERS

• Made the code we had for behaviors, helpers, components, etc. into a reusable generic events system

• The idea is simple: an object publishes a named event with some data and an unknown list of listeners will get notified and have the chance of changing the outcome of the original operation

Page 3: Making the most out of CakePHP 2.2

SOME BUILT-IN EVENTS

• Model.beforeValidate, Model.afterValidate, Model.beforeSave, Model.afterSave...

• Controller.initialize, Controller.startup, Controller.beforeRender...

• View.beforeRender, View.beforeLayout...

• Dispatcher.beforeDispatch, Dispatcher.afterDispatch

Page 4: Making the most out of CakePHP 2.2

CREATE YOUR OWN EVENTS!

• A Paypal plugin received a notification from payments server. What do you do? (You need to send emails, mark order s processed, decrement stock...)

• Trigger a new Paypal.afterProcess event!

• Any number of listeners can processes the rest of the actions for you while leaving the Paypal plugin free of any external concerns

Page 5: Making the most out of CakePHP 2.2

A SIMPLE EXAMPLE

Page 6: Making the most out of CakePHP 2.2

SUBSCRIPTION PATTERNS

• Direct: during runtime an object attaches event listeners to another as needed

• Configuration based: define a list or listeners. Read the list in the publisher class or factory when needed

• Indirect global: Use the global event manager to attach indirectly to any event.

• Convention based: load listeners for an object from a convention-defined location

Page 7: Making the most out of CakePHP 2.2

USING THE GLOBAL MANAGER

Page 8: Making the most out of CakePHP 2.2

DISPATCHER FILTERSLet’s jump directly to examples :)

Page 9: Making the most out of CakePHP 2.2

AN ASSET COMPRESSOR

Page 10: Making the most out of CakePHP 2.2

A RESPONSE CACHER

Page 11: Making the most out of CakePHP 2.2

SOME COOL IDEAS WITH FILTERS

• Complete reverse proxy

• Pluggable OAuth Authorization

• Performance monitoring

Page 12: Making the most out of CakePHP 2.2

HTTP CACHING

Page 13: Making the most out of CakePHP 2.2

TYPES OF CACHE

• Browser caches

• Proxy caches

• Gateway caches (or reverse proxy)

Page 14: Making the most out of CakePHP 2.2

HEADERS INVOLVED IN CACHING

• Cache-Control - This it the most important one

• Expires

• ETag

• Last-Modified

Page 15: Making the most out of CakePHP 2.2

MESSING WITH CACHE-CONTROL

Proxies use shared cache, so setting a response as private will not be cached by

default

Page 16: Making the most out of CakePHP 2.2

VALIDATION VS EXPIRATION

• Expiration model: you specify for how long a response is valid, new requests will not be made until time expires

• Validation: requests are made every time and server responds with either a 304 status or a new response.

Page 17: Making the most out of CakePHP 2.2

MESSING WITH EXPIRATION

Using max-age and s-maxage is preferred because of several limitations in the Expire header

Page 18: Making the most out of CakePHP 2.2

VALIDATION MODEL

If browser send If-Modified-Since header, then a 304response will be issued and view rendering process will be skipped entirely

Page 19: Making the most out of CakePHP 2.2

CONDITIONAL VIEW RENDER

Same as previous example, but automatic and awesome

Page 20: Making the most out of CakePHP 2.2

CACHE GROUPS

Page 21: Making the most out of CakePHP 2.2

WHAT ARE THEY?

• A declarative, static way of tagging cache entries with any string

• Entries tagged with the same string can be invalidated all at the same time in an atomic way

• A way to organize your cache entries into logical groups

Page 22: Making the most out of CakePHP 2.2

A SIMPLE EXAMPLE

Page 23: Making the most out of CakePHP 2.2

CACHE GROUPS FACTS

• Clearing all entries under a groups will delete all entries for all Cache configurations having the same groups in them and sharing the same prefix. Even across engines!

• You cannot dynamically add new groups to cache configurations, you need to plan your strategy

• You *could* dynamically create cache configs having exactly the groups you want to tag entries with, but it’s super hard

Page 24: Making the most out of CakePHP 2.2

MODEL VALIDATOR

Page 25: Making the most out of CakePHP 2.2

WHAT FOR?

• Some applications require dynamic validations rules depending on the action being executed

• Directly modifying $validate property on the model can be tricky

• Several parts of CakePHP had to implement the same $validates parsing to extract information out of it

Page 26: Making the most out of CakePHP 2.2

WHAT CAN I DO WITH IT?

• Dynamically inspect, add or remove validations rules for a model

• Swap at runtime validation objects depending on your own logic

• Create with an easy API complex validation systems

Page 27: Making the most out of CakePHP 2.2

ADDING NEW RULES

Page 28: Making the most out of CakePHP 2.2

MODIFYING RULES

Page 29: Making the most out of CakePHP 2.2

DELETING RULES

Page 30: Making the most out of CakePHP 2.2

USING ARRAY ACCESS