2017 © Pedro Peláez
 

library hack-router-codegen

URI routing for Hack with codegen

image

facebook/hack-router-codegen

URI routing for Hack with codegen

  • Thursday, July 5, 2018
  • by fredemmott
  • Repository
  • 7 Watchers
  • 5 Stars
  • 417 Installations
  • Hack
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 11 Versions
  • 21 % Grown

The README.md

Hack-Router-Codegen

Continuous Integration, (*1)

Code generation for controller classes using the UriPattern system from hhvm/hack-router, (*2)

This currently supports generating: - Request routing maps - Hack request routing classes for your site, (*3)

For now, looking at the unit tests is the best way to learn how to use it., (*4)

Building a Request Router

<?hh
use \Facebook\HackRouter\Codegen;

final class UpdateCodegen {
  public function main(): void {
    Codegen::forTree(
      __DIR__.'/../src/',
      shape(
        'controllerBase' => WebController::class,
        'router' => shape(
          'abstract' => false,
          'file' => __DIR__.'/../codegen/Router.php',
          'class' => 'Router',
        ),
      ),
    )->build;
  }
}

This will generate a class called 'Router', complete with an automatically-generated route map, based on the URI patterns in your controllers., (*5)

WebController is the root controller for your site, and must implement Facebook\HackRouter\IncludeInUriMap, which in turn requires Facebook\HackRouter\HasUriPattern - for example:, (*6)

public static function getUriPattern(): UriPattern {
  return (new UriPattern())
    ->literal('/')
    ->string('MyString')
    ->literal('/')
    ->int('MyInt')
    ->literal('/')
    ->enum(MyEnum::class, 'MyEnum');
}

Commit Your Codegen!

This is unusual advice, but it's the best approach for Hack code as you otherwise have a circular dependency: - HHVM will not execute hack code if there are references to undefined classes - Once you use the codegen, you reference the codegen classes - ... so you can't build them if you don't already have them, (*7)

Contributing

We welcome GitHub issues and pull requests - please see CONTRIBUTING.md for details., (*8)

License

hack-router-codegen is MIT-licensed., (*9)

The Versions