{"id":15491737,"url":"https://github.com/nacmartin/phpexecjs","last_synced_at":"2025-04-04T13:13:21.963Z","repository":{"id":45296647,"uuid":"50528621","full_name":"nacmartin/phpexecjs","owner":"nacmartin","description":"Run JavaScript code from PHP","archived":false,"fork":false,"pushed_at":"2023-05-25T07:42:48.000Z","size":57,"stargazers_count":146,"open_issues_count":4,"forks_count":26,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-30T06:58:24.482Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nacmartin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-27T18:30:57.000Z","updated_at":"2024-10-26T14:53:50.000Z","dependencies_parsed_at":"2024-06-18T12:23:54.985Z","dependency_job_id":"ca51853c-b1c4-40c9-859c-664fa944ce60","html_url":"https://github.com/nacmartin/phpexecjs","commit_stats":{"total_commits":65,"total_committers":9,"mean_commits":7.222222222222222,"dds":"0.36923076923076925","last_synced_commit":"afafdd81ff6f90440a9865b40a9d6cb4ba73aa41"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacmartin%2Fphpexecjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacmartin%2Fphpexecjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacmartin%2Fphpexecjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacmartin%2Fphpexecjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nacmartin","download_url":"https://codeload.github.com/nacmartin/phpexecjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238603664,"owners_count":19499488,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T07:55:51.354Z","updated_at":"2025-02-15T05:10:45.943Z","avatar_url":"https://github.com/nacmartin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PhpExecJs\n\nPhpExecJS lets you run JavaScript code from PHP.\n\nShort example:\n\n    print_r($phpexecjs-\u003eevalJs(\"'red yellow blue'.split(' ')\"));\n\nWill print:\n\n    Array\n    (\n        [0] =\u003e red\n        [1] =\u003e yellow\n        [2] =\u003e blue\n    )\n\n[![Build Status](https://travis-ci.org/nacmartin/phpexecjs.svg?branch=master)](https://travis-ci.org/nacmartin/phpexecjs)\n[![Latest Stable Version](https://poser.pugx.org/nacmartin/phpexecjs/v/stable)](https://packagist.org/packages/nacmartin/phpexecjs)\n[![Latest Unstable Version](https://poser.pugx.org/nacmartin/phpexecjs/v/unstable)](https://packagist.org/packages/nacmartin/phpexecjs)\n[![License](https://poser.pugx.org/nacmartin/phpexecjs/license)](https://packagist.org/packages/nacmartin/phpexecjs)\n\n# Installation\n\n    composer require nacmartin/phpexecjs\n\nSample program\n\n# Usage\n\n```php\n\u003c?php\n    require __DIR__ . '/../vendor/autoload.php';\n    \n    use Nacmartin\\PhpExecJs\\PhpExecJs;\n    \n    $phpexecjs = new PhpExecJs();\n    \n    print_r($phpexecjs-\u003eevalJs(\"'red yellow blue'.split(' ')\"));\n```\n\nWill print:\n\n    Array\n    (\n        [0] =\u003e red\n        [1] =\u003e yellow\n        [2] =\u003e blue\n    )\n\n\n# Using contexts\n\nYou can set up a context, like libraries and whatnot, that you want to use in your eval'd code. This is used for instance by the [ReactBundle](https://github.com/limenius/ReactBundle/) to render React server-side.\n\nFor instance, we can compile CoffeeScript using this feature:\n\n```php\n    $phpexecjs-\u003ecreateContextFromFile(\"http://coffeescript.org/extras/coffee-script.js\");\n    print_r($phpexecjs-\u003ecall(\"CoffeeScript.compile\", [\"square = (x) -\u003e x * x\", ['bare' =\u003e true]]));\n\n\nThat will print:\n\n      var square;\n    \n      square = function(x) {\n        return x * x;\n      };\n```      \n    \nYou can extend this example to do things like use this function as context:\n\n```php\n    $square = $phpexecjs-\u003ecall(\"CoffeeScript.compile\", [\"square = (x) -\u003e x * x\", ['bare' =\u003e true]]);\n    $phpexecjs-\u003ecreateContext($square);\n    print_r($phpexecjs-\u003eevalJs('square(3)'));\n```    \n    \nThat will print `9`.\n\nThis can be used for instance, to use CoffeeScript or compile templates in JavaScript templating languages. \n\n# How it works\n\nWhen you run `evalJs`, the code will be inserted into a small wrapper used to run JavaScript's `eval()` against your code and check the status for error handling.\n\nIf you set up a context, the code will be inserted before the call to `eval()` in JavaScript, and if you have [the V8Js extension](https://github.com/phpv8/v8js) installed, it will precompile it.\n\n# Runtimes supported\n\nBy default, PhPExecjs will auto-detect the best runtime available. Currently, the routines supported are:\n\n* [V8Js (PHP extension)](https://github.com/phpv8/v8js)\n* node.js\n\nIt is recommended to have V8Js installed, but you may want to have it installed in production and still be able to use PhpExecJs calling node as a subprocess during development, so you don't need to install the extension.\n\n## Adding a external runtime\n\nIf you have a external runner (let's say, Spidermonkey), and you want to use it, pass it to the constructor:\n\n```php\n    $myRuntime = new ExternalRuntime('My runtime name', 'my_command');\n    $phpExecJs = new PhpExecJs($myRuntime);\n```    \n\n## Contributing with runtimes\n\nWe would like to support more runtimes (Duktape, for instance). If you want to contribute with a runtime, it is pretty simple. You just have to implement `src/Runtimes/RuntimeInterface`. Check the directory `src/Runtimes` for examples.\n\n# Why can't I use some functions like `setTimeout`?\n\nPhpExecJs provides a common denominator interface to JavaScript runtimes, so it can only run code that is agnostic about the interpreter. Thus, some features are disabled. Notably, timer functions are disabled because not all runtimes guarantee a full JavaScript event loop. If you want to use any of these please use directly node.js instead of this higher level library:\n\n`global`, `module`, `exports`, `require`, `console`, `setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `setImmediate`, `clearImmediate`\n\n# Credits\n\nThis library is inspired in [ExecJs](https://github.com/rails/execjs), A Ruby library.\n\nThe code used to manage processes and temporary files has been adapted from the [Snappy](https://github.com/KnpLabs/snappy) library by KNP Labs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnacmartin%2Fphpexecjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnacmartin%2Fphpexecjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnacmartin%2Fphpexecjs/lists"}