Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# hack2php
[![Build Status](https://travis-ci.org/codeneric/hack2php.svg?branch=master)](https://travis-ci.org/codeneric/hack2php)

hack2php is a project which aims to implement a compiler to translate Hack files to PHP 5.4 files.
hack2php is a project which aims to implement a compiler to translate Hack files to PHP 7 files.
This ability becomes useful when you have no control over the environment in which your code is supposed to run, but you still want to write your code in Hack.
An example might be the development of WordPress plugins or themes.
An example might be the development of WordPress plugins or themes.

E.g. this Hack code:

```php
<?hh //strict

Expand Down Expand Up @@ -46,24 +47,43 @@ function ano(){
}
```

## Disclaimer

This project is in a very early stage, so you will probably encounter problems in some cases. If so, please create a PR and an issue as described in the Contribution section!

Development is done using version HHVM LTS (3.30), HHVM 4.X and *.hack files have not been tested yet.

# Disclaimer
This project is in a very early stage, so you will probably encounter problems in some cases. If so, please create a PR and an issue as described in the Contribution section!
## Getting started

Add this to your _composer.json_:

```json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/codeneric/hack2php"
}
],
```

Then run `hhvm composer.phar require --dev codeneric\hack2php dev-master`

### Compile a Hack file to PHP

First make sure you have a directory for the new php files (`mkdir <output-target>`).

Compile: `vendor/bin/hack2php <src/file.hh> > <output-target/file.php>`

# Getting started
Clone this repository and run `hhvm composer.phar install`
To validate the php code you can run `php -l <output-target/file.php>`

Compile a Hack file to PHP: `./bin/hack2php <hack-file>`
## Tests

# Tests
There is only one test (HackToPhpTest). It reads each Hack file from the example-files directory, compiles it to PHP and checks the PHP syntax for errors. If no error were found, the test succeeds. Otherwise it fails.
To add a test, simply create a new Hack file in example-files.
Run the test: ` hhvm vendor/bin/phpunit`
There is only one test (HackToPhpTest). It reads each Hack file from the example-files directory, compiles it to PHP and checks the PHP syntax for errors. If no error were found, the test succeeds. Otherwise it fails.
To add a test, simply create a new Hack file in example-files.
Run the test: `hhvm vendor/bin//hacktest tests/`

# Contributing
If you find an issue you can help to fix it. Please add a Hack file which is not compiled correctly in the example-files folder and create a PR.
Or you can fix the issue directly :)
But please add an example Hack file to the example-files folder nonetheless.
## Contributing

If you find an issue you can help to fix it. Please add a Hack file which is not compiled correctly in the example-files folder and create a PR.
Or you can fix the issue directly :)
But please add an example Hack file to the example-files folder nonetheless.
49 changes: 28 additions & 21 deletions bin/hack2php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env hhvm
<?hh
<?hh // strict
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
Expand All @@ -9,28 +9,35 @@
*
*/

namespace Facebook\HHAST\__Private;
namespace codeneric\hack2php;

$root = realpath(__DIR__.'/..');
$found_autoloader = false;
while (true) {
$autoloader = $root.'/vendor/hh_autoload.php';
if (file_exists($autoloader)) {
$found_autoloader = true;
require_once($autoloader);
break;
<<__EntryPoint>>
async function transpileAsync(): Awaitable<noreturn> {

$root = realpath(__DIR__.'/..');
$found_autoloader = false;
while (true) {
$autoloader = $root.'/vendor/hh_autoload.php';
if (file_exists($autoloader)) {
$found_autoloader = true;
require_once($autoloader);
break;
}
if ($root === '') {
break;
}
$parts = explode('/', $root);
array_pop(&$parts);
$root = implode('/', $parts);
}
if ($root === '') {
break;

if (!$found_autoloader) {
fprintf(STDERR, "Failed to find autoloader.\n");

exit(1);
}
$parts = explode('/', $root);
array_pop(&$parts);
$root = implode('/', $parts);
}

if (!$found_autoloader) {
fprintf(STDERR, "Failed to find autoloader.\n");
exit(1);
}

LinterCLI::main();
$code = await TranspilerCLI::runAsync();
exit($code);
}
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
"hhvm/hhast": "^3.27"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"91carriage/phpunit-hhi": "^5.7",
"facebook/fbexpect": "^0.4.0|^1.0.0",
"facebook/hack-codegen": "~3.0.3",
"hhvm/hhvm-autoload": "^1.5"
"hhvm/hhvm-autoload": "^1.5",
"hhvm/hacktest": "^1.3"
},
"license": "MIT",
"authors": [
Expand Down
Loading