Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
Don't generate <?php any more
Browse files Browse the repository at this point in the history
Removes need for HHI.

This was previously blocked due to being unable to execute Hack code
if there were type errors.

With this, there /may/ be type errors until after the autoload map is
generated.
  • Loading branch information
fredemmott committed Dec 14, 2018
1 parent d1dd7ff commit 2e8b1ec
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Usage

1. Add an `hh_autoload.json` file (see section below) and optionally remove your configuration from composer.json
2. `composer require hhvm/hhvm-autoload`
3. Replace any references to `vendor/autoload.php` with `vendor/hh_autoload.php`
4. If you are using PHPUnit, you will need to add `vendor/hh_autoload.php` to your `bootstrap.php`, or to `phpunit.xml` as a `bootstrap` file if you don't already have one. This is because PHPUnit automatically loads `vendor/autoload.php`, but is not aware of `vendor/hh_autoload.php`
3. Replace any references to `vendor/autoload.php` with `vendor/hh_autoload.hh`
4. If you are using PHPUnit, you will need to add `vendor/hh_autoload.hh` to your `bootstrap.php`, or to `phpunit.xml` as a `bootstrap` file if you don't already have one. This is because PHPUnit automatically loads `vendor/autoload.php`, but is not aware of `vendor/hh_autoload.hh`
5. To re-generate the map, run `vendor/bin/hh-autoload`, `composer dump-autoload`, or any other command that generates the map

Configuration (`hh_autoload.json`)
Expand All @@ -47,9 +47,9 @@ This will look for autoloadable definitions in `src/`, and also look in `vendor/

The following settings are optional:

- `"extraFiles": ["file1.php"]` - files that should not be autoloaded, but should be `require()`ed by `vendor/hh_autoload.php`. This should be needed much less frequently than under Composer
- `"includeVendor": false` - do not include `vendor/` definitions in `vendor/hh_autoload.php`
- `"autoloadFilesBehavior": "scan"|"exec"` - whether autoload `files` from vendor should be `scan`ned for definitions, or `exec`uted by `vendor/hh_autoload.php` - `scan` is the default, and generally favorable, but `exec` is needed if you have dependencies that need code to be executed on startup. `scan` is sufficient if your dependencies just use `files` because they need to define things that aren't classes, which is usually the case.
- `"extraFiles": ["file1.php"]` - files that should not be autoloaded, but should be `require()`ed by `vendor/hh_autoload.hh`. This should be needed much less frequently than under Composer
- `"includeVendor": false` - do not include `vendor/` definitions in `vendor/hh_autoload.hh`
- `"autoloadFilesBehavior": "scan"|"exec"` - whether autoload `files` from vendor should be `scan`ned for definitions, or `exec`uted by `vendor/hh_autoload.hh` - `scan` is the default, and generally favorable, but `exec` is needed if you have dependencies that need code to be executed on startup. `scan` is sufficient if your dependencies just use `files` because they need to define things that aren't classes, which is usually the case.
- `"devRoots": [ "path/", ...]` - additional roots to only include in dev mode, not when installed as a dependency.
- `"relativeAutoloadRoot": false` - do not use a path relative to `__DIR__` for autoloading. Instead, use the path to the folder containing `hh_autoload.json` when building the autoload map.
- `"failureHandler:" classname<Facebook\AutoloadMap\FailureHandler>` - use the specified class to handle definitions that aren't the Map. Your handler will not be invoked for functions or constants
Expand Down Expand Up @@ -121,7 +121,7 @@ How It Works
- This is used to generate something similar to a classmap, except including other kinds of definitions
- The map is provided to HHVM with [`HH\autoload_set_paths()`](https://docs.hhvm.com/hack/reference/function/HH.autoload_set_paths/)

The [Composer plugin API](https://getcomposer.org/doc/articles/plugins.md) allows it to re-generate the `vendor/hh_autoload.php` file automatically whenever Composer itself regenerates `vendor/autoload.php`
The [Composer plugin API](https://getcomposer.org/doc/articles/plugins.md) allows it to re-generate the `vendor/hh_autoload.hh` file automatically whenever Composer itself regenerates `vendor/autoload.php`

Contributing
============
Expand Down
2 changes: 1 addition & 1 deletion bin/hh-autoload
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final class GenerateScript {
? ($importer->getConfig()['devFailureHandler'] ?? null)
: ($importer->getConfig()['failureHandler'] ?? null);

$file = getcwd().'/vendor/hh_autoload.php';
$file = getcwd().'/vendor/hh_autoload.hh';
(new Writer())
->setBuilder($importer)
->setRoot(getcwd())
Expand Down
2 changes: 1 addition & 1 deletion src/ComposerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function onPostAutoloadDump(Event $event) {
->setRelativeAutoloadRoot($importer->getConfig()['relativeAutoloadRoot'])
->setFailureHandler($handler)
->setIsDev($event->isDevMode())
->writeToFile($this->vendor.'/hh_autoload.php');
->writeToFile($this->vendor.'/hh_autoload.hh');
}

private function debugMessage(\HH\string $message) {
Expand Down
20 changes: 0 additions & 20 deletions src/Generated.hhi

This file was deleted.

26 changes: 20 additions & 6 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,33 @@ function ($sub_map): mixed {
true,
);

$map = \var_export($map, true);
$map = \var_export($map, true)
|> \str_replace('array (', 'darray[', $$)
|> \str_replace(')', ']', $$);
$autoload_map_typedef = \var_export(__DIR__.'/AutoloadMap.php', true);
$code = <<<EOF
<?php
<?hh
/// Generated file, do not edit by hand ///
namespace Facebook\AutoloadMap\Generated;
function build_id() {
require_once($autoload_map_typedef);
function build_id(): string {
return $build_id;
}
function root() {
function root(): string {
return $root;
}
function is_dev() {
function is_dev(): bool {
return $is_dev;
}
function map() {
function map(): \Facebook\AutoloadMap\AutoloadMap {
/* HH_IGNORE_ERROR[4110] invalid return type */
return $map;
}
Expand All @@ -203,6 +209,14 @@ function map() {
$code,
);

$legacy_file =
\dirname($destination_file).'/'.\basename($destination_file, '.hh').'.php';
$relative_file = \basename($destination_file);
\file_put_contents(
$legacy_file,
"<?hh\nrequire_once(__DIR__.'/$relative_file');\n",
);

return $this;
}

Expand Down

0 comments on commit 2e8b1ec

Please sign in to comment.