Symfony bundle using Hack

I’ve been working with the Symfony.se website the last couple of weeks. In a discussion on Github we argued how a nice excerpt should look like. We decided that the excerpt twig filter should take a HTML string as input and make it shorter without breaking the HTML. It should also remove tables and convert headings.

Since Symfony.se runs on HHVM I decided to write the Twig filter in Hack. The bundle is not optimized or optimal, it is written to demonstrate some cool Hack features. You will find the bundle here and the cool hack features is found in this file. I wanted to make sure you could run the symfony.se website on a normal PHP installation as well. So I added two excerpt services. One written in PHP and one written in Hack. In the dependency injection configuration I made a check if HACK_VERSION is defined to decide which service to load.

If you want to try the Hack bundle with the symfony.se project I recommend you reading my post about how to install HHVM and Hack.

What to look at

When you are reading the HackExcerpt class pay extra attention to:

  • the first rows declaring the Heading shape
  • the constructor
  • the method declaration of getDefaults
  • the preg_replace_callback function in convertHeadings

2 Comments

  1. Very cool! Definitely makes me want to try it out as well.

    Anything you feel yet have been weird or annoying in Hack?

    Reply
    • Actually no. Everything made perfectly sense. But you really have to know what you are doing. If you know Java or C# you will have no trouble learning Hack.

      One thing that was weird before I understood what I was doing was when I tried to implement an Interface.

      //PHP file
      interface Foo { public function baz($text); }
      //Hack file
      class Bar implements Foo { public function baz(string $text): void {} }

      The code above is invalid. At a first glance i thought that Hack classes could not implement PHP interfaces. But that is wrong. They can. The problem here is that Foo::baz only takes string. That is a narrower declaration then what the interface states. You have to change it to:

      //Hack file
      class Bar implements Foo { public function baz(mixed $text): void {} }

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>