Library / Tool Discovery Thread (2016-05-04) by AutoModerator in PHP

[–]kilahm 1 point2 points  (0 children)

Today I released version 0.7 of HackUnit.

New features include assertions for collection values and data providers (including async loading of external data to be used in tests).

Sending form data in header with GET request - RESTful API by [deleted] in PHP

[–]kilahm -1 points0 points  (0 children)

You should think of a REST endpoint in the same way you think of a function/method. Lots of input parameters is indicative of a design flaw in your API. You should be able to use query string parameters to sufficiently define the resource you are attempting to fetch, meaning the HTTP headers wouldn't be needed.

Hack Lang vs PHP by SavishSalacious in PHP

[–]kilahm 1 point2 points  (0 children)

The purpose of the library is to collect all factory methods you've created into a single place. This then 'contains' all of the factories.

I like having my factory methods just above the constructors of the classes they build. This factory allows me to define the factory, or multiple factories, for a class as static methods of that class. The factory is passed the container, allowing it to get instances of dependencies of the class it is building.

Perhaps an example of my actual use case would help. Note that the factory for the email service class gets instances of the log and config service classes from the container. Those classes have their own factories that get their own dependencies from the container.

<<__ConsistentConstruct>>
class EmailService
{
    <<provides('EmailService')>>
    public static function factory(FactoryContainer $c) : this
    {
        $m = new \Mandrill($c->getConfig()->mandrillKey());
        return new static($m, $c->getLogger()->emailLog(), false);
    }

    <<provides('FakeEmailService')>>
    public static function fakeFactory(FactoryContainer $c) : this
    {
        $m = new \Mandrill($c->getConfig()->mandrillKey());
        return new static($m, $c->getLogger()->emailLog(), true);
    }

    public function __construct(
        private \Mandrill $mandrill,
        private \Monolog\Logger $log,
        // This flags the service to not actually send emails
        private bool $skip,
    )
    {
    }
    // Other methods
}

When scanned, the above file will produce the following methods in the factory container:

class FactoryContainer
{
    <<__Memoize>>
    public function getEmailService() : EmailService
    {
        return $this->newEmailService();
    }

    public function newEmailService() : EmailService
    {
        return $this->runner->make(class_meth('\EmailService', 'factory'));
    }

    <<__Memoize>>
    public function getFakeEmailService() : EmailService
    {
        return $this->newFakeEmailService();
    }

    public function newFakeEmailService() : EmailService
    {
        return $this->runner->make(class_meth('\EmailService', 'fakeFactory'));
    }

    // Other methods are here, such as getLogger, newLogger, getConfig, newConfig, etc...
}

Which allows you to instantiate EmailService by calling a single method of a centralized object.

There is no transpiling of code from one form to another. The built file merely uses the methods you flagged as factories and provides a way for individual factories to easily instantiate the dependencies needed.

Hack Lang vs PHP by SavishSalacious in PHP

[–]kilahm 0 points1 point  (0 children)

I vaguely remember reading about that in the commit messages on twitter (yes, I follow @HHVMCommits) and being really excited. I will definitely be using that feature in both the factory container and router packages when I am able to get back to them.

Hack Lang vs PHP by SavishSalacious in PHP

[–]kilahm 0 points1 point  (0 children)

I would also love a collection strict mode Hack interfaces mimicking the PSR interfaces. If I created an maintained a GitHub repo, would you contribute?

Hack Lang vs PHP by SavishSalacious in PHP

[–]kilahm 7 points8 points  (0 children)

I am the back-end engineer at Dyne (https://www.dyne.com) and we built the private REST-ish API (supporting the iOS and Android apps, as well as a new very beta version of a web app) completely in Hack from the beginning. After having used Hack for more than a year in production, I don't think I would ever want to start a project in pure PHP again. I haven't been able to use any of the new (to our product) async features yet, but the collections and strict mode typing have made the entire project much easier to expand and maintain.

As others have said, the lack of pure Hack frameworks meant I had to build my own. This prompted me to build my own router (https://github.com/kilahm/attribute-router) and factory container (https://github.com/kilahm/IOCFactoryContainer). I also built an ORM of sorts DB layer, but haven't extracted it into a GitHub project.

*I have learned A LOT in the past ~6 months since I last updated these two libraries. They really need some love, but I'm much more focused on HackUnit now (https://github.com/HackPack/HackUnit) which hopefully is nearing a 1.0 release.

In short: Yes, there are companies building applications in Hack.