Last active
July 20, 2023 08:21
-
-
Save gabrieljmj/e5218de1a3e9286f4cba to your computer and use it in GitHub Desktop.
Psr4 implementation NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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