Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 909feda

Browse files
committed
Remove top-level requires(), stricitfy dorm demo
also, fix some long-standing bugs in dorm.
1 parent d96effb commit 909feda

File tree

5 files changed

+110
-96
lines changed

5 files changed

+110
-96
lines changed

examples/dorm/CodegenMutator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private function getCreateMethod(): CodegenMethod {
123123
$cg = $this->codegen;
124124
// This is a very simple example of generating a method
125125
return $cg->codegenMethod('create')
126+
->setIsStatic(true)
126127
->setReturnType('this')
127128
->setBody(
128129
$cg->codegenHackBuilder()
@@ -135,6 +136,7 @@ private function getUpdateMethod(): CodegenMethod {
135136
$cg = $this->codegen;
136137
// This is a very simple example of generating a method
137138
return $cg->codegenMethod('update')
139+
->setIsStatic(true)
138140
->addParameter('int $id')
139141
->setReturnType('this')
140142
->setBody(

examples/dorm/DormCodegenCLI.hack

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
*/
9+
10+
namespace Facebook\HackCodegen;
11+
12+
use namespace HH\Lib\{C, Vec};
13+
14+
final class DormCodegenCLI extends \Facebook\CLILib\CLIWithRequiredArguments {
15+
<<__Override>>
16+
public static function getHelpTextForRequiredArguments(): vec<string> {
17+
return vec['FILENAME'];
18+
}
19+
20+
<<__Override>>
21+
protected function getSupportedOptions(
22+
): vec<\Facebook\CLILib\CLIOptions\CLIOption> { return vec[];
23+
}
24+
25+
<<__Override>>
26+
public async function mainAsync(): Awaitable<int> {
27+
$fname = C\firstx($this->getArguments());
28+
if (!\file_exists($fname)) {
29+
await $this->getStderr()
30+
->writeAsync(" File doesn't exist: ".$fname."\n\n");
31+
return 1;
32+
}
33+
34+
$classes = \get_declared_classes();
35+
require_once($fname);
36+
$new_classes = Vec\diff(\get_declared_classes(), $classes);
37+
38+
39+
foreach ($new_classes as $class_name) {
40+
$ref = new \ReflectionClass($class_name);
41+
if ($ref->isAbstract()) {
42+
continue;
43+
}
44+
$instance = $ref->newInstance() as DormSchema;
45+
/* HHAST_IGNORE_ERROR[DontAwaitInALoop] */
46+
await $this->getStdout()
47+
->writeAsync("Generating code for ".$class_name."\n");
48+
(new CodegenDorm($instance))->generate();
49+
(new CodegenMutator($instance))->generate();
50+
}
51+
52+
return 0;
53+
}
54+
}

examples/dorm/codegen.hack

100644100755
Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,10 @@
1010

1111
namespace Facebook\HackCodegen;
1212

13-
use namespace HH\Lib\{C, Vec};
14-
15-
final class DormCodegenCLI extends \Facebook\CLILib\CLIWithRequiredArguments {
16-
<<__Override>>
17-
public static function getHelpTextForRequiredArguments(): vec<string> {
18-
return vec['FILENAME'];
19-
}
20-
21-
<<__Override>>
22-
protected function getSupportedOptions(
23-
): vec<\Facebook\CLILib\CLIOptions\CLIOption> {
24-
return vec[];
25-
}
26-
27-
<<__Override>>
28-
public async function mainAsync(): Awaitable<int> {
29-
$fname = C\firstx($this->getArguments());
30-
if (!\file_exists($fname)) {
31-
await $this->getStderr()
32-
->writeAsync(" File doesn't exist: ".$fname."\n\n");
33-
return 1;
34-
}
35-
36-
$classes = \get_declared_classes();
37-
require_once($fname);
38-
$new_classes = Vec\diff(\get_declared_classes(), $classes);
39-
40-
41-
foreach ($new_classes as $class_name) {
42-
$ref = new \ReflectionClass($class_name);
43-
if ($ref->isAbstract()) {
44-
continue;
45-
}
46-
$instance = $ref->newInstance() as DormSchema;
47-
/* HHAST_IGNORE_ERROR[DontAwaitInALoop] */
48-
await $this->getStdout()
49-
->writeAsync("Generating code for ".$class_name."\n");
50-
(new CodegenDorm($instance))->generate();
51-
(new CodegenMutator($instance))->generate();
52-
}
53-
54-
return 0;
55-
}
56-
}
57-
5813
<<__EntryPoint>>
5914
async function dorm_codegen_cli_main_async(): Awaitable<noreturn> {
60-
(() ==> {
61-
// HHAST-generated to avoid pseudomain local leaks
62-
require_once(__DIR__.'/../../vendor/hh_autoload.hh');
63-
})();
15+
require_once(__DIR__.'/../../vendor/autoload.hack');
16+
\Facebook\AutoloadMap\initialize();
6417
$exit_code = await DormCodegenCLI::runAsync();
6518
exit($exit_code);
6619
}

examples/dorm/demo/DormUserMutator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* To re-generate this file run codegen.php DormUserSchema
77
*
88
*
9-
* @partially-generated SignedSource<<3eab32bd5832befe294cd6d493ac3953>>
9+
* @partially-generated SignedSource<<48c599867b0b679f5bb9cf27d8801e89>>
1010
*/
1111

1212
final class DormUserMutator {
@@ -24,11 +24,11 @@ final class DormUserMutator {
2424
private function __construct(private ?int $id = null) {
2525
}
2626

27-
public function create(): this {
27+
public static function create(): this {
2828
return new DormUserMutator();
2929
}
3030

31-
public function update(int $id): this {
31+
public static function update(int $id): this {
3232
return new DormUserMutator($id);
3333
}
3434

examples/dorm/demo/demo_usage.php

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?hh // partial
1+
<?hh // strict
22
/*
33
* Copyright (c) 2015-present, Facebook, Inc.
44
* All rights reserved.
@@ -10,46 +10,51 @@
1010

1111
namespace Facebook\HackCodegen;
1212

13-
/* HH_FIXME[1002] HHAST: move to <<__EntryPoint>> function */ require_once('DormUserMutator.php');
14-
/* HH_FIXME[1002] HHAST: move to <<__EntryPoint>> function */ require_once('DormUser.php');
15-
16-
/**
17-
* This is to demo how the Dorm classes for User can be used.
18-
* Notice that before being able to run this, you should set
19-
* up your DB dsn in DormUserSchema and create the table in
20-
* your db using, for example:
21-
*
22-
* create table user
23-
* (
24-
* user_id integer primary key,
25-
* first_name varchar(50) not null,
26-
* last_name varchar(50) not null,
27-
* birthday date,
28-
* country_id integer,
29-
* is_active boolean not null
30-
* );
31-
*
32-
* Before trying this example, you'll need to generate the code by running
33-
* php codegen.hack demo/DormUserSchema.php.
34-
*/
35-
$id = DormUserMutator::create()
36-
->setFirstName('John')
37-
->setLastName('Smith')
38-
->setBirthday(new \DateTime('1978-03-26'))
39-
->setCountryId(54)
40-
->setIsActive(true)
41-
->save();
42-
43-
echo "Created user with id ".$id."\n";
44-
45-
$user = DormUser::load($id);
46-
echo "Loaded: ".$user->getFirstName()." ".$user->getLastName()."\n";
47-
48-
DormUserMutator::update($id)
49-
->setFirstName('Peter')
50-
->save();
51-
52-
echo "Updated the user first name.\n";
53-
54-
$user = DormUser::load($id);
55-
echo "Loaded: ".$user->getFirstName()." ".$user->getLastName()."\n";
13+
<<__EntryPoint>>
14+
function dorm_demo_main(): void {
15+
require_once('DormUserMutator.php');
16+
require_once('DormUser.php');
17+
18+
/**
19+
* This is to demo how the Dorm classes for User can be used.
20+
* Notice that before being able to run this, you should set
21+
* up your DB dsn in DormUserSchema and create the table in
22+
* your db using, for example:
23+
*
24+
* create table user
25+
* (
26+
* user_id integer primary key,
27+
* first_name varchar(50) not null,
28+
* last_name varchar(50) not null,
29+
* birthday date,
30+
* country_id integer,
31+
* is_active boolean not null
32+
* );
33+
*
34+
* Before trying this example, you'll need to generate the code by running
35+
* php codegen.hack demo/DormUserSchema.php.
36+
*/
37+
$id = \DormUserMutator::create()
38+
->setFirstName('John')
39+
->setLastName('Smith')
40+
->setBirthday(new \DateTime('1978-03-26'))
41+
->setCountryId(54)
42+
->setIsActive(true)
43+
->save();
44+
45+
echo "Created user with id ".$id."\n";
46+
47+
$user = \DormUser::load($id);
48+
invariant($user is nonnull, 'Failed to load user');
49+
echo "Loaded: ".$user->getFirstName()." ".$user->getLastName()."\n";
50+
51+
\DormUserMutator::update($id)
52+
->setFirstName('Peter')
53+
->save();
54+
55+
echo "Updated the user first name.\n";
56+
57+
$user = \DormUser::load($id);
58+
invariant($user is nonnull, 'Failed to load user');
59+
echo "Loaded: ".$user->getFirstName()." ".$user->getLastName()."\n";
60+
}

0 commit comments

Comments
 (0)