CreatePHP Taking Shape – Another IKS Early Adopter

It has taken some doing, but the CreateJS PHP Adapter we’ve been working on is getting ready for some more eyeballs to look at it. As you may have read in my other post, we’re currently working on a PHP library for managing data based on VIE/CreateJS to facilitate the integration of the CreateJS interface and the necessary RDF rendering into the plethora of PHP-based frameworks and applications.

Since these goals (and VIE’s concepts) are quite lofty, while code (some might say especially PHP code) is usually messy, we went for a pragmatic approach. That is, we didn’t set out to create the purest, most faithful representation of RDF concepts that would require a rewrite of the entire framework in order to work seamlessly, or to abstract away so many details that the library is easy to use, but impossible to customize.

Instead, we went for the lowest common denominator, which is of course HTML, the format that PHP produces and VIE consumes. Consequently, the most basic building block of CreatePHP is the node object. Nodes represent DOM nodes, in particular those that are supposed to hold semantically-enhanced information that CreateJS can operate on. The node’s content comes from the framework’s storage layer, whatever that might be (you’ll have to implement CreatePHP’s rdfMapper interface to talk to it).

At the moment, there are three different node classes in CreatePHP, that represent different types of data. First, there are controllers, which correspond to the entire entity (might be an ORM object, a database row, or whatever you tell your rdfMapper to work with). Controllers can have any number of properties (e.g. a title for a blog post, an abstract etc.), which are represented by propertyNodes. Lastly, controllers can also have collections which represent other entities linked to the current one (e.g. individual images in a gallery, or blog entries in a particular category). These are represented by controllers again, so you could recursively model a tree representation of almost all of your content with this structure.

In order to make it easier to get started with CreatePHP, the arrayMapper class provides a way to setup such a tree by specifying its structure in a PHP array, like so:

$config = array
(
    'employees' => array(
        'attributes' => array(
            'typeof' => 'foaf:Agent',
        ),
        'vocabularies' => array(
           'foaf' => 'http://xmlns.com/foaf/0.1/',
        ),
        'properties' => array(
            'name' => array(
                'attributes' => array(
                    'property' => 'foaf:name',
                    'class' => 'employee-name'
                )
            ),
            'role' => array(
                'attributes' => array(
                    'property' => 'foaf:status',
                    'class' => 'employee-role'
                )
            )
        )
    )
);

This defines a very simple employee schema that captures the person’s name and role. You can transform this into a CreatePHP controller like this:

$mapper = new your_rdfMapper_class;
$manager = new createphp\arrayManager($mapper, $config);
$controller = $manager->get_controller('employee');

To render CreateJS-compatible markup, use

echo $controller;

which will produce output like the following:

<div typeof="foaf:Agent"
     xmlns:foaf="http://xmlns.com/foaf/0.1/"
     about="http://yourserver/unique_identifier">
  <div property="foaf:name">
  </div>
  <div property="foaf:status">
  </div>
</div>

To populate the controller with data, use

$data = load_from_somewhere($id);
$controller = $manager->get_controller('employee', $data);

You can also render individual parts of the controller separately or modify them on the fly:

$controller->role->set_attribute('style', 'display:none');
echo $controller->name;

In fact, since the entire controller structure is built by API methods, you can also implement a class that automatically instantiates a controller object based on introspection or other means.

The REST service class that is responsible for saving data sent by CreateJS to the storage backend operates on the same controller structure, so the integration code necessary is almost the same:

$received_data = json_decode(file_get_contents("php://input"), true);
$mapper = new your_rdfMapper_class;
$manager = new createphp\arrayManager($mapper, $config);
$controller = $manager->get_controller('employee');
$service = new createphp\restservice($mapper, $received_data);
$jsonld = $service->run($controller);

And that’s all you need to get started. Of course, the API is not yet set in stone, so suggestions or feedback is welcome. The next step for CreatePHP will most likely be the automatic rendering of the CreateJS constructor (ATM, this has to be done by hand), but it has quite an interesting structure with regards to configuration options, so this will require some thought. But since the code is on github (and installable with Composer) now, you can also start playing with it right away.

Comments are closed.