netzmacht / php-javascript-builder
PHP Javascript builder library
Fund package maintenance!
dmolineus
Installs: 26 799
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^7.1 || ^8.0
- ext-ctype: *
- ext-json: *
- symfony/event-dispatcher-contracts: ^1.1 || ^2.0
Requires (Dev)
- phpcq/coding-standard: ^2.1
- phpcq/runner-bootstrap: ^1.0@dev
- phpspec/phpspec: ^6.0 || ^7.0
Suggests
- symfony/event-dispatcher: Install the symfony event dispatcher to use the event dispatching encoder.
This package is auto-updated.
Last update: 2024-10-22 14:28:19 UTC
README
This library is an event based javascript builder/compiler form PHP 5.4.
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.
Install
This library can be installed using composer:
$ 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.
See the example below:
<?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:
$ 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.
// 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 ofNetzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent
is triggered when an reference is requested. It's called before theReferencedByIdentifier
is checked. -
javascript-builder.encode-value
with an event object ofNetzmacht\JavascriptBuilder\Symfony\Event\EncodeReferenceEvent
is triggered when an object value is being created. It's called before the default implementation checks for theConvertsToJavascript
interface or even theJsonSerialize