New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

phpify

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

phpify

Compiles PHP modules for the browser with Uniter

Source
npmnpm
Version
5.0.1
Version published
Weekly downloads
10
233.33%
Maintainers
1
Weekly downloads
 
Created
Source

PHPify

Build Status

Compiles PHP modules for the browser with Uniter.

For the Webpack loader, see PHPLoader.

Usage

npm install --save-dev browserify phpify

Simple usage (requiring a single PHP module)

Add to the "browserify" property in package.json:

{
  "version": "1.0.0",
  "name": "my-awesome-pkg",
  "browserify": {
    "transform": [
      "phpify"
    ]
  }
}

Create a PHP module php/src/MyApp/doubleIt.php:

<?php

namespace MyApp;

$doubleIt = function ($num) {
    return $num * 2;
};

return $doubleIt; 

Call from JS module index.js:

var doubleItModule = require('./php/src/MyApp/doubleIt.php')();

doubleItModule.execute().then(function (doubleIt) {
    console.log('Double 4 is ' + doubleIt(4));
});

Run Browserify:

mkdir dist
node_modules/.bin/browserify index > dist/bundle.js

Load the bundle on a webpage, demo.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">

        <title>PHPify demo</title>
    </head>
    <body>
        <h1>PHPify demo</h1>

        <script src="dist/bundle.js"></script>
    </body>
</html>

and open demo.html in a browser.

Complex usage (compiling a Composer app with the Symfony EventDispatcher component)

To avoid lots of typing, you can check out the source for this section here: https://github.com/uniter/event-dispatcher-demo

Install the Symfony EventDispatcher component

composer require symfony/event-dispatcher

Add to the "browserify" property in package.json:

{
  "version": "1.0.0",
  "name": "my-awesome-pkg",
  "browserify": {
    "transform": [
      "phpify"
    ]
  },
  "phpify": {
    "phpToJS": {
      "include": [
        "php/**/*.php",
        "vendor/autoload.php",
        "vendor/composer/**/*.php",
        "vendor/symfony/event-dispatcher/**/*.php"
      ]
    }
  }
}

Create a PHP module php/src/MyApp/dispatchIt.php:

<?php

namespace MyApp;

use Symfony\Component\EventDispatcher\EventDispatcher;

// Load Composer's autoloader
require_once __DIR__ . '/../../../vendor/autoload.php';

$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('my.event', function () {
    print 'Listener called!';
});

$eventDispatcher->dispatch('my.event');
print 'and...';
$eventDispatcher->dispatch('my.event');

Call from JS module index.js:

var dispatchItModule = require('./php/src/MyApp/dispatchIt.php')();

// Hook stdout and stderr up to the DOM
dispatchItModule.getStdout().on('data', function (data) {
    document.body.insertAdjacentHTML('beforeEnd', data + '<br>');
});
dispatchItModule.getStderr().on('data', function (data) {
    document.body.insertAdjacentHTML('beforeEnd', data + '<br>');
});

dispatchItModule.execute();

Run Browserify:

mkdir dist
node_modules/.bin/browserify index > dist/bundle.js

Load the bundle on a webpage, demo.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">

        <title>PHPify demo</title>
    </head>
    <body>
        <h1>PHPify demo</h1>

        <script src="dist/bundle.js"></script>
    </body>
</html>

and open demo.html in a browser.

You should then see the output on the page from running the PHP code browser-side:

Listener called!
and...
Listener called!

Keywords

php

FAQs

Package last updated on 11 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts