PHP Classes

File: src/js/Dromeo.min.js

Recommend this page to a friend!
  Classes of Nikos M.  >  Dromeo PHP Router Library  >  src/js/Dromeo.min.js  >  Download  
File: src/js/Dromeo.min.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Dromeo PHP Router Library
Route HTTP requests to functions with URL patterns
Author: By
Last change: v.1.1.1

* Routes have unique key which combines both route pattern and methods
* fix typos
Date: 1 year ago
Size: 15,787 bytes
 

Contents

Class file image Download
/**
*
*   Dromeo
*   Simple and Flexible Routing Framework for PHP, Python, Node.js / Browser / XPCOM Javascript
*   @version: 1.1.1
*
*   https://github.com/foo123/Dromeo
*
**/
!function(e,t,n){"use strict";var r;"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils.import?(e.EXPORTED_SYMBOLS=[t],e[t]=n.call(e)):"object"==typeof module&&module.exports?module.exports=n.call(e):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(t)?define(t,["require","exports","module"],function(){return n.call(e)}):t in e||(e[t]=r=n.call(e))&&"function"==typeof define&&define.amd&&define(function(){return r})}("undefined"!=typeof self?self:this,"Dromeo",function(){"use strict";function o(e){return e instanceof Array||"[object Array]"===p.call(e)}function f(e){return"[object Object]"===p.call(e)}function m(e){return e instanceof String||"[object String]"===p.call(e)}function _(e){return"function"==typeof e}function h(e){return 0<e.length}function b(e){return e.replace(n,"\\$1")}function P(e){return decodeURIComponent(""+e)}function i(e){return encodeURIComponent(""+e).split("!").join("%21").split("'").join("%27").split("(").join("%28").split(")").join("%29").split("*").join("%2A")}function a(e){return i(e).split("%20").join("+")}function r(e,t,n){var r,l=t.length;if(!0===n)return 0<l&&-1!==t.indexOf(e);for(r=0;r<l;r++)if(e==t[r])return 1}function S(e,t,n){if(t!==n&&n){var r,l,u=[];for(e=e.split(t),l=0;l<e.length;l++)r=(r=e[l]).split(n),u.push(r[0]),1<r.length&&u.push(r[1]);return u}return e.split(t)}function v(e,t,n){var r,l,u,o,i,a,s,f,c=0,p={};for(o=[],a=[],s=null,l=(n=S(n,e[2],e[3])).length,u=!1,r=0;r<l;r++)u=u?(n[r].length&&(A.call(t,n[r])?(o.push("("+t[n[r]][0]+")"),c++,t[n[r]][1]&&(p[c]=t[n[r]][1]),null===s&&(s=o[o.length-1])):(i=n[r].match(d))?(o.push("("+i[1].split("|").filter(h).map(b).join("|")+")"),c++,null===s&&(s=o[o.length-1])):n[r].length&&(o.push("("+b(n[r])+")"),c++,null===s&&(s=o[o.length-1]))),a.push(!0),!1):(n[r].length&&(o.push(b(n[r])),a.push(n[r])),!0);return 1===o.length&&1===c?(p[0]=p[1]?p[1]:null,[f=o.join(""),c,p,a,s||f]):(p[0]=null,[f="("+o.join("")+")",c+1,p,a,s||f])}function y(e,t){return t.join(",")+"->"+e}function R(e){return e=e?e.map?e.map(function(e){return e.toLowerCase()}):[String(e).toLowerCase()]:["*"],r("*",e)&&(e=["*"]),e.sort(),e}function c(e,t,n){var r;for(r=e.length-1;0<=r;r--)n===e[r].key&&(route.name&&A.call(t,route.name)&&delete t[route.name],e[r].dispose(),e.splice(r,1))}function s(e,t,n,r,l,u,o){if(u&&m(u.route)&&u.route.length&&u.handler&&_(u.handler)){o=!0===o;var i,a,s,f,c=u.handler,p=u.defaults||{},h=u.types||null,d=u.name||null,g=R(u.method);for(u=u.route,f=y(u,g),i=null,a=0,s=e.length;a<s;a++)if(f===e[a].key){i=e[a];break}i||(i=new L(n,r,u,g,d,l),e.push(i),i.name&&i.name.length&&(t[i.name]=i)),i.handlers.push([c,p,h,o,0])}}function l(e,t,n,r,l,u,o){var i,a;for(o=!!o,a=0;a<u.length;a++)i=u[a],s(e,t,n,r,l,i,o)}var d=/^([^|]+\|.+)$/,E=/\((\d+)\)$/,t=/^\s+|\s+$/g,n=/([*+\[\]\(\)?^$\/\\:.])/g,e="prototype",u=Object[e],p=(Array[e],Function[e],u.toString),A=u.hasOwnProperty,g="undefined"!=typeof global&&"[object global]"==p.call(global),k=String[e].trim?function(e){return e.trim()}:function(e){return e.replace(t,"")},T=function(e,t,n){var r,l,u;if(n=!0===n,t)for(r in t)A.call(t,r)&&(l=t[r],(u=l)instanceof Number||"[object Number]"===p.call(u)?e[r]=0+l:m(l)?e[r]=l.slice():o(l)?e[r]=n?T(new Array(l.length),l,n):l:f(l)?e[r]=n?T({},l,n):l:e[r]=l);return e},j={php:/^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},N=["source","scheme","authority","userInfo","user","pass","host","port","relative","path","directory","file","query","fragment"],C=function(e){if(o(e))return e;if("function"==typeof Object.values)return Object.values(e);var t,n=[];for(t in e)A.call(e,t)&&n.push(e[t]);return n},w=function(e){if(o(e))return!0;if(f(e)){var t,n=function(e){if("function"==typeof Object.keys)return Object.keys(e);var t,n,r;if(o(e))for(t=new Array(r=e.length),n=0;n<r;n++)t[n]=String(n);else for(n in t=[],e)A.call(e,n)&&t.push(n);return t}(e),r=n.length;for(t=0;t<r;t++)if(t!==+n[t])return!1;return!0}return!1},x=function(e,t,n,r){var l,u,o=r?i:a;if(!0===t?t="1":!1===t&&(t="0"),null==t)return"";if("object"!=typeof t)return o(e)+"="+o(t);for(l in u=[],t)A.call(t,l)&&null!=t[l]&&u.push(x(e+"["+l+"]",t[l],n,r));return u.join(n)};function L(e,t,n,r,l,u){var o=this;o.__args__=[e,t],o.isParsed=!1,o.handlers=[],o.route=null!=n?String(n):"",o.prefix=null!=u?String(u):"",o.method=r,o.pattern=null,o.captures=null,o.literal=!1,o.namespace=null,o.tpl=null,o.name=null!=l?String(l):null,o.key=y(o.route,o.method)}function q(e){var t=this;if(!(t instanceof q))return new q(e);t._delims=["{","}","%","%",":"],t._patterns={},t.definePattern("ALPHA","[a-zA-Z\\-_]+"),t.definePattern("ALNUM","[a-zA-Z0-9\\-_]+"),t.definePattern("NUMBR","[0-9]+"),t.definePattern("INT","[0-9]+","INT"),t.definePattern("PART","[^\\/?#]+"),t.definePattern("VAR","[^=?&#\\/]+","VAR"),t.definePattern("QUERY","\\?[^?#]+"),t.definePattern("FRAGMENT","#[^?#]+"),t.definePattern("URLENCODED","[^\\/?#]+","URLENCODED"),t.definePattern("ALL",".+"),t._routes=[],t._named_routes={},t._fallback=!1,t._prefix=null!=e?String(e):""}return L.to_key=y,L[e]={constructor:L,__args__:null,isParsed:!1,handlers:null,route:null,prefix:null,pattern:null,captures:null,tpl:null,method:null,literal:null,namespace:null,name:null,key:null,dispose:function(){var e=this;return e.__args__=null,e.isParsed=null,e.handlers=null,e.route=null,e.prefix=null,e.pattern=null,e.captures=null,e.tpl=null,e.method=null,e.literal=null,e.namespace=null,e.name=null,e.key=null,e},parse:function(){var e=this;if(e.isParsed)return e;var t=function(e,t,n,r,l){var u,o,i,a,s,f,c,p,h,d,g,m,_,y,P,R,T;if(n.indexOf(e[0])<0)return[n,l&&l.length?l+n:n,{},r,!0,[n]];for(a=(u=S(n,e[0],e[1])).length,c=!1,p="",g=0,_={},T=[],l&&l.length&&(p+=b(l)),i=0;i<a;i++)o=u[i],c=c?(f=s=!1,m=null,(h=o.split(e[4]))[0].length||(h[0]=e[2]+"PART"+e[3]),P=v(e,t,h[0]),1<h.length&&((s=(y=k(h[1])).length&&"?"===y.charAt(0))&&(y=y.slice(1)),(d=y.match(E))?(y=y.slice(0,-d[0].length),R=parseInt(d[1],10),m=A.call(P[2],R)?P[2][R]:null,0<=R&&R<P[1]?R+=g+1:R=g+1):(m=P[2][0]?P[2][0]:null,R=g+1),f=0<y.length),p+=P[0],g+=P[1],s&&(p+="?"),f&&(_[y]=[R,m]),f&&T.push({name:y,optional:s,re:new RegExp("^"+P[4]+"$"),tpl:P[3]}),!1):(p+=b(o),T.push(o),!0);return[n,new RegExp("^"+p+"$"),_,r,!1,T]}(e.__args__[0],e.__args__[1],e.route,e.method,e.prefix);return e.pattern=t[1],e.captures=t[2],e.tpl=t[5],e.literal=!0===t[4],e.__args__=null,e.isParsed=!0,e},match:function(e,t){var n=this;return r(t=t||"*",n.method)||"*"===n.method[0]?(n.isParsed||n.parse(),e=String(e),n.literal?e===n.pattern?[]:null:e.match(n.pattern)):null},make:function(e,t){var n,r,l,u,o,i,a,s=this,f="";for(e=e||{},t=!0===t,s.isParsed||s.parse(),n=0,r=(a=s.tpl).length;n<r;n++)if(m(a[n]))f+=a[n];else{if(!A.call(e,a[n].name)||null==e[a[n].name]){if(a[n].optional)continue;throw new ReferenceError('Dromeo: Route "'+s.name+'" (Pattern: "'+s.route+'") missing parameter "'+a[n].name+'"!')}if(o=String(e[a[n].name]),t&&!a[n].re.test(o))throw new ReferenceError('Dromeo: Route "'+s.name+'" (Pattern: "'+s.route+'") parameter "'+a[n].name+'" value "'+o+'" does not match pattern!');for(l=0,u=(i=a[n].tpl).length;l<u;l++)f+=!0===i[l]?o:i[l]}return f},sub:function(e,t,n){var r,l,u,o,i,a=this;if(!a.isParsed||a.literal)return a;for(r in a.captures)A.call(a.captures,r)&&(u=(l=a.captures[r])[0],o=l[1],e[u]?n&&A.call(n,r)&&n[r]?(i=n[r],m(i)&&A.call(q.TYPES,i)&&(i=q.TYPES[i]),t[r]=_(i)?i(e[u]):e[u]):o?(i=o,t[r]=_(i)?i(e[u]):e[u]):t[r]=e[u]:A.call(t,r)||(t[r]=null));return a}},q.VERSION="1.1.1",q.HTTP_STATUS={100:"Continue",101:"Switching Protocols",102:"Processing",103:"Early Hints",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",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",306:"Switch Proxy",307:"Temporary Redirect",308:"Permanent Redirect",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 Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Request Entity Too Large",414:"Request-URI Too Long",415:"Unsupported Media Type",416:"Requested Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",419:"Authentication Timeout",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",440:"Login Timeout",444:"No Response",449:"Retry With",450:"Blocked by Windows Parental Controls",451:"Unavailable For Legal Reasons",494:"Request Header Too Large",495:"Cert Error",496:"No Cert",497:"HTTP to HTTPS",498:"Token expired/invalid",499:"Client Closed Request",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",509:"Bandwidth Limit Exceeded",510:"Not Extended",511:"Network Authentication Required",520:"Origin Error",521:"Web server is down",522:"Connection timed out",523:"Proxy Declined Request",524:"A timeout occurred",598:"Network read timeout error",599:"Network connect timeout error"},q.Route=L,q.to_method=R,(q.TYPES={INTEGER:function(e){return parseInt(e,10)||0},STRING:function(e){return m(e)?e:""+String(e)},URLDECODE:function(e){return P((""+e).split("+").join("%20"))},ARRAY:function(e){return o(e)?e:[e]},PARAMS:function(e){return m(e)?q.unglue_params(e):e}}).INT=q.TYPES.INTEGER,q.TYPES.STR=q.TYPES.STRING,q.TYPES.VAR=q.TYPES.URLDECODE,q.TYPES.URLENCODED=q.TYPES.PARAMS,q.glue_params=function(e){var t="";return e&&(t+=function(e,t,n){var r,l,u,o=[];for(l in arguments.length<2&&(t="&"),arguments.length<3&&(n=!1),e)A.call(e,l)&&(r=e[l],""!=(u=x(l,r,t,n))&&o.push(u));return o.join(t)}(e,"&",!0)),t},q.unglue_params=function(e){return e?function(e){var t,n,r,l,u,o,i,a,s,f,c,p,h,d,g=e.replace(/^&+|&+$/g,"").split("&"),m=g.length,_={},y=[];for(t=0;t<m;t++)if(o=g[t].split("="),i=P(k(o[0])),a=o.length<2?"":P(k(o[1])),-1<(n=i.indexOf("\0"))&&(i=i.slice(0,n)),i&&"["!==i.charAt(0)){for(f=[],n=s=0;n<i.length;n++)if("["!==i.charAt(n)||s){if("]"===i.charAt(n)&&s&&(f.length||f.push(i.slice(0,s-1)),f.push(i.substr(s,n-s)),s=0,"["!==i.charAt(n+1)))break}else s=n+1;for(f.length||(f=[i]),n=0;n<f[0].length&&(" "!==(u=f[0].charAt(n))&&"."!==u&&"["!==u||(f[0]=f[0].substr(0,n)+"_"+f[0].substr(n+1)),"["!==u);n++);for(i=null,r=l=_,p=f.length?k(f[f.length-1].replace(/^['"]|['"]$/g,"")):null,n=0,c=f.length;n<c;n++)h=i,i=f[n].replace(/^['"]|['"]$/g,""),d=r,r=l,""!==k(i)||0===n?(A.call(l,i)||(l[i]=n+1===c-1&&""===p?[]:{}),l=l[i]):i=!0;!0===i?r.push(a):(i==+i&&y.push({key:h,obj:d}),r[i]=a)}for(t=y.length-1;0<=t;t--)l=y[t].key?y[t].obj[y[t].key]:y[t].obj,w(l)&&(l=C(l),y[t].key?y[t].obj[y[t].key]=l:_=l);return _}(e):{}},q.parse_components=function(e,t,n){var r={};return e&&((arguments.length<3||null==n)&&(n="fragment_params"),(arguments.length<2||null==t)&&(t="query_params"),r=function(e,t,n){for(var r=j[n||"php"].exec(e),l={},u=14;u--;)r[u]&&(l[N[u]]=r[u]);return A.call(l,"port")&&(l.port=parseInt(l.port,10)),t?l[t.replace("PHP_URL_","").toLowerCase()]||null:(l.source&&delete l.source,l)}(e),t&&(r.query?r[t]=this.unglue_params(r.query):r[t]={}),n&&(r.fragment?r[n]=this.unglue_params(r.fragment):r[n]={})),r},q.build_components=function(e,t,n,r,l){var u=""+e;return(arguments.length<5||null==l)&&(l="#"),(arguments.length<4||null==r)&&(r="?"),t&&(u+=r+this.glue_params(t)),n&&(u+=l+this.glue_params(n)),u},q.defType=function(e,t){e&&_(t)&&(q.TYPES[e]=t)},q.TYPE=function(e){return e&&A.call(q.TYPES,e)?q.TYPES[e]:null},q[e]={constructor:q,_delims:null,_patterns:null,_routes:null,_named_routes:null,_fallback:!1,_prefix:"",dispose:function(){var e,t,n=this;if(n._delims=null,n._patterns=null,n._fallback=null,n._prefix=null,n._routes)for(e=0,t=n._routes.length;e<t;e++)n._routes[e].dispose();return n._routes=null,n._named_routes=null,n},reset:function(){return this._routes=[],this._named_routes={},this._fallback=!1,this},defineDelimiters:function(e){var t,n=this._delims;return e&&(0<(t=e.length)&&e[0]&&(n[0]=e[0]),1<t&&e[1]&&(n[1]=e[1]),2<t&&e[2]&&(n[2]=e[2]),3<t&&e[3]&&(n[3]=e[3]),4<t&&e[4]&&(n[4]=e[4])),this},definePattern:function(e,t,n){return n&&m(n)&&n.length&&A.call(q.TYPES,n)&&(n=q.TYPES[n]),n&&_(n)||(n=null),this._patterns[e]=[t,n],this},dropPattern:function(e){var t=this._patterns;return A.call(t,e)&&delete t[e],this},defineType:function(e,t){return q.defType(e,t),this},glue:function(e){return q.glue_params(e)},unglue:function(e){return q.unglue_params(e)},parse:function(e,t,n){return q.parse_components(e,t,n)},build:function(e,t,n,r,l){return q.build_components(e,t,n,r,l)},redirect:function(e,t,n,r){return e&&(g?t&&(arguments.length<3&&(n=302),arguments.length<4&&(r=!0),r?(!0===r&&(r=q.HTTP_STATUS[n]||""),t.writeHead(n,r,{Location:e})):t.writeHead(n,{Location:e}),t.end()):document.location.href=e),this},on:function(){var e,t=this,n=arguments,r=n.length;return e=1===r?o(n[0])?n[0]:[n[0]]:2===r&&m(n[0])&&_(n[1])?[{route:n[0],handler:n[1],method:"*",defaults:{},types:null}]:n,l(t._routes,t._named_routes,t._delims,t._patterns,t._prefix,e),t},one:function(){var e,t=this,n=arguments,r=n.length;return e=1===r?o(n[0])?n[0]:[n[0]]:2===r&&m(n[0])&&_(n[1])?[{route:n[0],handler:n[1],method:"*",defaults:{},types:null}]:n,l(t._routes,t._named_routes,t._delims,t._patterns,t._prefix,e,!0),t},off:function(e,t,n){var r,l,u,o,i=this,a=i._routes,s=i._named_routes;i._prefix;if(!e)return i;if(null==n&&(n="*"),f(e)){if(t=e.handler||t,n=e.method||n,!(e=e.route))return i;for(e=String(e),o=y(e,R(n)),l=null,r=0,u=a.length;r<u;r++)if(o===a[r].key){l=a[r];break}if(!l)return i;if(t&&_(t)){for(r=(u=l.handlers.length)-1;0<=r;r--)t===l.handlers[r][0]&&l.handlers.splice(r,1);l.handlers.length||c(a,s,o)}else c(a,s,o)}else if(m(e)&&e.length){for(e=String(e),o=y(e,R(n)),l=null,r=0,u=a.length;r<u;r++)if(o===a[r].key){l=a[r];break}if(!l)return i;if(t&&_(t)){for(r=(u=l.handlers.length)-1;0<=r;r--)t===l.handlers[r][0]&&l.handlers.splice(r,1);l.handlers.length||c(a,s,o)}else c(a,s,o)}return i},fallback:function(e){return arguments.length<1&&(e=!1),!1!==e&&null!==e&&!_(e)||(this._fallback=e),this},make:function(e,t,n){var r=this._named_routes;return A.call(r,e)?r[e].make(t,n):null},route:function(e,t,n){var r,l,u,o,i,a,s,f,c,p,h,d,g;for(e=null!=e?String(e):"",t=null!=t?String(t).toLowerCase():"*",n=!1!==n,g=!1,s=(r=this._routes.slice()).length,a=0;a<s;a++)if(null!=(p=(l=r[a]).match(e,t))){for(g=!0,f=(h=l.handlers.slice(0)).length,c=0;c<f;c++)(d=h[c])[3]&&d[4]||(o=d[1],i=d[2],u={route:e,method:t,pattern:l.route,fallback:!1,data:T({},o,!0)},l.sub(p,u.data,i),d[4]=1,d[0](u));if(n)return!0}return!!g||(this._fallback&&this._fallback({route:e,method:t,pattern:null,fallback:!0,data:null}),!1)}},q});
For more information send a message to info at phpclasses dot org.