newsomc / transit-php

Transit for PHP
http://transit-format.org
Apache License 2.0
0 stars 0 forks source link

Dissecting other implementations #1

Open DarrenN opened 10 years ago

DarrenN commented 10 years ago

Looking at other implementations it looks like Cognitect was able to lean on existing parser libs and extend them for Transit.

Ruby https://github.com/cognitect/transit-ruby/blob/master/transit-ruby.gemspec#L24 they lean on oj and msgpack for JSON/MessagePack parsing and then extend them with transit specific capabilities.

Python - leans on msgpack-python https://github.com/cognitect/transit-python/blob/master/setup.py#L27

JavaScript version does not support MessagePack -> "transit-js does not currently support encoding to MessagePack."

The MessagePack implementation for PHP (https://github.com/msgpack/msgpack-php) is a PECL extension, which means it is written in C and has to be compiled/installed using pecl. This is good in that it should be very fast, but it isn't a library you can just pull into your PHP project - you will have to use pecl. We will need to play around with its API to see if its something we can work with from vanilla PHP.

This makes it very hard to write for HHVM - they don't currently support the MessagePack pecl extension so we would have to write our own in either pure PHP or C++ using their extension API.

newsomc commented 10 years ago

Regarding MessagePack and PECL installation, we might be able to get away with installing it using composer: https://github.com/composer/composer/pull/498

DarrenN commented 10 years ago

Good point - I'm playing with a vagrant file right now to get msgpack installed. Composer too.

newsomc commented 10 years ago

Actually, I may have posted that a little too soon. Looks like C extensions may not be quite ready for Composer:

https://github.com/composer/composer/pull/2898

On Sat, Sep 6, 2014 at 10:22 AM, Darren_N notifications@github.com wrote:

Good point - I'm playing with a vagrant file right now to get msgpack installed. Composer too.

— Reply to this email directly or view it on GitHub https://github.com/newsomc/transit-php/issues/1#issuecomment-54713583.

DarrenN commented 10 years ago

I've been looking around at other PHP libraries to see what the current state of the art is. I see a lot of brain damage, but Composer seems to have settled people on a common package format (like Bundler / npm) at least.

Looking through the JS, Ruby and Java implementations there's a common API centered around a Transit object which return Readers and Writers based on the params you pass into their constructors.

PHP seems to desperately want to be Java these days and gets really hung up on interfaces, factories, dependency injection / Inversion of Control and all that OOP jazz. That said, we do have closures and some functional-esque capabilities in PHP now that might ease some pain when it comes to recursively walking data.

DarrenN commented 10 years ago

Also Composer is baked into the vagrant VM so we're good to go there.

DarrenN commented 10 years ago

Also interesting about implementations: In transit-js David converts { objects } into an ES6 Map object with getters/setters and iterators - https://github.com/cognitect/transit-js#transit-maps.

transit-ruby just hands back regular hashes, and I bet Python hands back vanilla Dictionaries.

Something to consider with PHP, do we just cast transit's Map structure to a vanilla PHP object:

$obj = (object) array('foo' => 'bar', 'property' => 'value');
echo $obj->foo; // prints 'bar'

Or did we want to convert it to a more robust object that implements some extras like ArrayIterator or the Iterator interface. Something to consider.