CommonJS for PHP

587 views
Skip to first unread message

Dr. Benton

unread,
Sep 13, 2012, 2:24:09 PM9/13/12
to comm...@googlegroups.com
Hi !

Just a word for people who love using CommonJS Module pattern but have to deal with PHP projects at work, like me :-)

I've created a light-weight CommonJS implementation for PHP (≈ 150 lines of code), available on GitHub : https://github.com/DrBenton/CommonJSForPHP 
It has the following CommonJS features:
  • Modules run in a "Closure sandbox"
  • $require method, available in or outside Modules
  • in the Modules sandbox we have access to the following variables (and only to them):
    • the $require function
    • a $exports array, for exposing stuff
    • a $module array, with $module['id'] and $module['uri'] properties according to the CommonJS spec. One can also set a direct export with $module['exports'].
Furthermore, I have added 2 features inspired by RequireJS : a "defined-by-Closure" functionallity with a "$define" function, and Modules "plugins" with the syntax $require('[plugin name]![resource path']').
I also added a PHP version of Node's "Folder as Module".

I wish that it can be useful for others Javascript lovers who work with PHP too!

Olivier

Christoph Dorn

unread,
Sep 13, 2012, 3:17:53 PM9/13/12
to comm...@googlegroups.com

Interesting.

I built a prototype to bring some CommonJS concepts to PHP as well:

  https://github.com/pinf/loader-php

Rather than asking PHP developers to use so much of the JS-specific terminology (define, require, ...) I opted to focus on transferring the concepts.

You can map packages/dependencies using `package.json` [1] and use them with [2]:

  use lang-1/Greetings as Greets1;

The loader creates sandboxes for each package and builds on the PHP5 concept of namespaces to uniquely identify mapped packages. This is 100% compatible with the package/module isolation strategy CommonJS is trying to achieve.

The PHP loader is not up to date with the latest refinements (descriptor formats, layering, abstraction) I am working on as part of sourcemint [3] but I am planning on porting it soon.

I see room for both your and my approach and would welcome more discussion. I tried to setup a "commonphp" google group but keep getting an error. Maybe you have more luck.

I own the `commonphp.org` domain and was hoping to eventually publish some prototypes.

Christoph

[1] - https://github.com/pinf/loader-php/blob/master/demos/StaticMappings/package.json
[2] - https://github.com/pinf/loader-php/blob/master/demos/StaticMappings/main.php
[3] - https://github.com/sourcemint


Dr. Benton
13 September, 2012 11:24 AM
--
You received this message because you are subscribed to the Google Groups "CommonJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/commonjs/-/eL4nJsPGTj4J.
To post to this group, send email to comm...@googlegroups.com.
To unsubscribe from this group, send email to commonjs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/commonjs?hl=en.


Reply all
Reply to author
Forward
0 new messages