Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Build a custom (micro)framework with ZF2 Components (as building blocks)
1. ZFDAY - 2014
BUILD A CUSTOM (MICRO)FRAMEWORK
WITH ZF2 COMPONENTS AS BUILDING BLOCKS
Author Walter Dal Mut / @walterdalmut
2. WHO AM I?
Walter Dal Mut, I work at Corley S.r.l.
and also at UpCloo Ltd.
You can contact me at: walter.dalmut@gmail.com
On Twitter: @walterdalmut
On GitHub: wdalmut
3. WHY A CUSTOM FRAMEWORK?
Tipically because we want to solve a particular problem.
ZendFramework is a general purpose Web Framework that
can fit different problems easily.
4. IT SOUNDS LIKE REINVENTING THE WHEEL
NO, WE DON'T WANT!
We will use ZF2 components as building blocks
We will drive a personal behaviour
11. LET'S GO TO SCHOOL
We have to learn our building blocks!
12. THE EVENT MANAGER
$vnMngr=nwZnvnMngrEetaae(;
eetaae
e edEetaaevnMngr)
/ Tigra eet
/ rge n vn
pbi fnto tigr$vn,$agt=nl,$rv=nl)
ulc ucin rge(eet tre
ul ag
ul;
/ Atc alsee t a eet
/ tah
itnr o n vn
pbi fnto atc(eet $alak $roiy;
ulc ucin tah$vn, clbc, pirt)
There are many other methods (detach, triggerUntil, etc...)
See it on Github
13. HOW EVENT MANAGER WORKS?
pbi fnto tsEetaae(
ulc ucin etvnMngr)
{
$v =nwEetaae(;
e
e vnMngr)
$sald=fle
iCle
as;
$v>tah'neet,fnto(eet ue($sald {
e-atc(a-vn' ucin$vn) s &iCle)
$sald=tu;
iCle
re
};
)
$v>rge(a-vn";
e-tigr"neet)
}
$hs>setre$sald;
ti-asrTu(iCle)
14. THE SERVICE LOCATOR
$evcMngr=nwZnevcMngrSrieaae(;
srieaae
e edSrieaaeevcMngr)
"evcs = [
srie" >
"noals = [
ivkbe" >
"oraeop = "oraeop,
YuNmCm" > YuNmCm"
]
,
"atre"= [
fcois >
"oraeevc"= "oraeevcFcoy,
YuNmSrie > YuNmSrieatr"
]
,
"lae"= [
aiss >
"orais = "oraeevc"
yu.la" > YuNmSrie,
]
]
See it on Github
24. THE FRAMEWORK EVENTS LIST
BEGIN
The loop begins
ROUTE
Parse the actual route in order to found a dispatchable action
PRE.FETCH
Before dispatch the action
EXECUTE
Dispatch all actions
RENDER
Render your data
FINISH
The loop ends
33. HEY, CAN YOU DO IT?
poetdfnto dsacUeAtoRltdvns)
rtce ucin ipthsrcineaeEet(
{
ty{
r
$vnsit=$hs>ipthsreus(;
eetLs
ti-dsacUeRqet)
}cth(atxeto $){
ac HlEcpin e
$vnsit=$hs>rge(hl";
eetLs
ti-tigr"at)
}cth(aeoFudxeto $){
ac PgNtonEcpin e
$hs>epne)>eSauCd(0)
ti-rsos(-stttsoe44;
$vnsit=$hs>rge(44)
eetLs
ti-tigr"0";
}cth(Ecpin$){
ac xeto e
$hs>epne)>eSauCd(0)
ti-rsos(-stttsoe50;
$vnsit=$hs>rge(50)
eetLs
ti-tigr"0";
}
}
rtr $vnsit
eun eetLs;
34. JUST TRY IT!
poetdfnto dsacUeRqet)
rtce ucin ipthsreus(
{
$vit=$hs>rge(rue,[rqet = $hs>eus(];
eLs
ti-tigr"ot" "eus" > ti-rqet))
$otMth=$vit>at)
rueac
eLs-ls(;
i (ul==$otMth {
f nl = rueac)
trwnwPgNtonEcpin"aentfud";
ho e aeoFudxeto(Pg o on!)
}
$hs>epne)>eSauCd(0)
ti-rsos(-stttsoe20;
$hs>rge(pefth,[rueac"= $otMth)
ti-tigr"r.ec" "otMth > rueac];
$otolr =$hs>vns)>rge(eeue,$otMth;
cnrles
ti-eet(-tigr"xct" rueac)
}
rtr $otolr;
eun cnrles
Why the "execute" trigger is different?
35. NOW IT WORKS!
pbi fnto tsBsAplw)
ulc ucin etaepFo(
{
$eus =Rqetatr:cetRqet"apg" "E";
rqet
eusFcoy:raeeus(/-ae, GT)
$p =nwAp)
ap
e p(;
$p-steus(rqet;
ap>eRqet$eus)
$p-stevcMngrnwZnevcMngrSrieaae()
ap>eSrieaae(e edSrieaaeevcMngr);
$p-stvnMngrnwZnvnMngrEetaae()
ap>eEetaae(e edEetaaevnMngr);
$epne=$p-rn)>epne)
rsos
ap>u(-rsos(;
}
$hs>setqas44 $epne>eSauCd()
ti-asrEul(0, rsos-gtttsoe);
.
O ( tss 1asrin)
K 1 et,
setos
See it on Github Gist
36. EASY TO MOCK!
pbi fnto tsORsosORueac(
ulc ucin etkepnenotMth)
{
$p =nwAp)
ap
e p(;
$vnMngr=nwZnvnMngrEetaae(;
eetaae
e edEetaaevnMngr)
$vnmngr>tah"ot" fnto( {
eetaae-atc(rue, ucin)
rtr nwRueac(]
eun e otMth[)
};
)
$p-stvnMngr$vnMngr;
ap>eEetaae(eetaae)
$p-stevcMngrnwZnevcMngrSrieaae()
ap>eSrieaae(e edSrieaaeevcMngr);
$epne=$p-rn)>epne)
rsos
ap>u(-rsos(;
}
$hs>setqas20 $epne>eSauCd()
ti-asrEul(0, rsos-gtttsoe);
.
.
O ( tss 2asrin)
K 2 et,
setos
37. WE NEED A BOOTSTRAPPER!
Or better, something that can prepare the event manager and
the service manager from a default configuration!
45. BUT, POPOS ARE TOO SIMPLE
We want to use other services into actions!
pbi fnto tecin)
ulc ucin hAto(
{
/Ine asriehr!
/ ed
evc ee
$yevc =$hs>evcs)>e(m.evc";
mSrie
ti-srie(-gt"ysrie)
}
/uei..
/s t.
But how to do that?
53. EMIT!
casEi
ls mt
{
pbi fnto sn(eet
ulc ucin ed$vn)
{
$agt=$vn-gtagt)
tre
eet>eTre(;
}
}
$agt>epne)>ed)
tre-rsos(-sn(;
/Ap
/p
$hs>vns)>tah"iih,[eitr "ed];
ti-eet(-atc(fns" $mte, sn")
We can remove it or mock it out during testing!
55. AT LEAST 3 RESPONSIBILITIES
BUT EFFECTIVELY MORE...
Configure the application
Prepare the application
Run the application
Single Responsibility Principle