api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

api.php 244KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465
  1. <?php
  2. /**
  3. * PHP-CRUD-API v2 License: MIT
  4. * Maurits van der Schee: maurits@vdschee.nl
  5. * https://github.com/mevdschee/php-crud-api
  6. **/
  7. namespace Tqdev\PhpCrudApi;
  8. // file: src/Psr/Http/Message/MessageInterface.php
  9. interface MessageInterface
  10. {
  11. public function getProtocolVersion();
  12. public function withProtocolVersion($version);
  13. public function getHeaders();
  14. public function hasHeader($name);
  15. public function getHeader($name);
  16. public function getHeaderLine($name);
  17. public function withHeader($name, $value);
  18. public function withAddedHeader($name, $value);
  19. public function withoutHeader($name);
  20. public function getBody();
  21. public function withBody(StreamInterface $body);
  22. }
  23. // file: src/Psr/Http/Message/RequestFactoryInterface.php
  24. interface RequestFactoryInterface
  25. {
  26. public function createRequest(string $method, $uri): RequestInterface;
  27. }
  28. // file: src/Psr/Http/Message/RequestInterface.php
  29. interface RequestInterface extends MessageInterface
  30. {
  31. public function getRequestTarget();
  32. public function withRequestTarget($requestTarget);
  33. public function getMethod();
  34. public function withMethod($method);
  35. public function getUri();
  36. public function withUri(UriInterface $uri, $preserveHost = false);
  37. }
  38. // file: src/Psr/Http/Message/ResponseFactoryInterface.php
  39. interface ResponseFactoryInterface
  40. {
  41. public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface;
  42. }
  43. // file: src/Psr/Http/Message/ResponseInterface.php
  44. interface ResponseInterface extends MessageInterface
  45. {
  46. public function getStatusCode();
  47. public function withStatus($code, $reasonPhrase = '');
  48. public function getReasonPhrase();
  49. }
  50. // file: src/Psr/Http/Message/ServerRequestFactoryInterface.php
  51. interface ServerRequestFactoryInterface
  52. {
  53. public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface;
  54. }
  55. // file: src/Psr/Http/Message/ServerRequestInterface.php
  56. interface ServerRequestInterface extends RequestInterface
  57. {
  58. public function getServerParams();
  59. public function getCookieParams();
  60. public function withCookieParams(array $cookies);
  61. public function getQueryParams();
  62. public function withQueryParams(array $query);
  63. public function getUploadedFiles();
  64. public function withUploadedFiles(array $uploadedFiles);
  65. public function getParsedBody();
  66. public function withParsedBody($data);
  67. public function getAttributes();
  68. public function getAttribute($name, $default = null);
  69. public function withAttribute($name, $value);
  70. public function withoutAttribute($name);
  71. }
  72. // file: src/Psr/Http/Message/StreamFactoryInterface.php
  73. interface StreamFactoryInterface
  74. {
  75. public function createStream(string $content = ''): StreamInterface;
  76. public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface;
  77. public function createStreamFromResource($resource): StreamInterface;
  78. }
  79. // file: src/Psr/Http/Message/StreamInterface.php
  80. interface StreamInterface
  81. {
  82. public function __toString();
  83. public function close();
  84. public function detach();
  85. public function getSize();
  86. public function tell();
  87. public function eof();
  88. public function isSeekable();
  89. public function seek($offset, $whence = SEEK_SET);
  90. public function rewind();
  91. public function isWritable();
  92. public function write($string);
  93. public function isReadable();
  94. public function read($length);
  95. public function getContents();
  96. public function getMetadata($key = null);
  97. }
  98. // file: src/Psr/Http/Message/UploadedFileFactoryInterface.php
  99. interface UploadedFileFactoryInterface
  100. {
  101. public function createUploadedFile(
  102. StreamInterface $stream,
  103. int $size = null,
  104. int $error = \UPLOAD_ERR_OK,
  105. string $clientFilename = null,
  106. string $clientMediaType = null
  107. ): UploadedFileInterface;
  108. }
  109. // file: src/Psr/Http/Message/UploadedFileInterface.php
  110. interface UploadedFileInterface
  111. {
  112. public function getStream();
  113. public function moveTo($targetPath);
  114. public function getSize();
  115. public function getError();
  116. public function getClientFilename();
  117. public function getClientMediaType();
  118. }
  119. // file: src/Psr/Http/Message/UriFactoryInterface.php
  120. interface UriFactoryInterface
  121. {
  122. public function createUri(string $uri = ''): UriInterface;
  123. }
  124. // file: src/Psr/Http/Message/UriInterface.php
  125. interface UriInterface
  126. {
  127. public function getScheme();
  128. public function getAuthority();
  129. public function getUserInfo();
  130. public function getHost();
  131. public function getPort();
  132. public function getPath();
  133. public function getQuery();
  134. public function getFragment();
  135. public function withScheme($scheme);
  136. public function withUserInfo($user, $password = null);
  137. public function withHost($host);
  138. public function withPort($port);
  139. public function withPath($path);
  140. public function withQuery($query);
  141. public function withFragment($fragment);
  142. public function __toString();
  143. }
  144. // file: src/Psr/Http/Server/MiddlewareInterface.php
  145. interface MiddlewareInterface
  146. {
  147. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface;
  148. }
  149. // file: src/Psr/Http/Server/RequestHandlerInterface.php
  150. interface RequestHandlerInterface
  151. {
  152. public function handle(ServerRequestInterface $request): ResponseInterface;
  153. }
  154. // file: src/Nyholm/Psr7/Factory/Psr17Factory.php
  155. final class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
  156. {
  157. public function createRequest(string $method, $uri): RequestInterface
  158. {
  159. return new Request($method, $uri);
  160. }
  161. public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
  162. {
  163. return new Response($code, [], null, '1.1', $reasonPhrase);
  164. }
  165. public function createStream(string $content = ''): StreamInterface
  166. {
  167. return Stream::create($content);
  168. }
  169. public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
  170. {
  171. $resource = @\fopen($filename, $mode);
  172. if (false === $resource) {
  173. if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
  174. throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
  175. }
  176. throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
  177. }
  178. return Stream::create($resource);
  179. }
  180. public function createStreamFromResource($resource): StreamInterface
  181. {
  182. return Stream::create($resource);
  183. }
  184. public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface
  185. {
  186. if (null === $size) {
  187. $size = $stream->getSize();
  188. }
  189. return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
  190. }
  191. public function createUri(string $uri = ''): UriInterface
  192. {
  193. return new Uri($uri);
  194. }
  195. public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
  196. {
  197. return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
  198. }
  199. }
  200. // file: src/Nyholm/Psr7/MessageTrait.php
  201. trait MessageTrait
  202. {
  203. private $headers = [];
  204. private $headerNames = [];
  205. private $protocol = '1.1';
  206. private $stream;
  207. public function getProtocolVersion(): string
  208. {
  209. return $this->protocol;
  210. }
  211. public function withProtocolVersion($version): self
  212. {
  213. if ($this->protocol === $version) {
  214. return $this;
  215. }
  216. $new = clone $this;
  217. $new->protocol = $version;
  218. return $new;
  219. }
  220. public function getHeaders(): array
  221. {
  222. return $this->headers;
  223. }
  224. public function hasHeader($header): bool
  225. {
  226. return isset($this->headerNames[\strtolower($header)]);
  227. }
  228. public function getHeader($header): array
  229. {
  230. $header = \strtolower($header);
  231. if (!isset($this->headerNames[$header])) {
  232. return [];
  233. }
  234. $header = $this->headerNames[$header];
  235. return $this->headers[$header];
  236. }
  237. public function getHeaderLine($header): string
  238. {
  239. return \implode(', ', $this->getHeader($header));
  240. }
  241. public function withHeader($header, $value): self
  242. {
  243. $value = $this->validateAndTrimHeader($header, $value);
  244. $normalized = \strtolower($header);
  245. $new = clone $this;
  246. if (isset($new->headerNames[$normalized])) {
  247. unset($new->headers[$new->headerNames[$normalized]]);
  248. }
  249. $new->headerNames[$normalized] = $header;
  250. $new->headers[$header] = $value;
  251. return $new;
  252. }
  253. public function withAddedHeader($header, $value): self
  254. {
  255. if (!\is_string($header) || '' === $header) {
  256. throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string.');
  257. }
  258. $new = clone $this;
  259. $new->setHeaders([$header => $value]);
  260. return $new;
  261. }
  262. public function withoutHeader($header): self
  263. {
  264. $normalized = \strtolower($header);
  265. if (!isset($this->headerNames[$normalized])) {
  266. return $this;
  267. }
  268. $header = $this->headerNames[$normalized];
  269. $new = clone $this;
  270. unset($new->headers[$header], $new->headerNames[$normalized]);
  271. return $new;
  272. }
  273. public function getBody(): StreamInterface
  274. {
  275. if (null === $this->stream) {
  276. $this->stream = Stream::create('');
  277. }
  278. return $this->stream;
  279. }
  280. public function withBody(StreamInterface $body): self
  281. {
  282. if ($body === $this->stream) {
  283. return $this;
  284. }
  285. $new = clone $this;
  286. $new->stream = $body;
  287. return $new;
  288. }
  289. private function setHeaders(array $headers): void
  290. {
  291. foreach ($headers as $header => $value) {
  292. $value = $this->validateAndTrimHeader($header, $value);
  293. $normalized = \strtolower($header);
  294. if (isset($this->headerNames[$normalized])) {
  295. $header = $this->headerNames[$normalized];
  296. $this->headers[$header] = \array_merge($this->headers[$header], $value);
  297. } else {
  298. $this->headerNames[$normalized] = $header;
  299. $this->headers[$header] = $value;
  300. }
  301. }
  302. }
  303. private function validateAndTrimHeader($header, $values): array
  304. {
  305. if (!\is_string($header) || 1 !== \preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header)) {
  306. throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string.');
  307. }
  308. if (!\is_array($values)) {
  309. if ((!\is_numeric($values) && !\is_string($values)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $values)) {
  310. throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings.');
  311. }
  312. return [\trim((string) $values, " \t")];
  313. }
  314. if (empty($values)) {
  315. throw new \InvalidArgumentException('Header values must be a string or an array of strings, empty array given.');
  316. }
  317. $returnValues = [];
  318. foreach ($values as $v) {
  319. if ((!\is_numeric($v) && !\is_string($v)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $v)) {
  320. throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings.');
  321. }
  322. $returnValues[] = \trim((string) $v, " \t");
  323. }
  324. return $returnValues;
  325. }
  326. }
  327. // file: src/Nyholm/Psr7/Request.php
  328. final class Request implements RequestInterface
  329. {
  330. use MessageTrait;
  331. use RequestTrait;
  332. public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1')
  333. {
  334. if (!($uri instanceof UriInterface)) {
  335. $uri = new Uri($uri);
  336. }
  337. $this->method = $method;
  338. $this->uri = $uri;
  339. $this->setHeaders($headers);
  340. $this->protocol = $version;
  341. if (!$this->hasHeader('Host')) {
  342. $this->updateHostFromUri();
  343. }
  344. if ('' !== $body && null !== $body) {
  345. $this->stream = Stream::create($body);
  346. }
  347. }
  348. }
  349. // file: src/Nyholm/Psr7/RequestTrait.php
  350. trait RequestTrait
  351. {
  352. private $method;
  353. private $requestTarget;
  354. private $uri;
  355. public function getRequestTarget(): string
  356. {
  357. if (null !== $this->requestTarget) {
  358. return $this->requestTarget;
  359. }
  360. if ('' === $target = $this->uri->getPath()) {
  361. $target = '/';
  362. }
  363. if ('' !== $this->uri->getQuery()) {
  364. $target .= '?' . $this->uri->getQuery();
  365. }
  366. return $target;
  367. }
  368. public function withRequestTarget($requestTarget): self
  369. {
  370. if (\preg_match('#\s#', $requestTarget)) {
  371. throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
  372. }
  373. $new = clone $this;
  374. $new->requestTarget = $requestTarget;
  375. return $new;
  376. }
  377. public function getMethod(): string
  378. {
  379. return $this->method;
  380. }
  381. public function withMethod($method): self
  382. {
  383. if (!\is_string($method)) {
  384. throw new \InvalidArgumentException('Method must be a string');
  385. }
  386. $new = clone $this;
  387. $new->method = $method;
  388. return $new;
  389. }
  390. public function getUri(): UriInterface
  391. {
  392. return $this->uri;
  393. }
  394. public function withUri(UriInterface $uri, $preserveHost = false): self
  395. {
  396. if ($uri === $this->uri) {
  397. return $this;
  398. }
  399. $new = clone $this;
  400. $new->uri = $uri;
  401. if (!$preserveHost || !$this->hasHeader('Host')) {
  402. $new->updateHostFromUri();
  403. }
  404. return $new;
  405. }
  406. private function updateHostFromUri(): void
  407. {
  408. if ('' === $host = $this->uri->getHost()) {
  409. return;
  410. }
  411. if (null !== ($port = $this->uri->getPort())) {
  412. $host .= ':' . $port;
  413. }
  414. if (isset($this->headerNames['host'])) {
  415. $header = $this->headerNames['host'];
  416. } else {
  417. $this->headerNames['host'] = $header = 'Host';
  418. }
  419. $this->headers = [$header => [$host]] + $this->headers;
  420. }
  421. }
  422. // file: src/Nyholm/Psr7/Response.php
  423. final class Response implements ResponseInterface
  424. {
  425. use MessageTrait;
  426. private const PHRASES = [
  427. 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing',
  428. 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-status', 208 => 'Already Reported',
  429. 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => 'Switch Proxy', 307 => 'Temporary Redirect',
  430. 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a teapot', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 425 => 'Unordered Collection', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 451 => 'Unavailable For Legal Reasons',
  431. 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 511 => 'Network Authentication Required',
  432. ];
  433. private $reasonPhrase = '';
  434. private $statusCode;
  435. public function __construct(int $status = 200, array $headers = [], $body = null, string $version = '1.1', string $reason = null)
  436. {
  437. if ('' !== $body && null !== $body) {
  438. $this->stream = Stream::create($body);
  439. }
  440. $this->statusCode = $status;
  441. $this->setHeaders($headers);
  442. if (null === $reason && isset(self::PHRASES[$this->statusCode])) {
  443. $this->reasonPhrase = self::PHRASES[$status];
  444. } else {
  445. $this->reasonPhrase = $reason;
  446. }
  447. $this->protocol = $version;
  448. }
  449. public function getStatusCode(): int
  450. {
  451. return $this->statusCode;
  452. }
  453. public function getReasonPhrase(): string
  454. {
  455. return $this->reasonPhrase;
  456. }
  457. public function withStatus($code, $reasonPhrase = ''): self
  458. {
  459. if (!\is_int($code) && !\is_string($code)) {
  460. throw new \InvalidArgumentException('Status code has to be an integer');
  461. }
  462. $code = (int) $code;
  463. if ($code < 100 || $code > 599) {
  464. throw new \InvalidArgumentException('Status code has to be an integer between 100 and 599');
  465. }
  466. $new = clone $this;
  467. $new->statusCode = $code;
  468. if ((null === $reasonPhrase || '' === $reasonPhrase) && isset(self::PHRASES[$new->statusCode])) {
  469. $reasonPhrase = self::PHRASES[$new->statusCode];
  470. }
  471. $new->reasonPhrase = $reasonPhrase;
  472. return $new;
  473. }
  474. }
  475. // file: src/Nyholm/Psr7/ServerRequest.php
  476. final class ServerRequest implements ServerRequestInterface
  477. {
  478. use MessageTrait;
  479. use RequestTrait;
  480. private $attributes = [];
  481. private $cookieParams = [];
  482. private $parsedBody;
  483. private $queryParams = [];
  484. private $serverParams;
  485. private $uploadedFiles = [];
  486. public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = [])
  487. {
  488. $this->serverParams = $serverParams;
  489. if (!($uri instanceof UriInterface)) {
  490. $uri = new Uri($uri);
  491. }
  492. $this->method = $method;
  493. $this->uri = $uri;
  494. $this->setHeaders($headers);
  495. $this->protocol = $version;
  496. if (!$this->hasHeader('Host')) {
  497. $this->updateHostFromUri();
  498. }
  499. if ('' !== $body && null !== $body) {
  500. $this->stream = Stream::create($body);
  501. }
  502. }
  503. public function getServerParams(): array
  504. {
  505. return $this->serverParams;
  506. }
  507. public function getUploadedFiles(): array
  508. {
  509. return $this->uploadedFiles;
  510. }
  511. public function withUploadedFiles(array $uploadedFiles)
  512. {
  513. $new = clone $this;
  514. $new->uploadedFiles = $uploadedFiles;
  515. return $new;
  516. }
  517. public function getCookieParams(): array
  518. {
  519. return $this->cookieParams;
  520. }
  521. public function withCookieParams(array $cookies)
  522. {
  523. $new = clone $this;
  524. $new->cookieParams = $cookies;
  525. return $new;
  526. }
  527. public function getQueryParams(): array
  528. {
  529. return $this->queryParams;
  530. }
  531. public function withQueryParams(array $query)
  532. {
  533. $new = clone $this;
  534. $new->queryParams = $query;
  535. return $new;
  536. }
  537. public function getParsedBody()
  538. {
  539. return $this->parsedBody;
  540. }
  541. public function withParsedBody($data)
  542. {
  543. if (!\is_array($data) && !\is_object($data) && null !== $data) {
  544. throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
  545. }
  546. $new = clone $this;
  547. $new->parsedBody = $data;
  548. return $new;
  549. }
  550. public function getAttributes(): array
  551. {
  552. return $this->attributes;
  553. }
  554. public function getAttribute($attribute, $default = null)
  555. {
  556. if (false === \array_key_exists($attribute, $this->attributes)) {
  557. return $default;
  558. }
  559. return $this->attributes[$attribute];
  560. }
  561. public function withAttribute($attribute, $value): self
  562. {
  563. $new = clone $this;
  564. $new->attributes[$attribute] = $value;
  565. return $new;
  566. }
  567. public function withoutAttribute($attribute): self
  568. {
  569. if (false === \array_key_exists($attribute, $this->attributes)) {
  570. return $this;
  571. }
  572. $new = clone $this;
  573. unset($new->attributes[$attribute]);
  574. return $new;
  575. }
  576. }
  577. // file: src/Nyholm/Psr7/Stream.php
  578. final class Stream implements StreamInterface
  579. {
  580. private $stream;
  581. private $seekable;
  582. private $readable;
  583. private $writable;
  584. private $uri;
  585. private $size;
  586. private const READ_WRITE_HASH = [
  587. 'read' => [
  588. 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
  589. 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
  590. 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,
  591. 'x+t' => true, 'c+t' => true, 'a+' => true,
  592. ],
  593. 'write' => [
  594. 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,
  595. 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,
  596. 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,
  597. 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true,
  598. ],
  599. ];
  600. private function __construct()
  601. {
  602. }
  603. public static function create($body = ''): StreamInterface
  604. {
  605. if ($body instanceof StreamInterface) {
  606. return $body;
  607. }
  608. if (\is_string($body)) {
  609. $resource = \fopen('php://temp', 'rw+');
  610. \fwrite($resource, $body);
  611. $body = $resource;
  612. }
  613. if (\is_resource($body)) {
  614. $new = new self();
  615. $new->stream = $body;
  616. $meta = \stream_get_meta_data($new->stream);
  617. $new->seekable = $meta['seekable'];
  618. $new->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]);
  619. $new->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]);
  620. $new->uri = $new->getMetadata('uri');
  621. return $new;
  622. }
  623. throw new \InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.');
  624. }
  625. public function __destruct()
  626. {
  627. $this->close();
  628. }
  629. public function __toString(): string
  630. {
  631. try {
  632. if ($this->isSeekable()) {
  633. $this->seek(0);
  634. }
  635. return $this->getContents();
  636. } catch (\Exception $e) {
  637. return '';
  638. }
  639. }
  640. public function close(): void
  641. {
  642. if (isset($this->stream)) {
  643. if (\is_resource($this->stream)) {
  644. \fclose($this->stream);
  645. }
  646. $this->detach();
  647. }
  648. }
  649. public function detach()
  650. {
  651. if (!isset($this->stream)) {
  652. return null;
  653. }
  654. $result = $this->stream;
  655. unset($this->stream);
  656. $this->size = $this->uri = null;
  657. $this->readable = $this->writable = $this->seekable = false;
  658. return $result;
  659. }
  660. public function getSize(): ?int
  661. {
  662. if (null !== $this->size) {
  663. return $this->size;
  664. }
  665. if (!isset($this->stream)) {
  666. return null;
  667. }
  668. if ($this->uri) {
  669. \clearstatcache(true, $this->uri);
  670. }
  671. $stats = \fstat($this->stream);
  672. if (isset($stats['size'])) {
  673. $this->size = $stats['size'];
  674. return $this->size;
  675. }
  676. return null;
  677. }
  678. public function tell(): int
  679. {
  680. if (false === $result = \ftell($this->stream)) {
  681. throw new \RuntimeException('Unable to determine stream position');
  682. }
  683. return $result;
  684. }
  685. public function eof(): bool
  686. {
  687. return !$this->stream || \feof($this->stream);
  688. }
  689. public function isSeekable(): bool
  690. {
  691. return $this->seekable;
  692. }
  693. public function seek($offset, $whence = \SEEK_SET): void
  694. {
  695. if (!$this->seekable) {
  696. throw new \RuntimeException('Stream is not seekable');
  697. }
  698. if (-1 === \fseek($this->stream, $offset, $whence)) {
  699. throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true));
  700. }
  701. }
  702. public function rewind(): void
  703. {
  704. $this->seek(0);
  705. }
  706. public function isWritable(): bool
  707. {
  708. return $this->writable;
  709. }
  710. public function write($string): int
  711. {
  712. if (!$this->writable) {
  713. throw new \RuntimeException('Cannot write to a non-writable stream');
  714. }
  715. $this->size = null;
  716. if (false === $result = \fwrite($this->stream, $string)) {
  717. throw new \RuntimeException('Unable to write to stream');
  718. }
  719. return $result;
  720. }
  721. public function isReadable(): bool
  722. {
  723. return $this->readable;
  724. }
  725. public function read($length): string
  726. {
  727. if (!$this->readable) {
  728. throw new \RuntimeException('Cannot read from non-readable stream');
  729. }
  730. return \fread($this->stream, $length);
  731. }
  732. public function getContents(): string
  733. {
  734. if (!isset($this->stream)) {
  735. throw new \RuntimeException('Unable to read stream contents');
  736. }
  737. if (false === $contents = \stream_get_contents($this->stream)) {
  738. throw new \RuntimeException('Unable to read stream contents');
  739. }
  740. return $contents;
  741. }
  742. public function getMetadata($key = null)
  743. {
  744. if (!isset($this->stream)) {
  745. return $key ? null : [];
  746. }
  747. $meta = \stream_get_meta_data($this->stream);
  748. if (null === $key) {
  749. return $meta;
  750. }
  751. return $meta[$key] ?? null;
  752. }
  753. }
  754. // file: src/Nyholm/Psr7/UploadedFile.php
  755. final class UploadedFile implements UploadedFileInterface
  756. {
  757. private const ERRORS = [
  758. \UPLOAD_ERR_OK => 1,
  759. \UPLOAD_ERR_INI_SIZE => 1,
  760. \UPLOAD_ERR_FORM_SIZE => 1,
  761. \UPLOAD_ERR_PARTIAL => 1,
  762. \UPLOAD_ERR_NO_FILE => 1,
  763. \UPLOAD_ERR_NO_TMP_DIR => 1,
  764. \UPLOAD_ERR_CANT_WRITE => 1,
  765. \UPLOAD_ERR_EXTENSION => 1,
  766. ];
  767. private $clientFilename;
  768. private $clientMediaType;
  769. private $error;
  770. private $file;
  771. private $moved = false;
  772. private $size;
  773. private $stream;
  774. public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
  775. {
  776. if (false === \is_int($errorStatus) || !isset(self::ERRORS[$errorStatus])) {
  777. throw new \InvalidArgumentException('Upload file error status must be an integer value and one of the "UPLOAD_ERR_*" constants.');
  778. }
  779. if (false === \is_int($size)) {
  780. throw new \InvalidArgumentException('Upload file size must be an integer');
  781. }
  782. if (null !== $clientFilename && !\is_string($clientFilename)) {
  783. throw new \InvalidArgumentException('Upload file client filename must be a string or null');
  784. }
  785. if (null !== $clientMediaType && !\is_string($clientMediaType)) {
  786. throw new \InvalidArgumentException('Upload file client media type must be a string or null');
  787. }
  788. $this->error = $errorStatus;
  789. $this->size = $size;
  790. $this->clientFilename = $clientFilename;
  791. $this->clientMediaType = $clientMediaType;
  792. if (\UPLOAD_ERR_OK === $this->error) {
  793. if (\is_string($streamOrFile)) {
  794. $this->file = $streamOrFile;
  795. } elseif (\is_resource($streamOrFile)) {
  796. $this->stream = Stream::create($streamOrFile);
  797. } elseif ($streamOrFile instanceof StreamInterface) {
  798. $this->stream = $streamOrFile;
  799. } else {
  800. throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile');
  801. }
  802. }
  803. }
  804. private function validateActive(): void
  805. {
  806. if (\UPLOAD_ERR_OK !== $this->error) {
  807. throw new \RuntimeException('Cannot retrieve stream due to upload error');
  808. }
  809. if ($this->moved) {
  810. throw new \RuntimeException('Cannot retrieve stream after it has already been moved');
  811. }
  812. }
  813. public function getStream(): StreamInterface
  814. {
  815. $this->validateActive();
  816. if ($this->stream instanceof StreamInterface) {
  817. return $this->stream;
  818. }
  819. $resource = \fopen($this->file, 'r');
  820. return Stream::create($resource);
  821. }
  822. public function moveTo($targetPath): void
  823. {
  824. $this->validateActive();
  825. if (!\is_string($targetPath) || '' === $targetPath) {
  826. throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
  827. }
  828. if (null !== $this->file) {
  829. $this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
  830. } else {
  831. $stream = $this->getStream();
  832. if ($stream->isSeekable()) {
  833. $stream->rewind();
  834. }
  835. $dest = Stream::create(\fopen($targetPath, 'w'));
  836. while (!$stream->eof()) {
  837. if (!$dest->write($stream->read(1048576))) {
  838. break;
  839. }
  840. }
  841. $this->moved = true;
  842. }
  843. if (false === $this->moved) {
  844. throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
  845. }
  846. }
  847. public function getSize(): int
  848. {
  849. return $this->size;
  850. }
  851. public function getError(): int
  852. {
  853. return $this->error;
  854. }
  855. public function getClientFilename(): ?string
  856. {
  857. return $this->clientFilename;
  858. }
  859. public function getClientMediaType(): ?string
  860. {
  861. return $this->clientMediaType;
  862. }
  863. }
  864. // file: src/Nyholm/Psr7/Uri.php
  865. final class Uri implements UriInterface
  866. {
  867. private const SCHEMES = ['http' => 80, 'https' => 443];
  868. private const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~';
  869. private const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=';
  870. private $scheme = '';
  871. private $userInfo = '';
  872. private $host = '';
  873. private $port;
  874. private $path = '';
  875. private $query = '';
  876. private $fragment = '';
  877. public function __construct(string $uri = '')
  878. {
  879. if ('' !== $uri) {
  880. if (false === $parts = \parse_url($uri)) {
  881. throw new \InvalidArgumentException("Unable to parse URI: $uri");
  882. }
  883. $this->scheme = isset($parts['scheme']) ? \strtolower($parts['scheme']) : '';
  884. $this->userInfo = $parts['user'] ?? '';
  885. $this->host = isset($parts['host']) ? \strtolower($parts['host']) : '';
  886. $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null;
  887. $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : '';
  888. $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : '';
  889. $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : '';
  890. if (isset($parts['pass'])) {
  891. $this->userInfo .= ':' . $parts['pass'];
  892. }
  893. }
  894. }
  895. public function __toString(): string
  896. {
  897. return self::createUriString($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment);
  898. }
  899. public function getScheme(): string
  900. {
  901. return $this->scheme;
  902. }
  903. public function getAuthority(): string
  904. {
  905. if ('' === $this->host) {
  906. return '';
  907. }
  908. $authority = $this->host;
  909. if ('' !== $this->userInfo) {
  910. $authority = $this->userInfo . '@' . $authority;
  911. }
  912. if (null !== $this->port) {
  913. $authority .= ':' . $this->port;
  914. }
  915. return $authority;
  916. }
  917. public function getUserInfo(): string
  918. {
  919. return $this->userInfo;
  920. }
  921. public function getHost(): string
  922. {
  923. return $this->host;
  924. }
  925. public function getPort(): ?int
  926. {
  927. return $this->port;
  928. }
  929. public function getPath(): string
  930. {
  931. return $this->path;
  932. }
  933. public function getQuery(): string
  934. {
  935. return $this->query;
  936. }
  937. public function getFragment(): string
  938. {
  939. return $this->fragment;
  940. }
  941. public function withScheme($scheme): self
  942. {
  943. if (!\is_string($scheme)) {
  944. throw new \InvalidArgumentException('Scheme must be a string');
  945. }
  946. if ($this->scheme === $scheme = \strtolower($scheme)) {
  947. return $this;
  948. }
  949. $new = clone $this;
  950. $new->scheme = $scheme;
  951. $new->port = $new->filterPort($new->port);
  952. return $new;
  953. }
  954. public function withUserInfo($user, $password = null): self
  955. {
  956. $info = $user;
  957. if (null !== $password && '' !== $password) {
  958. $info .= ':' . $password;
  959. }
  960. if ($this->userInfo === $info) {
  961. return $this;
  962. }
  963. $new = clone $this;
  964. $new->userInfo = $info;
  965. return $new;
  966. }
  967. public function withHost($host): self
  968. {
  969. if (!\is_string($host)) {
  970. throw new \InvalidArgumentException('Host must be a string');
  971. }
  972. if ($this->host === $host = \strtolower($host)) {
  973. return $this;
  974. }
  975. $new = clone $this;
  976. $new->host = $host;
  977. return $new;
  978. }
  979. public function withPort($port): self
  980. {
  981. if ($this->port === $port = $this->filterPort($port)) {
  982. return $this;
  983. }
  984. $new = clone $this;
  985. $new->port = $port;
  986. return $new;
  987. }
  988. public function withPath($path): self
  989. {
  990. if ($this->path === $path = $this->filterPath($path)) {
  991. return $this;
  992. }
  993. $new = clone $this;
  994. $new->path = $path;
  995. return $new;
  996. }
  997. public function withQuery($query): self
  998. {
  999. if ($this->query === $query = $this->filterQueryAndFragment($query)) {
  1000. return $this;
  1001. }
  1002. $new = clone $this;
  1003. $new->query = $query;
  1004. return $new;
  1005. }
  1006. public function withFragment($fragment): self
  1007. {
  1008. if ($this->fragment === $fragment = $this->filterQueryAndFragment($fragment)) {
  1009. return $this;
  1010. }
  1011. $new = clone $this;
  1012. $new->fragment = $fragment;
  1013. return $new;
  1014. }
  1015. private static function createUriString(string $scheme, string $authority, string $path, string $query, string $fragment): string
  1016. {
  1017. $uri = '';
  1018. if ('' !== $scheme) {
  1019. $uri .= $scheme . ':';
  1020. }
  1021. if ('' !== $authority) {
  1022. $uri .= '//' . $authority;
  1023. }
  1024. if ('' !== $path) {
  1025. if ('/' !== $path[0]) {
  1026. if ('' !== $authority) {
  1027. $path = '/' . $path;
  1028. }
  1029. } elseif (isset($path[1]) && '/' === $path[1]) {
  1030. if ('' === $authority) {
  1031. $path = '/' . \ltrim($path, '/');
  1032. }
  1033. }
  1034. $uri .= $path;
  1035. }
  1036. if ('' !== $query) {
  1037. $uri .= '?' . $query;
  1038. }
  1039. if ('' !== $fragment) {
  1040. $uri .= '#' . $fragment;
  1041. }
  1042. return $uri;
  1043. }
  1044. private static function isNonStandardPort(string $scheme, int $port): bool
  1045. {
  1046. return !isset(self::SCHEMES[$scheme]) || $port !== self::SCHEMES[$scheme];
  1047. }
  1048. private function filterPort($port): ?int
  1049. {
  1050. if (null === $port) {
  1051. return null;
  1052. }
  1053. $port = (int) $port;
  1054. if (1 > $port || 0xffff < $port) {
  1055. throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 1 and 65535', $port));
  1056. }
  1057. return self::isNonStandardPort($this->scheme, $port) ? $port : null;
  1058. }
  1059. private function filterPath($path): string
  1060. {
  1061. if (!\is_string($path)) {
  1062. throw new \InvalidArgumentException('Path must be a string');
  1063. }
  1064. return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path);
  1065. }
  1066. private function filterQueryAndFragment($str): string
  1067. {
  1068. if (!\is_string($str)) {
  1069. throw new \InvalidArgumentException('Query and fragment must be a string');
  1070. }
  1071. return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str);
  1072. }
  1073. private static function rawurlencodeMatchZero(array $match): string
  1074. {
  1075. return \rawurlencode($match[0]);
  1076. }
  1077. }
  1078. // file: src/Nyholm/Psr7Server/ServerRequestCreator.php
  1079. final class ServerRequestCreator implements ServerRequestCreatorInterface
  1080. {
  1081. private $serverRequestFactory;
  1082. private $uriFactory;
  1083. private $uploadedFileFactory;
  1084. private $streamFactory;
  1085. public function __construct(
  1086. ServerRequestFactoryInterface $serverRequestFactory,
  1087. UriFactoryInterface $uriFactory,
  1088. UploadedFileFactoryInterface $uploadedFileFactory,
  1089. StreamFactoryInterface $streamFactory
  1090. ) {
  1091. $this->serverRequestFactory = $serverRequestFactory;
  1092. $this->uriFactory = $uriFactory;
  1093. $this->uploadedFileFactory = $uploadedFileFactory;
  1094. $this->streamFactory = $streamFactory;
  1095. }
  1096. public function fromGlobals(): ServerRequestInterface
  1097. {
  1098. $server = $_SERVER;
  1099. if (false === isset($server['REQUEST_METHOD'])) {
  1100. $server['REQUEST_METHOD'] = 'GET';
  1101. }
  1102. $headers = \function_exists('getallheaders') ? getallheaders() : static::getHeadersFromServer($_SERVER);
  1103. return $this->fromArrays($server, $headers, $_COOKIE, $_GET, $_POST, $_FILES, \fopen('php://input', 'r') ?: null);
  1104. }
  1105. public function fromArrays(array $server, array $headers = [], array $cookie = [], array $get = [], array $post = [], array $files = [], $body = null): ServerRequestInterface
  1106. {
  1107. $method = $this->getMethodFromEnv($server);
  1108. $uri = $this->getUriFromEnvWithHTTP($server);
  1109. $protocol = isset($server['SERVER_PROTOCOL']) ? \str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1';
  1110. $serverRequest = $this->serverRequestFactory->createServerRequest($method, $uri, $server);
  1111. foreach ($headers as $name => $value) {
  1112. $serverRequest = $serverRequest->withAddedHeader($name, $value);
  1113. }
  1114. $serverRequest = $serverRequest
  1115. ->withProtocolVersion($protocol)
  1116. ->withCookieParams($cookie)
  1117. ->withQueryParams($get)
  1118. ->withParsedBody($post)
  1119. ->withUploadedFiles($this->normalizeFiles($files));
  1120. if (null === $body) {
  1121. return $serverRequest;
  1122. }
  1123. if (\is_resource($body)) {
  1124. $body = $this->streamFactory->createStreamFromResource($body);
  1125. } elseif (\is_string($body)) {
  1126. $body = $this->streamFactory->createStream($body);
  1127. } elseif (!$body instanceof StreamInterface) {
  1128. throw new \InvalidArgumentException('The $body parameter to ServerRequestCreator::fromArrays must be string, resource or StreamInterface');
  1129. }
  1130. return $serverRequest->withBody($body);
  1131. }
  1132. public static function getHeadersFromServer(array $server): array
  1133. {
  1134. $headers = [];
  1135. foreach ($server as $key => $value) {
  1136. if (0 === \strpos($key, 'REDIRECT_')) {
  1137. $key = \substr($key, 9);
  1138. if (\array_key_exists($key, $server)) {
  1139. continue;
  1140. }
  1141. }
  1142. if ($value && 0 === \strpos($key, 'HTTP_')) {
  1143. $name = \strtr(\strtolower(\substr($key, 5)), '_', '-');
  1144. $headers[$name] = $value;
  1145. continue;
  1146. }
  1147. if ($value && 0 === \strpos($key, 'CONTENT_')) {
  1148. $name = 'content-'.\strtolower(\substr($key, 8));
  1149. $headers[$name] = $value;
  1150. continue;
  1151. }
  1152. }
  1153. return $headers;
  1154. }
  1155. private function getMethodFromEnv(array $environment): string
  1156. {
  1157. if (false === isset($environment['REQUEST_METHOD'])) {
  1158. throw new \InvalidArgumentException('Cannot determine HTTP method');
  1159. }
  1160. return $environment['REQUEST_METHOD'];
  1161. }
  1162. private function getUriFromEnvWithHTTP(array $environment): UriInterface
  1163. {
  1164. $uri = $this->createUriFromArray($environment);
  1165. if (empty($uri->getScheme())) {
  1166. $uri = $uri->withScheme('http');
  1167. }
  1168. return $uri;
  1169. }
  1170. private function normalizeFiles(array $files): array
  1171. {
  1172. $normalized = [];
  1173. foreach ($files as $key => $value) {
  1174. if ($value instanceof UploadedFileInterface) {
  1175. $normalized[$key] = $value;
  1176. } elseif (\is_array($value) && isset($value['tmp_name'])) {
  1177. $normalized[$key] = $this->createUploadedFileFromSpec($value);
  1178. } elseif (\is_array($value)) {
  1179. $normalized[$key] = $this->normalizeFiles($value);
  1180. } else {
  1181. throw new \InvalidArgumentException('Invalid value in files specification');
  1182. }
  1183. }
  1184. return $normalized;
  1185. }
  1186. private function createUploadedFileFromSpec(array $value)
  1187. {
  1188. if (\is_array($value['tmp_name'])) {
  1189. return $this->normalizeNestedFileSpec($value);
  1190. }
  1191. try {
  1192. $stream = $this->streamFactory->createStreamFromFile($value['tmp_name']);
  1193. } catch (\RuntimeException $e) {
  1194. $stream = $this->streamFactory->createStream();
  1195. }
  1196. return $this->uploadedFileFactory->createUploadedFile(
  1197. $stream,
  1198. (int) $value['size'],
  1199. (int) $value['error'],
  1200. $value['name'],
  1201. $value['type']
  1202. );
  1203. }
  1204. private function normalizeNestedFileSpec(array $files = []): array
  1205. {
  1206. $normalizedFiles = [];
  1207. foreach (\array_keys($files['tmp_name']) as $key) {
  1208. $spec = [
  1209. 'tmp_name' => $files['tmp_name'][$key],
  1210. 'size' => $files['size'][$key],
  1211. 'error' => $files['error'][$key],
  1212. 'name' => $files['name'][$key],
  1213. 'type' => $files['type'][$key],
  1214. ];
  1215. $normalizedFiles[$key] = $this->createUploadedFileFromSpec($spec);
  1216. }
  1217. return $normalizedFiles;
  1218. }
  1219. private function createUriFromArray(array $server): UriInterface
  1220. {
  1221. $uri = $this->uriFactory->createUri('');
  1222. if (isset($server['REQUEST_SCHEME'])) {
  1223. $uri = $uri->withScheme($server['REQUEST_SCHEME']);
  1224. } elseif (isset($server['HTTPS'])) {
  1225. $uri = $uri->withScheme('on' === $server['HTTPS'] ? 'https' : 'http');
  1226. }
  1227. if (isset($server['SERVER_PORT'])) {
  1228. $uri = $uri->withPort($server['SERVER_PORT']);
  1229. }
  1230. if (isset($server['HTTP_HOST'])) {
  1231. if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) {
  1232. $uri = $uri->withHost($matches[1])->withPort($matches[2]);
  1233. } else {
  1234. $uri = $uri->withHost($server['HTTP_HOST']);
  1235. }
  1236. } elseif (isset($server['SERVER_NAME'])) {
  1237. $uri = $uri->withHost($server['SERVER_NAME']);
  1238. }
  1239. if (isset($server['REQUEST_URI'])) {
  1240. $uri = $uri->withPath(\current(\explode('?', $server['REQUEST_URI'])));
  1241. }
  1242. if (isset($server['QUERY_STRING'])) {
  1243. $uri = $uri->withQuery($server['QUERY_STRING']);
  1244. }
  1245. return $uri;
  1246. }
  1247. }
  1248. // file: src/Nyholm/Psr7Server/ServerRequestCreatorInterface.php
  1249. interface ServerRequestCreatorInterface
  1250. {
  1251. public function fromGlobals(): ServerRequestInterface;
  1252. public function fromArrays(
  1253. array $server,
  1254. array $headers = [],
  1255. array $cookie = [],
  1256. array $get = [],
  1257. array $post = [],
  1258. array $files = [],
  1259. $body = null
  1260. ): ServerRequestInterface;
  1261. public static function getHeadersFromServer(array $server): array;
  1262. }
  1263. // file: src/Tqdev/PhpCrudApi/Cache/Cache.php
  1264. interface Cache
  1265. {
  1266. public function set(string $key, string $value, int $ttl = 0): bool;
  1267. public function get(string $key): string;
  1268. public function clear(): bool;
  1269. }
  1270. // file: src/Tqdev/PhpCrudApi/Cache/CacheFactory.php
  1271. class CacheFactory
  1272. {
  1273. const PREFIX = 'phpcrudapi-%s-%s-%s-';
  1274. private static function getPrefix(Config $config): string
  1275. {
  1276. $driver = $config->getDriver();
  1277. $database = $config->getDatabase();
  1278. $filehash = substr(md5(__FILE__), 0, 8);
  1279. return sprintf(self::PREFIX, $driver, $database, $filehash);
  1280. }
  1281. public static function create(Config $config): Cache
  1282. {
  1283. switch ($config->getCacheType()) {
  1284. case 'TempFile':
  1285. $cache = new TempFileCache(self::getPrefix($config), $config->getCachePath());
  1286. break;
  1287. case 'Redis':
  1288. $cache = new RedisCache(self::getPrefix($config), $config->getCachePath());
  1289. break;
  1290. case 'Memcache':
  1291. $cache = new MemcacheCache(self::getPrefix($config), $config->getCachePath());
  1292. break;
  1293. case 'Memcached':
  1294. $cache = new MemcachedCache(self::getPrefix($config), $config->getCachePath());
  1295. break;
  1296. default:
  1297. $cache = new NoCache();
  1298. }
  1299. return $cache;
  1300. }
  1301. }
  1302. // file: src/Tqdev/PhpCrudApi/Cache/MemcacheCache.php
  1303. class MemcacheCache implements Cache
  1304. {
  1305. protected $prefix;
  1306. protected $memcache;
  1307. public function __construct(string $prefix, string $config)
  1308. {
  1309. $this->prefix = $prefix;
  1310. if ($config == '') {
  1311. $address = 'localhost';
  1312. $port = 11211;
  1313. } elseif (strpos($config, ':') === false) {
  1314. $address = $config;
  1315. $port = 11211;
  1316. } else {
  1317. list($address, $port) = explode(':', $config);
  1318. }
  1319. $this->memcache = $this->create();
  1320. $this->memcache->addServer($address, $port);
  1321. }
  1322. protected function create() /*: \Memcache*/
  1323. {
  1324. return new \Memcache();
  1325. }
  1326. public function set(string $key, string $value, int $ttl = 0): bool
  1327. {
  1328. return $this->memcache->set($this->prefix . $key, $value, 0, $ttl);
  1329. }
  1330. public function get(string $key): string
  1331. {
  1332. return $this->memcache->get($this->prefix . $key) ?: '';
  1333. }
  1334. public function clear(): bool
  1335. {
  1336. return $this->memcache->flush();
  1337. }
  1338. }
  1339. // file: src/Tqdev/PhpCrudApi/Cache/MemcachedCache.php
  1340. class MemcachedCache extends MemcacheCache
  1341. {
  1342. protected function create() /*: \Memcached*/
  1343. {
  1344. return new \Memcached();
  1345. }
  1346. public function set(string $key, string $value, int $ttl = 0): bool
  1347. {
  1348. return $this->memcache->set($this->prefix . $key, $value, $ttl);
  1349. }
  1350. }
  1351. // file: src/Tqdev/PhpCrudApi/Cache/NoCache.php
  1352. class NoCache implements Cache
  1353. {
  1354. public function __construct()
  1355. {
  1356. }
  1357. public function set(string $key, string $value, int $ttl = 0): bool
  1358. {
  1359. return true;
  1360. }
  1361. public function get(string $key): string
  1362. {
  1363. return '';
  1364. }
  1365. public function clear(): bool
  1366. {
  1367. return true;
  1368. }
  1369. }
  1370. // file: src/Tqdev/PhpCrudApi/Cache/RedisCache.php
  1371. class RedisCache implements Cache
  1372. {
  1373. protected $prefix;
  1374. protected $redis;
  1375. public function __construct(string $prefix, string $config)
  1376. {
  1377. $this->prefix = $prefix;
  1378. if ($config == '') {
  1379. $config = '127.0.0.1';
  1380. }
  1381. $params = explode(':', $config, 6);
  1382. if (isset($params[3])) {
  1383. $params[3] = null;
  1384. }
  1385. $this->redis = new \Redis();
  1386. call_user_func_array(array($this->redis, 'pconnect'), $params);
  1387. }
  1388. public function set(string $key, string $value, int $ttl = 0): bool
  1389. {
  1390. return $this->redis->set($this->prefix . $key, $value, $ttl);
  1391. }
  1392. public function get(string $key): string
  1393. {
  1394. return $this->redis->get($this->prefix . $key) ?: '';
  1395. }
  1396. public function clear(): bool
  1397. {
  1398. return $this->redis->flushDb();
  1399. }
  1400. }
  1401. // file: src/Tqdev/PhpCrudApi/Cache/TempFileCache.php
  1402. class TempFileCache implements Cache
  1403. {
  1404. const SUFFIX = 'cache';
  1405. private $path;
  1406. private $segments;
  1407. public function __construct(string $prefix, string $config)
  1408. {
  1409. $this->segments = [];
  1410. $s = DIRECTORY_SEPARATOR;
  1411. $ps = PATH_SEPARATOR;
  1412. if ($config == '') {
  1413. $this->path = sys_get_temp_dir() . $s . $prefix . self::SUFFIX;
  1414. } elseif (strpos($config, $ps) === false) {
  1415. $this->path = $config;
  1416. } else {
  1417. list($path, $segments) = explode($ps, $config);
  1418. $this->path = $path;
  1419. $this->segments = explode(',', $segments);
  1420. }
  1421. if (file_exists($this->path) && is_dir($this->path)) {
  1422. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), false);
  1423. }
  1424. }
  1425. private function getFileName(string $key): string
  1426. {
  1427. $s = DIRECTORY_SEPARATOR;
  1428. $md5 = md5($key);
  1429. $filename = rtrim($this->path, $s) . $s;
  1430. $i = 0;
  1431. foreach ($this->segments as $segment) {
  1432. $filename .= substr($md5, $i, $segment) . $s;
  1433. $i += $segment;
  1434. }
  1435. $filename .= substr($md5, $i);
  1436. return $filename;
  1437. }
  1438. public function set(string $key, string $value, int $ttl = 0): bool
  1439. {
  1440. $filename = $this->getFileName($key);
  1441. $dirname = dirname($filename);
  1442. if (!file_exists($dirname)) {
  1443. if (!mkdir($dirname, 0755, true)) {
  1444. return false;
  1445. }
  1446. }
  1447. $string = $ttl . '|' . $value;
  1448. return $this->filePutContents($filename, $string) !== false;
  1449. }
  1450. private function filePutContents($filename, $string)
  1451. {
  1452. return file_put_contents($filename, $string, LOCK_EX);
  1453. }
  1454. private function fileGetContents($filename)
  1455. {
  1456. $file = fopen($filename, 'rb');
  1457. if ($file === false) {
  1458. return false;
  1459. }
  1460. $lock = flock($file, LOCK_SH);
  1461. if (!$lock) {
  1462. fclose($file);
  1463. return false;
  1464. }
  1465. $string = '';
  1466. while (!feof($file)) {
  1467. $string .= fread($file, 8192);
  1468. }
  1469. flock($file, LOCK_UN);
  1470. fclose($file);
  1471. return $string;
  1472. }
  1473. private function getString($filename): string
  1474. {
  1475. $data = $this->fileGetContents($filename);
  1476. if ($data === false) {
  1477. return '';
  1478. }
  1479. list($ttl, $string) = explode('|', $data, 2);
  1480. if ($ttl > 0 && time() - filemtime($filename) > $ttl) {
  1481. return '';
  1482. }
  1483. return $string;
  1484. }
  1485. public function get(string $key): string
  1486. {
  1487. $filename = $this->getFileName($key);
  1488. if (!file_exists($filename)) {
  1489. return '';
  1490. }
  1491. $string = $this->getString($filename);
  1492. if ($string == null) {
  1493. return '';
  1494. }
  1495. return $string;
  1496. }
  1497. private function clean(string $path, array $segments, int $len, bool $all) /*: void*/
  1498. {
  1499. $entries = scandir($path);
  1500. foreach ($entries as $entry) {
  1501. if ($entry === '.' || $entry === '..') {
  1502. continue;
  1503. }
  1504. $filename = $path . DIRECTORY_SEPARATOR . $entry;
  1505. if (count($segments) == 0) {
  1506. if (strlen($entry) != $len) {
  1507. continue;
  1508. }
  1509. if (is_file($filename)) {
  1510. if ($all || $this->getString($filename) == null) {
  1511. unlink($filename);
  1512. }
  1513. }
  1514. } else {
  1515. if (strlen($entry) != $segments[0]) {
  1516. continue;
  1517. }
  1518. if (is_dir($filename)) {
  1519. $this->clean($filename, array_slice($segments, 1), $len - $segments[0], $all);
  1520. rmdir($filename);
  1521. }
  1522. }
  1523. }
  1524. }
  1525. public function clear(): bool
  1526. {
  1527. if (!file_exists($this->path) || !is_dir($this->path)) {
  1528. return false;
  1529. }
  1530. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), true);
  1531. return true;
  1532. }
  1533. }
  1534. // file: src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedColumn.php
  1535. class ReflectedColumn implements \JsonSerializable
  1536. {
  1537. const DEFAULT_LENGTH = 255;
  1538. const DEFAULT_PRECISION = 19;
  1539. const DEFAULT_SCALE = 4;
  1540. private $name;
  1541. private $type;
  1542. private $length;
  1543. private $precision;
  1544. private $scale;
  1545. private $nullable;
  1546. private $pk;
  1547. private $fk;
  1548. public function __construct(string $name, string $type, int $length, int $precision, int $scale, bool $nullable, bool $pk, string $fk)
  1549. {
  1550. $this->name = $name;
  1551. $this->type = $type;
  1552. $this->length = $length;
  1553. $this->precision = $precision;
  1554. $this->scale = $scale;
  1555. $this->nullable = $nullable;
  1556. $this->pk = $pk;
  1557. $this->fk = $fk;
  1558. $this->sanitize();
  1559. }
  1560. public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
  1561. {
  1562. $name = $columnResult['COLUMN_NAME'];
  1563. $length = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
  1564. $type = $reflection->toJdbcType($columnResult['DATA_TYPE'], $length);
  1565. $precision = (int) $columnResult['NUMERIC_PRECISION'];
  1566. $scale = (int) $columnResult['NUMERIC_SCALE'];
  1567. $nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
  1568. $pk = false;
  1569. $fk = '';
  1570. return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
  1571. }
  1572. public static function fromJson( /* object */$json): ReflectedColumn
  1573. {
  1574. $name = $json->name;
  1575. $type = $json->type;
  1576. $length = isset($json->length) ? $json->length : 0;
  1577. $precision = isset($json->precision) ? $json->precision : 0;
  1578. $scale = isset($json->scale) ? $json->scale : 0;
  1579. $nullable = isset($json->nullable) ? $json->nullable : false;
  1580. $pk = isset($json->pk) ? $json->pk : false;
  1581. $fk = isset($json->fk) ? $json->fk : '';
  1582. return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
  1583. }
  1584. private function sanitize()
  1585. {
  1586. $this->length = $this->hasLength() ? $this->getLength() : 0;
  1587. $this->precision = $this->hasPrecision() ? $this->getPrecision() : 0;
  1588. $this->scale = $this->hasScale() ? $this->getScale() : 0;
  1589. }
  1590. public function getName(): string
  1591. {
  1592. return $this->name;
  1593. }
  1594. public function getNullable(): bool
  1595. {
  1596. return $this->nullable;
  1597. }
  1598. public function getType(): string
  1599. {
  1600. return $this->type;
  1601. }
  1602. public function getLength(): int
  1603. {
  1604. return $this->length ?: self::DEFAULT_LENGTH;
  1605. }
  1606. public function getPrecision(): int
  1607. {
  1608. return $this->precision ?: self::DEFAULT_PRECISION;
  1609. }
  1610. public function getScale(): int
  1611. {
  1612. return $this->scale ?: self::DEFAULT_SCALE;
  1613. }
  1614. public function hasLength(): bool
  1615. {
  1616. return in_array($this->type, ['varchar', 'varbinary']);
  1617. }
  1618. public function hasPrecision(): bool
  1619. {
  1620. return $this->type == 'decimal';
  1621. }
  1622. public function hasScale(): bool
  1623. {
  1624. return $this->type == 'decimal';
  1625. }
  1626. public function isBinary(): bool
  1627. {
  1628. return in_array($this->type, ['blob', 'varbinary']);
  1629. }
  1630. public function isBoolean(): bool
  1631. {
  1632. return $this->type == 'boolean';
  1633. }
  1634. public function isGeometry(): bool
  1635. {
  1636. return $this->type == 'geometry';
  1637. }
  1638. public function isInteger(): bool
  1639. {
  1640. return in_array($this->type, ['integer', 'bigint', 'smallint', 'tinyint']);
  1641. }
  1642. public function setPk($value) /*: void*/
  1643. {
  1644. $this->pk = $value;
  1645. }
  1646. public function getPk(): bool
  1647. {
  1648. return $this->pk;
  1649. }
  1650. public function setFk($value) /*: void*/
  1651. {
  1652. $this->fk = $value;
  1653. }
  1654. public function getFk(): string
  1655. {
  1656. return $this->fk;
  1657. }
  1658. public function serialize()
  1659. {
  1660. return [
  1661. 'name' => $this->name,
  1662. 'type' => $this->type,
  1663. 'length' => $this->length,
  1664. 'precision' => $this->precision,
  1665. 'scale' => $this->scale,
  1666. 'nullable' => $this->nullable,
  1667. 'pk' => $this->pk,
  1668. 'fk' => $this->fk,
  1669. ];
  1670. }
  1671. public function jsonSerialize()
  1672. {
  1673. return array_filter($this->serialize());
  1674. }
  1675. }
  1676. // file: src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedDatabase.php
  1677. class ReflectedDatabase implements \JsonSerializable
  1678. {
  1679. private $tableTypes;
  1680. public function __construct(array $tableTypes)
  1681. {
  1682. $this->tableTypes = $tableTypes;
  1683. }
  1684. public static function fromReflection(GenericReflection $reflection): ReflectedDatabase
  1685. {
  1686. $tableTypes = [];
  1687. foreach ($reflection->getTables() as $table) {
  1688. $tableName = $table['TABLE_NAME'];
  1689. $tableType = $table['TABLE_TYPE'];
  1690. if (in_array($tableName, $reflection->getIgnoredTables())) {
  1691. continue;
  1692. }
  1693. $tableTypes[$tableName] = $tableType;
  1694. }
  1695. return new ReflectedDatabase($tableTypes);
  1696. }
  1697. public static function fromJson( /* object */$json): ReflectedDatabase
  1698. {
  1699. $tableTypes = (array) $json->tables;
  1700. return new ReflectedDatabase($tableTypes);
  1701. }
  1702. public function hasTable(string $tableName): bool
  1703. {
  1704. return isset($this->tableTypes[$tableName]);
  1705. }
  1706. public function getType(string $tableName): string
  1707. {
  1708. return isset($this->tableTypes[$tableName]) ? $this->tableTypes[$tableName] : '';
  1709. }
  1710. public function getTableNames(): array
  1711. {
  1712. return array_keys($this->tableTypes);
  1713. }
  1714. public function removeTable(string $tableName): bool
  1715. {
  1716. if (!isset($this->tableTypes[$tableName])) {
  1717. return false;
  1718. }
  1719. unset($this->tableTypes[$tableName]);
  1720. return true;
  1721. }
  1722. public function serialize()
  1723. {
  1724. return [
  1725. 'tables' => $this->tableTypes,
  1726. ];
  1727. }
  1728. public function jsonSerialize()
  1729. {
  1730. return $this->serialize();
  1731. }
  1732. }
  1733. // file: src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedTable.php
  1734. class ReflectedTable implements \JsonSerializable
  1735. {
  1736. private $name;
  1737. private $type;
  1738. private $columns;
  1739. private $pk;
  1740. private $fks;
  1741. public function __construct(string $name, string $type, array $columns)
  1742. {
  1743. $this->name = $name;
  1744. $this->type = $type;
  1745. $this->columns = [];
  1746. foreach ($columns as $column) {
  1747. $columnName = $column->getName();
  1748. $this->columns[$columnName] = $column;
  1749. }
  1750. $this->pk = null;
  1751. foreach ($columns as $column) {
  1752. if ($column->getPk() == true) {
  1753. $this->pk = $column;
  1754. }
  1755. }
  1756. $this->fks = [];
  1757. foreach ($columns as $column) {
  1758. $columnName = $column->getName();
  1759. $referencedTableName = $column->getFk();
  1760. if ($referencedTableName != '') {
  1761. $this->fks[$columnName] = $referencedTableName;
  1762. }
  1763. }
  1764. }
  1765. public static function fromReflection(GenericReflection $reflection, string $name, string $type): ReflectedTable
  1766. {
  1767. $columns = [];
  1768. foreach ($reflection->getTableColumns($name, $type) as $tableColumn) {
  1769. $column = ReflectedColumn::fromReflection($reflection, $tableColumn);
  1770. $columns[$column->getName()] = $column;
  1771. }
  1772. $columnNames = $reflection->getTablePrimaryKeys($name);
  1773. if (count($columnNames) == 1) {
  1774. $columnName = $columnNames[0];
  1775. if (isset($columns[$columnName])) {
  1776. $pk = $columns[$columnName];
  1777. $pk->setPk(true);
  1778. }
  1779. }
  1780. $fks = $reflection->getTableForeignKeys($name);
  1781. foreach ($fks as $columnName => $table) {
  1782. $columns[$columnName]->setFk($table);
  1783. }
  1784. return new ReflectedTable($name, $type, array_values($columns));
  1785. }
  1786. public static function fromJson( /* object */$json): ReflectedTable
  1787. {
  1788. $name = $json->name;
  1789. $type = $json->type;
  1790. $columns = [];
  1791. if (isset($json->columns) && is_array($json->columns)) {
  1792. foreach ($json->columns as $column) {
  1793. $columns[] = ReflectedColumn::fromJson($column);
  1794. }
  1795. }
  1796. return new ReflectedTable($name, $type, $columns);
  1797. }
  1798. public function hasColumn(string $columnName): bool
  1799. {
  1800. return isset($this->columns[$columnName]);
  1801. }
  1802. public function hasPk(): bool
  1803. {
  1804. return $this->pk != null;
  1805. }
  1806. public function getPk() /*: ?ReflectedColumn */
  1807. {
  1808. return $this->pk;
  1809. }
  1810. public function getName(): string
  1811. {
  1812. return $this->name;
  1813. }
  1814. public function getType(): string
  1815. {
  1816. return $this->type;
  1817. }
  1818. public function getColumnNames(): array
  1819. {
  1820. return array_keys($this->columns);
  1821. }
  1822. public function getColumn($columnName): ReflectedColumn
  1823. {
  1824. return $this->columns[$columnName];
  1825. }
  1826. public function getFksTo(string $tableName): array
  1827. {
  1828. $columns = array();
  1829. foreach ($this->fks as $columnName => $referencedTableName) {
  1830. if ($tableName == $referencedTableName) {
  1831. $columns[] = $this->columns[$columnName];
  1832. }
  1833. }
  1834. return $columns;
  1835. }
  1836. public function removeColumn(string $columnName): bool
  1837. {
  1838. if (!isset($this->columns[$columnName])) {
  1839. return false;
  1840. }
  1841. unset($this->columns[$columnName]);
  1842. return true;
  1843. }
  1844. public function serialize()
  1845. {
  1846. return [
  1847. 'name' => $this->name,
  1848. 'type' => $this->type,
  1849. 'columns' => array_values($this->columns),
  1850. ];
  1851. }
  1852. public function jsonSerialize()
  1853. {
  1854. return $this->serialize();
  1855. }
  1856. }
  1857. // file: src/Tqdev/PhpCrudApi/Column/DefinitionService.php
  1858. class DefinitionService
  1859. {
  1860. private $db;
  1861. private $reflection;
  1862. public function __construct(GenericDB $db, ReflectionService $reflection)
  1863. {
  1864. $this->db = $db;
  1865. $this->reflection = $reflection;
  1866. }
  1867. public function updateTable(string $tableName, /* object */ $changes): bool
  1868. {
  1869. $table = $this->reflection->getTable($tableName);
  1870. $newTable = ReflectedTable::fromJson((object) array_merge((array) $table->jsonSerialize(), (array) $changes));
  1871. if ($table->getName() != $newTable->getName()) {
  1872. if (!$this->db->definition()->renameTable($table->getName(), $newTable->getName())) {
  1873. return false;
  1874. }
  1875. }
  1876. return true;
  1877. }
  1878. public function updateColumn(string $tableName, string $columnName, /* object */ $changes): bool
  1879. {
  1880. $table = $this->reflection->getTable($tableName);
  1881. $column = $table->getColumn($columnName);
  1882. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
  1883. if ($newColumn->getPk() != $column->getPk() && $table->hasPk()) {
  1884. $oldColumn = $table->getPk();
  1885. if ($oldColumn->getName() != $columnName) {
  1886. $oldColumn->setPk(false);
  1887. if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $oldColumn->getName(), $oldColumn)) {
  1888. return false;
  1889. }
  1890. }
  1891. }
  1892. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), ['pk' => false, 'fk' => false]));
  1893. if ($newColumn->getPk() != $column->getPk() && !$newColumn->getPk()) {
  1894. if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $column->getName(), $newColumn)) {
  1895. return false;
  1896. }
  1897. }
  1898. if ($newColumn->getFk() != $column->getFk() && !$newColumn->getFk()) {
  1899. if (!$this->db->definition()->removeColumnForeignKey($table->getName(), $column->getName(), $newColumn)) {
  1900. return false;
  1901. }
  1902. }
  1903. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
  1904. $newColumn->setPk(false);
  1905. $newColumn->setFk('');
  1906. if ($newColumn->getName() != $column->getName()) {
  1907. if (!$this->db->definition()->renameColumn($table->getName(), $column->getName(), $newColumn)) {
  1908. return false;
  1909. }
  1910. }
  1911. if ($newColumn->getType() != $column->getType() ||
  1912. $newColumn->getLength() != $column->getLength() ||
  1913. $newColumn->getPrecision() != $column->getPrecision() ||
  1914. $newColumn->getScale() != $column->getScale()
  1915. ) {
  1916. if (!$this->db->definition()->retypeColumn($table->getName(), $newColumn->getName(), $newColumn)) {
  1917. return false;
  1918. }
  1919. }
  1920. if ($newColumn->getNullable() != $column->getNullable()) {
  1921. if (!$this->db->definition()->setColumnNullable($table->getName(), $newColumn->getName(), $newColumn)) {
  1922. return false;
  1923. }
  1924. }
  1925. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
  1926. if ($newColumn->getFk()) {
  1927. if (!$this->db->definition()->addColumnForeignKey($table->getName(), $newColumn->getName(), $newColumn)) {
  1928. return false;
  1929. }
  1930. }
  1931. if ($newColumn->getPk()) {
  1932. if (!$this->db->definition()->addColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
  1933. return false;
  1934. }
  1935. }
  1936. return true;
  1937. }
  1938. public function addTable( /* object */$definition)
  1939. {
  1940. $newTable = ReflectedTable::fromJson($definition);
  1941. if (!$this->db->definition()->addTable($newTable)) {
  1942. return false;
  1943. }
  1944. return true;
  1945. }
  1946. public function addColumn(string $tableName, /* object */ $definition)
  1947. {
  1948. $newColumn = ReflectedColumn::fromJson($definition);
  1949. if (!$this->db->definition()->addColumn($tableName, $newColumn)) {
  1950. return false;
  1951. }
  1952. if ($newColumn->getFk()) {
  1953. if (!$this->db->definition()->addColumnForeignKey($tableName, $newColumn->getName(), $newColumn)) {
  1954. return false;
  1955. }
  1956. }
  1957. if ($newColumn->getPk()) {
  1958. if (!$this->db->definition()->addColumnPrimaryKey($tableName, $newColumn->getName(), $newColumn)) {
  1959. return false;
  1960. }
  1961. }
  1962. return true;
  1963. }
  1964. public function removeTable(string $tableName)
  1965. {
  1966. if (!$this->db->definition()->removeTable($tableName)) {
  1967. return false;
  1968. }
  1969. return true;
  1970. }
  1971. public function removeColumn(string $tableName, string $columnName)
  1972. {
  1973. $table = $this->reflection->getTable($tableName);
  1974. $newColumn = $table->getColumn($columnName);
  1975. if ($newColumn->getPk()) {
  1976. $newColumn->setPk(false);
  1977. if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
  1978. return false;
  1979. }
  1980. }
  1981. if ($newColumn->getFk()) {
  1982. $newColumn->setFk("");
  1983. if (!$this->db->definition()->removeColumnForeignKey($tableName, $columnName, $newColumn)) {
  1984. return false;
  1985. }
  1986. }
  1987. if (!$this->db->definition()->removeColumn($tableName, $columnName)) {
  1988. return false;
  1989. }
  1990. return true;
  1991. }
  1992. }
  1993. // file: src/Tqdev/PhpCrudApi/Column/ReflectionService.php
  1994. class ReflectionService
  1995. {
  1996. private $db;
  1997. private $cache;
  1998. private $ttl;
  1999. private $database;
  2000. private $tables;
  2001. public function __construct(GenericDB $db, Cache $cache, int $ttl)
  2002. {
  2003. $this->db = $db;
  2004. $this->cache = $cache;
  2005. $this->ttl = $ttl;
  2006. $this->database = $this->loadDatabase(true);
  2007. $this->tables = [];
  2008. }
  2009. private function loadDatabase(bool $useCache): ReflectedDatabase
  2010. {
  2011. $data = $useCache ? $this->cache->get('ReflectedDatabase') : '';
  2012. if ($data != '') {
  2013. $database = ReflectedDatabase::fromJson(json_decode(gzuncompress($data)));
  2014. } else {
  2015. $database = ReflectedDatabase::fromReflection($this->db->reflection());
  2016. $data = gzcompress(json_encode($database, JSON_UNESCAPED_UNICODE));
  2017. $this->cache->set('ReflectedDatabase', $data, $this->ttl);
  2018. }
  2019. return $database;
  2020. }
  2021. private function loadTable(string $tableName, bool $useCache): ReflectedTable
  2022. {
  2023. $data = $useCache ? $this->cache->get("ReflectedTable($tableName)") : '';
  2024. if ($data != '') {
  2025. $table = ReflectedTable::fromJson(json_decode(gzuncompress($data)));
  2026. } else {
  2027. $tableType = $this->database->getType($tableName);
  2028. $table = ReflectedTable::fromReflection($this->db->reflection(), $tableName, $tableType);
  2029. $data = gzcompress(json_encode($table, JSON_UNESCAPED_UNICODE));
  2030. $this->cache->set("ReflectedTable($tableName)", $data, $this->ttl);
  2031. }
  2032. return $table;
  2033. }
  2034. public function refreshTables()
  2035. {
  2036. $this->database = $this->loadDatabase(false);
  2037. }
  2038. public function refreshTable(string $tableName)
  2039. {
  2040. $this->tables[$tableName] = $this->loadTable($tableName, false);
  2041. }
  2042. public function hasTable(string $tableName): bool
  2043. {
  2044. return $this->database->hasTable($tableName);
  2045. }
  2046. public function getType(string $tableName): string
  2047. {
  2048. return $this->database->getType($tableName);
  2049. }
  2050. public function getTable(string $tableName): ReflectedTable
  2051. {
  2052. if (!isset($this->tables[$tableName])) {
  2053. $this->tables[$tableName] = $this->loadTable($tableName, true);
  2054. }
  2055. return $this->tables[$tableName];
  2056. }
  2057. public function getTableNames(): array
  2058. {
  2059. return $this->database->getTableNames();
  2060. }
  2061. public function getDatabaseName(): string
  2062. {
  2063. return $this->database->getName();
  2064. }
  2065. public function removeTable(string $tableName): bool
  2066. {
  2067. unset($this->tables[$tableName]);
  2068. return $this->database->removeTable($tableName);
  2069. }
  2070. }
  2071. // file: src/Tqdev/PhpCrudApi/Controller/CacheController.php
  2072. class CacheController
  2073. {
  2074. private $cache;
  2075. private $responder;
  2076. public function __construct(Router $router, Responder $responder, Cache $cache)
  2077. {
  2078. $router->register('GET', '/cache/clear', array($this, 'clear'));
  2079. $this->cache = $cache;
  2080. $this->responder = $responder;
  2081. }
  2082. public function clear(ServerRequestInterface $request): ResponseInterface
  2083. {
  2084. return $this->responder->success($this->cache->clear());
  2085. }
  2086. }
  2087. // file: src/Tqdev/PhpCrudApi/Controller/ColumnController.php
  2088. class ColumnController
  2089. {
  2090. private $responder;
  2091. private $reflection;
  2092. private $definition;
  2093. public function __construct(Router $router, Responder $responder, ReflectionService $reflection, DefinitionService $definition)
  2094. {
  2095. $router->register('GET', '/columns', array($this, 'getDatabase'));
  2096. $router->register('GET', '/columns/*', array($this, 'getTable'));
  2097. $router->register('GET', '/columns/*/*', array($this, 'getColumn'));
  2098. $router->register('PUT', '/columns/*', array($this, 'updateTable'));
  2099. $router->register('PUT', '/columns/*/*', array($this, 'updateColumn'));
  2100. $router->register('POST', '/columns', array($this, 'addTable'));
  2101. $router->register('POST', '/columns/*', array($this, 'addColumn'));
  2102. $router->register('DELETE', '/columns/*', array($this, 'removeTable'));
  2103. $router->register('DELETE', '/columns/*/*', array($this, 'removeColumn'));
  2104. $this->responder = $responder;
  2105. $this->reflection = $reflection;
  2106. $this->definition = $definition;
  2107. }
  2108. public function getDatabase(ServerRequestInterface $request): ResponseInterface
  2109. {
  2110. $tables = [];
  2111. foreach ($this->reflection->getTableNames() as $table) {
  2112. $tables[] = $this->reflection->getTable($table);
  2113. }
  2114. $database = ['tables' => $tables];
  2115. return $this->responder->success($database);
  2116. }
  2117. public function getTable(ServerRequestInterface $request): ResponseInterface
  2118. {
  2119. $tableName = RequestUtils::getPathSegment($request, 2);
  2120. if (!$this->reflection->hasTable($tableName)) {
  2121. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  2122. }
  2123. $table = $this->reflection->getTable($tableName);
  2124. return $this->responder->success($table);
  2125. }
  2126. public function getColumn(ServerRequestInterface $request): ResponseInterface
  2127. {
  2128. $tableName = RequestUtils::getPathSegment($request, 2);
  2129. $columnName = RequestUtils::getPathSegment($request, 3);
  2130. if (!$this->reflection->hasTable($tableName)) {
  2131. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  2132. }
  2133. $table = $this->reflection->getTable($tableName);
  2134. if (!$table->hasColumn($columnName)) {
  2135. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  2136. }
  2137. $column = $table->getColumn($columnName);
  2138. return $this->responder->success($column);
  2139. }
  2140. public function updateTable(ServerRequestInterface $request): ResponseInterface
  2141. {
  2142. $tableName = RequestUtils::getPathSegment($request, 2);
  2143. if (!$this->reflection->hasTable($tableName)) {
  2144. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  2145. }
  2146. $success = $this->definition->updateTable($tableName, $request->getParsedBody());
  2147. if ($success) {
  2148. $this->reflection->refreshTables();
  2149. }
  2150. return $this->responder->success($success);
  2151. }
  2152. public function updateColumn(ServerRequestInterface $request): ResponseInterface
  2153. {
  2154. $tableName = RequestUtils::getPathSegment($request, 2);
  2155. $columnName = RequestUtils::getPathSegment($request, 3);
  2156. if (!$this->reflection->hasTable($tableName)) {
  2157. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  2158. }
  2159. $table = $this->reflection->getTable($tableName);
  2160. if (!$table->hasColumn($columnName)) {
  2161. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  2162. }
  2163. $success = $this->definition->updateColumn($tableName, $columnName, $request->getParsedBody());
  2164. if ($success) {
  2165. $this->reflection->refreshTable($tableName);
  2166. }
  2167. return $this->responder->success($success);
  2168. }
  2169. public function addTable(ServerRequestInterface $request): ResponseInterface
  2170. {
  2171. $tableName = $request->getParsedBody()->name;
  2172. if ($this->reflection->hasTable($tableName)) {
  2173. return $this->responder->error(ErrorCode::TABLE_ALREADY_EXISTS, $tableName);
  2174. }
  2175. $success = $this->definition->addTable($request->getParsedBody());
  2176. if ($success) {
  2177. $this->reflection->refreshTables();
  2178. }
  2179. return $this->responder->success($success);
  2180. }
  2181. public function addColumn(ServerRequestInterface $request): ResponseInterface
  2182. {
  2183. $tableName = RequestUtils::getPathSegment($request, 2);
  2184. if (!$this->reflection->hasTable($tableName)) {
  2185. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  2186. }
  2187. $columnName = $request->getParsedBody()->name;
  2188. $table = $this->reflection->getTable($tableName);
  2189. if ($table->hasColumn($columnName)) {
  2190. return $this->responder->error(ErrorCode::COLUMN_ALREADY_EXISTS, $columnName);
  2191. }
  2192. $success = $this->definition->addColumn($tableName, $request->getParsedBody());
  2193. if ($success) {
  2194. $this->reflection->refreshTable($tableName);
  2195. }
  2196. return $this->responder->success($success);
  2197. }
  2198. public function removeTable(ServerRequestInterface $request): ResponseInterface
  2199. {
  2200. $tableName = RequestUtils::getPathSegment($request, 2);
  2201. if (!$this->reflection->hasTable($tableName)) {
  2202. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  2203. }
  2204. $success = $this->definition->removeTable($tableName);
  2205. if ($success) {
  2206. $this->reflection->refreshTables();
  2207. }
  2208. return $this->responder->success($success);
  2209. }
  2210. public function removeColumn(ServerRequestInterface $request): ResponseInterface
  2211. {
  2212. $tableName = RequestUtils::getPathSegment($request, 2);
  2213. $columnName = RequestUtils::getPathSegment($request, 3);
  2214. if (!$this->reflection->hasTable($tableName)) {
  2215. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  2216. }
  2217. $table = $this->reflection->getTable($tableName);
  2218. if (!$table->hasColumn($columnName)) {
  2219. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  2220. }
  2221. $success = $this->definition->removeColumn($tableName, $columnName);
  2222. if ($success) {
  2223. $this->reflection->refreshTable($tableName);
  2224. }
  2225. return $this->responder->success($success);
  2226. }
  2227. }
  2228. // file: src/Tqdev/PhpCrudApi/Controller/OpenApiController.php
  2229. class OpenApiController
  2230. {
  2231. private $openApi;
  2232. private $responder;
  2233. public function __construct(Router $router, Responder $responder, OpenApiService $openApi)
  2234. {
  2235. $router->register('GET', '/openapi', array($this, 'openapi'));
  2236. $this->openApi = $openApi;
  2237. $this->responder = $responder;
  2238. }
  2239. public function openapi(ServerRequestInterface $request): ResponseInterface
  2240. {
  2241. return $this->responder->success($this->openApi->get());
  2242. }
  2243. }
  2244. // file: src/Tqdev/PhpCrudApi/Controller/RecordController.php
  2245. class RecordController
  2246. {
  2247. private $service;
  2248. private $responder;
  2249. public function __construct(Router $router, Responder $responder, RecordService $service)
  2250. {
  2251. $router->register('GET', '/records/*', array($this, '_list'));
  2252. $router->register('POST', '/records/*', array($this, 'create'));
  2253. $router->register('GET', '/records/*/*', array($this, 'read'));
  2254. $router->register('PUT', '/records/*/*', array($this, 'update'));
  2255. $router->register('DELETE', '/records/*/*', array($this, 'delete'));
  2256. $router->register('PATCH', '/records/*/*', array($this, 'increment'));
  2257. $this->service = $service;
  2258. $this->responder = $responder;
  2259. }
  2260. public function _list(ServerRequestInterface $request): ResponseInterface
  2261. {
  2262. $table = RequestUtils::getPathSegment($request, 2);
  2263. $params = RequestUtils::getParams($request);
  2264. if (!$this->service->hasTable($table)) {
  2265. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  2266. }
  2267. return $this->responder->success($this->service->_list($table, $params));
  2268. }
  2269. public function read(ServerRequestInterface $request): ResponseInterface
  2270. {
  2271. $table = RequestUtils::getPathSegment($request, 2);
  2272. if (!$this->service->hasTable($table)) {
  2273. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  2274. }
  2275. if ($this->service->getType($table) != 'table') {
  2276. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  2277. }
  2278. $id = RequestUtils::getPathSegment($request, 3);
  2279. $params = RequestUtils::getParams($request);
  2280. if (strpos($id, ',') !== false) {
  2281. $ids = explode(',', $id);
  2282. $result = [];
  2283. for ($i = 0; $i < count($ids); $i++) {
  2284. array_push($result, $this->service->read($table, $ids[$i], $params));
  2285. }
  2286. return $this->responder->success($result);
  2287. } else {
  2288. $response = $this->service->read($table, $id, $params);
  2289. if ($response === null) {
  2290. return $this->responder->error(ErrorCode::RECORD_NOT_FOUND, $id);
  2291. }
  2292. return $this->responder->success($response);
  2293. }
  2294. }
  2295. public function create(ServerRequestInterface $request): ResponseInterface
  2296. {
  2297. $table = RequestUtils::getPathSegment($request, 2);
  2298. if (!$this->service->hasTable($table)) {
  2299. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  2300. }
  2301. if ($this->service->getType($table) != 'table') {
  2302. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  2303. }
  2304. $record = $request->getParsedBody();
  2305. if ($record === null) {
  2306. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  2307. }
  2308. $params = RequestUtils::getParams($request);
  2309. if (is_array($record)) {
  2310. $result = array();
  2311. foreach ($record as $r) {
  2312. $result[] = $this->service->create($table, $r, $params);
  2313. }
  2314. return $this->responder->success($result);
  2315. } else {
  2316. return $this->responder->success($this->service->create($table, $record, $params));
  2317. }
  2318. }
  2319. public function update(ServerRequestInterface $request): ResponseInterface
  2320. {
  2321. $table = RequestUtils::getPathSegment($request, 2);
  2322. if (!$this->service->hasTable($table)) {
  2323. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  2324. }
  2325. if ($this->service->getType($table) != 'table') {
  2326. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  2327. }
  2328. $id = RequestUtils::getPathSegment($request, 3);
  2329. $params = RequestUtils::getParams($request);
  2330. $record = $request->getParsedBody();
  2331. if ($record === null) {
  2332. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  2333. }
  2334. $ids = explode(',', $id);
  2335. if (is_array($record)) {
  2336. if (count($ids) != count($record)) {
  2337. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  2338. }
  2339. $result = array();
  2340. for ($i = 0; $i < count($ids); $i++) {
  2341. $result[] = $this->service->update($table, $ids[$i], $record[$i], $params);
  2342. }
  2343. return $this->responder->success($result);
  2344. } else {
  2345. if (count($ids) != 1) {
  2346. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  2347. }
  2348. return $this->responder->success($this->service->update($table, $id, $record, $params));
  2349. }
  2350. }
  2351. public function delete(ServerRequestInterface $request): ResponseInterface
  2352. {
  2353. $table = RequestUtils::getPathSegment($request, 2);
  2354. if (!$this->service->hasTable($table)) {
  2355. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  2356. }
  2357. if ($this->service->getType($table) != 'table') {
  2358. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  2359. }
  2360. $id = RequestUtils::getPathSegment($request, 3);
  2361. $params = RequestUtils::getParams($request);
  2362. $ids = explode(',', $id);
  2363. if (count($ids) > 1) {
  2364. $result = array();
  2365. for ($i = 0; $i < count($ids); $i++) {
  2366. $result[] = $this->service->delete($table, $ids[$i], $params);
  2367. }
  2368. return $this->responder->success($result);
  2369. } else {
  2370. return $this->responder->success($this->service->delete($table, $id, $params));
  2371. }
  2372. }
  2373. public function increment(ServerRequestInterface $request): ResponseInterface
  2374. {
  2375. $table = RequestUtils::getPathSegment($request, 2);
  2376. if (!$this->service->hasTable($table)) {
  2377. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  2378. }
  2379. if ($this->service->getType($table) != 'table') {
  2380. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  2381. }
  2382. $id = RequestUtils::getPathSegment($request, 3);
  2383. $record = $request->getParsedBody();
  2384. if ($record === null) {
  2385. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  2386. }
  2387. $params = RequestUtils::getParams($request);
  2388. $ids = explode(',', $id);
  2389. if (is_array($record)) {
  2390. if (count($ids) != count($record)) {
  2391. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  2392. }
  2393. $result = array();
  2394. for ($i = 0; $i < count($ids); $i++) {
  2395. $result[] = $this->service->increment($table, $ids[$i], $record[$i], $params);
  2396. }
  2397. return $this->responder->success($result);
  2398. } else {
  2399. if (count($ids) != 1) {
  2400. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  2401. }
  2402. return $this->responder->success($this->service->increment($table, $id, $record, $params));
  2403. }
  2404. }
  2405. }
  2406. // file: src/Tqdev/PhpCrudApi/Controller/Responder.php
  2407. class Responder
  2408. {
  2409. public function error(int $error, string $argument, $details = null): ResponseInterface
  2410. {
  2411. $errorCode = new ErrorCode($error);
  2412. $status = $errorCode->getStatus();
  2413. $document = new ErrorDocument($errorCode, $argument, $details);
  2414. return ResponseFactory::fromObject($status, $document);
  2415. }
  2416. public function success($result): ResponseInterface
  2417. {
  2418. return ResponseFactory::fromObject(ResponseFactory::OK, $result);
  2419. }
  2420. }
  2421. // file: src/Tqdev/PhpCrudApi/Database/ColumnConverter.php
  2422. class ColumnConverter
  2423. {
  2424. private $driver;
  2425. public function __construct(string $driver)
  2426. {
  2427. $this->driver = $driver;
  2428. }
  2429. public function convertColumnValue(ReflectedColumn $column): string
  2430. {
  2431. if ($column->isBinary()) {
  2432. switch ($this->driver) {
  2433. case 'mysql':
  2434. return "FROM_BASE64(?)";
  2435. case 'pgsql':
  2436. return "decode(?, 'base64')";
  2437. case 'sqlsrv':
  2438. return "CONVERT(XML, ?).value('.','varbinary(max)')";
  2439. }
  2440. }
  2441. if ($column->isGeometry()) {
  2442. switch ($this->driver) {
  2443. case 'mysql':
  2444. case 'pgsql':
  2445. return "ST_GeomFromText(?)";
  2446. case 'sqlsrv':
  2447. return "geometry::STGeomFromText(?,0)";
  2448. }
  2449. }
  2450. return '?';
  2451. }
  2452. public function convertColumnName(ReflectedColumn $column, $value): string
  2453. {
  2454. if ($column->isBinary()) {
  2455. switch ($this->driver) {
  2456. case 'mysql':
  2457. return "TO_BASE64($value) as $value";
  2458. case 'pgsql':
  2459. return "encode($value::bytea, 'base64') as $value";
  2460. case 'sqlsrv':
  2461. return "CASE WHEN $value IS NULL THEN NULL ELSE (SELECT CAST($value as varbinary(max)) FOR XML PATH(''), BINARY BASE64) END as $value";
  2462. }
  2463. }
  2464. if ($column->isGeometry()) {
  2465. switch ($this->driver) {
  2466. case 'mysql':
  2467. case 'pgsql':
  2468. return "ST_AsText($value) as $value";
  2469. case 'sqlsrv':
  2470. return "REPLACE($value.STAsText(),' (','(') as $value";
  2471. }
  2472. }
  2473. return $value;
  2474. }
  2475. }
  2476. // file: src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php
  2477. class ColumnsBuilder
  2478. {
  2479. private $driver;
  2480. private $converter;
  2481. public function __construct(string $driver)
  2482. {
  2483. $this->driver = $driver;
  2484. $this->converter = new ColumnConverter($driver);
  2485. }
  2486. public function getOffsetLimit(int $offset, int $limit): string
  2487. {
  2488. if ($limit < 0 || $offset < 0) {
  2489. return '';
  2490. }
  2491. switch ($this->driver) {
  2492. case 'mysql':return " LIMIT $offset, $limit";
  2493. case 'pgsql':return " LIMIT $limit OFFSET $offset";
  2494. case 'sqlsrv':return " OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
  2495. }
  2496. }
  2497. private function quoteColumnName(ReflectedColumn $column): string
  2498. {
  2499. return '"' . $column->getName() . '"';
  2500. }
  2501. public function getOrderBy(ReflectedTable $table, array $columnOrdering): string
  2502. {
  2503. if (count($columnOrdering) == 0) {
  2504. return '';
  2505. }
  2506. $results = array();
  2507. foreach ($columnOrdering as $i => list($columnName, $ordering)) {
  2508. $column = $table->getColumn($columnName);
  2509. $quotedColumnName = $this->quoteColumnName($column);
  2510. $results[] = $quotedColumnName . ' ' . $ordering;
  2511. }
  2512. return ' ORDER BY ' . implode(',', $results);
  2513. }
  2514. public function getSelect(ReflectedTable $table, array $columnNames): string
  2515. {
  2516. $results = array();
  2517. foreach ($columnNames as $columnName) {
  2518. $column = $table->getColumn($columnName);
  2519. $quotedColumnName = $this->quoteColumnName($column);
  2520. $quotedColumnName = $this->converter->convertColumnName($column, $quotedColumnName);
  2521. $results[] = $quotedColumnName;
  2522. }
  2523. return implode(',', $results);
  2524. }
  2525. public function getInsert(ReflectedTable $table, array $columnValues): string
  2526. {
  2527. $columns = array();
  2528. $values = array();
  2529. foreach ($columnValues as $columnName => $columnValue) {
  2530. $column = $table->getColumn($columnName);
  2531. $quotedColumnName = $this->quoteColumnName($column);
  2532. $columns[] = $quotedColumnName;
  2533. $columnValue = $this->converter->convertColumnValue($column);
  2534. $values[] = $columnValue;
  2535. }
  2536. $columnsSql = '(' . implode(',', $columns) . ')';
  2537. $valuesSql = '(' . implode(',', $values) . ')';
  2538. $outputColumn = $this->quoteColumnName($table->getPk());
  2539. switch ($this->driver) {
  2540. case 'mysql':return "$columnsSql VALUES $valuesSql";
  2541. case 'pgsql':return "$columnsSql VALUES $valuesSql RETURNING $outputColumn";
  2542. case 'sqlsrv':return "$columnsSql OUTPUT INSERTED.$outputColumn VALUES $valuesSql";
  2543. }
  2544. }
  2545. public function getUpdate(ReflectedTable $table, array $columnValues): string
  2546. {
  2547. $results = array();
  2548. foreach ($columnValues as $columnName => $columnValue) {
  2549. $column = $table->getColumn($columnName);
  2550. $quotedColumnName = $this->quoteColumnName($column);
  2551. $columnValue = $this->converter->convertColumnValue($column);
  2552. $results[] = $quotedColumnName . '=' . $columnValue;
  2553. }
  2554. return implode(',', $results);
  2555. }
  2556. public function getIncrement(ReflectedTable $table, array $columnValues): string
  2557. {
  2558. $results = array();
  2559. foreach ($columnValues as $columnName => $columnValue) {
  2560. if (!is_numeric($columnValue)) {
  2561. continue;
  2562. }
  2563. $column = $table->getColumn($columnName);
  2564. $quotedColumnName = $this->quoteColumnName($column);
  2565. $columnValue = $this->converter->convertColumnValue($column);
  2566. $results[] = $quotedColumnName . '=' . $quotedColumnName . '+' . $columnValue;
  2567. }
  2568. return implode(',', $results);
  2569. }
  2570. }
  2571. // file: src/Tqdev/PhpCrudApi/Database/ConditionsBuilder.php
  2572. class ConditionsBuilder
  2573. {
  2574. private $driver;
  2575. public function __construct(string $driver)
  2576. {
  2577. $this->driver = $driver;
  2578. }
  2579. private function getConditionSql(Condition $condition, array &$arguments): string
  2580. {
  2581. if ($condition instanceof AndCondition) {
  2582. return $this->getAndConditionSql($condition, $arguments);
  2583. }
  2584. if ($condition instanceof OrCondition) {
  2585. return $this->getOrConditionSql($condition, $arguments);
  2586. }
  2587. if ($condition instanceof NotCondition) {
  2588. return $this->getNotConditionSql($condition, $arguments);
  2589. }
  2590. if ($condition instanceof SpatialCondition) {
  2591. return $this->getSpatialConditionSql($condition, $arguments);
  2592. }
  2593. if ($condition instanceof ColumnCondition) {
  2594. return $this->getColumnConditionSql($condition, $arguments);
  2595. }
  2596. throw new \Exception('Unknown Condition: ' . get_class($condition));
  2597. }
  2598. private function getAndConditionSql(AndCondition $and, array &$arguments): string
  2599. {
  2600. $parts = [];
  2601. foreach ($and->getConditions() as $condition) {
  2602. $parts[] = $this->getConditionSql($condition, $arguments);
  2603. }
  2604. return '(' . implode(' AND ', $parts) . ')';
  2605. }
  2606. private function getOrConditionSql(OrCondition $or, array &$arguments): string
  2607. {
  2608. $parts = [];
  2609. foreach ($or->getConditions() as $condition) {
  2610. $parts[] = $this->getConditionSql($condition, $arguments);
  2611. }
  2612. return '(' . implode(' OR ', $parts) . ')';
  2613. }
  2614. private function getNotConditionSql(NotCondition $not, array &$arguments): string
  2615. {
  2616. $condition = $not->getCondition();
  2617. return '(NOT ' . $this->getConditionSql($condition, $arguments) . ')';
  2618. }
  2619. private function quoteColumnName(ReflectedColumn $column): string
  2620. {
  2621. return '"' . $column->getName() . '"';
  2622. }
  2623. private function escapeLikeValue(string $value): string
  2624. {
  2625. return addcslashes($value, '%_');
  2626. }
  2627. private function getColumnConditionSql(ColumnCondition $condition, array &$arguments): string
  2628. {
  2629. $column = $this->quoteColumnName($condition->getColumn());
  2630. $operator = $condition->getOperator();
  2631. $value = $condition->getValue();
  2632. switch ($operator) {
  2633. case 'cs':
  2634. $sql = "$column LIKE ?";
  2635. $arguments[] = '%' . $this->escapeLikeValue($value) . '%';
  2636. break;
  2637. case 'sw':
  2638. $sql = "$column LIKE ?";
  2639. $arguments[] = $this->escapeLikeValue($value) . '%';
  2640. break;
  2641. case 'ew':
  2642. $sql = "$column LIKE ?";
  2643. $arguments[] = '%' . $this->escapeLikeValue($value);
  2644. break;
  2645. case 'eq':
  2646. $sql = "$column = ?";
  2647. $arguments[] = $value;
  2648. break;
  2649. case 'lt':
  2650. $sql = "$column < ?";
  2651. $arguments[] = $value;
  2652. break;
  2653. case 'le':
  2654. $sql = "$column <= ?";
  2655. $arguments[] = $value;
  2656. break;
  2657. case 'ge':
  2658. $sql = "$column >= ?";
  2659. $arguments[] = $value;
  2660. break;
  2661. case 'gt':
  2662. $sql = "$column > ?";
  2663. $arguments[] = $value;
  2664. break;
  2665. case 'bt':
  2666. $parts = explode(',', $value, 2);
  2667. $count = count($parts);
  2668. if ($count == 2) {
  2669. $sql = "($column >= ? AND $column <= ?)";
  2670. $arguments[] = $parts[0];
  2671. $arguments[] = $parts[1];
  2672. } else {
  2673. $sql = "FALSE";
  2674. }
  2675. break;
  2676. case 'in':
  2677. $parts = explode(',', $value);
  2678. $count = count($parts);
  2679. if ($count > 0) {
  2680. $qmarks = implode(',', str_split(str_repeat('?', $count)));
  2681. $sql = "$column IN ($qmarks)";
  2682. for ($i = 0; $i < $count; $i++) {
  2683. $arguments[] = $parts[$i];
  2684. }
  2685. } else {
  2686. $sql = "FALSE";
  2687. }
  2688. break;
  2689. case 'is':
  2690. $sql = "$column IS NULL";
  2691. break;
  2692. }
  2693. return $sql;
  2694. }
  2695. private function getSpatialFunctionName(string $operator): string
  2696. {
  2697. switch ($operator) {
  2698. case 'co':return 'ST_Contains';
  2699. case 'cr':return 'ST_Crosses';
  2700. case 'di':return 'ST_Disjoint';
  2701. case 'eq':return 'ST_Equals';
  2702. case 'in':return 'ST_Intersects';
  2703. case 'ov':return 'ST_Overlaps';
  2704. case 'to':return 'ST_Touches';
  2705. case 'wi':return 'ST_Within';
  2706. case 'ic':return 'ST_IsClosed';
  2707. case 'is':return 'ST_IsSimple';
  2708. case 'iv':return 'ST_IsValid';
  2709. }
  2710. }
  2711. private function hasSpatialArgument(string $operator): bool
  2712. {
  2713. return in_array($operator, ['ic', 'is', 'iv']) ? false : true;
  2714. }
  2715. private function getSpatialFunctionCall(string $functionName, string $column, bool $hasArgument): string
  2716. {
  2717. switch ($this->driver) {
  2718. case 'mysql':
  2719. case 'pgsql':
  2720. $argument = $hasArgument ? 'ST_GeomFromText(?)' : '';
  2721. return "$functionName($column, $argument)=TRUE";
  2722. case 'sqlsrv':
  2723. $functionName = str_replace('ST_', 'ST', $functionName);
  2724. $argument = $hasArgument ? 'geometry::STGeomFromText(?,0)' : '';
  2725. return "$column.$functionName($argument)=1";
  2726. }
  2727. }
  2728. private function getSpatialConditionSql(ColumnCondition $condition, array &$arguments): string
  2729. {
  2730. $column = $this->quoteColumnName($condition->getColumn());
  2731. $operator = $condition->getOperator();
  2732. $value = $condition->getValue();
  2733. $functionName = $this->getSpatialFunctionName($operator);
  2734. $hasArgument = $this->hasSpatialArgument($operator);
  2735. $sql = $this->getSpatialFunctionCall($functionName, $column, $hasArgument);
  2736. if ($hasArgument) {
  2737. $arguments[] = $value;
  2738. }
  2739. return $sql;
  2740. }
  2741. public function getWhereClause(Condition $condition, array &$arguments): string
  2742. {
  2743. if ($condition instanceof NoCondition) {
  2744. return '';
  2745. }
  2746. return ' WHERE ' . $this->getConditionSql($condition, $arguments);
  2747. }
  2748. }
  2749. // file: src/Tqdev/PhpCrudApi/Database/DataConverter.php
  2750. class DataConverter
  2751. {
  2752. private $driver;
  2753. public function __construct(string $driver)
  2754. {
  2755. $this->driver = $driver;
  2756. }
  2757. private function convertRecordValue($conversion, $value)
  2758. {
  2759. switch ($conversion) {
  2760. case 'boolean':
  2761. return $value ? true : false;
  2762. case 'integer':
  2763. return (int) $value;
  2764. }
  2765. return $value;
  2766. }
  2767. private function getRecordValueConversion(ReflectedColumn $column): string
  2768. {
  2769. if (in_array($this->driver, ['mysql', 'sqlsrv']) && $column->isBoolean()) {
  2770. return 'boolean';
  2771. }
  2772. if ($this->driver == 'sqlsrv' && $column->getType() == 'bigint') {
  2773. return 'integer';
  2774. }
  2775. return 'none';
  2776. }
  2777. public function convertRecords(ReflectedTable $table, array $columnNames, array &$records) /*: void*/
  2778. {
  2779. foreach ($columnNames as $columnName) {
  2780. $column = $table->getColumn($columnName);
  2781. $conversion = $this->getRecordValueConversion($column);
  2782. if ($conversion != 'none') {
  2783. foreach ($records as $i => $record) {
  2784. $value = $records[$i][$columnName];
  2785. if ($value === null) {
  2786. continue;
  2787. }
  2788. $records[$i][$columnName] = $this->convertRecordValue($conversion, $value);
  2789. }
  2790. }
  2791. }
  2792. }
  2793. private function convertInputValue($conversion, $value)
  2794. {
  2795. switch ($conversion) {
  2796. case 'base64url_to_base64':
  2797. return str_pad(strtr($value, '-_', '+/'), ceil(strlen($value) / 4) * 4, '=', STR_PAD_RIGHT);
  2798. }
  2799. return $value;
  2800. }
  2801. private function getInputValueConversion(ReflectedColumn $column): string
  2802. {
  2803. if ($column->isBinary()) {
  2804. return 'base64url_to_base64';
  2805. }
  2806. return 'none';
  2807. }
  2808. public function convertColumnValues(ReflectedTable $table, array &$columnValues) /*: void*/
  2809. {
  2810. $columnNames = array_keys($columnValues);
  2811. foreach ($columnNames as $columnName) {
  2812. $column = $table->getColumn($columnName);
  2813. $conversion = $this->getInputValueConversion($column);
  2814. if ($conversion != 'none') {
  2815. $value = $columnValues[$columnName];
  2816. if ($value !== null) {
  2817. $columnValues[$columnName] = $this->convertInputValue($conversion, $value);
  2818. }
  2819. }
  2820. }
  2821. }
  2822. }
  2823. // file: src/Tqdev/PhpCrudApi/Database/GenericDB.php
  2824. class GenericDB
  2825. {
  2826. private $driver;
  2827. private $database;
  2828. private $pdo;
  2829. private $reflection;
  2830. private $definition;
  2831. private $conditions;
  2832. private $columns;
  2833. private $converter;
  2834. private function getDsn(string $address, string $port = null, string $database = null): string
  2835. {
  2836. switch ($this->driver) {
  2837. case 'mysql':return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
  2838. case 'pgsql':return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
  2839. case 'sqlsrv':return "$this->driver:Server=$address,$port;Database=$database";
  2840. }
  2841. }
  2842. private function getCommands(): array
  2843. {
  2844. switch ($this->driver) {
  2845. case 'mysql':return [
  2846. 'SET SESSION sql_warnings=1;',
  2847. 'SET NAMES utf8mb4;',
  2848. 'SET SESSION sql_mode = "ANSI,TRADITIONAL";',
  2849. ];
  2850. case 'pgsql':return [
  2851. "SET NAMES 'UTF8';",
  2852. ];
  2853. case 'sqlsrv':return [
  2854. ];
  2855. }
  2856. }
  2857. private function getOptions(): array
  2858. {
  2859. $options = array(
  2860. \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  2861. \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
  2862. );
  2863. switch ($this->driver) {
  2864. case 'mysql':return $options + [
  2865. \PDO::ATTR_EMULATE_PREPARES => false,
  2866. \PDO::MYSQL_ATTR_FOUND_ROWS => true,
  2867. \PDO::ATTR_PERSISTENT => true,
  2868. ];
  2869. case 'pgsql':return $options + [
  2870. \PDO::ATTR_EMULATE_PREPARES => false,
  2871. \PDO::ATTR_PERSISTENT => true,
  2872. ];
  2873. case 'sqlsrv':return $options + [
  2874. \PDO::SQLSRV_ATTR_DIRECT_QUERY => false,
  2875. \PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
  2876. ];
  2877. }
  2878. }
  2879. public function __construct(string $driver, string $address, string $port = null, string $database = null, string $username = null, string $password = null)
  2880. {
  2881. $this->driver = $driver;
  2882. $this->database = $database;
  2883. $dsn = $this->getDsn($address, $port, $database);
  2884. $options = $this->getOptions();
  2885. $this->pdo = new \PDO($dsn, $username, $password, $options);
  2886. $commands = $this->getCommands();
  2887. foreach ($commands as $command) {
  2888. $this->pdo->query($command);
  2889. }
  2890. $this->reflection = new GenericReflection($this->pdo, $driver, $database);
  2891. $this->definition = new GenericDefinition($this->pdo, $driver, $database);
  2892. $this->conditions = new ConditionsBuilder($driver);
  2893. $this->columns = new ColumnsBuilder($driver);
  2894. $this->converter = new DataConverter($driver);
  2895. }
  2896. public function pdo(): \PDO
  2897. {
  2898. return $this->pdo;
  2899. }
  2900. public function reflection(): GenericReflection
  2901. {
  2902. return $this->reflection;
  2903. }
  2904. public function definition(): GenericDefinition
  2905. {
  2906. return $this->definition;
  2907. }
  2908. private function addMiddlewareConditions(string $tableName, Condition $condition): Condition
  2909. {
  2910. $condition1 = VariableStore::get("authorization.conditions.$tableName");
  2911. if ($condition1) {
  2912. $condition = $condition->_and($condition1);
  2913. }
  2914. $condition2 = VariableStore::get("multiTenancy.conditions.$tableName");
  2915. if ($condition2) {
  2916. $condition = $condition->_and($condition2);
  2917. }
  2918. return $condition;
  2919. }
  2920. public function createSingle(ReflectedTable $table, array $columnValues) /*: ?String*/
  2921. {
  2922. $this->converter->convertColumnValues($table, $columnValues);
  2923. $insertColumns = $this->columns->getInsert($table, $columnValues);
  2924. $tableName = $table->getName();
  2925. $pkName = $table->getPk()->getName();
  2926. $parameters = array_values($columnValues);
  2927. $sql = 'INSERT INTO "' . $tableName . '" ' . $insertColumns;
  2928. $stmt = $this->query($sql, $parameters);
  2929. if (isset($columnValues[$pkName])) {
  2930. return $columnValues[$pkName];
  2931. }
  2932. switch ($this->driver) {
  2933. case 'mysql':
  2934. $stmt = $this->query('SELECT LAST_INSERT_ID()', []);
  2935. break;
  2936. }
  2937. $pkValue = $stmt->fetchColumn(0);
  2938. if ($this->driver == 'sqlsrv' && $table->getPk()->getType() == 'bigint') {
  2939. return (int) $pkValue;
  2940. }
  2941. return $pkValue;
  2942. }
  2943. public function selectSingle(ReflectedTable $table, array $columnNames, string $id) /*: ?array*/
  2944. {
  2945. $selectColumns = $this->columns->getSelect($table, $columnNames);
  2946. $tableName = $table->getName();
  2947. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  2948. $condition = $this->addMiddlewareConditions($tableName, $condition);
  2949. $parameters = array();
  2950. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  2951. $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
  2952. $stmt = $this->query($sql, $parameters);
  2953. $record = $stmt->fetch() ?: null;
  2954. if ($record === null) {
  2955. return null;
  2956. }
  2957. $records = array($record);
  2958. $this->converter->convertRecords($table, $columnNames, $records);
  2959. return $records[0];
  2960. }
  2961. public function selectMultiple(ReflectedTable $table, array $columnNames, array $ids): array
  2962. {
  2963. if (count($ids) == 0) {
  2964. return [];
  2965. }
  2966. $selectColumns = $this->columns->getSelect($table, $columnNames);
  2967. $tableName = $table->getName();
  2968. $condition = new ColumnCondition($table->getPk(), 'in', implode(',', $ids));
  2969. $condition = $this->addMiddlewareConditions($tableName, $condition);
  2970. $parameters = array();
  2971. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  2972. $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
  2973. $stmt = $this->query($sql, $parameters);
  2974. $records = $stmt->fetchAll();
  2975. $this->converter->convertRecords($table, $columnNames, $records);
  2976. return $records;
  2977. }
  2978. public function selectCount(ReflectedTable $table, Condition $condition): int
  2979. {
  2980. $tableName = $table->getName();
  2981. $condition = $this->addMiddlewareConditions($tableName, $condition);
  2982. $parameters = array();
  2983. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  2984. $sql = 'SELECT COUNT(*) FROM "' . $tableName . '"' . $whereClause;
  2985. $stmt = $this->query($sql, $parameters);
  2986. return $stmt->fetchColumn(0);
  2987. }
  2988. public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
  2989. {
  2990. if ($limit == 0) {
  2991. return array();
  2992. }
  2993. $selectColumns = $this->columns->getSelect($table, $columnNames);
  2994. $tableName = $table->getName();
  2995. $condition = $this->addMiddlewareConditions($tableName, $condition);
  2996. $parameters = array();
  2997. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  2998. $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
  2999. $offsetLimit = $this->columns->getOffsetLimit($offset, $limit);
  3000. $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause . $orderBy . $offsetLimit;
  3001. $stmt = $this->query($sql, $parameters);
  3002. $records = $stmt->fetchAll();
  3003. $this->converter->convertRecords($table, $columnNames, $records);
  3004. return $records;
  3005. }
  3006. public function updateSingle(ReflectedTable $table, array $columnValues, string $id)
  3007. {
  3008. if (count($columnValues) == 0) {
  3009. return 0;
  3010. }
  3011. $this->converter->convertColumnValues($table, $columnValues);
  3012. $updateColumns = $this->columns->getUpdate($table, $columnValues);
  3013. $tableName = $table->getName();
  3014. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  3015. $condition = $this->addMiddlewareConditions($tableName, $condition);
  3016. $parameters = array_values($columnValues);
  3017. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  3018. $sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
  3019. $stmt = $this->query($sql, $parameters);
  3020. return $stmt->rowCount();
  3021. }
  3022. public function deleteSingle(ReflectedTable $table, string $id)
  3023. {
  3024. $tableName = $table->getName();
  3025. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  3026. $condition = $this->addMiddlewareConditions($tableName, $condition);
  3027. $parameters = array();
  3028. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  3029. $sql = 'DELETE FROM "' . $tableName . '" ' . $whereClause;
  3030. $stmt = $this->query($sql, $parameters);
  3031. return $stmt->rowCount();
  3032. }
  3033. public function incrementSingle(ReflectedTable $table, array $columnValues, string $id)
  3034. {
  3035. if (count($columnValues) == 0) {
  3036. return 0;
  3037. }
  3038. $this->converter->convertColumnValues($table, $columnValues);
  3039. $updateColumns = $this->columns->getIncrement($table, $columnValues);
  3040. $tableName = $table->getName();
  3041. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  3042. $condition = $this->addMiddlewareConditions($tableName, $condition);
  3043. $parameters = array_values($columnValues);
  3044. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  3045. $sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
  3046. $stmt = $this->query($sql, $parameters);
  3047. return $stmt->rowCount();
  3048. }
  3049. private function query(string $sql, array $parameters): \PDOStatement
  3050. {
  3051. $stmt = $this->pdo->prepare($sql);
  3052. $stmt->execute($parameters);
  3053. return $stmt;
  3054. }
  3055. }
  3056. // file: src/Tqdev/PhpCrudApi/Database/GenericDefinition.php
  3057. class GenericDefinition
  3058. {
  3059. private $pdo;
  3060. private $driver;
  3061. private $database;
  3062. private $typeConverter;
  3063. private $reflection;
  3064. public function __construct(\PDO $pdo, string $driver, string $database)
  3065. {
  3066. $this->pdo = $pdo;
  3067. $this->driver = $driver;
  3068. $this->database = $database;
  3069. $this->typeConverter = new TypeConverter($driver);
  3070. $this->reflection = new GenericReflection($pdo, $driver, $database);
  3071. }
  3072. private function quote(string $identifier): string
  3073. {
  3074. return '"' . str_replace('"', '', $identifier) . '"';
  3075. }
  3076. public function getColumnType(ReflectedColumn $column, bool $update): string
  3077. {
  3078. if ($this->driver == 'pgsql' && !$update && $column->getPk() && $this->canAutoIncrement($column)) {
  3079. return 'serial';
  3080. }
  3081. $type = $this->typeConverter->fromJdbc($column->getType());
  3082. if ($column->hasPrecision() && $column->hasScale()) {
  3083. $size = '(' . $column->getPrecision() . ',' . $column->getScale() . ')';
  3084. } else if ($column->hasPrecision()) {
  3085. $size = '(' . $column->getPrecision() . ')';
  3086. } else if ($column->hasLength()) {
  3087. $size = '(' . $column->getLength() . ')';
  3088. } else {
  3089. $size = '';
  3090. }
  3091. $null = $this->getColumnNullType($column, $update);
  3092. $auto = $this->getColumnAutoIncrement($column, $update);
  3093. return $type . $size . $null . $auto;
  3094. }
  3095. private function getPrimaryKey(string $tableName): string
  3096. {
  3097. $pks = $this->reflection->getTablePrimaryKeys($tableName);
  3098. if (count($pks) == 1) {
  3099. return $pks[0];
  3100. }
  3101. return "";
  3102. }
  3103. private function canAutoIncrement(ReflectedColumn $column): bool
  3104. {
  3105. return in_array($column->getType(), ['integer', 'bigint']);
  3106. }
  3107. private function getColumnAutoIncrement(ReflectedColumn $column, bool $update): string
  3108. {
  3109. if (!$this->canAutoIncrement($column)) {
  3110. return '';
  3111. }
  3112. switch ($this->driver) {
  3113. case 'mysql':
  3114. return $column->getPk() ? ' AUTO_INCREMENT' : '';
  3115. case 'pgsql':
  3116. case 'sqlsrv':
  3117. return '';
  3118. }
  3119. }
  3120. private function getColumnNullType(ReflectedColumn $column, bool $update): string
  3121. {
  3122. if ($this->driver == 'pgsql' && $update) {
  3123. return '';
  3124. }
  3125. return $column->getNullable() ? ' NULL' : ' NOT NULL';
  3126. }
  3127. private function getTableRenameSQL(string $tableName, string $newTableName): string
  3128. {
  3129. $p1 = $this->quote($tableName);
  3130. $p2 = $this->quote($newTableName);
  3131. switch ($this->driver) {
  3132. case 'mysql':
  3133. return "RENAME TABLE $p1 TO $p2";
  3134. case 'pgsql':
  3135. return "ALTER TABLE $p1 RENAME TO $p2";
  3136. case 'sqlsrv':
  3137. return "EXEC sp_rename $p1, $p2";
  3138. }
  3139. }
  3140. private function getColumnRenameSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3141. {
  3142. $p1 = $this->quote($tableName);
  3143. $p2 = $this->quote($columnName);
  3144. $p3 = $this->quote($newColumn->getName());
  3145. switch ($this->driver) {
  3146. case 'mysql':
  3147. $p4 = $this->getColumnType($newColumn, true);
  3148. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  3149. case 'pgsql':
  3150. return "ALTER TABLE $p1 RENAME COLUMN $p2 TO $p3";
  3151. case 'sqlsrv':
  3152. $p4 = $this->quote($tableName . '.' . $columnName);
  3153. return "EXEC sp_rename $p4, $p3, 'COLUMN'";
  3154. }
  3155. }
  3156. private function getColumnRetypeSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3157. {
  3158. $p1 = $this->quote($tableName);
  3159. $p2 = $this->quote($columnName);
  3160. $p3 = $this->quote($newColumn->getName());
  3161. $p4 = $this->getColumnType($newColumn, true);
  3162. switch ($this->driver) {
  3163. case 'mysql':
  3164. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  3165. case 'pgsql':
  3166. return "ALTER TABLE $p1 ALTER COLUMN $p3 TYPE $p4";
  3167. case 'sqlsrv':
  3168. return "ALTER TABLE $p1 ALTER COLUMN $p3 $p4";
  3169. }
  3170. }
  3171. private function getSetColumnNullableSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3172. {
  3173. $p1 = $this->quote($tableName);
  3174. $p2 = $this->quote($columnName);
  3175. $p3 = $this->quote($newColumn->getName());
  3176. $p4 = $this->getColumnType($newColumn, true);
  3177. switch ($this->driver) {
  3178. case 'mysql':
  3179. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  3180. case 'pgsql':
  3181. $p5 = $newColumn->getNullable() ? 'DROP NOT NULL' : 'SET NOT NULL';
  3182. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p5";
  3183. case 'sqlsrv':
  3184. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p4";
  3185. }
  3186. }
  3187. private function getSetColumnPkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3188. {
  3189. $p1 = $this->quote($tableName);
  3190. $p2 = $this->quote($columnName);
  3191. $p3 = $this->quote($tableName . '_pkey');
  3192. switch ($this->driver) {
  3193. case 'mysql':
  3194. $p4 = $newColumn->getPk() ? "ADD PRIMARY KEY ($p2)" : 'DROP PRIMARY KEY';
  3195. return "ALTER TABLE $p1 $p4";
  3196. case 'pgsql':
  3197. case 'sqlsrv':
  3198. $p4 = $newColumn->getPk() ? "ADD CONSTRAINT $p3 PRIMARY KEY ($p2)" : "DROP CONSTRAINT $p3";
  3199. return "ALTER TABLE $p1 $p4";
  3200. }
  3201. }
  3202. private function getSetColumnPkSequenceSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3203. {
  3204. $p1 = $this->quote($tableName);
  3205. $p2 = $this->quote($columnName);
  3206. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  3207. switch ($this->driver) {
  3208. case 'mysql':
  3209. return "select 1";
  3210. case 'pgsql':
  3211. return $newColumn->getPk() ? "CREATE SEQUENCE $p3 OWNED BY $p1.$p2" : "DROP SEQUENCE $p3";
  3212. case 'sqlsrv':
  3213. return $newColumn->getPk() ? "CREATE SEQUENCE $p3" : "DROP SEQUENCE $p3";
  3214. }
  3215. }
  3216. private function getSetColumnPkSequenceStartSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3217. {
  3218. $p1 = $this->quote($tableName);
  3219. $p2 = $this->quote($columnName);
  3220. switch ($this->driver) {
  3221. case 'mysql':
  3222. return "select 1";
  3223. case 'pgsql':
  3224. $p3 = $this->pdo->quote($tableName . '_' . $columnName . '_seq');
  3225. return "SELECT setval($p3, (SELECT max($p2)+1 FROM $p1));";
  3226. case 'sqlsrv':
  3227. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  3228. $p4 = $this->pdo->query("SELECT max($p2)+1 FROM $p1")->fetchColumn();
  3229. return "ALTER SEQUENCE $p3 RESTART WITH $p4";
  3230. }
  3231. }
  3232. private function getSetColumnPkDefaultSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3233. {
  3234. $p1 = $this->quote($tableName);
  3235. $p2 = $this->quote($columnName);
  3236. switch ($this->driver) {
  3237. case 'mysql':
  3238. $p3 = $this->quote($newColumn->getName());
  3239. $p4 = $this->getColumnType($newColumn, true);
  3240. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  3241. case 'pgsql':
  3242. if ($newColumn->getPk()) {
  3243. $p3 = $this->pdo->quote($tableName . '_' . $columnName . '_seq');
  3244. $p4 = "SET DEFAULT nextval($p3)";
  3245. } else {
  3246. $p4 = 'DROP DEFAULT';
  3247. }
  3248. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p4";
  3249. case 'sqlsrv':
  3250. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  3251. $p4 = $this->quote($tableName . '_' . $columnName . '_def');
  3252. if ($newColumn->getPk()) {
  3253. return "ALTER TABLE $p1 ADD CONSTRAINT $p4 DEFAULT NEXT VALUE FOR $p3 FOR $p2";
  3254. } else {
  3255. return "ALTER TABLE $p1 DROP CONSTRAINT $p4";
  3256. }
  3257. }
  3258. }
  3259. private function getAddColumnFkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3260. {
  3261. $p1 = $this->quote($tableName);
  3262. $p2 = $this->quote($columnName);
  3263. $p3 = $this->quote($tableName . '_' . $columnName . '_fkey');
  3264. $p4 = $this->quote($newColumn->getFk());
  3265. $p5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
  3266. return "ALTER TABLE $p1 ADD CONSTRAINT $p3 FOREIGN KEY ($p2) REFERENCES $p4 ($p5)";
  3267. }
  3268. private function getRemoveColumnFkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  3269. {
  3270. $p1 = $this->quote($tableName);
  3271. $p2 = $this->quote($tableName . '_' . $columnName . '_fkey');
  3272. switch ($this->driver) {
  3273. case 'mysql':
  3274. return "ALTER TABLE $p1 DROP FOREIGN KEY $p2";
  3275. case 'pgsql':
  3276. case 'sqlsrv':
  3277. return "ALTER TABLE $p1 DROP CONSTRAINT $p2";
  3278. }
  3279. }
  3280. private function getAddTableSQL(ReflectedTable $newTable): string
  3281. {
  3282. $tableName = $newTable->getName();
  3283. $p1 = $this->quote($tableName);
  3284. $fields = [];
  3285. $constraints = [];
  3286. foreach ($newTable->getColumnNames() as $columnName) {
  3287. $pkColumn = $this->getPrimaryKey($tableName);
  3288. $newColumn = $newTable->getColumn($columnName);
  3289. $f1 = $this->quote($columnName);
  3290. $f2 = $this->getColumnType($newColumn, false);
  3291. $f3 = $this->quote($tableName . '_' . $columnName . '_fkey');
  3292. $f4 = $this->quote($newColumn->getFk());
  3293. $f5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
  3294. $f6 = $this->quote($tableName . '_' . $pkColumn . '_pkey');
  3295. $fields[] = "$f1 $f2";
  3296. if ($newColumn->getPk()) {
  3297. $constraints[] = "CONSTRAINT $f6 PRIMARY KEY ($f1)";
  3298. }
  3299. if ($newColumn->getFk()) {
  3300. $constraints[] = "CONSTRAINT $f3 FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
  3301. }
  3302. }
  3303. $p2 = implode(',', array_merge($fields, $constraints));
  3304. return "CREATE TABLE $p1 ($p2);";
  3305. }
  3306. private function getAddColumnSQL(string $tableName, ReflectedColumn $newColumn): string
  3307. {
  3308. $p1 = $this->quote($tableName);
  3309. $p2 = $this->quote($newColumn->getName());
  3310. $p3 = $this->getColumnType($newColumn, false);
  3311. switch ($this->driver) {
  3312. case 'mysql':
  3313. case 'pgsql':
  3314. return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
  3315. case 'sqlsrv':
  3316. return "ALTER TABLE $p1 ADD $p2 $p3";
  3317. }
  3318. }
  3319. private function getRemoveTableSQL(string $tableName): string
  3320. {
  3321. $p1 = $this->quote($tableName);
  3322. switch ($this->driver) {
  3323. case 'mysql':
  3324. case 'pgsql':
  3325. return "DROP TABLE $p1 CASCADE;";
  3326. case 'sqlsrv':
  3327. return "DROP TABLE $p1;";
  3328. }
  3329. }
  3330. private function getRemoveColumnSQL(string $tableName, string $columnName): string
  3331. {
  3332. $p1 = $this->quote($tableName);
  3333. $p2 = $this->quote($columnName);
  3334. switch ($this->driver) {
  3335. case 'mysql':
  3336. case 'pgsql':
  3337. return "ALTER TABLE $p1 DROP COLUMN $p2 CASCADE;";
  3338. case 'sqlsrv':
  3339. return "ALTER TABLE $p1 DROP COLUMN $p2;";
  3340. }
  3341. }
  3342. public function renameTable(string $tableName, string $newTableName)
  3343. {
  3344. $sql = $this->getTableRenameSQL($tableName, $newTableName);
  3345. return $this->query($sql);
  3346. }
  3347. public function renameColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
  3348. {
  3349. $sql = $this->getColumnRenameSQL($tableName, $columnName, $newColumn);
  3350. return $this->query($sql);
  3351. }
  3352. public function retypeColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
  3353. {
  3354. $sql = $this->getColumnRetypeSQL($tableName, $columnName, $newColumn);
  3355. return $this->query($sql);
  3356. }
  3357. public function setColumnNullable(string $tableName, string $columnName, ReflectedColumn $newColumn)
  3358. {
  3359. $sql = $this->getSetColumnNullableSQL($tableName, $columnName, $newColumn);
  3360. return $this->query($sql);
  3361. }
  3362. public function addColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  3363. {
  3364. $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
  3365. $this->query($sql);
  3366. if ($this->canAutoIncrement($newColumn)) {
  3367. $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
  3368. $this->query($sql);
  3369. $sql = $this->getSetColumnPkSequenceStartSQL($tableName, $columnName, $newColumn);
  3370. $this->query($sql);
  3371. $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
  3372. $this->query($sql);
  3373. }
  3374. return true;
  3375. }
  3376. public function removeColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  3377. {
  3378. if ($this->canAutoIncrement($newColumn)) {
  3379. $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
  3380. $this->query($sql);
  3381. $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
  3382. $this->query($sql);
  3383. }
  3384. $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
  3385. $this->query($sql);
  3386. return true;
  3387. }
  3388. public function addColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  3389. {
  3390. $sql = $this->getAddColumnFkConstraintSQL($tableName, $columnName, $newColumn);
  3391. return $this->query($sql);
  3392. }
  3393. public function removeColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  3394. {
  3395. $sql = $this->getRemoveColumnFkConstraintSQL($tableName, $columnName, $newColumn);
  3396. return $this->query($sql);
  3397. }
  3398. public function addTable(ReflectedTable $newTable)
  3399. {
  3400. $sql = $this->getAddTableSQL($newTable);
  3401. return $this->query($sql);
  3402. }
  3403. public function addColumn(string $tableName, ReflectedColumn $newColumn)
  3404. {
  3405. $sql = $this->getAddColumnSQL($tableName, $newColumn);
  3406. return $this->query($sql);
  3407. }
  3408. public function removeTable(string $tableName)
  3409. {
  3410. $sql = $this->getRemoveTableSQL($tableName);
  3411. return $this->query($sql);
  3412. }
  3413. public function removeColumn(string $tableName, string $columnName)
  3414. {
  3415. $sql = $this->getRemoveColumnSQL($tableName, $columnName);
  3416. return $this->query($sql);
  3417. }
  3418. private function query(string $sql): bool
  3419. {
  3420. $stmt = $this->pdo->prepare($sql);
  3421. return $stmt->execute();
  3422. }
  3423. }
  3424. // file: src/Tqdev/PhpCrudApi/Database/GenericReflection.php
  3425. class GenericReflection
  3426. {
  3427. private $pdo;
  3428. private $driver;
  3429. private $database;
  3430. private $typeConverter;
  3431. public function __construct(\PDO $pdo, string $driver, string $database)
  3432. {
  3433. $this->pdo = $pdo;
  3434. $this->driver = $driver;
  3435. $this->database = $database;
  3436. $this->typeConverter = new TypeConverter($driver);
  3437. }
  3438. public function getIgnoredTables(): array
  3439. {
  3440. switch ($this->driver) {
  3441. case 'mysql':return [];
  3442. case 'pgsql':return ['spatial_ref_sys', 'raster_columns', 'raster_overviews', 'geography_columns', 'geometry_columns'];
  3443. case 'sqlsrv':return [];
  3444. }
  3445. }
  3446. private function getTablesSQL(): string
  3447. {
  3448. switch ($this->driver) {
  3449. case 'mysql':return 'SELECT "TABLE_NAME", "TABLE_TYPE" FROM "INFORMATION_SCHEMA"."TABLES" WHERE "TABLE_TYPE" IN (\'BASE TABLE\' , \'VIEW\') AND "TABLE_SCHEMA" = ? ORDER BY BINARY "TABLE_NAME"';
  3450. case 'pgsql':return 'SELECT c.relname as "TABLE_NAME", c.relkind as "TABLE_TYPE" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN (\'r\', \'v\') AND n.nspname <> \'pg_catalog\' AND n.nspname <> \'information_schema\' AND n.nspname !~ \'^pg_toast\' AND pg_catalog.pg_table_is_visible(c.oid) AND \'\' <> ? ORDER BY "TABLE_NAME";';
  3451. case 'sqlsrv':return 'SELECT o.name as "TABLE_NAME", o.xtype as "TABLE_TYPE" FROM sysobjects o WHERE o.xtype IN (\'U\', \'V\') ORDER BY "TABLE_NAME"';
  3452. }
  3453. }
  3454. private function getTableColumnsSQL(): string
  3455. {
  3456. switch ($this->driver) {
  3457. case 'mysql':return 'SELECT "COLUMN_NAME", "IS_NULLABLE", "DATA_TYPE", "CHARACTER_MAXIMUM_LENGTH", "NUMERIC_PRECISION", "NUMERIC_SCALE" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ?';
  3458. case 'pgsql':return 'SELECT a.attname AS "COLUMN_NAME", case when a.attnotnull then \'NO\' else \'YES\' end as "IS_NULLABLE", pg_catalog.format_type(a.atttypid, -1) as "DATA_TYPE", case when a.atttypmod < 0 then NULL else a.atttypmod-4 end as "CHARACTER_MAXIMUM_LENGTH", case when a.atttypid != 1700 then NULL else ((a.atttypmod - 4) >> 16) & 65535 end as "NUMERIC_PRECISION", case when a.atttypid != 1700 then NULL else (a.atttypmod - 4) & 65535 end as "NUMERIC_SCALE" FROM pg_attribute a JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND a.attnum > 0 AND NOT a.attisdropped;';
  3459. case 'sqlsrv':return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ?';
  3460. }
  3461. }
  3462. private function getTablePrimaryKeysSQL(): string
  3463. {
  3464. switch ($this->driver) {
  3465. case 'mysql':return 'SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."KEY_COLUMN_USAGE" WHERE "CONSTRAINT_NAME" = \'PRIMARY\' AND "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ?';
  3466. case 'pgsql':return 'SELECT a.attname AS "COLUMN_NAME" FROM pg_attribute a JOIN pg_constraint c ON (c.conrelid, c.conkey[1]) = (a.attrelid, a.attnum) JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND c.contype = \'p\'';
  3467. case 'sqlsrv':return 'SELECT c.NAME as "COLUMN_NAME" FROM sys.key_constraints kc inner join sys.objects t on t.object_id = kc.parent_object_id INNER JOIN sys.index_columns ic ON kc.parent_object_id = ic.object_id and kc.unique_index_id = ic.index_id INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id WHERE kc.type = \'PK\' and t.object_id = OBJECT_ID(?) and \'\' <> ?';
  3468. }
  3469. }
  3470. private function getTableForeignKeysSQL(): string
  3471. {
  3472. switch ($this->driver) {
  3473. case 'mysql':return 'SELECT "COLUMN_NAME", "REFERENCED_TABLE_NAME" FROM "INFORMATION_SCHEMA"."KEY_COLUMN_USAGE" WHERE "REFERENCED_TABLE_NAME" IS NOT NULL AND "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ?';
  3474. case 'pgsql':return 'SELECT a.attname AS "COLUMN_NAME", c.confrelid::regclass::text AS "REFERENCED_TABLE_NAME" FROM pg_attribute a JOIN pg_constraint c ON (c.conrelid, c.conkey[1]) = (a.attrelid, a.attnum) JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND c.contype = \'f\'';
  3475. case 'sqlsrv':return 'SELECT COL_NAME(fc.parent_object_id, fc.parent_column_id) AS "COLUMN_NAME", OBJECT_NAME (f.referenced_object_id) AS "REFERENCED_TABLE_NAME" FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id WHERE f.parent_object_id = OBJECT_ID(?) and \'\' <> ?';
  3476. }
  3477. }
  3478. public function getDatabaseName(): string
  3479. {
  3480. return $this->database;
  3481. }
  3482. public function getTables(): array
  3483. {
  3484. $sql = $this->getTablesSQL();
  3485. $results = $this->query($sql, [$this->database]);
  3486. foreach ($results as &$result) {
  3487. switch ($this->driver) {
  3488. case 'mysql':
  3489. $map = ['BASE TABLE' => 'table', 'VIEW' => 'view'];
  3490. $result['TABLE_TYPE'] = $map[$result['TABLE_TYPE']];
  3491. break;
  3492. case 'pgsql':
  3493. $map = ['r' => 'table', 'v' => 'view'];
  3494. $result['TABLE_TYPE'] = $map[$result['TABLE_TYPE']];
  3495. break;
  3496. case 'sqlsrv':
  3497. $map = ['U' => 'table', 'V' => 'view'];
  3498. $result['TABLE_TYPE'] = $map[trim($result['TABLE_TYPE'])];
  3499. break;
  3500. }
  3501. }
  3502. return $results;
  3503. }
  3504. public function getTableColumns(string $tableName, string $type): array
  3505. {
  3506. $sql = $this->getTableColumnsSQL();
  3507. $results = $this->query($sql, [$tableName, $this->database]);
  3508. if ($type == 'view') {
  3509. foreach ($results as &$result) {
  3510. $result['IS_NULLABLE'] = false;
  3511. }
  3512. }
  3513. return $results;
  3514. }
  3515. public function getTablePrimaryKeys(string $tableName): array
  3516. {
  3517. $sql = $this->getTablePrimaryKeysSQL();
  3518. $results = $this->query($sql, [$tableName, $this->database]);
  3519. $primaryKeys = [];
  3520. foreach ($results as $result) {
  3521. $primaryKeys[] = $result['COLUMN_NAME'];
  3522. }
  3523. return $primaryKeys;
  3524. }
  3525. public function getTableForeignKeys(string $tableName): array
  3526. {
  3527. $sql = $this->getTableForeignKeysSQL();
  3528. $results = $this->query($sql, [$tableName, $this->database]);
  3529. $foreignKeys = [];
  3530. foreach ($results as $result) {
  3531. $foreignKeys[$result['COLUMN_NAME']] = $result['REFERENCED_TABLE_NAME'];
  3532. }
  3533. return $foreignKeys;
  3534. }
  3535. public function toJdbcType(string $type, int $size): string
  3536. {
  3537. return $this->typeConverter->toJdbc($type, $size);
  3538. }
  3539. private function query(string $sql, array $parameters): array
  3540. {
  3541. $stmt = $this->pdo->prepare($sql);
  3542. $stmt->execute($parameters);
  3543. return $stmt->fetchAll();
  3544. }
  3545. }
  3546. // file: src/Tqdev/PhpCrudApi/Database/TypeConverter.php
  3547. class TypeConverter
  3548. {
  3549. private $driver;
  3550. public function __construct(string $driver)
  3551. {
  3552. $this->driver = $driver;
  3553. }
  3554. private $fromJdbc = [
  3555. 'mysql' => [
  3556. 'clob' => 'longtext',
  3557. 'boolean' => 'bit',
  3558. 'blob' => 'longblob',
  3559. 'timestamp' => 'datetime',
  3560. ],
  3561. 'pgsql' => [
  3562. 'clob' => 'text',
  3563. 'blob' => 'bytea',
  3564. ],
  3565. 'sqlsrv' => [
  3566. 'boolean' => 'bit',
  3567. 'varchar' => 'nvarchar',
  3568. 'clob' => 'ntext',
  3569. 'blob' => 'image',
  3570. ],
  3571. ];
  3572. private $toJdbc = [
  3573. 'simplified' => [
  3574. 'char' => 'varchar',
  3575. 'longvarchar' => 'clob',
  3576. 'nchar' => 'varchar',
  3577. 'nvarchar' => 'varchar',
  3578. 'longnvarchar' => 'clob',
  3579. 'binary' => 'varbinary',
  3580. 'longvarbinary' => 'blob',
  3581. 'tinyint' => 'integer',
  3582. 'smallint' => 'integer',
  3583. 'real' => 'float',
  3584. 'numeric' => 'decimal',
  3585. 'time_with_timezone' => 'time',
  3586. 'timestamp_with_timezone' => 'timestamp',
  3587. ],
  3588. 'mysql' => [
  3589. 'bit' => 'boolean',
  3590. 'tinyblob' => 'blob',
  3591. 'mediumblob' => 'blob',
  3592. 'longblob' => 'blob',
  3593. 'tinytext' => 'clob',
  3594. 'mediumtext' => 'clob',
  3595. 'longtext' => 'clob',
  3596. 'text' => 'clob',
  3597. 'mediumint' => 'integer',
  3598. 'int' => 'integer',
  3599. 'polygon' => 'geometry',
  3600. 'point' => 'geometry',
  3601. 'datetime' => 'timestamp',
  3602. 'year' => 'integer',
  3603. 'enum' => 'varchar',
  3604. 'json' => 'clob',
  3605. ],
  3606. 'pgsql' => [
  3607. 'bigserial' => 'bigint',
  3608. 'bit varying' => 'bit',
  3609. 'box' => 'geometry',
  3610. 'bytea' => 'blob',
  3611. 'character varying' => 'varchar',
  3612. 'character' => 'char',
  3613. 'cidr' => 'varchar',
  3614. 'circle' => 'geometry',
  3615. 'double precision' => 'double',
  3616. 'inet' => 'integer',
  3617. 'json' => 'clob',
  3618. 'jsonb' => 'clob',
  3619. 'line' => 'geometry',
  3620. 'lseg' => 'geometry',
  3621. 'macaddr' => 'varchar',
  3622. 'money' => 'decimal',
  3623. 'path' => 'geometry',
  3624. 'point' => 'geometry',
  3625. 'polygon' => 'geometry',
  3626. 'real' => 'float',
  3627. 'serial' => 'integer',
  3628. 'text' => 'clob',
  3629. 'time without time zone' => 'time',
  3630. 'time with time zone' => 'time_with_timezone',
  3631. 'timestamp without time zone' => 'timestamp',
  3632. 'timestamp with time zone' => 'timestamp_with_timezone',
  3633. 'uuid' => 'char',
  3634. 'xml' => 'clob',
  3635. ],
  3636. 'sqlsrv' => [
  3637. 'varbinary(0)' => 'blob',
  3638. 'bit' => 'boolean',
  3639. 'datetime' => 'timestamp',
  3640. 'datetime2' => 'timestamp',
  3641. 'float' => 'double',
  3642. 'image' => 'blob',
  3643. 'int' => 'integer',
  3644. 'money' => 'decimal',
  3645. 'ntext' => 'clob',
  3646. 'smalldatetime' => 'timestamp',
  3647. 'smallmoney' => 'decimal',
  3648. 'text' => 'clob',
  3649. 'timestamp' => 'binary',
  3650. 'udt' => 'varbinary',
  3651. 'uniqueidentifier' => 'char',
  3652. 'xml' => 'clob',
  3653. ],
  3654. ];
  3655. private $valid = [
  3656. 'bigint' => true,
  3657. 'binary' => true,
  3658. 'bit' => true,
  3659. 'blob' => true,
  3660. 'boolean' => true,
  3661. 'char' => true,
  3662. 'clob' => true,
  3663. 'date' => true,
  3664. 'decimal' => true,
  3665. 'distinct' => true,
  3666. 'double' => true,
  3667. 'float' => true,
  3668. 'integer' => true,
  3669. 'longnvarchar' => true,
  3670. 'longvarbinary' => true,
  3671. 'longvarchar' => true,
  3672. 'nchar' => true,
  3673. 'nclob' => true,
  3674. 'numeric' => true,
  3675. 'nvarchar' => true,
  3676. 'real' => true,
  3677. 'smallint' => true,
  3678. 'time' => true,
  3679. 'time_with_timezone' => true,
  3680. 'timestamp' => true,
  3681. 'timestamp_with_timezone' => true,
  3682. 'tinyint' => true,
  3683. 'varbinary' => true,
  3684. 'varchar' => true,
  3685. 'geometry' => true,
  3686. ];
  3687. public function toJdbc(string $type, int $size): string
  3688. {
  3689. $jdbcType = strtolower($type);
  3690. if (isset($this->toJdbc[$this->driver]["$jdbcType($size)"])) {
  3691. $jdbcType = $this->toJdbc[$this->driver]["$jdbcType($size)"];
  3692. }
  3693. if (isset($this->toJdbc[$this->driver][$jdbcType])) {
  3694. $jdbcType = $this->toJdbc[$this->driver][$jdbcType];
  3695. }
  3696. if (isset($this->toJdbc['simplified'][$jdbcType])) {
  3697. $jdbcType = $this->toJdbc['simplified'][$jdbcType];
  3698. }
  3699. if (!isset($this->valid[$jdbcType])) {
  3700. throw new \Exception("Unsupported type '$jdbcType' for driver '$this->driver'");
  3701. }
  3702. return $jdbcType;
  3703. }
  3704. public function fromJdbc(string $type): string
  3705. {
  3706. $jdbcType = strtolower($type);
  3707. if (isset($this->fromJdbc[$this->driver][$jdbcType])) {
  3708. $jdbcType = $this->fromJdbc[$this->driver][$jdbcType];
  3709. }
  3710. return $jdbcType;
  3711. }
  3712. }
  3713. // file: src/Tqdev/PhpCrudApi/Middleware/Base/Middleware.php
  3714. abstract class Middleware implements MiddlewareInterface
  3715. {
  3716. protected $next;
  3717. protected $responder;
  3718. private $properties;
  3719. public function __construct(Router $router, Responder $responder, array $properties)
  3720. {
  3721. $router->load($this);
  3722. $this->responder = $responder;
  3723. $this->properties = $properties;
  3724. }
  3725. protected function getArrayProperty(string $key, string $default): array
  3726. {
  3727. return array_filter(array_map('trim', explode(',', $this->getProperty($key, $default))));
  3728. }
  3729. protected function getProperty(string $key, $default)
  3730. {
  3731. return isset($this->properties[$key]) ? $this->properties[$key] : $default;
  3732. }
  3733. }
  3734. // file: src/Tqdev/PhpCrudApi/Middleware/Communication/VariableStore.php
  3735. class VariableStore
  3736. {
  3737. static $values = array();
  3738. public static function get(string $key)
  3739. {
  3740. if (isset(self::$values[$key])) {
  3741. return self::$values[$key];
  3742. }
  3743. return null;
  3744. }
  3745. public static function set(string $key, /* object */ $value)
  3746. {
  3747. self::$values[$key] = $value;
  3748. }
  3749. }
  3750. // file: src/Tqdev/PhpCrudApi/Middleware/Router/Router.php
  3751. interface Router extends RequestHandlerInterface
  3752. {
  3753. public function register(string $method, string $path, array $handler);
  3754. public function load(Middleware $middleware);
  3755. public function route(ServerRequestInterface $request): ResponseInterface;
  3756. }
  3757. // file: src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php
  3758. class SimpleRouter implements Router
  3759. {
  3760. private $responder;
  3761. private $cache;
  3762. private $ttl;
  3763. private $debug;
  3764. private $registration;
  3765. private $routes;
  3766. private $routeHandlers;
  3767. private $middlewares;
  3768. public function __construct(Responder $responder, Cache $cache, int $ttl, bool $debug)
  3769. {
  3770. $this->responder = $responder;
  3771. $this->cache = $cache;
  3772. $this->ttl = $ttl;
  3773. $this->debug = $debug;
  3774. $this->registration = true;
  3775. $this->routes = $this->loadPathTree();
  3776. $this->routeHandlers = [];
  3777. $this->middlewares = array();
  3778. }
  3779. private function loadPathTree(): PathTree
  3780. {
  3781. $data = $this->cache->get('PathTree');
  3782. if ($data != '') {
  3783. $tree = PathTree::fromJson(json_decode(gzuncompress($data)));
  3784. $this->registration = false;
  3785. } else {
  3786. $tree = new PathTree();
  3787. }
  3788. return $tree;
  3789. }
  3790. public function register(string $method, string $path, array $handler)
  3791. {
  3792. $routeNumber = count($this->routeHandlers);
  3793. $this->routeHandlers[$routeNumber] = $handler;
  3794. if ($this->registration) {
  3795. $parts = explode('/', trim($path, '/'));
  3796. array_unshift($parts, $method);
  3797. $this->routes->put($parts, $routeNumber);
  3798. }
  3799. }
  3800. public function load(Middleware $middleware) /*: void*/
  3801. {
  3802. array_push($this->middlewares, $middleware);
  3803. }
  3804. public function route(ServerRequestInterface $request): ResponseInterface
  3805. {
  3806. if ($this->registration) {
  3807. $data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
  3808. $this->cache->set('PathTree', $data, $this->ttl);
  3809. }
  3810. return $this->handle($request);
  3811. }
  3812. private function getRouteNumbers(ServerRequestInterface $request): array
  3813. {
  3814. $method = strtoupper($request->getMethod());
  3815. $path = explode('/', trim($request->getRequestTarget(), '/'));
  3816. array_unshift($path, $method);
  3817. return $this->routes->match($path);
  3818. }
  3819. public function handle(ServerRequestInterface $request): ResponseInterface
  3820. {
  3821. if (count($this->middlewares)) {
  3822. $handler = array_pop($this->middlewares);
  3823. return $handler->process($request, $this);
  3824. }
  3825. $routeNumbers = $this->getRouteNumbers($request);
  3826. if (count($routeNumbers) == 0) {
  3827. return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getRequestTarget());
  3828. }
  3829. try {
  3830. $response = call_user_func($this->routeHandlers[$routeNumbers[0]], $request);
  3831. } catch (\PDOException $e) {
  3832. if (strpos(strtolower($e->getMessage()), 'duplicate') !== false) {
  3833. $response = $this->responder->error(ErrorCode::DUPLICATE_KEY_EXCEPTION, '');
  3834. } elseif (strpos(strtolower($e->getMessage()), 'default value') !== false) {
  3835. $response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
  3836. } elseif (strpos(strtolower($e->getMessage()), 'allow nulls') !== false) {
  3837. $response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
  3838. } elseif (strpos(strtolower($e->getMessage()), 'constraint') !== false) {
  3839. $response = $this->responder->error(ErrorCode::DATA_INTEGRITY_VIOLATION, '');
  3840. }
  3841. if ($this->debug) {
  3842. $response = ResponseUtils::addExceptionHeaders($response, $e);
  3843. }
  3844. }
  3845. return $response;
  3846. }
  3847. }
  3848. // file: src/Tqdev/PhpCrudApi/Middleware/AjaxOnlyMiddleware.php
  3849. class AjaxOnlyMiddleware extends Middleware
  3850. {
  3851. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  3852. {
  3853. $method = $request->getMethod();
  3854. $excludeMethods = $this->getArrayProperty('excludeMethods', 'OPTIONS,GET');
  3855. if (!in_array($method, $excludeMethods)) {
  3856. $headerName = $this->getProperty('headerName', 'X-Requested-With');
  3857. $headerValue = $this->getProperty('headerValue', 'XMLHttpRequest');
  3858. if ($headerValue != RequestUtils::getHeader($request, $headerName)) {
  3859. return $this->responder->error(ErrorCode::ONLY_AJAX_REQUESTS_ALLOWED, $method);
  3860. }
  3861. }
  3862. return $next->handle($request);
  3863. }
  3864. }
  3865. // file: src/Tqdev/PhpCrudApi/Middleware/AuthorizationMiddleware.php
  3866. class AuthorizationMiddleware extends Middleware
  3867. {
  3868. private $reflection;
  3869. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  3870. {
  3871. parent::__construct($router, $responder, $properties);
  3872. $this->reflection = $reflection;
  3873. }
  3874. private function handleColumns(string $operation, string $tableName) /*: void*/
  3875. {
  3876. $columnHandler = $this->getProperty('columnHandler', '');
  3877. if ($columnHandler) {
  3878. $table = $this->reflection->getTable($tableName);
  3879. foreach ($table->getColumnNames() as $columnName) {
  3880. $allowed = call_user_func($columnHandler, $operation, $tableName, $columnName);
  3881. if (!$allowed) {
  3882. $table->removeColumn($columnName);
  3883. }
  3884. }
  3885. }
  3886. }
  3887. private function handleTable(string $operation, string $tableName) /*: void*/
  3888. {
  3889. if (!$this->reflection->hasTable($tableName)) {
  3890. return;
  3891. }
  3892. $tableHandler = $this->getProperty('tableHandler', '');
  3893. if ($tableHandler) {
  3894. $allowed = call_user_func($tableHandler, $operation, $tableName);
  3895. if (!$allowed) {
  3896. $this->reflection->removeTable($tableName);
  3897. } else {
  3898. $this->handleColumns($operation, $tableName);
  3899. }
  3900. }
  3901. }
  3902. private function handleRecords(string $operation, string $tableName) /*: void*/
  3903. {
  3904. if (!$this->reflection->hasTable($tableName)) {
  3905. return;
  3906. }
  3907. $recordHandler = $this->getProperty('recordHandler', '');
  3908. if ($recordHandler) {
  3909. $query = call_user_func($recordHandler, $operation, $tableName);
  3910. $filters = new FilterInfo();
  3911. $table = $this->reflection->getTable($tableName);
  3912. $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
  3913. parse_str($query, $params);
  3914. $condition = $filters->getCombinedConditions($table, $params);
  3915. VariableStore::set("authorization.conditions.$tableName", $condition);
  3916. }
  3917. }
  3918. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  3919. {
  3920. $path = RequestUtils::getPathSegment($request, 1);
  3921. $operation = RequestUtils::getOperation($request);
  3922. $tableNames = RequestUtils::getTableNames($request, $this->reflection);
  3923. foreach ($tableNames as $tableName) {
  3924. $this->handleTable($operation, $tableName);
  3925. if ($path == 'records') {
  3926. $this->handleRecords($operation, $tableName);
  3927. }
  3928. }
  3929. if ($path == 'openapi') {
  3930. VariableStore::set('authorization.tableHandler', $this->getProperty('tableHandler', ''));
  3931. VariableStore::set('authorization.columnHandler', $this->getProperty('columnHandler', ''));
  3932. }
  3933. return $next->handle($request);
  3934. }
  3935. }
  3936. // file: src/Tqdev/PhpCrudApi/Middleware/BasicAuthMiddleware.php
  3937. class BasicAuthMiddleware extends Middleware
  3938. {
  3939. private function hasCorrectPassword(string $username, string $password, array &$passwords): bool
  3940. {
  3941. $hash = isset($passwords[$username]) ? $passwords[$username] : false;
  3942. if ($hash && password_verify($password, $hash)) {
  3943. if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
  3944. $passwords[$username] = password_hash($password, PASSWORD_DEFAULT);
  3945. }
  3946. return true;
  3947. }
  3948. return false;
  3949. }
  3950. private function getValidUsername(string $username, string $password, string $passwordFile): string
  3951. {
  3952. $passwords = $this->readPasswords($passwordFile);
  3953. $valid = $this->hasCorrectPassword($username, $password, $passwords);
  3954. $this->writePasswords($passwordFile, $passwords);
  3955. return $valid ? $username : '';
  3956. }
  3957. private function readPasswords(string $passwordFile): array
  3958. {
  3959. $passwords = [];
  3960. $passwordLines = file($passwordFile);
  3961. foreach ($passwordLines as $passwordLine) {
  3962. if (strpos($passwordLine, ':') !== false) {
  3963. list($username, $hash) = explode(':', trim($passwordLine), 2);
  3964. if (strlen($hash) > 0 && $hash[0] != '$') {
  3965. $hash = password_hash($hash, PASSWORD_DEFAULT);
  3966. }
  3967. $passwords[$username] = $hash;
  3968. }
  3969. }
  3970. return $passwords;
  3971. }
  3972. private function writePasswords(string $passwordFile, array $passwords): bool
  3973. {
  3974. $success = false;
  3975. $passwordFileContents = '';
  3976. foreach ($passwords as $username => $hash) {
  3977. $passwordFileContents .= "$username:$hash\n";
  3978. }
  3979. if (file_get_contents($passwordFile) != $passwordFileContents) {
  3980. $success = file_put_contents($passwordFile, $passwordFileContents) !== false;
  3981. }
  3982. return $success;
  3983. }
  3984. private function getAuthorizationCredentials(ServerRequestInterface $request): string
  3985. {
  3986. if (isset($_SERVER['PHP_AUTH_USER'])) {
  3987. return $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'];
  3988. }
  3989. $header = RequestUtils::getHeader($request, 'Authorization');
  3990. $parts = explode(' ', trim($header), 2);
  3991. if (count($parts) != 2) {
  3992. return '';
  3993. }
  3994. if ($parts[0] != 'Basic') {
  3995. return '';
  3996. }
  3997. return base64_decode(strtr($parts[1], '-_', '+/'));
  3998. }
  3999. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4000. {
  4001. if (session_status() == PHP_SESSION_NONE) {
  4002. session_start();
  4003. }
  4004. $credentials = $this->getAuthorizationCredentials($request);
  4005. if ($credentials) {
  4006. list($username, $password) = array('', '');
  4007. if (strpos($credentials, ':') !== false) {
  4008. list($username, $password) = explode(':', $credentials, 2);
  4009. }
  4010. $passwordFile = $this->getProperty('passwordFile', '.htpasswd');
  4011. $validUser = $this->getValidUsername($username, $password, $passwordFile);
  4012. $_SESSION['username'] = $validUser;
  4013. if (!$validUser) {
  4014. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
  4015. }
  4016. if (!headers_sent()) {
  4017. session_regenerate_id();
  4018. }
  4019. }
  4020. if (!isset($_SESSION['username']) || !$_SESSION['username']) {
  4021. $authenticationMode = $this->getProperty('mode', 'required');
  4022. if ($authenticationMode == 'required') {
  4023. $response = $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
  4024. $realm = $this->getProperty('realm', 'Username and password required');
  4025. $response = $response->withHeader('WWW-Authenticate', "Basic realm=\"$realm\"");
  4026. return $response;
  4027. }
  4028. }
  4029. return $next->handle($request);
  4030. }
  4031. }
  4032. // file: src/Tqdev/PhpCrudApi/Middleware/CorsMiddleware.php
  4033. class CorsMiddleware extends Middleware
  4034. {
  4035. private function isOriginAllowed(string $origin, string $allowedOrigins): bool
  4036. {
  4037. $found = false;
  4038. foreach (explode(',', $allowedOrigins) as $allowedOrigin) {
  4039. $hostname = preg_quote(strtolower(trim($allowedOrigin)));
  4040. $regex = '/^' . str_replace('\*', '.*', $hostname) . '$/';
  4041. if (preg_match($regex, $origin)) {
  4042. $found = true;
  4043. break;
  4044. }
  4045. }
  4046. return $found;
  4047. }
  4048. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4049. {
  4050. $method = $request->getMethod();
  4051. $origin = count($request->getHeader('Origin')) ? $request->getHeader('Origin')[0] : '';
  4052. $allowedOrigins = $this->getProperty('allowedOrigins', '*');
  4053. if ($origin && !$this->isOriginAllowed($origin, $allowedOrigins)) {
  4054. $response = $this->responder->error(ErrorCode::ORIGIN_FORBIDDEN, $origin);
  4055. } elseif ($method == 'OPTIONS') {
  4056. $response = ResponseFactory::fromStatus(ResponseFactory::OK);
  4057. $allowHeaders = $this->getProperty('allowHeaders', 'Content-Type, X-XSRF-TOKEN, X-Authorization');
  4058. if ($allowHeaders) {
  4059. $response = $response->withHeader('Access-Control-Allow-Headers', $allowHeaders);
  4060. }
  4061. $allowMethods = $this->getProperty('allowMethods', 'OPTIONS, GET, PUT, POST, DELETE, PATCH');
  4062. if ($allowMethods) {
  4063. $response = $response->withHeader('Access-Control-Allow-Methods', $allowMethods);
  4064. }
  4065. $allowCredentials = $this->getProperty('allowCredentials', 'true');
  4066. if ($allowCredentials) {
  4067. $response = $response->withHeader('Access-Control-Allow-Credentials', $allowCredentials);
  4068. }
  4069. $maxAge = $this->getProperty('maxAge', '1728000');
  4070. if ($maxAge) {
  4071. $response = $response->withHeader('Access-Control-Max-Age', $maxAge);
  4072. }
  4073. $exposeHeaders = $this->getProperty('exposeHeaders', '');
  4074. if ($exposeHeaders) {
  4075. $response = $response->withHeader('Access-Control-Expose-Headers', $exposeHeaders);
  4076. }
  4077. } else {
  4078. $response = $next->handle($request);
  4079. }
  4080. if ($origin) {
  4081. $allowCredentials = $this->getProperty('allowCredentials', 'true');
  4082. if ($allowCredentials) {
  4083. $response = $response->withHeader('Access-Control-Allow-Credentials', $allowCredentials);
  4084. }
  4085. $response = $response->withHeader('Access-Control-Allow-Origin', $origin);
  4086. }
  4087. return $response;
  4088. }
  4089. }
  4090. // file: src/Tqdev/PhpCrudApi/Middleware/CustomizationMiddleware.php
  4091. class CustomizationMiddleware extends Middleware
  4092. {
  4093. private $reflection;
  4094. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  4095. {
  4096. parent::__construct($router, $responder, $properties);
  4097. $this->reflection = $reflection;
  4098. }
  4099. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4100. {
  4101. $operation = RequestUtils::getOperation($request);
  4102. $tableName = RequestUtils::getPathSegment($request, 2);
  4103. $beforeHandler = $this->getProperty('beforeHandler', '');
  4104. $environment = (object) array();
  4105. if ($beforeHandler !== '') {
  4106. $result = call_user_func($beforeHandler, $operation, $tableName, $request, $environment);
  4107. $request = $result ?: $request;
  4108. }
  4109. $response = $next->handle($request);
  4110. $afterHandler = $this->getProperty('afterHandler', '');
  4111. if ($afterHandler !== '') {
  4112. $result = call_user_func($afterHandler, $operation, $tableName, $response, $environment);
  4113. $response = $result ?: $response;
  4114. }
  4115. return $response;
  4116. }
  4117. }
  4118. // file: src/Tqdev/PhpCrudApi/Middleware/FirewallMiddleware.php
  4119. class FirewallMiddleware extends Middleware
  4120. {
  4121. private function ipMatch(string $ip, string $cidr): bool
  4122. {
  4123. if (strpos($cidr, '/') !== false) {
  4124. list($subnet, $mask) = explode('/', trim($cidr));
  4125. if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
  4126. return true;
  4127. }
  4128. } else {
  4129. if (ip2long($ip) == ip2long($cidr)) {
  4130. return true;
  4131. }
  4132. }
  4133. return false;
  4134. }
  4135. private function isIpAllowed(string $ipAddress, string $allowedIpAddresses): bool
  4136. {
  4137. foreach (explode(',', $allowedIpAddresses) as $allowedIp) {
  4138. if ($this->ipMatch($ipAddress, $allowedIp)) {
  4139. return true;
  4140. }
  4141. }
  4142. return false;
  4143. }
  4144. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4145. {
  4146. $reverseProxy = $this->getProperty('reverseProxy', '');
  4147. if ($reverseProxy) {
  4148. $ipAddress = array_pop(explode(',', $request->getHeader('X-Forwarded-For')));
  4149. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  4150. $ipAddress = $_SERVER['REMOTE_ADDR'];
  4151. } else {
  4152. $ipAddress = '127.0.0.1';
  4153. }
  4154. $allowedIpAddresses = $this->getProperty('allowedIpAddresses', '');
  4155. if (!$this->isIpAllowed($ipAddress, $allowedIpAddresses)) {
  4156. $response = $this->responder->error(ErrorCode::TEMPORARY_OR_PERMANENTLY_BLOCKED, '');
  4157. } else {
  4158. $response = $next->handle($request);
  4159. }
  4160. return $response;
  4161. }
  4162. }
  4163. // file: src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php
  4164. class IpAddressMiddleware extends Middleware
  4165. {
  4166. private $reflection;
  4167. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  4168. {
  4169. parent::__construct($router, $responder, $properties);
  4170. $this->reflection = $reflection;
  4171. }
  4172. private function callHandler($record, string $operation, ReflectedTable $table) /*: object */
  4173. {
  4174. $context = (array) $record;
  4175. $columnNames = $this->getProperty('columns', '');
  4176. if ($columnNames) {
  4177. foreach (explode(',', $columnNames) as $columnName) {
  4178. if ($table->hasColumn($columnName)) {
  4179. if ($operation == 'create') {
  4180. $context[$columnName] = $_SERVER['REMOTE_ADDR'];
  4181. } else {
  4182. unset($context[$columnName]);
  4183. }
  4184. }
  4185. }
  4186. }
  4187. return (object) $context;
  4188. }
  4189. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4190. {
  4191. $operation = RequestUtils::getOperation($request);
  4192. if (in_array($operation, ['create', 'update', 'increment'])) {
  4193. $tableNames = $this->getProperty('tables', '');
  4194. $tableName = RequestUtils::getPathSegment($request, 2);
  4195. if (!$tableNames || in_array($tableName, explode(',', $tableNames))) {
  4196. if ($this->reflection->hasTable($tableName)) {
  4197. $record = $request->getParsedBody();
  4198. if ($record !== null) {
  4199. $table = $this->reflection->getTable($tableName);
  4200. if (is_array($record)) {
  4201. foreach ($record as &$r) {
  4202. $r = $this->callHandler($r, $operation, $table);
  4203. }
  4204. } else {
  4205. $record = $this->callHandler($record, $operation, $table);
  4206. }
  4207. $request = $request->withParsedBody($record);
  4208. }
  4209. }
  4210. }
  4211. }
  4212. return $next->handle($request);
  4213. }
  4214. }
  4215. // file: src/Tqdev/PhpCrudApi/Middleware/JoinLimitsMiddleware.php
  4216. class JoinLimitsMiddleware extends Middleware
  4217. {
  4218. private $reflection;
  4219. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  4220. {
  4221. parent::__construct($router, $responder, $properties);
  4222. $this->reflection = $reflection;
  4223. }
  4224. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4225. {
  4226. $operation = RequestUtils::getOperation($request);
  4227. $params = RequestUtils::getParams($request);
  4228. if (in_array($operation, ['read', 'list']) && isset($params['join'])) {
  4229. $maxDepth = (int) $this->getProperty('depth', '3');
  4230. $maxTables = (int) $this->getProperty('tables', '10');
  4231. $maxRecords = (int) $this->getProperty('records', '1000');
  4232. $tableCount = 0;
  4233. $joinPaths = array();
  4234. for ($i = 0; $i < count($params['join']); $i++) {
  4235. $joinPath = array();
  4236. $tables = explode(',', $params['join'][$i]);
  4237. for ($depth = 0; $depth < min($maxDepth, count($tables)); $depth++) {
  4238. array_push($joinPath, $tables[$depth]);
  4239. $tableCount += 1;
  4240. if ($tableCount == $maxTables) {
  4241. break;
  4242. }
  4243. }
  4244. array_push($joinPaths, implode(',', $joinPath));
  4245. if ($tableCount == $maxTables) {
  4246. break;
  4247. }
  4248. }
  4249. $params['join'] = $joinPaths;
  4250. $request = RequestUtils::setParams($request, $params);
  4251. VariableStore::set("joinLimits.maxRecords", $maxRecords);
  4252. }
  4253. return $next->handle($request);
  4254. }
  4255. }
  4256. // file: src/Tqdev/PhpCrudApi/Middleware/JwtAuthMiddleware.php
  4257. class JwtAuthMiddleware extends Middleware
  4258. {
  4259. private function getVerifiedClaims(string $token, int $time, int $leeway, int $ttl, string $secret, array $requirements): array
  4260. {
  4261. $algorithms = array(
  4262. 'HS256' => 'sha256',
  4263. 'HS384' => 'sha384',
  4264. 'HS512' => 'sha512',
  4265. 'RS256' => 'sha256',
  4266. 'RS384' => 'sha384',
  4267. 'RS512' => 'sha512',
  4268. );
  4269. $token = explode('.', $token);
  4270. if (count($token) < 3) {
  4271. return array();
  4272. }
  4273. $header = json_decode(base64_decode(strtr($token[0], '-_', '+/')), true);
  4274. if (!$secret) {
  4275. return array();
  4276. }
  4277. if ($header['typ'] != 'JWT') {
  4278. return array();
  4279. }
  4280. $algorithm = $header['alg'];
  4281. if (!isset($algorithms[$algorithm])) {
  4282. return array();
  4283. }
  4284. if (!empty($requirements['alg']) && !in_array($algorithm, $requirements['alg'])) {
  4285. return array();
  4286. }
  4287. $hmac = $algorithms[$algorithm];
  4288. $signature = base64_decode(strtr($token[2], '-_', '+/'));
  4289. $data = "$token[0].$token[1]";
  4290. switch ($algorithm[0]) {
  4291. case 'H':
  4292. $hash = hash_hmac($hmac, $data, $secret, true);
  4293. if (function_exists('hash_equals')) {
  4294. $equals = hash_equals($signature, $hash);
  4295. } else {
  4296. $equals = $signature == $hash;
  4297. }
  4298. if (!$equals) {
  4299. return array();
  4300. }
  4301. break;
  4302. case 'R':
  4303. $equals = openssl_verify($data, $signature, $secret, $hmac) == 1;
  4304. if (!$equals) {
  4305. return array();
  4306. }
  4307. break;
  4308. }
  4309. $claims = json_decode(base64_decode(strtr($token[1], '-_', '+/')), true);
  4310. if (!$claims) {
  4311. return array();
  4312. }
  4313. foreach ($requirements as $field => $values) {
  4314. if (!empty($values)) {
  4315. if ($field != 'alg') {
  4316. if (!isset($claims[$field]) || !in_array($claims[$field], $values)) {
  4317. return array();
  4318. }
  4319. }
  4320. }
  4321. }
  4322. if (isset($claims['nbf']) && $time + $leeway < $claims['nbf']) {
  4323. return array();
  4324. }
  4325. if (isset($claims['iat']) && $time + $leeway < $claims['iat']) {
  4326. return array();
  4327. }
  4328. if (isset($claims['exp']) && $time - $leeway > $claims['exp']) {
  4329. return array();
  4330. }
  4331. if (isset($claims['iat']) && !isset($claims['exp'])) {
  4332. if ($time - $leeway > $claims['iat'] + $ttl) {
  4333. return array();
  4334. }
  4335. }
  4336. return $claims;
  4337. }
  4338. private function getClaims(string $token): array
  4339. {
  4340. $time = (int) $this->getProperty('time', time());
  4341. $leeway = (int) $this->getProperty('leeway', '5');
  4342. $ttl = (int) $this->getProperty('ttl', '30');
  4343. $secret = $this->getProperty('secret', '');
  4344. $requirements = array(
  4345. 'alg' => $this->getArrayProperty('algorithms', ''),
  4346. 'aud' => $this->getArrayProperty('audiences', ''),
  4347. 'iss' => $this->getArrayProperty('issuers', ''),
  4348. );
  4349. if (!$secret) {
  4350. return array();
  4351. }
  4352. return $this->getVerifiedClaims($token, $time, $leeway, $ttl, $secret, $requirements);
  4353. }
  4354. private function getAuthorizationToken(ServerRequestInterface $request): string
  4355. {
  4356. $headerName = $this->getProperty('header', 'X-Authorization');
  4357. $headerValue = RequestUtils::getHeader($request, $headerName);
  4358. $parts = explode(' ', trim($headerValue), 2);
  4359. if (count($parts) != 2) {
  4360. return '';
  4361. }
  4362. if ($parts[0] != 'Bearer') {
  4363. return '';
  4364. }
  4365. return $parts[1];
  4366. }
  4367. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4368. {
  4369. if (session_status() == PHP_SESSION_NONE) {
  4370. session_start();
  4371. }
  4372. $token = $this->getAuthorizationToken($request);
  4373. if ($token) {
  4374. $claims = $this->getClaims($token);
  4375. $_SESSION['claims'] = $claims;
  4376. if (empty($claims)) {
  4377. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, 'JWT');
  4378. }
  4379. if (!headers_sent()) {
  4380. session_regenerate_id();
  4381. }
  4382. }
  4383. if (empty($_SESSION['claims'])) {
  4384. $authenticationMode = $this->getProperty('mode', 'required');
  4385. if ($authenticationMode == 'required') {
  4386. return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
  4387. }
  4388. }
  4389. return $next->handle($request);
  4390. }
  4391. }
  4392. // file: src/Tqdev/PhpCrudApi/Middleware/MultiTenancyMiddleware.php
  4393. class MultiTenancyMiddleware extends Middleware
  4394. {
  4395. private $reflection;
  4396. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  4397. {
  4398. parent::__construct($router, $responder, $properties);
  4399. $this->reflection = $reflection;
  4400. }
  4401. private function getCondition(string $tableName, array $pairs): Condition
  4402. {
  4403. $condition = new NoCondition();
  4404. $table = $this->reflection->getTable($tableName);
  4405. foreach ($pairs as $k => $v) {
  4406. $condition = $condition->_and(new ColumnCondition($table->getColumn($k), 'eq', $v));
  4407. }
  4408. return $condition;
  4409. }
  4410. private function getPairs($handler, string $operation, string $tableName): array
  4411. {
  4412. $result = array();
  4413. $pairs = call_user_func($handler, $operation, $tableName);
  4414. $table = $this->reflection->getTable($tableName);
  4415. foreach ($pairs as $k => $v) {
  4416. if ($table->hasColumn($k)) {
  4417. $result[$k] = $v;
  4418. }
  4419. }
  4420. return $result;
  4421. }
  4422. private function handleRecord(ServerRequestInterface $request, string $operation, array $pairs): ServerRequestInterface
  4423. {
  4424. $record = $request->getParsedBody();
  4425. if ($record === null) {
  4426. return $request;
  4427. }
  4428. $multi = is_array($record);
  4429. $records = $multi ? $record : [$record];
  4430. foreach ($records as &$record) {
  4431. foreach ($pairs as $column => $value) {
  4432. if ($operation == 'create') {
  4433. $record->$column = $value;
  4434. } else {
  4435. if (isset($record->$column)) {
  4436. unset($record->$column);
  4437. }
  4438. }
  4439. }
  4440. }
  4441. return $request->withParsedBody($multi ? $records : $records[0]);
  4442. }
  4443. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4444. {
  4445. $handler = $this->getProperty('handler', '');
  4446. if ($handler !== '') {
  4447. $path = RequestUtils::getPathSegment($request, 1);
  4448. if ($path == 'records') {
  4449. $operation = RequestUtils::getOperation($request);
  4450. $tableNames = RequestUtils::getTableNames($request, $this->reflection);
  4451. foreach ($tableNames as $i => $tableName) {
  4452. if (!$this->reflection->hasTable($tableName)) {
  4453. continue;
  4454. }
  4455. $pairs = $this->getPairs($handler, $operation, $tableName);
  4456. if ($i == 0) {
  4457. if (in_array($operation, ['create', 'update', 'increment'])) {
  4458. $request = $this->handleRecord($request, $operation, $pairs);
  4459. }
  4460. }
  4461. $condition = $this->getCondition($tableName, $pairs);
  4462. VariableStore::set("multiTenancy.conditions.$tableName", $condition);
  4463. }
  4464. }
  4465. }
  4466. return $next->handle($request);
  4467. }
  4468. }
  4469. // file: src/Tqdev/PhpCrudApi/Middleware/PageLimitsMiddleware.php
  4470. class PageLimitsMiddleware extends Middleware
  4471. {
  4472. private $reflection;
  4473. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  4474. {
  4475. parent::__construct($router, $responder, $properties);
  4476. $this->reflection = $reflection;
  4477. }
  4478. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4479. {
  4480. $operation = RequestUtils::getOperation($request);
  4481. if ($operation == 'list') {
  4482. $params = RequestUtils::getParams($request);
  4483. $maxPage = (int) $this->getProperty('pages', '100');
  4484. if (isset($params['page']) && $params['page'] && $maxPage > 0) {
  4485. if (strpos($params['page'][0], ',') === false) {
  4486. $page = $params['page'][0];
  4487. } else {
  4488. list($page, $size) = explode(',', $params['page'][0], 2);
  4489. }
  4490. if ($page > $maxPage) {
  4491. return $this->responder->error(ErrorCode::PAGINATION_FORBIDDEN, '');
  4492. }
  4493. }
  4494. $maxSize = (int) $this->getProperty('records', '1000');
  4495. if (!isset($params['size']) || !$params['size'] && $maxSize > 0) {
  4496. $params['size'] = array($maxSize);
  4497. } else {
  4498. $params['size'] = array(min($params['size'][0], $maxSize));
  4499. }
  4500. $request = RequestUtils::setParams($request, $params);
  4501. }
  4502. return $next->handle($request);
  4503. }
  4504. }
  4505. // file: src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php
  4506. class SanitationMiddleware extends Middleware
  4507. {
  4508. private $reflection;
  4509. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  4510. {
  4511. parent::__construct($router, $responder, $properties);
  4512. $this->reflection = $reflection;
  4513. }
  4514. private function callHandler($handler, $record, string $operation, ReflectedTable $table) /*: object */
  4515. {
  4516. $context = (array) $record;
  4517. $tableName = $table->getName();
  4518. foreach ($context as $columnName => &$value) {
  4519. if ($table->hasColumn($columnName)) {
  4520. $column = $table->getColumn($columnName);
  4521. $value = call_user_func($handler, $operation, $tableName, $column->serialize(), $value);
  4522. }
  4523. }
  4524. return (object) $context;
  4525. }
  4526. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4527. {
  4528. $operation = RequestUtils::getOperation($request);
  4529. if (in_array($operation, ['create', 'update', 'increment'])) {
  4530. $tableName = RequestUtils::getPathSegment($request, 2);
  4531. if ($this->reflection->hasTable($tableName)) {
  4532. $record = $request->getParsedBody();
  4533. if ($record !== null) {
  4534. $handler = $this->getProperty('handler', '');
  4535. if ($handler !== '') {
  4536. $table = $this->reflection->getTable($tableName);
  4537. if (is_array($record)) {
  4538. foreach ($record as &$r) {
  4539. $r = $this->callHandler($handler, $r, $operation, $table);
  4540. }
  4541. } else {
  4542. $record = $this->callHandler($handler, $record, $operation, $table);
  4543. }
  4544. $request = $request->withParsedBody($record);
  4545. }
  4546. }
  4547. }
  4548. }
  4549. return $next->handle($request);
  4550. }
  4551. }
  4552. // file: src/Tqdev/PhpCrudApi/Middleware/ValidationMiddleware.php
  4553. class ValidationMiddleware extends Middleware
  4554. {
  4555. private $reflection;
  4556. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  4557. {
  4558. parent::__construct($router, $responder, $properties);
  4559. $this->reflection = $reflection;
  4560. }
  4561. private function callHandler($handler, $record, string $operation, ReflectedTable $table) /*: ResponseInterface?*/
  4562. {
  4563. $context = (array) $record;
  4564. $details = array();
  4565. $tableName = $table->getName();
  4566. foreach ($context as $columnName => $value) {
  4567. if ($table->hasColumn($columnName)) {
  4568. $column = $table->getColumn($columnName);
  4569. $valid = call_user_func($handler, $operation, $tableName, $column->serialize(), $value, $context);
  4570. if ($valid !== true && $valid !== '') {
  4571. $details[$columnName] = $valid;
  4572. }
  4573. }
  4574. }
  4575. if (count($details) > 0) {
  4576. return $this->responder->error(ErrorCode::INPUT_VALIDATION_FAILED, $tableName, $details);
  4577. }
  4578. return null;
  4579. }
  4580. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4581. {
  4582. $operation = RequestUtils::getOperation($request);
  4583. if (in_array($operation, ['create', 'update', 'increment'])) {
  4584. $tableName = RequestUtils::getPathSegment($request, 2);
  4585. if ($this->reflection->hasTable($tableName)) {
  4586. $record = $request->getParsedBody();
  4587. if ($record !== null) {
  4588. $handler = $this->getProperty('handler', '');
  4589. if ($handler !== '') {
  4590. $table = $this->reflection->getTable($tableName);
  4591. if (is_array($record)) {
  4592. foreach ($record as $r) {
  4593. $response = $this->callHandler($handler, $r, $operation, $table);
  4594. if ($response !== null) {
  4595. return $response;
  4596. }
  4597. }
  4598. } else {
  4599. $response = $this->callHandler($handler, $record, $operation, $table);
  4600. if ($response !== null) {
  4601. return $response;
  4602. }
  4603. }
  4604. }
  4605. }
  4606. }
  4607. }
  4608. return $next->handle($request);
  4609. }
  4610. }
  4611. // file: src/Tqdev/PhpCrudApi/Middleware/XsrfMiddleware.php
  4612. class XsrfMiddleware extends Middleware
  4613. {
  4614. private function getToken(): string
  4615. {
  4616. $cookieName = $this->getProperty('cookieName', 'XSRF-TOKEN');
  4617. if (isset($_COOKIE[$cookieName])) {
  4618. $token = $_COOKIE[$cookieName];
  4619. } else {
  4620. $secure = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
  4621. $token = bin2hex(random_bytes(8));
  4622. if (!headers_sent()) {
  4623. setcookie($cookieName, $token, 0, '', '', $secure);
  4624. }
  4625. }
  4626. return $token;
  4627. }
  4628. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  4629. {
  4630. $token = $this->getToken();
  4631. $method = $request->getMethod();
  4632. $excludeMethods = $this->getArrayProperty('excludeMethods', 'OPTIONS,GET');
  4633. if (!in_array($method, $excludeMethods)) {
  4634. $headerName = $this->getProperty('headerName', 'X-XSRF-TOKEN');
  4635. if ($token != $request->getHeader($headerName)) {
  4636. return $this->responder->error(ErrorCode::BAD_OR_MISSING_XSRF_TOKEN, '');
  4637. }
  4638. }
  4639. return $next->handle($request);
  4640. }
  4641. }
  4642. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php
  4643. class OpenApiBuilder
  4644. {
  4645. private $openapi;
  4646. private $reflection;
  4647. private $operations = [
  4648. 'list' => 'get',
  4649. 'create' => 'post',
  4650. 'read' => 'get',
  4651. 'update' => 'put',
  4652. 'delete' => 'delete',
  4653. 'increment' => 'patch',
  4654. ];
  4655. private $types = [
  4656. 'integer' => ['type' => 'integer', 'format' => 'int32'],
  4657. 'bigint' => ['type' => 'integer', 'format' => 'int64'],
  4658. 'varchar' => ['type' => 'string'],
  4659. 'clob' => ['type' => 'string'],
  4660. 'varbinary' => ['type' => 'string', 'format' => 'byte'],
  4661. 'blob' => ['type' => 'string', 'format' => 'byte'],
  4662. 'decimal' => ['type' => 'string'],
  4663. 'float' => ['type' => 'number', 'format' => 'float'],
  4664. 'double' => ['type' => 'number', 'format' => 'double'],
  4665. 'date' => ['type' => 'string', 'format' => 'date'],
  4666. 'time' => ['type' => 'string', 'format' => 'date-time'],
  4667. 'timestamp' => ['type' => 'string', 'format' => 'date-time'],
  4668. 'geometry' => ['type' => 'string'],
  4669. 'boolean' => ['type' => 'boolean'],
  4670. ];
  4671. public function __construct(ReflectionService $reflection, $base)
  4672. {
  4673. $this->reflection = $reflection;
  4674. $this->openapi = new OpenApiDefinition($base);
  4675. }
  4676. private function getServerUrl(): string
  4677. {
  4678. $protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
  4679. $port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80);
  4680. $host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'];
  4681. $port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
  4682. $path = @trim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '/openapi')), '/');
  4683. return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
  4684. }
  4685. private function getAllTableReferences(): array
  4686. {
  4687. $tableReferences = array();
  4688. foreach ($this->reflection->getTableNames() as $tableName) {
  4689. $table = $this->reflection->getTable($tableName);
  4690. foreach ($table->getColumnNames() as $columnName) {
  4691. $column = $table->getColumn($columnName);
  4692. $referencedTableName = $column->getFk();
  4693. if ($referencedTableName) {
  4694. if (!isset($tableReferences[$referencedTableName])) {
  4695. $tableReferences[$referencedTableName] = array();
  4696. }
  4697. $tableReferences[$referencedTableName][] = "$tableName.$columnName";
  4698. }
  4699. }
  4700. }
  4701. return $tableReferences;
  4702. }
  4703. public function build(): OpenApiDefinition
  4704. {
  4705. $this->openapi->set("openapi", "3.0.0");
  4706. if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
  4707. $this->openapi->set("servers|0|url", $this->getServerUrl());
  4708. }
  4709. $tableNames = $this->reflection->getTableNames();
  4710. foreach ($tableNames as $tableName) {
  4711. $this->setPath($tableName);
  4712. }
  4713. $this->openapi->set("components|responses|pk_integer|description", "inserted primary key value (integer)");
  4714. $this->openapi->set("components|responses|pk_integer|content|application/json|schema|type", "integer");
  4715. $this->openapi->set("components|responses|pk_integer|content|application/json|schema|format", "int64");
  4716. $this->openapi->set("components|responses|pk_string|description", "inserted primary key value (string)");
  4717. $this->openapi->set("components|responses|pk_string|content|application/json|schema|type", "string");
  4718. $this->openapi->set("components|responses|pk_string|content|application/json|schema|format", "uuid");
  4719. $this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
  4720. $this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
  4721. $this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
  4722. $tableReferences = $this->getAllTableReferences();
  4723. foreach ($tableNames as $tableName) {
  4724. $references = isset($tableReferences[$tableName]) ? $tableReferences[$tableName] : array();
  4725. $this->setComponentSchema($tableName, $references);
  4726. $this->setComponentResponse($tableName);
  4727. $this->setComponentRequestBody($tableName);
  4728. }
  4729. $this->setComponentParameters();
  4730. foreach ($tableNames as $index => $tableName) {
  4731. $this->setTag($index, $tableName);
  4732. }
  4733. return $this->openapi;
  4734. }
  4735. private function isOperationOnTableAllowed(string $operation, string $tableName): bool
  4736. {
  4737. $tableHandler = VariableStore::get('authorization.tableHandler');
  4738. if (!$tableHandler) {
  4739. return true;
  4740. }
  4741. return (bool) call_user_func($tableHandler, $operation, $tableName);
  4742. }
  4743. private function isOperationOnColumnAllowed(string $operation, string $tableName, string $columnName): bool
  4744. {
  4745. $columnHandler = VariableStore::get('authorization.columnHandler');
  4746. if (!$columnHandler) {
  4747. return true;
  4748. }
  4749. return (bool) call_user_func($columnHandler, $operation, $tableName, $columnName);
  4750. }
  4751. private function setPath(string $tableName) /*: void*/
  4752. {
  4753. $table = $this->reflection->getTable($tableName);
  4754. $type = $table->getType();
  4755. $pk = $table->getPk();
  4756. $pkName = $pk ? $pk->getName() : '';
  4757. foreach ($this->operations as $operation => $method) {
  4758. if (!$pkName && $operation != 'list') {
  4759. continue;
  4760. }
  4761. if ($type != 'table' && $operation != 'list') {
  4762. continue;
  4763. }
  4764. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  4765. continue;
  4766. }
  4767. $parameters = [];
  4768. if (in_array($operation, ['list', 'create'])) {
  4769. $path = sprintf('/records/%s', $tableName);
  4770. if ($operation == 'list') {
  4771. $parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
  4772. }
  4773. } else {
  4774. $path = sprintf('/records/%s/{%s}', $tableName, $pkName);
  4775. if ($operation == 'read') {
  4776. $parameters = ['pk', 'include', 'exclude', 'join'];
  4777. } else {
  4778. $parameters = ['pk'];
  4779. }
  4780. }
  4781. foreach ($parameters as $p => $parameter) {
  4782. $this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
  4783. }
  4784. if (in_array($operation, ['create', 'update', 'increment'])) {
  4785. $this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . urlencode($tableName));
  4786. }
  4787. $this->openapi->set("paths|$path|$method|tags|0", "$tableName");
  4788. $this->openapi->set("paths|$path|$method|description", "$operation $tableName");
  4789. switch ($operation) {
  4790. case 'list':
  4791. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . urlencode($tableName));
  4792. break;
  4793. case 'create':
  4794. if ($pk->getType() == 'integer') {
  4795. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
  4796. } else {
  4797. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
  4798. }
  4799. break;
  4800. case 'read':
  4801. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . urlencode($tableName));
  4802. break;
  4803. case 'update':
  4804. case 'delete':
  4805. case 'increment':
  4806. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
  4807. break;
  4808. }
  4809. }
  4810. }
  4811. private function setComponentSchema(string $tableName, array $references) /*: void*/
  4812. {
  4813. $table = $this->reflection->getTable($tableName);
  4814. $type = $table->getType();
  4815. $pk = $table->getPk();
  4816. $pkName = $pk ? $pk->getName() : '';
  4817. foreach ($this->operations as $operation => $method) {
  4818. if (!$pkName && $operation != 'list') {
  4819. continue;
  4820. }
  4821. if ($type != 'table' && $operation != 'list') {
  4822. continue;
  4823. }
  4824. if ($operation == 'delete') {
  4825. continue;
  4826. }
  4827. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  4828. continue;
  4829. }
  4830. if ($operation == 'list') {
  4831. $this->openapi->set("components|schemas|$operation-$tableName|type", "object");
  4832. $this->openapi->set("components|schemas|$operation-$tableName|properties|results|type", "integer");
  4833. $this->openapi->set("components|schemas|$operation-$tableName|properties|results|format", "int64");
  4834. $this->openapi->set("components|schemas|$operation-$tableName|properties|records|type", "array");
  4835. $prefix = "components|schemas|$operation-$tableName|properties|records|items";
  4836. } else {
  4837. $prefix = "components|schemas|$operation-$tableName";
  4838. }
  4839. $this->openapi->set("$prefix|type", "object");
  4840. foreach ($table->getColumnNames() as $columnName) {
  4841. if (!$this->isOperationOnColumnAllowed($operation, $tableName, $columnName)) {
  4842. continue;
  4843. }
  4844. $column = $table->getColumn($columnName);
  4845. $properties = $this->types[$column->getType()];
  4846. foreach ($properties as $key => $value) {
  4847. $this->openapi->set("$prefix|properties|$columnName|$key", $value);
  4848. }
  4849. if ($column->getPk()) {
  4850. $this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
  4851. $this->openapi->set("$prefix|properties|$columnName|x-referenced", $references);
  4852. }
  4853. $fk = $column->getFk();
  4854. if ($fk) {
  4855. $this->openapi->set("$prefix|properties|$columnName|x-references", $fk);
  4856. }
  4857. }
  4858. }
  4859. }
  4860. private function setComponentResponse(string $tableName) /*: void*/
  4861. {
  4862. $table = $this->reflection->getTable($tableName);
  4863. $type = $table->getType();
  4864. $pk = $table->getPk();
  4865. $pkName = $pk ? $pk->getName() : '';
  4866. foreach (['list', 'read'] as $operation) {
  4867. if (!$pkName && $operation != 'list') {
  4868. continue;
  4869. }
  4870. if ($type != 'table' && $operation != 'list') {
  4871. continue;
  4872. }
  4873. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  4874. continue;
  4875. }
  4876. if ($operation == 'list') {
  4877. $this->openapi->set("components|responses|$operation-$tableName|description", "list of $tableName records");
  4878. } else {
  4879. $this->openapi->set("components|responses|$operation-$tableName|description", "single $tableName record");
  4880. }
  4881. $this->openapi->set("components|responses|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . urlencode($tableName));
  4882. }
  4883. }
  4884. private function setComponentRequestBody(string $tableName) /*: void*/
  4885. {
  4886. $table = $this->reflection->getTable($tableName);
  4887. $type = $table->getType();
  4888. $pk = $table->getPk();
  4889. $pkName = $pk ? $pk->getName() : '';
  4890. if ($pkName && $type == 'table') {
  4891. foreach (['create', 'update', 'increment'] as $operation) {
  4892. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  4893. continue;
  4894. }
  4895. $this->openapi->set("components|requestBodies|$operation-$tableName|description", "single $tableName record");
  4896. $this->openapi->set("components|requestBodies|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . urlencode($tableName));
  4897. }
  4898. }
  4899. }
  4900. private function setComponentParameters() /*: void*/
  4901. {
  4902. $this->openapi->set("components|parameters|pk|name", "id");
  4903. $this->openapi->set("components|parameters|pk|in", "path");
  4904. $this->openapi->set("components|parameters|pk|schema|type", "string");
  4905. $this->openapi->set("components|parameters|pk|description", "primary key value");
  4906. $this->openapi->set("components|parameters|pk|required", true);
  4907. $this->openapi->set("components|parameters|filter|name", "filter");
  4908. $this->openapi->set("components|parameters|filter|in", "query");
  4909. $this->openapi->set("components|parameters|filter|schema|type", "array");
  4910. $this->openapi->set("components|parameters|filter|schema|items|type", "string");
  4911. $this->openapi->set("components|parameters|filter|description", "Filters to be applied. Each filter consists of a column, an operator and a value (comma separated). Example: id,eq,1");
  4912. $this->openapi->set("components|parameters|filter|required", false);
  4913. $this->openapi->set("components|parameters|include|name", "include");
  4914. $this->openapi->set("components|parameters|include|in", "query");
  4915. $this->openapi->set("components|parameters|include|schema|type", "string");
  4916. $this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
  4917. $this->openapi->set("components|parameters|include|required", false);
  4918. $this->openapi->set("components|parameters|exclude|name", "exclude");
  4919. $this->openapi->set("components|parameters|exclude|in", "query");
  4920. $this->openapi->set("components|parameters|exclude|schema|type", "string");
  4921. $this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
  4922. $this->openapi->set("components|parameters|exclude|required", false);
  4923. $this->openapi->set("components|parameters|order|name", "order");
  4924. $this->openapi->set("components|parameters|order|in", "query");
  4925. $this->openapi->set("components|parameters|order|schema|type", "array");
  4926. $this->openapi->set("components|parameters|order|schema|items|type", "string");
  4927. $this->openapi->set("components|parameters|order|description", "Column you want to sort on and the sort direction (comma separated). Example: id,desc");
  4928. $this->openapi->set("components|parameters|order|required", false);
  4929. $this->openapi->set("components|parameters|size|name", "size");
  4930. $this->openapi->set("components|parameters|size|in", "query");
  4931. $this->openapi->set("components|parameters|size|schema|type", "string");
  4932. $this->openapi->set("components|parameters|size|description", "Maximum number of results (for top lists). Example: 10");
  4933. $this->openapi->set("components|parameters|size|required", false);
  4934. $this->openapi->set("components|parameters|page|name", "page");
  4935. $this->openapi->set("components|parameters|page|in", "query");
  4936. $this->openapi->set("components|parameters|page|schema|type", "string");
  4937. $this->openapi->set("components|parameters|page|description", "Page number and page size (comma separated). Example: 1,10");
  4938. $this->openapi->set("components|parameters|page|required", false);
  4939. $this->openapi->set("components|parameters|join|name", "join");
  4940. $this->openapi->set("components|parameters|join|in", "query");
  4941. $this->openapi->set("components|parameters|join|schema|type", "array");
  4942. $this->openapi->set("components|parameters|join|schema|items|type", "string");
  4943. $this->openapi->set("components|parameters|join|description", "Paths (comma separated) to related entities that you want to include. Example: comments,users");
  4944. $this->openapi->set("components|parameters|join|required", false);
  4945. }
  4946. private function setTag(int $index, string $tableName) /*: void*/
  4947. {
  4948. $this->openapi->set("tags|$index|name", "$tableName");
  4949. $this->openapi->set("tags|$index|description", "$tableName operations");
  4950. }
  4951. }
  4952. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiDefinition.php
  4953. class OpenApiDefinition implements \JsonSerializable
  4954. {
  4955. private $root;
  4956. public function __construct($base)
  4957. {
  4958. $this->root = $base;
  4959. }
  4960. public function set(string $path, $value) /*: void*/
  4961. {
  4962. $parts = explode('|', trim($path, '|'));
  4963. $current = &$this->root;
  4964. while (count($parts) > 0) {
  4965. $part = array_shift($parts);
  4966. if (!isset($current[$part])) {
  4967. $current[$part] = [];
  4968. }
  4969. $current = &$current[$part];
  4970. }
  4971. $current = $value;
  4972. }
  4973. public function has(string $path): bool
  4974. {
  4975. $parts = explode('|', trim($path, '|'));
  4976. $current = &$this->root;
  4977. while (count($parts) > 0) {
  4978. $part = array_shift($parts);
  4979. if (!isset($current[$part])) {
  4980. return false;
  4981. }
  4982. $current = &$current[$part];
  4983. }
  4984. return true;
  4985. }
  4986. public function jsonSerialize()
  4987. {
  4988. return $this->root;
  4989. }
  4990. }
  4991. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php
  4992. class OpenApiService
  4993. {
  4994. private $builder;
  4995. public function __construct(ReflectionService $reflection, array $base)
  4996. {
  4997. $this->builder = new OpenApiBuilder($reflection, $base);
  4998. }
  4999. public function get(): OpenApiDefinition
  5000. {
  5001. return $this->builder->build();
  5002. }
  5003. }
  5004. // file: src/Tqdev/PhpCrudApi/Record/Condition/AndCondition.php
  5005. class AndCondition extends Condition
  5006. {
  5007. private $conditions;
  5008. public function __construct(Condition $condition1, Condition $condition2)
  5009. {
  5010. $this->conditions = [$condition1, $condition2];
  5011. }
  5012. public function _and(Condition $condition): Condition
  5013. {
  5014. if ($condition instanceof NoCondition) {
  5015. return $this;
  5016. }
  5017. $this->conditions[] = $condition;
  5018. return $this;
  5019. }
  5020. public function getConditions(): array
  5021. {
  5022. return $this->conditions;
  5023. }
  5024. public static function fromArray(array $conditions): Condition
  5025. {
  5026. $condition = new NoCondition();
  5027. foreach ($conditions as $c) {
  5028. $condition = $condition->_and($c);
  5029. }
  5030. return $condition;
  5031. }
  5032. }
  5033. // file: src/Tqdev/PhpCrudApi/Record/Condition/ColumnCondition.php
  5034. class ColumnCondition extends Condition
  5035. {
  5036. private $column;
  5037. private $operator;
  5038. private $value;
  5039. public function __construct(ReflectedColumn $column, string $operator, string $value)
  5040. {
  5041. $this->column = $column;
  5042. $this->operator = $operator;
  5043. $this->value = $value;
  5044. }
  5045. public function getColumn(): ReflectedColumn
  5046. {
  5047. return $this->column;
  5048. }
  5049. public function getOperator(): string
  5050. {
  5051. return $this->operator;
  5052. }
  5053. public function getValue(): string
  5054. {
  5055. return $this->value;
  5056. }
  5057. }
  5058. // file: src/Tqdev/PhpCrudApi/Record/Condition/Condition.php
  5059. abstract class Condition
  5060. {
  5061. public function _and(Condition $condition): Condition
  5062. {
  5063. if ($condition instanceof NoCondition) {
  5064. return $this;
  5065. }
  5066. return new AndCondition($this, $condition);
  5067. }
  5068. public function _or(Condition $condition): Condition
  5069. {
  5070. if ($condition instanceof NoCondition) {
  5071. return $this;
  5072. }
  5073. return new OrCondition($this, $condition);
  5074. }
  5075. public function _not(): Condition
  5076. {
  5077. return new NotCondition($this);
  5078. }
  5079. public static function fromString(ReflectedTable $table, string $value): Condition
  5080. {
  5081. $condition = new NoCondition();
  5082. $parts = explode(',', $value, 3);
  5083. if (count($parts) < 2) {
  5084. return $condition;
  5085. }
  5086. if (count($parts) < 3) {
  5087. $parts[2] = '';
  5088. }
  5089. $field = $table->getColumn($parts[0]);
  5090. $command = $parts[1];
  5091. $negate = false;
  5092. $spatial = false;
  5093. if (strlen($command) > 2) {
  5094. if (substr($command, 0, 1) == 'n') {
  5095. $negate = true;
  5096. $command = substr($command, 1);
  5097. }
  5098. if (substr($command, 0, 1) == 's') {
  5099. $spatial = true;
  5100. $command = substr($command, 1);
  5101. }
  5102. }
  5103. if ($spatial) {
  5104. if (in_array($command, ['co', 'cr', 'di', 'eq', 'in', 'ov', 'to', 'wi', 'ic', 'is', 'iv'])) {
  5105. $condition = new SpatialCondition($field, $command, $parts[2]);
  5106. }
  5107. } else {
  5108. if (in_array($command, ['cs', 'sw', 'ew', 'eq', 'lt', 'le', 'ge', 'gt', 'bt', 'in', 'is'])) {
  5109. $condition = new ColumnCondition($field, $command, $parts[2]);
  5110. }
  5111. }
  5112. if ($negate) {
  5113. $condition = $condition->_not();
  5114. }
  5115. return $condition;
  5116. }
  5117. }
  5118. // file: src/Tqdev/PhpCrudApi/Record/Condition/NoCondition.php
  5119. class NoCondition extends Condition
  5120. {
  5121. public function _and(Condition $condition): Condition
  5122. {
  5123. return $condition;
  5124. }
  5125. public function _or(Condition $condition): Condition
  5126. {
  5127. return $condition;
  5128. }
  5129. public function _not(): Condition
  5130. {
  5131. return $this;
  5132. }
  5133. }
  5134. // file: src/Tqdev/PhpCrudApi/Record/Condition/NotCondition.php
  5135. class NotCondition extends Condition
  5136. {
  5137. private $condition;
  5138. public function __construct(Condition $condition)
  5139. {
  5140. $this->condition = $condition;
  5141. }
  5142. public function getCondition(): Condition
  5143. {
  5144. return $this->condition;
  5145. }
  5146. }
  5147. // file: src/Tqdev/PhpCrudApi/Record/Condition/OrCondition.php
  5148. class OrCondition extends Condition
  5149. {
  5150. private $conditions;
  5151. public function __construct(Condition $condition1, Condition $condition2)
  5152. {
  5153. $this->conditions = [$condition1, $condition2];
  5154. }
  5155. public function _or(Condition $condition): Condition
  5156. {
  5157. if ($condition instanceof NoCondition) {
  5158. return $this;
  5159. }
  5160. $this->conditions[] = $condition;
  5161. return $this;
  5162. }
  5163. public function getConditions(): array
  5164. {
  5165. return $this->conditions;
  5166. }
  5167. public static function fromArray(array $conditions): Condition
  5168. {
  5169. $condition = new NoCondition();
  5170. foreach ($conditions as $c) {
  5171. $condition = $condition->_or($c);
  5172. }
  5173. return $condition;
  5174. }
  5175. }
  5176. // file: src/Tqdev/PhpCrudApi/Record/Condition/SpatialCondition.php
  5177. class SpatialCondition extends ColumnCondition
  5178. {
  5179. }
  5180. // file: src/Tqdev/PhpCrudApi/Record/Document/ErrorDocument.php
  5181. class ErrorDocument implements \JsonSerializable
  5182. {
  5183. public $code;
  5184. public $message;
  5185. public $details;
  5186. public function __construct(ErrorCode $errorCode, string $argument, $details)
  5187. {
  5188. $this->code = $errorCode->getCode();
  5189. $this->message = $errorCode->getMessage($argument);
  5190. $this->details = $details;
  5191. }
  5192. public function getCode(): int
  5193. {
  5194. return $this->code;
  5195. }
  5196. public function getMessage(): string
  5197. {
  5198. return $this->message;
  5199. }
  5200. public function serialize()
  5201. {
  5202. return [
  5203. 'code' => $this->code,
  5204. 'message' => $this->message,
  5205. 'details' => $this->details,
  5206. ];
  5207. }
  5208. public function jsonSerialize()
  5209. {
  5210. return array_filter($this->serialize());
  5211. }
  5212. }
  5213. // file: src/Tqdev/PhpCrudApi/Record/Document/ListDocument.php
  5214. class ListDocument implements \JsonSerializable
  5215. {
  5216. private $records;
  5217. private $results;
  5218. public function __construct(array $records, int $results)
  5219. {
  5220. $this->records = $records;
  5221. $this->results = $results;
  5222. }
  5223. public function getRecords(): array
  5224. {
  5225. return $this->records;
  5226. }
  5227. public function getResults(): int
  5228. {
  5229. return $this->results;
  5230. }
  5231. public function serialize()
  5232. {
  5233. return [
  5234. 'records' => $this->records,
  5235. 'results' => $this->results,
  5236. ];
  5237. }
  5238. public function jsonSerialize()
  5239. {
  5240. return array_filter($this->serialize(), function ($v) {
  5241. return $v !== 0;
  5242. });
  5243. }
  5244. }
  5245. // file: src/Tqdev/PhpCrudApi/Record/ColumnIncluder.php
  5246. class ColumnIncluder
  5247. {
  5248. private function isMandatory(string $tableName, string $columnName, array $params): bool
  5249. {
  5250. return isset($params['mandatory']) && in_array($tableName . "." . $columnName, $params['mandatory']);
  5251. }
  5252. private function select(string $tableName, bool $primaryTable, array $params, string $paramName,
  5253. array $columnNames, bool $include): array{
  5254. if (!isset($params[$paramName])) {
  5255. return $columnNames;
  5256. }
  5257. $columns = array();
  5258. foreach (explode(',', $params[$paramName][0]) as $columnName) {
  5259. $columns[$columnName] = true;
  5260. }
  5261. $result = array();
  5262. foreach ($columnNames as $columnName) {
  5263. $match = isset($columns['*.*']);
  5264. if (!$match) {
  5265. $match = isset($columns[$tableName . '.*']) || isset($columns[$tableName . '.' . $columnName]);
  5266. }
  5267. if ($primaryTable && !$match) {
  5268. $match = isset($columns['*']) || isset($columns[$columnName]);
  5269. }
  5270. if ($match) {
  5271. if ($include || $this->isMandatory($tableName, $columnName, $params)) {
  5272. $result[] = $columnName;
  5273. }
  5274. } else {
  5275. if (!$include || $this->isMandatory($tableName, $columnName, $params)) {
  5276. $result[] = $columnName;
  5277. }
  5278. }
  5279. }
  5280. return $result;
  5281. }
  5282. public function getNames(ReflectedTable $table, bool $primaryTable, array $params): array
  5283. {
  5284. $tableName = $table->getName();
  5285. $results = $table->getColumnNames();
  5286. $results = $this->select($tableName, $primaryTable, $params, 'include', $results, true);
  5287. $results = $this->select($tableName, $primaryTable, $params, 'exclude', $results, false);
  5288. return $results;
  5289. }
  5290. public function getValues(ReflectedTable $table, bool $primaryTable, /* object */ $record, array $params): array
  5291. {
  5292. $results = array();
  5293. $columnNames = $this->getNames($table, $primaryTable, $params);
  5294. foreach ($columnNames as $columnName) {
  5295. if (property_exists($record, $columnName)) {
  5296. $results[$columnName] = $record->$columnName;
  5297. }
  5298. }
  5299. return $results;
  5300. }
  5301. }
  5302. // file: src/Tqdev/PhpCrudApi/Record/ErrorCode.php
  5303. class ErrorCode
  5304. {
  5305. private $code;
  5306. private $message;
  5307. private $status;
  5308. const ERROR_NOT_FOUND = 9999;
  5309. const ROUTE_NOT_FOUND = 1000;
  5310. const TABLE_NOT_FOUND = 1001;
  5311. const ARGUMENT_COUNT_MISMATCH = 1002;
  5312. const RECORD_NOT_FOUND = 1003;
  5313. const ORIGIN_FORBIDDEN = 1004;
  5314. const COLUMN_NOT_FOUND = 1005;
  5315. const TABLE_ALREADY_EXISTS = 1006;
  5316. const COLUMN_ALREADY_EXISTS = 1007;
  5317. const HTTP_MESSAGE_NOT_READABLE = 1008;
  5318. const DUPLICATE_KEY_EXCEPTION = 1009;
  5319. const DATA_INTEGRITY_VIOLATION = 1010;
  5320. const AUTHENTICATION_REQUIRED = 1011;
  5321. const AUTHENTICATION_FAILED = 1012;
  5322. const INPUT_VALIDATION_FAILED = 1013;
  5323. const OPERATION_FORBIDDEN = 1014;
  5324. const OPERATION_NOT_SUPPORTED = 1015;
  5325. const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016;
  5326. const BAD_OR_MISSING_XSRF_TOKEN = 1017;
  5327. const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
  5328. const PAGINATION_FORBIDDEN = 1019;
  5329. private $values = [
  5330. 9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
  5331. 1000 => ["Route '%s' not found", ResponseFactory::NOT_FOUND],
  5332. 1001 => ["Table '%s' not found", ResponseFactory::NOT_FOUND],
  5333. 1002 => ["Argument count mismatch in '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
  5334. 1003 => ["Record '%s' not found", ResponseFactory::NOT_FOUND],
  5335. 1004 => ["Origin '%s' is forbidden", ResponseFactory::FORBIDDEN],
  5336. 1005 => ["Column '%s' not found", ResponseFactory::NOT_FOUND],
  5337. 1006 => ["Table '%s' already exists", ResponseFactory::CONFLICT],
  5338. 1007 => ["Column '%s' already exists", ResponseFactory::CONFLICT],
  5339. 1008 => ["Cannot read HTTP message", ResponseFactory::UNPROCESSABLE_ENTITY],
  5340. 1009 => ["Duplicate key exception", ResponseFactory::CONFLICT],
  5341. 1010 => ["Data integrity violation", ResponseFactory::CONFLICT],
  5342. 1011 => ["Authentication required", ResponseFactory::UNAUTHORIZED],
  5343. 1012 => ["Authentication failed for '%s'", ResponseFactory::FORBIDDEN],
  5344. 1013 => ["Input validation failed for '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
  5345. 1014 => ["Operation forbidden", ResponseFactory::FORBIDDEN],
  5346. 1015 => ["Operation '%s' not supported", ResponseFactory::METHOD_NOT_ALLOWED],
  5347. 1016 => ["Temporary or permanently blocked", ResponseFactory::FORBIDDEN],
  5348. 1017 => ["Bad or missing XSRF token", ResponseFactory::FORBIDDEN],
  5349. 1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
  5350. 1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
  5351. ];
  5352. public function __construct(int $code)
  5353. {
  5354. if (!isset($this->values[$code])) {
  5355. $code = 9999;
  5356. }
  5357. $this->code = $code;
  5358. $this->message = $this->values[$code][0];
  5359. $this->status = $this->values[$code][1];
  5360. }
  5361. public function getCode(): int
  5362. {
  5363. return $this->code;
  5364. }
  5365. public function getMessage(string $argument): string
  5366. {
  5367. return sprintf($this->message, $argument);
  5368. }
  5369. public function getStatus(): int
  5370. {
  5371. return $this->status;
  5372. }
  5373. }
  5374. // file: src/Tqdev/PhpCrudApi/Record/FilterInfo.php
  5375. class FilterInfo
  5376. {
  5377. private function addConditionFromFilterPath(PathTree $conditions, array $path, ReflectedTable $table, array $params)
  5378. {
  5379. $key = 'filter' . implode('', $path);
  5380. if (isset($params[$key])) {
  5381. foreach ($params[$key] as $filter) {
  5382. $condition = Condition::fromString($table, $filter);
  5383. if (($condition instanceof NoCondition) == false) {
  5384. $conditions->put($path, $condition);
  5385. }
  5386. }
  5387. }
  5388. }
  5389. private function getConditionsAsPathTree(ReflectedTable $table, array $params): PathTree
  5390. {
  5391. $conditions = new PathTree();
  5392. $this->addConditionFromFilterPath($conditions, [], $table, $params);
  5393. for ($n = ord('0'); $n <= ord('9'); $n++) {
  5394. $this->addConditionFromFilterPath($conditions, [chr($n)], $table, $params);
  5395. for ($l = ord('a'); $l <= ord('f'); $l++) {
  5396. $this->addConditionFromFilterPath($conditions, [chr($n), chr($l)], $table, $params);
  5397. }
  5398. }
  5399. return $conditions;
  5400. }
  5401. private function combinePathTreeOfConditions(PathTree $tree): Condition
  5402. {
  5403. $andConditions = $tree->getValues();
  5404. $and = AndCondition::fromArray($andConditions);
  5405. $orConditions = [];
  5406. foreach ($tree->getKeys() as $p) {
  5407. $orConditions[] = $this->combinePathTreeOfConditions($tree->get($p));
  5408. }
  5409. $or = OrCondition::fromArray($orConditions);
  5410. return $and->_and($or);
  5411. }
  5412. public function getCombinedConditions(ReflectedTable $table, array $params): Condition
  5413. {
  5414. return $this->combinePathTreeOfConditions($this->getConditionsAsPathTree($table, $params));
  5415. }
  5416. }
  5417. // file: src/Tqdev/PhpCrudApi/Record/HabtmValues.php
  5418. class HabtmValues
  5419. {
  5420. public $pkValues;
  5421. public $fkValues;
  5422. public function __construct(array $pkValues, array $fkValues)
  5423. {
  5424. $this->pkValues = $pkValues;
  5425. $this->fkValues = $fkValues;
  5426. }
  5427. }
  5428. // file: src/Tqdev/PhpCrudApi/Record/OrderingInfo.php
  5429. class OrderingInfo
  5430. {
  5431. public function getColumnOrdering(ReflectedTable $table, array $params): array
  5432. {
  5433. $fields = array();
  5434. if (isset($params['order'])) {
  5435. foreach ($params['order'] as $order) {
  5436. $parts = explode(',', $order, 3);
  5437. $columnName = $parts[0];
  5438. if (!$table->hasColumn($columnName)) {
  5439. continue;
  5440. }
  5441. $ascending = 'ASC';
  5442. if (count($parts) > 1) {
  5443. if (substr(strtoupper($parts[1]), 0, 4) == "DESC") {
  5444. $ascending = 'DESC';
  5445. }
  5446. }
  5447. $fields[] = [$columnName, $ascending];
  5448. }
  5449. }
  5450. if (count($fields) == 0) {
  5451. return $this->getDefaultColumnOrdering($table);
  5452. }
  5453. return $fields;
  5454. }
  5455. public function getDefaultColumnOrdering(ReflectedTable $table): array
  5456. {
  5457. $fields = array();
  5458. $pk = $table->getPk();
  5459. if ($pk) {
  5460. $fields[] = [$pk->getName(), 'ASC'];
  5461. } else {
  5462. foreach ($table->getColumnNames() as $columnName) {
  5463. $fields[] = [$columnName, 'ASC'];
  5464. }
  5465. }
  5466. return $fields;
  5467. }
  5468. }
  5469. // file: src/Tqdev/PhpCrudApi/Record/PaginationInfo.php
  5470. class PaginationInfo
  5471. {
  5472. public $DEFAULT_PAGE_SIZE = 20;
  5473. public function hasPage(array $params): bool
  5474. {
  5475. return isset($params['page']);
  5476. }
  5477. public function getPageOffset(array $params): int
  5478. {
  5479. $offset = 0;
  5480. $pageSize = $this->getPageSize($params);
  5481. if (isset($params['page'])) {
  5482. foreach ($params['page'] as $page) {
  5483. $parts = explode(',', $page, 2);
  5484. $page = intval($parts[0]) - 1;
  5485. $offset = $page * $pageSize;
  5486. }
  5487. }
  5488. return $offset;
  5489. }
  5490. private function getPageSize(array $params): int
  5491. {
  5492. $pageSize = $this->DEFAULT_PAGE_SIZE;
  5493. if (isset($params['page'])) {
  5494. foreach ($params['page'] as $page) {
  5495. $parts = explode(',', $page, 2);
  5496. if (count($parts) > 1) {
  5497. $pageSize = intval($parts[1]);
  5498. }
  5499. }
  5500. }
  5501. return $pageSize;
  5502. }
  5503. public function getResultSize(array $params): int
  5504. {
  5505. $numberOfRows = -1;
  5506. if (isset($params['size'])) {
  5507. foreach ($params['size'] as $size) {
  5508. $numberOfRows = intval($size);
  5509. }
  5510. }
  5511. return $numberOfRows;
  5512. }
  5513. public function getPageLimit(array $params): int
  5514. {
  5515. $pageLimit = -1;
  5516. if ($this->hasPage($params)) {
  5517. $pageLimit = $this->getPageSize($params);
  5518. }
  5519. $resultSize = $this->getResultSize($params);
  5520. if ($resultSize >= 0) {
  5521. if ($pageLimit >= 0) {
  5522. $pageLimit = min($pageLimit, $resultSize);
  5523. } else {
  5524. $pageLimit = $resultSize;
  5525. }
  5526. }
  5527. return $pageLimit;
  5528. }
  5529. }
  5530. // file: src/Tqdev/PhpCrudApi/Record/PathTree.php
  5531. class PathTree implements \JsonSerializable
  5532. {
  5533. const WILDCARD = '*';
  5534. private $tree;
  5535. public function __construct( /* object */&$tree = null)
  5536. {
  5537. if (!$tree) {
  5538. $tree = $this->newTree();
  5539. }
  5540. $this->tree = &$tree;
  5541. }
  5542. public function newTree()
  5543. {
  5544. return (object) ['values' => [], 'branches' => (object) []];
  5545. }
  5546. public function getKeys(): array
  5547. {
  5548. $branches = (array) $this->tree->branches;
  5549. return array_keys($branches);
  5550. }
  5551. public function getValues(): array
  5552. {
  5553. return $this->tree->values;
  5554. }
  5555. public function get(string $key): PathTree
  5556. {
  5557. if (!isset($this->tree->branches->$key)) {
  5558. return null;
  5559. }
  5560. return new PathTree($this->tree->branches->$key);
  5561. }
  5562. public function put(array $path, $value)
  5563. {
  5564. $tree = &$this->tree;
  5565. foreach ($path as $key) {
  5566. if (!isset($tree->branches->$key)) {
  5567. $tree->branches->$key = $this->newTree();
  5568. }
  5569. $tree = &$tree->branches->$key;
  5570. }
  5571. $tree->values[] = $value;
  5572. }
  5573. public function match(array $path): array
  5574. {
  5575. $star = self::WILDCARD;
  5576. $tree = &$this->tree;
  5577. foreach ($path as $key) {
  5578. if (isset($tree->branches->$key)) {
  5579. $tree = &$tree->branches->$key;
  5580. } else if (isset($tree->branches->$star)) {
  5581. $tree = &$tree->branches->$star;
  5582. } else {
  5583. return [];
  5584. }
  5585. }
  5586. return $tree->values;
  5587. }
  5588. public static function fromJson( /* object */$tree): PathTree
  5589. {
  5590. return new PathTree($tree);
  5591. }
  5592. public function jsonSerialize()
  5593. {
  5594. return $this->tree;
  5595. }
  5596. }
  5597. // file: src/Tqdev/PhpCrudApi/Record/RecordService.php
  5598. class RecordService
  5599. {
  5600. private $db;
  5601. private $reflection;
  5602. private $columns;
  5603. private $joiner;
  5604. private $filters;
  5605. private $ordering;
  5606. private $pagination;
  5607. public function __construct(GenericDB $db, ReflectionService $reflection)
  5608. {
  5609. $this->db = $db;
  5610. $this->reflection = $reflection;
  5611. $this->columns = new ColumnIncluder();
  5612. $this->joiner = new RelationJoiner($reflection, $this->columns);
  5613. $this->filters = new FilterInfo();
  5614. $this->ordering = new OrderingInfo();
  5615. $this->pagination = new PaginationInfo();
  5616. }
  5617. private function sanitizeRecord(string $tableName, /* object */ $record, string $id)
  5618. {
  5619. $keyset = array_keys((array) $record);
  5620. foreach ($keyset as $key) {
  5621. if (!$this->reflection->getTable($tableName)->hasColumn($key)) {
  5622. unset($record->$key);
  5623. }
  5624. }
  5625. if ($id != '') {
  5626. $pk = $this->reflection->getTable($tableName)->getPk();
  5627. foreach ($this->reflection->getTable($tableName)->getColumnNames() as $key) {
  5628. $field = $this->reflection->getTable($tableName)->getColumn($key);
  5629. if ($field->getName() == $pk->getName()) {
  5630. unset($record->$key);
  5631. }
  5632. }
  5633. }
  5634. }
  5635. public function hasTable(string $table): bool
  5636. {
  5637. return $this->reflection->hasTable($table);
  5638. }
  5639. public function getType(string $table): string
  5640. {
  5641. return $this->reflection->getType($table);
  5642. }
  5643. public function create(string $tableName, /* object */ $record, array $params)
  5644. {
  5645. $this->sanitizeRecord($tableName, $record, '');
  5646. $table = $this->reflection->getTable($tableName);
  5647. $columnValues = $this->columns->getValues($table, true, $record, $params);
  5648. return $this->db->createSingle($table, $columnValues);
  5649. }
  5650. public function read(string $tableName, string $id, array $params) /*: ?object*/
  5651. {
  5652. $table = $this->reflection->getTable($tableName);
  5653. $this->joiner->addMandatoryColumns($table, $params);
  5654. $columnNames = $this->columns->getNames($table, true, $params);
  5655. $record = $this->db->selectSingle($table, $columnNames, $id);
  5656. if ($record == null) {
  5657. return null;
  5658. }
  5659. $records = array($record);
  5660. $this->joiner->addJoins($table, $records, $params, $this->db);
  5661. return $records[0];
  5662. }
  5663. public function update(string $tableName, string $id, /* object */ $record, array $params)
  5664. {
  5665. $this->sanitizeRecord($tableName, $record, $id);
  5666. $table = $this->reflection->getTable($tableName);
  5667. $columnValues = $this->columns->getValues($table, true, $record, $params);
  5668. return $this->db->updateSingle($table, $columnValues, $id);
  5669. }
  5670. public function delete(string $tableName, string $id, array $params)
  5671. {
  5672. $table = $this->reflection->getTable($tableName);
  5673. return $this->db->deleteSingle($table, $id);
  5674. }
  5675. public function increment(string $tableName, string $id, /* object */ $record, array $params)
  5676. {
  5677. $this->sanitizeRecord($tableName, $record, $id);
  5678. $table = $this->reflection->getTable($tableName);
  5679. $columnValues = $this->columns->getValues($table, true, $record, $params);
  5680. return $this->db->incrementSingle($table, $columnValues, $id);
  5681. }
  5682. public function _list(string $tableName, array $params): ListDocument
  5683. {
  5684. $table = $this->reflection->getTable($tableName);
  5685. $this->joiner->addMandatoryColumns($table, $params);
  5686. $columnNames = $this->columns->getNames($table, true, $params);
  5687. $condition = $this->filters->getCombinedConditions($table, $params);
  5688. $columnOrdering = $this->ordering->getColumnOrdering($table, $params);
  5689. if (!$this->pagination->hasPage($params)) {
  5690. $offset = 0;
  5691. $limit = $this->pagination->getPageLimit($params);
  5692. $count = 0;
  5693. } else {
  5694. $offset = $this->pagination->getPageOffset($params);
  5695. $limit = $this->pagination->getPageLimit($params);
  5696. $count = $this->db->selectCount($table, $condition);
  5697. }
  5698. $records = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, $offset, $limit);
  5699. $this->joiner->addJoins($table, $records, $params, $this->db);
  5700. return new ListDocument($records, $count);
  5701. }
  5702. }
  5703. // file: src/Tqdev/PhpCrudApi/Record/RelationJoiner.php
  5704. class RelationJoiner
  5705. {
  5706. private $reflection;
  5707. private $columns;
  5708. public function __construct(ReflectionService $reflection, ColumnIncluder $columns)
  5709. {
  5710. $this->reflection = $reflection;
  5711. $this->ordering = new OrderingInfo();
  5712. $this->columns = $columns;
  5713. }
  5714. public function addMandatoryColumns(ReflectedTable $table, array &$params) /*: void*/
  5715. {
  5716. if (!isset($params['join']) || !isset($params['include'])) {
  5717. return;
  5718. }
  5719. $params['mandatory'] = array();
  5720. foreach ($params['join'] as $tableNames) {
  5721. $t1 = $table;
  5722. foreach (explode(',', $tableNames) as $tableName) {
  5723. if (!$this->reflection->hasTable($tableName)) {
  5724. continue;
  5725. }
  5726. $t2 = $this->reflection->getTable($tableName);
  5727. $fks1 = $t1->getFksTo($t2->getName());
  5728. $t3 = $this->hasAndBelongsToMany($t1, $t2);
  5729. if ($t3 != null || count($fks1) > 0) {
  5730. $params['mandatory'][] = $t2->getName() . '.' . $t2->getPk()->getName();
  5731. }
  5732. foreach ($fks1 as $fk) {
  5733. $params['mandatory'][] = $t1->getName() . '.' . $fk->getName();
  5734. }
  5735. $fks2 = $t2->getFksTo($t1->getName());
  5736. if ($t3 != null || count($fks2) > 0) {
  5737. $params['mandatory'][] = $t1->getName() . '.' . $t1->getPk()->getName();
  5738. }
  5739. foreach ($fks2 as $fk) {
  5740. $params['mandatory'][] = $t2->getName() . '.' . $fk->getName();
  5741. }
  5742. $t1 = $t2;
  5743. }
  5744. }
  5745. }
  5746. private function getJoinsAsPathTree(array $params): PathTree
  5747. {
  5748. $joins = new PathTree();
  5749. if (isset($params['join'])) {
  5750. foreach ($params['join'] as $tableNames) {
  5751. $path = array();
  5752. foreach (explode(',', $tableNames) as $tableName) {
  5753. $t = $this->reflection->getTable($tableName);
  5754. if ($t != null) {
  5755. $path[] = $t->getName();
  5756. }
  5757. }
  5758. $joins->put($path, true);
  5759. }
  5760. }
  5761. return $joins;
  5762. }
  5763. public function addJoins(ReflectedTable $table, array &$records, array $params, GenericDB $db) /*: void*/
  5764. {
  5765. $joins = $this->getJoinsAsPathTree($params);
  5766. $this->addJoinsForTables($table, $joins, $records, $params, $db);
  5767. }
  5768. private function hasAndBelongsToMany(ReflectedTable $t1, ReflectedTable $t2) /*: ?ReflectedTable*/
  5769. {
  5770. foreach ($this->reflection->getTableNames() as $tableName) {
  5771. $t3 = $this->reflection->getTable($tableName);
  5772. if (count($t3->getFksTo($t1->getName())) > 0 && count($t3->getFksTo($t2->getName())) > 0) {
  5773. return $t3;
  5774. }
  5775. }
  5776. return null;
  5777. }
  5778. private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$records, array $params, GenericDB $db)
  5779. {
  5780. foreach ($joins->getKeys() as $t2Name) {
  5781. $t2 = $this->reflection->getTable($t2Name);
  5782. $belongsTo = count($t1->getFksTo($t2->getName())) > 0;
  5783. $hasMany = count($t2->getFksTo($t1->getName())) > 0;
  5784. if (!$belongsTo && !$hasMany) {
  5785. $t3 = $this->hasAndBelongsToMany($t1, $t2);
  5786. } else {
  5787. $t3 = null;
  5788. }
  5789. $hasAndBelongsToMany = ($t3 != null);
  5790. $newRecords = array();
  5791. $fkValues = null;
  5792. $pkValues = null;
  5793. $habtmValues = null;
  5794. if ($belongsTo) {
  5795. $fkValues = $this->getFkEmptyValues($t1, $t2, $records);
  5796. $this->addFkRecords($t2, $fkValues, $params, $db, $newRecords);
  5797. }
  5798. if ($hasMany) {
  5799. $pkValues = $this->getPkEmptyValues($t1, $records);
  5800. $this->addPkRecords($t1, $t2, $pkValues, $params, $db, $newRecords);
  5801. }
  5802. if ($hasAndBelongsToMany) {
  5803. $habtmValues = $this->getHabtmEmptyValues($t1, $t2, $t3, $db, $records);
  5804. $this->addFkRecords($t2, $habtmValues->fkValues, $params, $db, $newRecords);
  5805. }
  5806. $this->addJoinsForTables($t2, $joins->get($t2Name), $newRecords, $params, $db);
  5807. if ($fkValues != null) {
  5808. $this->fillFkValues($t2, $newRecords, $fkValues);
  5809. $this->setFkValues($t1, $t2, $records, $fkValues);
  5810. }
  5811. if ($pkValues != null) {
  5812. $this->fillPkValues($t1, $t2, $newRecords, $pkValues);
  5813. $this->setPkValues($t1, $t2, $records, $pkValues);
  5814. }
  5815. if ($habtmValues != null) {
  5816. $this->fillFkValues($t2, $newRecords, $habtmValues->fkValues);
  5817. $this->setHabtmValues($t1, $t2, $records, $habtmValues);
  5818. }
  5819. }
  5820. }
  5821. private function getFkEmptyValues(ReflectedTable $t1, ReflectedTable $t2, array $records): array
  5822. {
  5823. $fkValues = array();
  5824. $fks = $t1->getFksTo($t2->getName());
  5825. foreach ($fks as $fk) {
  5826. $fkName = $fk->getName();
  5827. foreach ($records as $record) {
  5828. if (isset($record[$fkName])) {
  5829. $fkValue = $record[$fkName];
  5830. $fkValues[$fkValue] = null;
  5831. }
  5832. }
  5833. }
  5834. return $fkValues;
  5835. }
  5836. private function addFkRecords(ReflectedTable $t2, array $fkValues, array $params, GenericDB $db, array &$records) /*: void*/
  5837. {
  5838. $columnNames = $this->columns->getNames($t2, false, $params);
  5839. $fkIds = array_keys($fkValues);
  5840. foreach ($db->selectMultiple($t2, $columnNames, $fkIds) as $record) {
  5841. $records[] = $record;
  5842. }
  5843. }
  5844. private function fillFkValues(ReflectedTable $t2, array $fkRecords, array &$fkValues) /*: void*/
  5845. {
  5846. $pkName = $t2->getPk()->getName();
  5847. foreach ($fkRecords as $fkRecord) {
  5848. $pkValue = $fkRecord[$pkName];
  5849. $fkValues[$pkValue] = $fkRecord;
  5850. }
  5851. }
  5852. private function setFkValues(ReflectedTable $t1, ReflectedTable $t2, array &$records, array $fkValues) /*: void*/
  5853. {
  5854. $fks = $t1->getFksTo($t2->getName());
  5855. foreach ($fks as $fk) {
  5856. $fkName = $fk->getName();
  5857. foreach ($records as $i => $record) {
  5858. if (isset($record[$fkName])) {
  5859. $key = $record[$fkName];
  5860. $records[$i][$fkName] = $fkValues[$key];
  5861. }
  5862. }
  5863. }
  5864. }
  5865. private function getPkEmptyValues(ReflectedTable $t1, array $records): array
  5866. {
  5867. $pkValues = array();
  5868. $pkName = $t1->getPk()->getName();
  5869. foreach ($records as $record) {
  5870. $key = $record[$pkName];
  5871. $pkValues[$key] = array();
  5872. }
  5873. return $pkValues;
  5874. }
  5875. private function addPkRecords(ReflectedTable $t1, ReflectedTable $t2, array $pkValues, array $params, GenericDB $db, array &$records) /*: void*/
  5876. {
  5877. $fks = $t2->getFksTo($t1->getName());
  5878. $columnNames = $this->columns->getNames($t2, false, $params);
  5879. $pkValueKeys = implode(',', array_keys($pkValues));
  5880. $conditions = array();
  5881. foreach ($fks as $fk) {
  5882. $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
  5883. }
  5884. $condition = OrCondition::fromArray($conditions);
  5885. $columnOrdering = array();
  5886. $limit = VariableStore::get("joinLimits.maxRecords") ?: -1;
  5887. if ($limit != -1) {
  5888. $columnOrdering = $this->ordering->getDefaultColumnOrdering($t2);
  5889. }
  5890. foreach ($db->selectAll($t2, $columnNames, $condition, $columnOrdering, 0, $limit) as $record) {
  5891. $records[] = $record;
  5892. }
  5893. }
  5894. private function fillPkValues(ReflectedTable $t1, ReflectedTable $t2, array $pkRecords, array &$pkValues) /*: void*/
  5895. {
  5896. $fks = $t2->getFksTo($t1->getName());
  5897. foreach ($fks as $fk) {
  5898. $fkName = $fk->getName();
  5899. foreach ($pkRecords as $pkRecord) {
  5900. $key = $pkRecord[$fkName];
  5901. if (isset($pkValues[$key])) {
  5902. $pkValues[$key][] = $pkRecord;
  5903. }
  5904. }
  5905. }
  5906. }
  5907. private function setPkValues(ReflectedTable $t1, ReflectedTable $t2, array &$records, array $pkValues) /*: void*/
  5908. {
  5909. $pkName = $t1->getPk()->getName();
  5910. $t2Name = $t2->getName();
  5911. foreach ($records as $i => $record) {
  5912. $key = $record[$pkName];
  5913. $records[$i][$t2Name] = $pkValues[$key];
  5914. }
  5915. }
  5916. private function getHabtmEmptyValues(ReflectedTable $t1, ReflectedTable $t2, ReflectedTable $t3, GenericDB $db, array $records): HabtmValues
  5917. {
  5918. $pkValues = $this->getPkEmptyValues($t1, $records);
  5919. $fkValues = array();
  5920. $fk1 = $t3->getFksTo($t1->getName())[0];
  5921. $fk2 = $t3->getFksTo($t2->getName())[0];
  5922. $fk1Name = $fk1->getName();
  5923. $fk2Name = $fk2->getName();
  5924. $columnNames = array($fk1Name, $fk2Name);
  5925. $pkIds = implode(',', array_keys($pkValues));
  5926. $condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
  5927. $columnOrdering = array();
  5928. $limit = VariableStore::get("joinLimits.maxRecords") ?: -1;
  5929. if ($limit != -1) {
  5930. $columnOrdering = $this->ordering->getDefaultColumnOrdering($t3);
  5931. }
  5932. $records = $db->selectAll($t3, $columnNames, $condition, $columnOrdering, 0, $limit);
  5933. foreach ($records as $record) {
  5934. $val1 = $record[$fk1Name];
  5935. $val2 = $record[$fk2Name];
  5936. $pkValues[$val1][] = $val2;
  5937. $fkValues[$val2] = null;
  5938. }
  5939. return new HabtmValues($pkValues, $fkValues);
  5940. }
  5941. private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$records, HabtmValues $habtmValues) /*: void*/
  5942. {
  5943. $pkName = $t1->getPk()->getName();
  5944. $t2Name = $t2->getName();
  5945. foreach ($records as $i => $record) {
  5946. $key = $record[$pkName];
  5947. $val = array();
  5948. $fks = $habtmValues->pkValues[$key];
  5949. foreach ($fks as $fk) {
  5950. $val[] = $habtmValues->fkValues[$fk];
  5951. }
  5952. $records[$i][$t2Name] = $val;
  5953. }
  5954. }
  5955. }
  5956. // file: src/Tqdev/PhpCrudApi/Api.php
  5957. class Api
  5958. {
  5959. private $router;
  5960. private $responder;
  5961. private $debug;
  5962. public function __construct(Config $config)
  5963. {
  5964. $db = new GenericDB(
  5965. $config->getDriver(),
  5966. $config->getAddress(),
  5967. $config->getPort(),
  5968. $config->getDatabase(),
  5969. $config->getUsername(),
  5970. $config->getPassword()
  5971. );
  5972. $cache = CacheFactory::create($config);
  5973. $reflection = new ReflectionService($db, $cache, $config->getCacheTime());
  5974. $responder = new Responder();
  5975. $router = new SimpleRouter($responder, $cache, $config->getCacheTime(), $config->getDebug());
  5976. foreach ($config->getMiddlewares() as $middleware => $properties) {
  5977. switch ($middleware) {
  5978. case 'cors':
  5979. new CorsMiddleware($router, $responder, $properties);
  5980. break;
  5981. case 'firewall':
  5982. new FirewallMiddleware($router, $responder, $properties);
  5983. break;
  5984. case 'basicAuth':
  5985. new BasicAuthMiddleware($router, $responder, $properties);
  5986. break;
  5987. case 'jwtAuth':
  5988. new JwtAuthMiddleware($router, $responder, $properties);
  5989. break;
  5990. case 'validation':
  5991. new ValidationMiddleware($router, $responder, $properties, $reflection);
  5992. break;
  5993. case 'ipAddress':
  5994. new IpAddressMiddleware($router, $responder, $properties, $reflection);
  5995. break;
  5996. case 'sanitation':
  5997. new SanitationMiddleware($router, $responder, $properties, $reflection);
  5998. break;
  5999. case 'multiTenancy':
  6000. new MultiTenancyMiddleware($router, $responder, $properties, $reflection);
  6001. break;
  6002. case 'authorization':
  6003. new AuthorizationMiddleware($router, $responder, $properties, $reflection);
  6004. break;
  6005. case 'xsrf':
  6006. new XsrfMiddleware($router, $responder, $properties);
  6007. break;
  6008. case 'pageLimits':
  6009. new PageLimitsMiddleware($router, $responder, $properties, $reflection);
  6010. break;
  6011. case 'joinLimits':
  6012. new JoinLimitsMiddleware($router, $responder, $properties, $reflection);
  6013. break;
  6014. case 'customization':
  6015. new CustomizationMiddleware($router, $responder, $properties, $reflection);
  6016. break;
  6017. }
  6018. }
  6019. foreach ($config->getControllers() as $controller) {
  6020. switch ($controller) {
  6021. case 'records':
  6022. $records = new RecordService($db, $reflection);
  6023. new RecordController($router, $responder, $records);
  6024. break;
  6025. case 'columns':
  6026. $definition = new DefinitionService($db, $reflection);
  6027. new ColumnController($router, $responder, $reflection, $definition);
  6028. break;
  6029. case 'cache':
  6030. new CacheController($router, $responder, $cache);
  6031. break;
  6032. case 'openapi':
  6033. $openApi = new OpenApiService($reflection, $config->getOpenApiBase());
  6034. new OpenApiController($router, $responder, $openApi);
  6035. break;
  6036. }
  6037. }
  6038. $this->router = $router;
  6039. $this->responder = $responder;
  6040. $this->debug = $config->getDebug();
  6041. }
  6042. public function handle(ServerRequestInterface $request): ResponseInterface
  6043. {
  6044. $response = null;
  6045. try {
  6046. $response = $this->router->route($request);
  6047. } catch (\Throwable $e) {
  6048. $response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
  6049. if ($this->debug) {
  6050. $response = ResponseUtils::addExceptionHeaders($response, $e);
  6051. }
  6052. }
  6053. return $response;
  6054. }
  6055. }
  6056. // file: src/Tqdev/PhpCrudApi/Config.php
  6057. class Config
  6058. {
  6059. private $values = [
  6060. 'driver' => null,
  6061. 'address' => 'localhost',
  6062. 'port' => null,
  6063. 'username' => null,
  6064. 'password' => null,
  6065. 'database' => null,
  6066. 'middlewares' => 'cors',
  6067. 'controllers' => 'records,openapi',
  6068. 'cacheType' => 'TempFile',
  6069. 'cachePath' => '',
  6070. 'cacheTime' => 10,
  6071. 'debug' => false,
  6072. 'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
  6073. ];
  6074. private function getDefaultDriver(array $values): string
  6075. {
  6076. if (isset($values['driver'])) {
  6077. return $values['driver'];
  6078. }
  6079. return 'mysql';
  6080. }
  6081. private function getDefaultPort(string $driver): int
  6082. {
  6083. switch ($driver) {
  6084. case 'mysql':return 3306;
  6085. case 'pgsql':return 5432;
  6086. case 'sqlsrv':return 1433;
  6087. }
  6088. }
  6089. private function getDefaultAddress(string $driver): string
  6090. {
  6091. switch ($driver) {
  6092. case 'mysql':return 'localhost';
  6093. case 'pgsql':return 'localhost';
  6094. case 'sqlsrv':return 'localhost';
  6095. }
  6096. }
  6097. private function getDriverDefaults(string $driver): array
  6098. {
  6099. return [
  6100. 'driver' => $driver,
  6101. 'address' => $this->getDefaultAddress($driver),
  6102. 'port' => $this->getDefaultPort($driver),
  6103. ];
  6104. }
  6105. public function __construct(array $values)
  6106. {
  6107. $driver = $this->getDefaultDriver($values);
  6108. $defaults = $this->getDriverDefaults($driver);
  6109. $newValues = array_merge($this->values, $defaults, $values);
  6110. $newValues = $this->parseMiddlewares($newValues);
  6111. $diff = array_diff_key($newValues, $this->values);
  6112. if (!empty($diff)) {
  6113. $key = array_keys($diff)[0];
  6114. throw new \Exception("Config has invalid value '$key'");
  6115. }
  6116. $this->values = $newValues;
  6117. }
  6118. private function parseMiddlewares(array $values): array
  6119. {
  6120. $newValues = array();
  6121. $properties = array();
  6122. $middlewares = array_map('trim', explode(',', $values['middlewares']));
  6123. foreach ($middlewares as $middleware) {
  6124. $properties[$middleware] = [];
  6125. }
  6126. foreach ($values as $key => $value) {
  6127. if (strpos($key, '.') === false) {
  6128. $newValues[$key] = $value;
  6129. } else {
  6130. list($middleware, $key2) = explode('.', $key, 2);
  6131. if (isset($properties[$middleware])) {
  6132. $properties[$middleware][$key2] = $value;
  6133. } else {
  6134. throw new \Exception("Config has invalid value '$key'");
  6135. }
  6136. }
  6137. }
  6138. $newValues['middlewares'] = array_reverse($properties, true);
  6139. return $newValues;
  6140. }
  6141. public function getDriver(): string
  6142. {
  6143. return $this->values['driver'];
  6144. }
  6145. public function getAddress(): string
  6146. {
  6147. return $this->values['address'];
  6148. }
  6149. public function getPort(): int
  6150. {
  6151. return $this->values['port'];
  6152. }
  6153. public function getUsername(): string
  6154. {
  6155. return $this->values['username'];
  6156. }
  6157. public function getPassword(): string
  6158. {
  6159. return $this->values['password'];
  6160. }
  6161. public function getDatabase(): string
  6162. {
  6163. return $this->values['database'];
  6164. }
  6165. public function getMiddlewares(): array
  6166. {
  6167. return $this->values['middlewares'];
  6168. }
  6169. public function getControllers(): array
  6170. {
  6171. return array_map('trim', explode(',', $this->values['controllers']));
  6172. }
  6173. public function getCacheType(): string
  6174. {
  6175. return $this->values['cacheType'];
  6176. }
  6177. public function getCachePath(): string
  6178. {
  6179. return $this->values['cachePath'];
  6180. }
  6181. public function getCacheTime(): int
  6182. {
  6183. return $this->values['cacheTime'];
  6184. }
  6185. public function getDebug(): string
  6186. {
  6187. return $this->values['debug'];
  6188. }
  6189. public function getOpenApiBase(): array
  6190. {
  6191. return json_decode($this->values['openApiBase'], true);
  6192. }
  6193. }
  6194. // file: src/Tqdev/PhpCrudApi/RequestFactory.php
  6195. class RequestFactory
  6196. {
  6197. private static function parseBody(string $body) /*: ?object*/
  6198. {
  6199. $first = substr($body, 0, 1);
  6200. if ($first == '[' || $first == '{') {
  6201. $object = json_decode($body);
  6202. $causeCode = json_last_error();
  6203. if ($causeCode !== JSON_ERROR_NONE) {
  6204. $object = null;
  6205. }
  6206. } else {
  6207. parse_str($body, $input);
  6208. foreach ($input as $key => $value) {
  6209. if (substr($key, -9) == '__is_null') {
  6210. $input[substr($key, 0, -9)] = null;
  6211. unset($input[$key]);
  6212. }
  6213. }
  6214. $object = (object) $input;
  6215. }
  6216. return $object;
  6217. }
  6218. public static function fromGlobals(): ServerRequestInterface
  6219. {
  6220. $psr17Factory = new Psr17Factory();
  6221. $creator = new ServerRequestCreator($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
  6222. $serverRequest = $creator->fromGlobals();
  6223. if (isset($_SERVER['PATH_INFO'])) {
  6224. $serverRequest = $serverRequest->withUri($psr17Factory->createUri($_SERVER['PATH_INFO'] . '?' . $_SERVER['QUERY_STRING']));
  6225. }
  6226. $body = file_get_contents('php://input');
  6227. if ($body) {
  6228. $serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
  6229. }
  6230. return $serverRequest;
  6231. }
  6232. public static function fromString(string $request): ServerRequestInterface
  6233. {
  6234. $parts = explode("\n\n", trim($request), 2);
  6235. $lines = explode("\n", $parts[0]);
  6236. $first = explode(' ', trim(array_shift($lines)), 2);
  6237. $method = $first[0];
  6238. $body = isset($parts[1]) ? $parts[1] : '';
  6239. $url = isset($first[1]) ? $first[1] : '';
  6240. $psr17Factory = new Psr17Factory();
  6241. $serverRequest = $psr17Factory->createServerRequest($method, $url);
  6242. foreach ($lines as $line) {
  6243. list($key, $value) = explode(':', $line, 2);
  6244. $serverRequest = $serverRequest->withAddedHeader($key, $value);
  6245. }
  6246. if ($body) {
  6247. $stream = $psr17Factory->createStream($body);
  6248. $stream->rewind();
  6249. $serverRequest = $serverRequest->withBody($stream);
  6250. $serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
  6251. }
  6252. return $serverRequest;
  6253. }
  6254. }
  6255. // file: src/Tqdev/PhpCrudApi/RequestUtils.php
  6256. class RequestUtils
  6257. {
  6258. public static function setParams(ServerRequestInterface $request, array $params): ServerRequestInterface
  6259. {
  6260. $query = preg_replace('|%5B[0-9]+%5D=|', '=', http_build_query($params));
  6261. return $request->withUri($request->getUri()->withQuery($query));
  6262. }
  6263. public static function getHeader(ServerRequestInterface $request, string $header): string
  6264. {
  6265. $headers = $request->getHeader($header);
  6266. return isset($headers[0]) ? $headers[0] : '';
  6267. }
  6268. public static function getParams(ServerRequestInterface $request): array
  6269. {
  6270. $params = array();
  6271. $query = $request->getUri()->getQuery();
  6272. $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
  6273. parse_str($query, $params);
  6274. return $params;
  6275. }
  6276. public static function getPathSegment(ServerRequestInterface $request, int $part): string
  6277. {
  6278. $pathSegments = explode('/', rtrim($request->getUri()->getPath(), '/'));
  6279. if ($part < 0 || $part >= count($pathSegments)) {
  6280. return '';
  6281. }
  6282. return urldecode($pathSegments[$part]);
  6283. }
  6284. public static function getOperation(ServerRequestInterface $request): string
  6285. {
  6286. $method = $request->getMethod();
  6287. $path = RequestUtils::getPathSegment($request, 1);
  6288. $hasPk = RequestUtils::getPathSegment($request, 3) != '';
  6289. switch ($path) {
  6290. case 'openapi':
  6291. return 'document';
  6292. case 'columns':
  6293. return $method == 'get' ? 'reflect' : 'remodel';
  6294. case 'records':
  6295. switch ($method) {
  6296. case 'POST':
  6297. return 'create';
  6298. case 'GET':
  6299. return $hasPk ? 'read' : 'list';
  6300. case 'PUT':
  6301. return 'update';
  6302. case 'DELETE':
  6303. return 'delete';
  6304. case 'PATCH':
  6305. return 'increment';
  6306. }
  6307. }
  6308. return 'unknown';
  6309. }
  6310. private static function getJoinTables(string $tableName, array $parameters): array
  6311. {
  6312. $uniqueTableNames = array();
  6313. $uniqueTableNames[$tableName] = true;
  6314. if (isset($parameters['join'])) {
  6315. foreach ($parameters['join'] as $parameter) {
  6316. $tableNames = explode(',', trim($parameter));
  6317. foreach ($tableNames as $tableName) {
  6318. $uniqueTableNames[$tableName] = true;
  6319. }
  6320. }
  6321. }
  6322. return array_keys($uniqueTableNames);
  6323. }
  6324. public static function getTableNames(ServerRequestInterface $request, ReflectionService $reflection): array
  6325. {
  6326. $path = RequestUtils::getPathSegment($request, 1);
  6327. $tableName = RequestUtils::getPathSegment($request, 2);
  6328. $allTableNames = $reflection->getTableNames();
  6329. switch ($path) {
  6330. case 'openapi':
  6331. return $allTableNames;
  6332. case 'columns':
  6333. return $tableName ? [$tableName] : $allTableNames;
  6334. case 'records':
  6335. return self::getJoinTables($tableName, RequestUtils::getParams($request));
  6336. }
  6337. return $allTableNames;
  6338. }
  6339. }
  6340. // file: src/Tqdev/PhpCrudApi/ResponseFactory.php
  6341. class ResponseFactory
  6342. {
  6343. const OK = 200;
  6344. const UNAUTHORIZED = 401;
  6345. const FORBIDDEN = 403;
  6346. const NOT_FOUND = 404;
  6347. const METHOD_NOT_ALLOWED = 405;
  6348. const CONFLICT = 409;
  6349. const UNPROCESSABLE_ENTITY = 422;
  6350. const INTERNAL_SERVER_ERROR = 500;
  6351. public static function fromObject(int $status, $body): ResponseInterface
  6352. {
  6353. $psr17Factory = new Psr17Factory();
  6354. $response = $psr17Factory->createResponse($status);
  6355. $content = json_encode($body, JSON_UNESCAPED_UNICODE);
  6356. $stream = $psr17Factory->createStream($content);
  6357. $stream->rewind();
  6358. $response = $response->withBody($stream);
  6359. $response = $response->withHeader('Content-Type', 'application/json');
  6360. $response = $response->withHeader('Content-Length', strlen($content));
  6361. return $response;
  6362. }
  6363. public static function fromStatus(int $status): ResponseInterface
  6364. {
  6365. $psr17Factory = new Psr17Factory();
  6366. return $psr17Factory->createResponse($status);
  6367. }
  6368. }
  6369. // file: src/Tqdev/PhpCrudApi/ResponseUtils.php
  6370. class ResponseUtils
  6371. {
  6372. public static function output(ResponseInterface $response)
  6373. {
  6374. $status = $response->getStatusCode();
  6375. $headers = $response->getHeaders();
  6376. $body = $response->getBody()->getContents();
  6377. http_response_code($status);
  6378. foreach ($headers as $key => $values) {
  6379. foreach ($values as $value) {
  6380. header("$key: $value");
  6381. }
  6382. }
  6383. echo $body;
  6384. }
  6385. public static function addExceptionHeaders(ResponseInterface $response, \Throwable $e): ResponseInterface
  6386. {
  6387. $response = $response->withHeader('X-Exception-Name', get_class($e));
  6388. $response = $response->withHeader('X-Exception-Message', $e->getMessage());
  6389. $response = $response->withHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());
  6390. return $response;
  6391. }
  6392. public static function toString(ResponseInterface $response): string
  6393. {
  6394. $status = $response->getStatusCode();
  6395. $headers = $response->getHeaders();
  6396. $body = $response->getBody()->getContents();
  6397. $str = "$status\n";
  6398. foreach ($headers as $key => $values) {
  6399. foreach ($values as $value) {
  6400. $str .= "$key: $value\n";
  6401. }
  6402. }
  6403. if ($body !== '') {
  6404. $str .= "\n";
  6405. $str .= "$body\n";
  6406. }
  6407. return $str;
  6408. }
  6409. }
  6410. // file: src/index.php
  6411. $config = new Config([
  6412. 'username' => 'php-crud-api',
  6413. 'password' => 'php-crud-api',
  6414. 'database' => 'php-crud-api',
  6415. ]);
  6416. $request = RequestFactory::fromGlobals();
  6417. $api = new Api($config);
  6418. $response = $api->handle($request);
  6419. ResponseUtils::output($response);