Share
Embed
Transcript
85 slides
-
Profile • ᖒ ༗و / ytake • גࣜձࣾΞΠελΠϧ CTO •
PHP, Hack, Go, Scala • Apache Hadoop, Apache Spark, Apache Kafka • twitter https://twitter.com/ex_takezawa • facebook https://www.facebook.com/yuuki.takezawa • github https://github.com/ytake -
PHP-FIG? • PHP Framework Interop Group • ͓ͳ͡Έ PSR-x •
PHP Standards Recommendation -
PSR-7 was designed for PHP, not Hack, and some descisions
do not fit smoothly with Hack's type system. -
Additionally, with the planned end of PHP support in HHVM,
it will stop being possible to use the canonical definitions or common implementations of PSR-7 in Hack code. -
Ending PHP Support, and The Future Of Hack • PHP
Is Unsupported(·ͩͪΐͬͱಈ͘) • গͣͭ͠PHPͷػೳഉআ -
Ending PHP Support, and The Future Of Hack • .hack
֦ுࢠͷՃ <?hh // strict Λهड़ͤͣͱstrictʹ (ਪ) • extract()ͳͲͷআ • remove behavior that exists in PHP Arrays but not Hack Arrays or Hack Collections. -
HHVM 4.0 has dropped support for Composer, please use PHP
instead. Aborting. -
Package Manager • We are currently working to move
to a package manager that fully supports multiple languages. -
Package Manager • HackYarnΛϕʔεʹͨ͠Package ManagerҠߦத (Β͍͠) - https://github.com/composer/composer/issues/7734 - https://github.com/yarnpkg/yarn/issues/7001
- https://github.com/esy/esy/issues/850 -
enum HTTPMethod: string { PUT = 'PUT'; GET = 'GET';
POST = 'POST'; HEAD = 'HEAD'; PATCH = 'PATCH'; TRACE = 'TRACE'; DELETE = 'DELETE'; OPTIONS = 'OPTIONS'; CONNECT = 'CONNECT'; } -
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s). * @return static -
/** * Retrieves a message header value by the given
case-insensitive name. * * This method returns an array of all the header values of the given * case-insensitive header name. * * If the header does not appear in the message, this method MUST return an * empty array. * * @param string $name Case-insensitive header field name. * @return string[] An array of string values as provided for the given * header. If the header does not appear in the message, this method MUST * return an empty array. */ public function getHeader($name); -
/** * Retrieves all message header values. * * The
keys represent the header name as it will be sent over the wire, and * each value is a vec of strings associated with the header. * * While header names are not case-sensitive, getHeaders() will preserve the * exact case in which headers were originally specified. * * Implementations *MUST* return header names of the form `Foo-Bar`, but keys * should be considered case-insensitive. * * Implementations *MAY* choose to normalize some headers in different ways, * for example, `ETag` instead of `Etag`. */ public function getHeaders(): dict<string, vec<string>>; -
<<__Memoize>> public static function requestInput(): IO\ReadHandle { /* HH_IGNORE_ERROR[2049] __PHPStdLib
*/ /* HH_IGNORE_ERROR[4107] __PHPStdLib */ return new self(\fopen('php://input', 'r')); } <<__Memoize>> public static function requestOutput(): IO\WriteHandle{ /* HH_IGNORE_ERROR[2049] __PHPStdLib */ /* HH_IGNORE_ERROR[4107] __PHPStdLib */ return new self(\fopen('php://output', 'w')); } -
final class PipeHandle extends NativeHandle { public static function createPair():
(this, this) { /* HH_IGNORE_ERROR[2049] intentionally not in HHI */ /* HH_IGNORE_ERROR[4107] intentionally not in HHI */ list($r, $w) = Native\pipe() as (resource, resource); return tuple(new self($r), new self($w)); } } -
/** Create a pair of handles, where writes to the
`WriteHandle` can be * read from the `ReadHandle`. */ function pipe_non_disposable(): (ReadHandle, WriteHandle) { return _Private\PipeHandle::createPair(); } -
interface IDisposable { /** * This method is invoked exactly
once at the end of the scope of the * using statement, unless the program terminates with a fatal error. */ public function __dispose(): void; } interface IAsyncDisposable { /** * This method is invoked exactly once at the end of the scope of the * await using statement, unless the program terminates with a fatal error. */ public function __disposeAsync(): Awaitable<void>; } -
<<__ReturnDisposable>> protected function reader(): Filesystem\DisposableFileReadHandle { return Filesystem\open_read_only($this->filename); } public
async function saveAsync( Serializer\SerializeInterface $serializer ): Awaitable<void> { if($this->exists()) { return; } await using $handle = $this->writer(); await $handle->writeAsync($serializer->serialize()); await $handle->closeAsync(); } -
<<__ReturnDisposable>> protected function reader(): Filesystem\DisposableFileReadHandle { return Filesystem\open_read_only($this->filename); } public
async function saveAsync( Serializer\SerializeInterface $serializer ): Awaitable<void> { if($this->exists()) { return; } await using $handle = $this->writer(); await $handle->writeAsync($serializer->serialize()); await $handle->closeAsync(); } -
<<__ReturnDisposable>> protected function reader(): Filesystem\DisposableFileReadHandle { return Filesystem\open_read_only($this->filename); } public
async function saveAsync( Serializer\SerializeInterface $serializer ): Awaitable<void> { if($this->exists()) { return; } await using $handle = $this->writer(); await $handle->writeAsync($serializer->serialize()); await $handle->closeAsync(); } -
/** * Gets the body of the message. */ public
function getBody(): IO\ReadHandle; /** * Return an instance with the specified message body. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return a new instance that has the * new body stream. */ public function withBody(IO\ReadHandle $body): this; -
/** * Gets the body of the message. */ public
function getBody(): IO\WriteHandle; /** * Return an instance with the specified message body. * * This method MUST be implemented in such a way as to retain the * immutability of the message, and MUST return a new instance that has the * new body stream. */ public function withBody(IO\WriteHandle $body): this; -
To ReadHandle / WriteHandle • we need separte {Server,Client}{Request,Response} interfaces
too in a server context, request body is read, response body is write but it's the opposite for clients -
To ReadHandle / WriteHandle • server context the response is
both read and write the controller writes the body the emitter reads the body to send it to the client -
public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface {
if ($this->checkRequest($request)) { try { $request = $request->withParsedBody( $this->parse($request->getBody()) ); } catch (Exception $exception) { throw HttpErrorException::create(400, [], $exception); } } return $handler->handle($request); } -
public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface {
if ($this->checkRequest($request)) { try { $request = $request->withParsedBody( $this->parse($request->getBody()) ); } catch (Exception $exception) { throw HttpErrorException::create(400, [], $exception); } } return $handler->handle($request); } -
type HandlerFunc func(ResponseWriter, *Request) // ServeHTTP calls f(w, r). func
(f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) } -
final class PipeHandle extends NativeHandle { public static function createPair():
(this, this) { /* HH_IGNORE_ERROR[2049] intentionally not in HHI */ /* HH_IGNORE_ERROR[4107] intentionally not in HHI */ list($r, $w) = Native\pipe() as (resource, resource); return tuple(new self($r), new self($w)); } } -
list($read, $write) = IO\pipe_non_disposable(); $decode = \json_encode(dict['body' => 'message']); await
$write->writeAsync($decode); await $read->readAsync(); -
list($read, $write) = IO\pipe_non_disposable(); $decode = \json_encode(dict['body' => 'message']); await
$write->writeAsync($decode); await $read->readAsync(); -
list($read, $write) = IO\pipe_non_disposable(); $decode = \json_encode(dict['body' => 'message']); await
$write->writeAsync($decode); await $read->readAsync(); -
list($read, $write) = IO\pipe_non_disposable(); $decode = \json_encode(dict['body' => 'message']); await
$write->writeAsync($decode); await $read->readAsync(); -
WriteHandle • WriteHandleඇಉظʹ͢Δ͜ͱ͕ॏཁ (asyncΛ͏) • we really don't want a
standard that encourages hiding HH\Asio\join -
Asynchronous Operations namespace Hack\UserDocumentation\AsyncOps\Basics\Examples\AsyncCurl; use namespace HH\Lib\Vec; async function curl_A():
Awaitable<string> { $x = await \HH\Asio\curl_exec("http://example.com/"); return $x; } async function curl_B(): Awaitable<string> { $y = await \HH\Asio\curl_exec("http://example.net/"); return $y; } async function async_curl(): Awaitable<void> { $start = \microtime(true); list($a, $b) = await Vec\from_async(vec[curl_A(), curl_B()]); $end = \microtime(true); echo "Total time taken: " . \strval($end - $start) . " seconds\n"; } <<__EntryPoint>> function main():void { \HH\Asio\join(async_curl()); } -
Asynchronous Operations namespace Hack\UserDocumentation\AsyncOps\Basics\Examples\AsyncCurl; use namespace HH\Lib\Vec; async function curl_A():
Awaitable<string> { $x = await \HH\Asio\curl_exec("http://example.com/"); return $x; } async function curl_B(): Awaitable<string> { $y = await \HH\Asio\curl_exec("http://example.net/"); return $y; } async function async_curl(): Awaitable<void> { $start = \microtime(true); list($a, $b) = await Vec\from_async(vec[curl_A(), curl_B()]); $end = \microtime(true); echo "Total time taken: " . \strval($end - $start) . " seconds\n"; } <<__EntryPoint>> function main():void { \HH\Asio\join(async_curl()); } -
Asynchronous Operations namespace Hack\UserDocumentation\AsyncOps\Basics\Examples\AsyncCurl; use namespace HH\Lib\Vec; async function curl_A():
Awaitable<string> { $x = await \HH\Asio\curl_exec("http://example.com/"); return $x; } async function curl_B(): Awaitable<string> { $y = await \HH\Asio\curl_exec("http://example.net/"); return $y; } async function async_curl(): Awaitable<void> { $start = \microtime(true); list($a, $b) = await Vec\from_async(vec[curl_A(), curl_B()]); $end = \microtime(true); echo "Total time taken: " . \strval($end - $start) . " seconds\n"; } <<__EntryPoint>> function main():void { \HH\Asio\join(async_curl()); } -
Asynchronous Operations namespace Hack\UserDocumentation\AsyncOps\Basics\Examples\AsyncCurl; use namespace HH\Lib\Vec; async function curl_A():
Awaitable<string> { $x = await \HH\Asio\curl_exec("http://example.com/"); return $x; } async function curl_B(): Awaitable<string> { $y = await \HH\Asio\curl_exec("http://example.net/"); return $y; } async function async_curl(): Awaitable<void> { $start = \microtime(true); list($a, $b) = await Vec\from_async(vec[curl_A(), curl_B()]); $end = \microtime(true); echo "Total time taken: " . \strval($end - $start) . " seconds\n"; } <<__EntryPoint>> function main():void { \HH\Asio\join(async_curl()); } -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
// Emulate a client-server environment list($cr, $sw) = IO\pipe_non_disposable(); list($sr,
$cw) = IO\pipe_non_disposable(); await Tuple\from_async( async { // client await $cw->writeAsync("Herp\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Derp\n"); await $cw->writeAsync("Foo\n"); $response = await $cr->readLineAsync(); expect($response)->toBeSame("Bar\n"); }, async { // server $request = await $sr->readLineAsync(); expect($request)->toBeSame("Herp\n"); await $sw->writeAsync("Derp\n"); $request = await $sr->readLineAsync(); expect($request)->toBeSame("Foo\n"); await $sw->writeAsync("Bar\n"); }, ); -
if you would like to chat about hacklang, you are
welcome to join us on slack