Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chenos committed Feb 5, 2018
0 parents commit 0270f99
Show file tree
Hide file tree
Showing 95 changed files with 2,332 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage_clover: clover.xml
json_path: coveralls-upload.json
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.coveralls.yml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/composer.lock export-ignore
/docker/ export-ignore
/docker-compose.yml export-ignore
/Makefile export-ignore
/phpunit.xml export-ignore
/tests/ export-ignore
/examples/ export-ignore
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.env
vendor
composer.lock
composer.phar
Dockerfile-local
node_modules
package-lock.json
yarn.lock
clover.xml
coveralls-upload.json
build
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: php

services:
- docker

sudo: false
dist: trusty
group: deprecated-2017Q4

script: make test
48 changes: 48 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
install:
docker-compose run --rm php composer install

test: install
# docker-compose run --rm node bash -c "cd ./tests/javascript && npm install"
docker-compose run --rm php composer test-coverage
docker-compose run --rm php composer coveralls

markdown:
docker-compose run --rm php bash -c "cd ./examples/markdown && composer install"
docker-compose run --rm node bash -c "cd ./examples/markdown && npm install"
docker-compose run --rm php bash -c "time php ./examples/markdown/parsedown.php"
docker-compose run --rm php bash -c "time php ./examples/markdown/marked.php"
docker-compose run --rm node bash -c "time node ./examples/markdown/marked.js"
docker-compose run --rm php bash -c "time php ./examples/markdown/markdown-it.php"
docker-compose run --rm node bash -c "time node ./examples/markdown/markdown-it.js"

vue-simple:
docker-compose run --rm node bash -c "cd ./examples/vue-simple && npm install && npm run build"
docker-compose run --rm php bash -c "time php ./examples/vue-simple/test.php"
docker-compose run --rm node bash -c "time node ./examples/vue-simple/test.js"

vue-router:
docker-compose run --rm node bash -c "cd ./examples/vue-router && npm install && npm run build"
docker-compose run --rm php bash -c "time php ./examples/vue-router/test.php"
docker-compose run --rm node bash -c "time node ./examples/vue-router/test.js"

react-router:
docker-compose run --rm node bash -c "cd ./examples/react-router && npm install && npm run build"
docker-compose run --rm php bash -c "time php ./examples/react-router/test.php"
docker-compose run --rm node bash -c "time node ./examples/react-router/test.js"

liquidjs:
docker-compose run --rm php bash -c "cd ./examples/liquidjs && composer install"
docker-compose run --rm node bash -c "cd ./examples/liquidjs && yarn install && yarn liquidjs"
docker-compose run --rm php bash -c "time php ./examples/liquidjs/liquid.php"
docker-compose run --rm node bash -c "time node ./examples/liquidjs/liquid.js"
docker-compose run --rm php bash -c "time php ./examples/liquidjs/twig.php"

yaml:
docker-compose run --rm php bash -c "cd ./examples/yaml && composer install"
docker-compose run --rm node bash -c "cd ./examples/yaml && yarn install"
docker-compose run --rm php bash -c "time php ./examples/yaml/yaml.php"
docker-compose run --rm node bash -c "time node ./examples/yaml/yaml.js"
docker-compose run --rm php bash -c "time php ./examples/yaml/symfony.php"

example: install markdown vue-simple vue-router react-router liquidjs yaml
docker-compose up -d nginx
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# ExecJs

[![Build Status](https://travis-ci.org/chenos/execjs.svg?branch=master)](https://travis-ci.org/chenos/execjs) [![Coverage Status](https://coveralls.io/repos/github/chenos/execjs/badge.svg?branch=master&v1)](https://coveralls.io/github/chenos/execjs?branch=master)

## Requirements

- PHP 7.0+
- V8Js extension 2.0+

## Installation

```sh
composer require chenos/execjs
```

## Testing

```
make test
```

## Example

```php
make example
```

Access http://127.0.0.1:9999

## Usage

```php
use Chenos\ExecJs\Engine;

$engine = new Engine(__DIR__.'/entry', __DIR__.'/node_modules');

$engine->setExtensions(['.js', '.json']);
$engine->setEntryDirectory(__DIR__.'/entry');
$engine->addVendorDirectory(__DIR__.'/node_modules');
$engine->addOverride('vue', 'vue/dist/vue.runtime.common.js');

$engine->loadModule($file)
$engine->executeScript($script);
$engine->executeString($string);
$engine->executeFile($file);
$engine->compileString($string);
$engine->compileFile($file);
```

Custom Engine

```php
use Chenos\ExecJs\Engine;
use Chenos\ExecJs\Pool;

class Vue extends Engine
{
protected function initialize()
{
$this->setEntryDirectory(__DIR__)
->addVendorDirectory(__DIR__.'/node_modules')
->addOverride('vue', 'vue/dist/vue.runtime.common.js');

$this->executeString("
this.process = { env: { VUE_ENV: 'server', NODE_ENV: 'production' } }
this.global = { process: process }
var renderToString = require('./renderToString.js')
");
}

public function render($component, callable $callback = null)
{
$this->component = $component;
$this->callback = $callback;
$this->executeString("
var app = require(PHP.component)
renderToString(app).then(PHP.callback||print)
");
}
}

$pool = new Pool();

$vue = $pool->get(Vue::class);

$vue->render('./hello.js');

// I/O streams
$handle = fread('php://temp', 'wb');
$vue->render('./hello.js', function ($html) use ($handle) {
fwrite($handle, $html);
});
rewind($handle);
fpassthru($handle);

$pool->dispose($vue);
```
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "chenos/execjs",
"require": {
"chenos/v8js-module-loader": "^0.2.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5",
"php-coveralls/php-coveralls": "^2.0",
"kint-php/kint": "^2.2"
},
"autoload": {
"psr-4": {
"Chenos\\ExecJs\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Chenos\\ExecJs\\Tests\\": "tests"
}
},
"scripts": {
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"coveralls": "php-coveralls -v"
}
}
38 changes: 38 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: '3'

services:

nginx:
image: nginx
ports:
- ${PORT:-9999}:80
volumes:
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
- ./:/var/www/html
depends_on:
- php
networks:
- backend

php:
build:
context: ./docker/
dockerfile: ${DOCKERFILE:-Dockerfile-71}
environment:
TRAVIS: ${TRAVIS:-''}
TRAVIS_JOB_ID: ${TRAVIS_JOB_ID:-''}
working_dir: /var/www/html
volumes:
- ./:/var/www/html
networks:
- backend

node:
image: node
working_dir: /var/www/html
volumes:
- ./:/var/www/html

networks:
backend:
driver: bridge
25 changes: 25 additions & 0 deletions docker/Dockerfile-71
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM php:7.1-fpm

RUN apt-get update
RUN apt-get install -y --no-install-recommends git zlib1g-dev

RUN docker-php-ext-install zip

RUN git clone https://github.com/chenos/v8.git /opt/v8
RUN git clone https://github.com/phpv8/v8js.git /tmp/v8js

RUN cd /opt/v8 && git checkout tags/v6.4.388.18
# RUN cd /tmp/v8js && git checkout tags/2.1.0

RUN cd /tmp/v8js && phpize
RUN cd /tmp/v8js && ./configure CXXFLAGS="-Wall -Wno-write-strings -Werror" \
LDFLAGS="-lstdc++" --with-v8js=/opt/v8
RUN cd /tmp/v8js && make
RUN cd /tmp/v8js && make test
RUN cd /tmp/v8js && make install
RUN docker-php-ext-enable v8js

RUN curl -s http://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer

RUN pecl install xdebug && docker-php-ext-enable xdebug
35 changes: 35 additions & 0 deletions docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
server {

server_name app.test;
root /var/www/html/examples;
index index.php;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location /vue-router/ {
try_files $uri $uri/ /vue-router/index.php$is_args$args;
}

location /react-router/ {
try_files $uri $uri/ /react-router/index.php$is_args$args;
}

location ~ \.php$ {
# try_files $uri /index.php =404;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}

error_log /var/log/nginx/app_error.log;
access_log /var/log/nginx/app_access.log;
}
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
yarn.lock
1 change: 1 addition & 0 deletions examples/liquidjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/cache
24 changes: 24 additions & 0 deletions examples/liquidjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Liquidjs

## Performance

Loop 1,000 times

```
docker-compose run --rm php bash -c "time php ./examples/liquidjs/liquid.php"
Hello Liquid!
real 0m0.249s
user 0m0.070s
sys 0m0.080s
docker-compose run --rm node bash -c "time node ./examples/liquidjs/liquid.js"
Hello Liquid!
real 0m0.636s
user 0m0.200s
sys 0m0.140s
docker-compose run --rm php bash -c "time php ./examples/liquidjs/twig.php"
Hello Twig!
real 0m0.263s
user 0m0.050s
sys 0m0.120s
```
6 changes: 6 additions & 0 deletions examples/liquidjs/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

define('__ROOT__', __DIR__);

$loader = require __DIR__.'/../../vendor/autoload.php';
$loader->addPsr4('Chenos\\ExecJs\\Liquid\\', __DIR__.'/src');
5 changes: 5 additions & 0 deletions examples/liquidjs/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"twig/twig": "^2.4"
}
}
9 changes: 9 additions & 0 deletions examples/liquidjs/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Chenos\ExecJs\Liquid\Liquid;

require __DIR__.'/autoload.php';

$liquid = new Liquid();

$liquid->render('hello.html', ['name' => 'Liquidjs']);
23 changes: 23 additions & 0 deletions examples/liquidjs/liquid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var Liquid = require('liquidjs')

if (typeof PHP === 'undefined') {
var print = console.log
} else {
var __dirname = PHP.__ROOT__
}

var engine = Liquid({
root: __dirname + '/views',
extname: '.html',
cache: true,
})

async function output() {
var html
for (var i = 0; i < 1000; i++) {
html = await engine.renderFile('hello.html', {name: 'Liquid'})
}
return html
}

output().then(print).catch(print)
Loading

0 comments on commit 0270f99

Please sign in to comment.