The new Xajax is out, and the new Xajax is cool!

I started working on a recode of the Xajax library a few weeks ago, and today, I am pleased to present you with the new Xajax library.
The main goal was to use the latest PHP features to make the Xajax library extensible and modular.

So what is it about?

The new Xajax is modular and extensible

The library is now separated into two main packages.

The first package is the core PHP library https://github.com/lagdo/xajax-core.
It provides the main Xajax class, which is now named Xajax, and implements the functionalities needed to export PHP classes to javascript, and handle the Xajax requests and responses.

The second package contains the javascript files of the library https://github.com/lagdo/xajax-js.
Having a javascript only package will later allow to use an open source CDN like https://cdnjs.com/ or https://www.jsdelivr.com/ to deliver the files. By default, the Xajax core library gets the files from a server of mine, which offers HTTP and HTTPS access (thanks to Let’s Encrypt). Of course, they can also be installed on a private server.

This two packages are the minimum requirement to create an Xajax application, however the library can be extended with additional plugins.
I have created three plugins, which are used in the sample codes in the xajax-examples package https://github.com/lagdo/xajax-examples.

https://github.com/lagdo/xajax-toastr:
this package implements javascript notifications with the Toastr JQuery plugin https://github.com/CodeSeven/toastr.

https://github.com/lagdo/xajax-pgwjs:
this package implements responsive modal dialog with the PgwModal library http://pgwjs.com/pgwmodal/.

https://github.com/lagdo/xajax-bootstrap:
this package implements modal dialog with the Twitter Bootstrap library. It requires to manually load the Twitter Bootstrap javascript and CSS files to work properly.

What’s new is this package

First of all, the package is namespaced, uses PSR-4 autoloading, and installs with Composer.
Some features are deprecated, some others, like javascript code generation (see the examples below) are recoded, and some new features are added.

The new feature I consider the most important is the ability to automatically register all the classes in one or more directories, each directory being associated to a namespace or not.
When exporting PHP classes from a directory, the generated javascript classes are named accordingly. In a directory D, the class A in the file D/S/A.php will generate the javascript class S.A, and if the associated namespace is N, the generated javascript class will be N.S.A. As we’ll see in the examples below, the new Xajax library also takes advantage of PHP autoloading to optimize request processing.
This feature makes the Xajax library more suitable for use in modular applications with hundreds of classes.

Examples

Samples code demonstrating the use of the new Xajax library are provided in the xajax-examples package: https://github.com/lagdo/xajax-examples. All examples are based on the helloword.php example in the original Xajax repository https://github.com/Xajax/Xajax/blob/master/examples/helloworld.php. The installation process is described in the homepage of the repository. After the examples are installed, enter the name of any file in the browser to see the result.

– hello.php
This example shows how to export a function with Xajax.

– class.php
This example shows how to export a class with Xajax.

– merge.php
This example shows how to export the generated javascript code in an external file, which is then loaded into the webpage.
You’ll need to create the « deferred » directory, and adapt the parameter of the mergeJavascript() function to your webserver configuration for this example to work.
The compression of the generated javascript code is not yet implemented.

– plugins.php
The example shows the use of Xajax plugins, by adding javascript notifications and modal windows to the class.php example with the xajax-toastr, xajax-pgwjs and xajax-bootstrap packages.
Using an Xajax plugin is very simple. After a plugin is installed with Composer, its automatically registers into the Xajax core library. It can then be accessed both in the Xajax main object, for configuration, and in the Xajax response object, to provide additional functionalities to the application.

– classdirs.php
This example shows how to automatically register all the PHP classes in a set of directories.
The classes in this example are not namespaced, thus they all need to have different names, even if they are in different subdirs.

– namespaces.php
This example shows how to automatically register all the classes in a set of directories with namespaces.
The namespace is prepended to the generated javascript class names, and PHP classes in different subdirs can have the same name.

– autoload-default.php
This example shows how to optimize Xajax requests processing with autoloading.
In all the examples above, an instance of all exported Xajax classes is created when processing a request, while only an instance of the requested class is needed. In an application with a high number of classes, this can cause performance issues.
In this example, the Xajax classes are not registered when processing a request. However, the Xajax library is smart enough to detect that the required class is missing, and load only the necessary file.

– autoload-composer.php
This example illustrates the use of the Composer autoloader.
By default, the Xajax library implements a simple autoloading mechanism by require_once’ing the corresponding PHP file for each missing class. When provided with the Composer autoloader, the Xajax library registers all directories with a namespace into the PSR-4 autoloader, and it registers all the classes in directories with no namespace into the classmap autoloader.

– autoload-disabled.php
In this example the autoloading is disabled in the Xajax library.
The developer then needs to provides its own autoloading, otherwise the Xajax library will raise an error at any attempt to register classes from a directory.
This example will not work until the autoloading is setup.

What’s next?

This version of the package is still an early alpha release, not yet suitable for use in production.
There is still much work to do, and in my point of view the most important ones are:
– Check security, mainly for input data.
– Write tests.
– Write documentation and tutorials.

All ideas, all comments and any help are welcome.
The plugin interface is quite stable now, so anyone willing to write a plugin is encouraged to do so.

Thanks.