2017 © Pedro Peláez
 

library php-javascript-builder

PHP Javascript builder library

image

netzmacht/php-javascript-builder

PHP Javascript builder library

  • Wednesday, October 5, 2016
  • by netzmacht
  • Repository
  • 1 Watchers
  • 0 Stars
  • 893 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 5 % Grown

The README.md

PHP Javascript builder library

Build Status Version License Code quality Downloads, (*1)

This library is an event based javascript builder/compiler form PHP 5.4., (*2)

The goal of this library is to convert an object definition tree which was created in PHP into Javascript. This is useful if you have some dynamically defined javascript libraries., (*3)

Install

This library can be installed using composer:, (*4)


$ php composer.phar require netzmacht/php-javascript-builder:~1.0

Usage

The easiest way to implement the javascript encoding feature is to implement the ConvertsToJavascript interface. Then the encoder uses the provides encode method to encode the object., (*5)

See the example below:, (*6)

<?php

require_once dirname(__DIR__) . '/vendor/autoload.php';

class Foo implements ConvertsToJavascript
{
    private $bar;

    public function __construct($bar)
    {
        $this->bar = $bar;
    }

    public function encode(Encoder $encoder, $flags = null)
    {
        return 'console.log(' . $encoder->encodeReference($this->bar) . ')' . $encoder->close($flags);
    }
}

class Bar implements ConvertsToJavascript, ReferencedByIdentifier
{
    public function encode(Encoder $encoder, $flags = null)
    {
        return sprintf (
            '%s = new Bar()%s',
            $encoder->encodeReference($this),
            $encoder->close($flags)
        );
    }

    public function getReferenceIdentifier()
    {
        return 'bar';
    }
}

$builder = new Builder();

$bar = new Bar();
$foo = new Foo($bar);

echo '

<

pre>';
echo $builder->encode($foo);
// bar = new Bar();
// console.log(bar);

Custom encoders

You can also tweak the encoding process by add another encoder to the encoding chain. This library provides an implementation of an event dispatching encoder using the symfony/event-dispatcher. Be aware that the event dispatcher is not installed by default. If you want to use it, install it:, (*7)


$ php composer.phar require symfony/event-dispatcher:~2.3

The builder accepts an encoder factory callable. So you can easily assign other encoders. Be aware that the ResultCacheEncoder is required so that referenced items get rendered before they are getting referenced. Otherwise you would only see the bar.foo(); output of the example above., (*8)


// Setup the dispatcher outside so that the listeners can be added. $dispatcher = new EventDispatcher(); $factory = function(Output $output) use ($dispatcher) { $encoder = new ChainEncoder(); $encoder ->register(new ResultCacheEncoder()) ->register(new \EventDispatchingEncoder()) ->register(new JavascriptEncoder($output)); return $encoder; };

The event dispatching encoder fires two events: - javascript-builder.encode-reference with an event object of Netzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent is triggered when an reference is requested. It's called before the ReferencedByIdentifier is checked., (*9)

  • javascript-builder.encode-value with an event object of Netzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent is triggered when an object value is being created. It's called before the default implementation checks for the ConvertsToJavascript interface or even the JsonSerialize

The Versions

05/10 2016

dev-master

9999999-dev

PHP Javascript builder library

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4

 

The Development Requires

05/10 2016

1.0.0

1.0.0.0

PHP Javascript builder library

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4

 

The Development Requires

29/01 2015

dev-develop

dev-develop

PHP Javascript builder library

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4

 

The Development Requires

29/01 2015

1.0.0-rc1

1.0.0.0-RC1

PHP Javascript builder library

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4

 

The Development Requires

19/01 2015

1.0.0-beta3

1.0.0.0-beta3

PHP Javascript builder library

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4

 

The Development Requires

19/01 2015

1.0.0-beta2

1.0.0.0-beta2

PHP Javascript builder library

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4

 

The Development Requires

16/01 2015

1.0.0-beta1

1.0.0.0-beta1

PHP Javascript builder library

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4

 

The Development Requires