Skip to content

Instantly share code, notes, and snippets.

@gabrieljmj
Last active July 20, 2023 08:21
Show Gist options
  • Save gabrieljmj/e5218de1a3e9286f4cba to your computer and use it in GitHub Desktop.
Save gabrieljmj/e5218de1a3e9286f4cba to your computer and use it in GitHub Desktop.
Psr4 implementation NodeJS
psr4.add('Gabrieljmj\\Blog\\', 'modules/gabrieljmj/blog/');
psr4.add('Gabrieljmj\\Blog\\Controller\\', 'modules/gabrieljmj/blog/constrollers/');
var postsController = psr4.use('Gabrieljmj\\Blog\\Posts'); //require('modules/gabrieljmj/blog/Posts')
var postsController = psr4.use('Gabrieljmj\\Blog\\Controller\\Posts'); //require('modules/gabrieljmj/blog/controllers/posts')
function psr4 () {
}
psr4.namespaces = [];
psr4.add = function (namespace, path) {
this.namespaces.push({namespace: namespace, path: path});
}
psr4.use = function (module) {
var possibles = [];
this.namespaces.forEach(function (ns) {
if (module.substr(0, ns.namespace.length) == ns.namespace) {
possibles.push(ns);
}
});
if (possibles.length > 0) {
possibles.sort(function (a, b) {
return b.namespace.length - a.namespace.length;
});
var ns = possibles[0];
var newpath = ns.path + module.substr(ns.namespace.length).replace('\\', '/');
return require(newpath);
}
}
module.exports = ps4;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment