Skip to content

Commit 1833907

Browse files
authored
Merge pull request #673 from flightphp/fix-other-commands
Fixed route and controller commands to accept old configs
2 parents f6baf2a + 888ff46 commit 1833907

File tree

7 files changed

+71
-40
lines changed

7 files changed

+71
-40
lines changed

flight/commands/ControllerCommand.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@ public function __construct(array $config)
2929
public function execute(string $controller)
3030
{
3131
$io = $this->app()->io();
32-
if (isset($this->config['app_root']) === false) {
33-
$io->error('app_root not set in .runway-config.json', true);
32+
33+
if (empty($this->config['runway'])) {
34+
$io->warn('Using a .runway-config.json file is deprecated. Move your config values to app/config/config.php with `php runway config:migrate`.', true); // @codeCoverageIgnore
35+
$runwayConfig = json_decode(file_get_contents($this->projectRoot . '/.runway-config.json'), true); // @codeCoverageIgnore
36+
} else {
37+
$runwayConfig = $this->config['runway'];
38+
}
39+
40+
if (isset($runwayConfig['app_root']) === false) {
41+
$io->error('app_root not set in app/config/config.php', true);
3442
return;
3543
}
3644

3745
if (!preg_match('/Controller$/', $controller)) {
3846
$controller .= 'Controller';
3947
}
4048

41-
$controllerPath = getcwd() . DIRECTORY_SEPARATOR . $this->config['app_root'] . 'controllers' . DIRECTORY_SEPARATOR . $controller . '.php';
49+
$controllerPath = $this->projectRoot . '/' . $runwayConfig['app_root'] . 'controllers/' . $controller . '.php';
4250
if (file_exists($controllerPath) === true) {
4351
$io->error($controller . ' already exists.', true);
4452
return;
@@ -70,7 +78,7 @@ public function execute(string $controller)
7078
$namespace->add($class);
7179
$file->addNamespace($namespace);
7280

73-
$this->persistClass($controller, $file);
81+
$this->persistClass($controller, $file, $runwayConfig['app_root']);
7482

7583
$io->ok('Controller successfully created at ' . $controllerPath, true);
7684
}
@@ -80,12 +88,13 @@ public function execute(string $controller)
8088
*
8189
* @param string $controllerName Name of the Controller
8290
* @param PhpFile $file Class Object from Nette\PhpGenerator
91+
* @param string $appRoot App Root from runway config
8392
*
8493
* @return void
8594
*/
86-
protected function persistClass(string $controllerName, PhpFile $file)
95+
protected function persistClass(string $controllerName, PhpFile $file, string $appRoot)
8796
{
8897
$printer = new \Nette\PhpGenerator\PsrPrinter();
89-
file_put_contents(getcwd() . DIRECTORY_SEPARATOR . $this->config['app_root'] . 'controllers' . DIRECTORY_SEPARATOR . $controllerName . '.php', $printer->printFile($file));
98+
file_put_contents($this->projectRoot . '/' . $appRoot . 'controllers/' . $controllerName . '.php', $printer->printFile($file));
9099
}
91100
}

flight/commands/RouteCommand.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ public function execute()
4141
{
4242
$io = $this->app()->io();
4343

44-
if (isset($this->config['index_root']) === false) {
45-
$io->error('index_root not set in .runway-config.json', true);
44+
if (empty($this->config['runway'])) {
45+
$io->warn('Using a .runway-config.json file is deprecated. Move your config values to app/config/config.php with `php runway config:migrate`.', true); // @codeCoverageIgnore
46+
$runwayConfig = json_decode(file_get_contents($this->projectRoot . '/.runway-config.json'), true); // @codeCoverageIgnore
47+
} else {
48+
$runwayConfig = $this->config['runway'];
49+
}
50+
51+
if (isset($runwayConfig['index_root']) === false) {
52+
$io->error('index_root not set in app/config/config.php', true);
4653
return;
4754
}
4855

4956
$io->bold('Routes', true);
5057

51-
$cwd = getcwd();
52-
53-
$index_root = $cwd . '/' . $this->config['index_root'];
58+
$index_root = $this->projectRoot . '/' . $runwayConfig['index_root'];
5459

5560
// This makes it so the framework doesn't actually execute
5661
Flight::map('start', function () {
@@ -72,8 +77,8 @@ public function execute()
7277
}
7378
return preg_match("/^class@anonymous/", end($middleware_class_name)) ? 'Anonymous' : end($middleware_class_name);
7479
}, $route->middleware);
75-
} catch (\TypeError $e) {
76-
$middlewares[] = 'Bad Middleware';
80+
} catch (\TypeError $e) { // @codeCoverageIgnore
81+
$middlewares[] = 'Bad Middleware'; // @codeCoverageIgnore
7782
} finally {
7883
if (is_string($route->middleware) === true) {
7984
$middlewares[] = $route->middleware;

flight/net/Request.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function __construct(array $config = [])
174174
}
175175
$base = dirname($base);
176176
if ($base === '\\') {
177-
$base = '/';
177+
$base = '/'; // @codeCoverageIgnore
178178
}
179179
$config = [
180180
'url' => $url,
@@ -243,8 +243,8 @@ public function init(array $properties = []): self
243243
$this->data->setData($data);
244244
}
245245
}
246-
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data or multipart/form-data
247-
} elseif (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) {
246+
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data or multipart/form-data
247+
} elseif (in_array($this->method, ['PUT', 'DELETE', 'PATCH'], true) === true) {
248248
$this->parseRequestBodyForHttpMethods();
249249
}
250250

@@ -478,7 +478,7 @@ public function negotiateContentType(array $supported): ?string
478478
/**
479479
* Retrieves the array of uploaded files.
480480
*
481-
* @return array<string, UploadedFile|array<int, UploadedFile>> Key is field name; value is either a single UploadedFile or an array of UploadedFile when multiple were uploaded.
481+
* @return array<string, UploadedFile|array<int, UploadedFile>> Key is field name; value is either a single UploadedFile or an array of UploadedFile when multiple were uploaded.
482482
*/
483483
public function getUploadedFiles(): array
484484
{

tests/UploadedFileTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function tearDown(): void
2525
unlink('real_file');
2626
}
2727

28-
// not found with file_exists...just delete it brute force
29-
@unlink('tmp_symlink');
28+
// not found with file_exists...just delete it brute force
29+
@unlink('tmp_symlink');
3030
}
3131

3232
public function testMoveToFalseSuccess(): void

tests/commands/AiGenerateInstructionsCommandTest.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use PHPUnit\Framework\TestCase;
1010
use tests\classes\NoExitInteractor;
1111

12-
class AiGenerateInstructionsCommandTest extends TestCase {
12+
class AiGenerateInstructionsCommandTest extends TestCase
13+
{
1314
protected static $in;
1415
protected static $ou;
1516
protected $baseDir;
1617

17-
public function setUp(): void {
18+
public function setUp(): void
19+
{
1820
self::$in = __DIR__ . DIRECTORY_SEPARATOR . 'input.test' . uniqid('', true) . '.txt';
1921
self::$ou = __DIR__ . DIRECTORY_SEPARATOR . 'output.test' . uniqid('', true) . '.txt';
2022
file_put_contents(self::$in, '');
@@ -25,7 +27,8 @@ public function setUp(): void {
2527
}
2628
}
2729

28-
public function tearDown(): void {
30+
public function tearDown(): void
31+
{
2932
if (file_exists(self::$in)) {
3033
unlink(self::$in);
3134
}
@@ -35,7 +38,8 @@ public function tearDown(): void {
3538
$this->recursiveRmdir($this->baseDir);
3639
}
3740

38-
protected function recursiveRmdir($dir) {
41+
protected function recursiveRmdir($dir)
42+
{
3943
if (!is_dir($dir)) {
4044
return;
4145
}
@@ -46,7 +50,8 @@ protected function recursiveRmdir($dir) {
4650
return rmdir($dir);
4751
}
4852

49-
protected function newApp($command): Application {
53+
protected function newApp($command): Application
54+
{
5055
$app = new Application('test', '0.0.1', function ($exitCode) {
5156
return $exitCode;
5257
});
@@ -55,11 +60,13 @@ protected function newApp($command): Application {
5560
return $app;
5661
}
5762

58-
protected function setInput(array $lines): void {
63+
protected function setInput(array $lines): void
64+
{
5965
file_put_contents(self::$in, implode("\n", $lines) . "\n");
6066
}
6167

62-
protected function setProjectRoot($command, $path) {
68+
protected function setProjectRoot($command, $path)
69+
{
6370
$reflection = new \ReflectionClass(get_class($command));
6471
$property = null;
6572
$currentClass = $reflection;
@@ -79,7 +86,8 @@ protected function setProjectRoot($command, $path) {
7986
}
8087
}
8188

82-
public function testFailsIfAiConfigMissing() {
89+
public function testFailsIfAiConfigMissing()
90+
{
8391
$this->setInput([
8492
'desc',
8593
'none',
@@ -107,7 +115,8 @@ public function testFailsIfAiConfigMissing() {
107115
$this->assertStringContainsString('Missing AI configuration', file_get_contents(self::$ou));
108116
}
109117

110-
public function testWritesInstructionsToFiles() {
118+
public function testWritesInstructionsToFiles()
119+
{
111120
$creds = [
112121
'api_key' => 'key',
113122
'model' => 'gpt-4o',
@@ -154,7 +163,8 @@ public function testWritesInstructionsToFiles() {
154163
$this->assertFileExists($this->baseDir . '.windsurfrules');
155164
}
156165

157-
public function testNoInstructionsReturnedFromLlm() {
166+
public function testNoInstructionsReturnedFromLlm()
167+
{
158168
$creds = [
159169
'api_key' => 'key',
160170
'model' => 'gpt-4o',
@@ -196,7 +206,8 @@ public function testNoInstructionsReturnedFromLlm() {
196206
$this->assertSame(1, $result);
197207
}
198208

199-
public function testLlmApiCallFails() {
209+
public function testLlmApiCallFails()
210+
{
200211
$creds = [
201212
'api_key' => 'key',
202213
'model' => 'gpt-4o',
@@ -234,7 +245,8 @@ public function testLlmApiCallFails() {
234245
$this->assertSame(1, $result);
235246
}
236247

237-
public function testUsesDeprecatedConfigFile() {
248+
public function testUsesDeprecatedConfigFile()
249+
{
238250
$creds = [
239251
'ai' => [
240252
'api_key' => 'key',

tests/commands/ControllerCommandTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,26 @@ public function tearDown(): void
4848

4949
protected function newApp(string $name, string $version = '')
5050
{
51-
$app = @new Application($name, $version ?: '0.0.1', fn () => false);
51+
$app = @new Application($name, $version ?: '0.0.1', fn() => false);
5252

5353
return @$app->io(new Interactor(static::$in, static::$ou));
5454
}
5555

5656
public function testConfigAppRootNotSet(): void
5757
{
5858
$app = $this->newApp('test', '0.0.1');
59-
$app->add(new ControllerCommand([]));
59+
$app->add(new ControllerCommand(['runway' => ['something' => '']]));
6060
@$app->handle(['runway', 'make:controller', 'Test']);
6161

62-
$this->assertStringContainsString('app_root not set in .runway-config.json', file_get_contents(static::$ou));
62+
$this->assertStringContainsString('app_root not set in app/config/config.php', file_get_contents(static::$ou));
6363
}
6464

6565
public function testControllerAlreadyExists(): void
6666
{
6767
$app = $this->newApp('test', '0.0.1');
6868
mkdir(__DIR__ . '/controllers/');
6969
file_put_contents(__DIR__ . '/controllers/TestController.php', '<?php class TestController {}');
70-
$app->add(new ControllerCommand(['app_root' => 'tests/commands/']));
70+
$app->add(new ControllerCommand(['runway' => ['app_root' => 'tests/commands/']]));
7171
$app->handle(['runway', 'make:controller', 'Test']);
7272

7373
$this->assertStringContainsString('TestController already exists.', file_get_contents(static::$ou));
@@ -76,7 +76,7 @@ public function testControllerAlreadyExists(): void
7676
public function testCreateController(): void
7777
{
7878
$app = $this->newApp('test', '0.0.1');
79-
$app->add(new ControllerCommand(['app_root' => 'tests/commands/']));
79+
$app->add(new ControllerCommand(['runway' => ['app_root' => 'tests/commands/']]));
8080
$app->handle(['runway', 'make:controller', 'Test']);
8181

8282
$this->assertFileExists(__DIR__ . '/controllers/TestController.php');

tests/commands/RouteCommandTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function setUp(): void
2323
static::$ou = __DIR__ . DIRECTORY_SEPARATOR . 'output.test' . uniqid('', true) . '.txt';
2424
file_put_contents(static::$in, '');
2525
file_put_contents(static::$ou, '');
26+
2627
$_SERVER = [];
2728
$_REQUEST = [];
2829
Flight::init();
@@ -91,21 +92,22 @@ protected function removeColors(string $str): string
9192
public function testConfigIndexRootNotSet(): void
9293
{
9394
$app = @$this->newApp('test', '0.0.1');
94-
$app->add(new RouteCommand([]));
95+
$app->add(new RouteCommand(['runway' => ['something' => 'else']]));
9596
@$app->handle(['runway', 'routes']);
9697

97-
$this->assertStringContainsString('index_root not set in .runway-config.json', file_get_contents(static::$ou));
98+
$this->assertStringContainsString('index_root not set in app/config/config.php', file_get_contents(static::$ou));
9899
}
99100

100101
public function testGetRoutes(): void
101102
{
102103
$app = @$this->newApp('test', '0.0.1');
103104
$this->createIndexFile();
104-
$app->add(new RouteCommand(['index_root' => 'tests/commands/index.php']));
105+
$app->add(new RouteCommand(['runway' => ['index_root' => 'tests/commands/index.php']]));
105106
@$app->handle(['runway', 'routes']);
106107

107108
$this->assertStringContainsString('Routes', file_get_contents(static::$ou));
108109
$expected = <<<'output'
110+
Routes
109111
+---------+--------------------+-------+----------+----------------+
110112
| Pattern | Methods | Alias | Streamed | Middleware |
111113
+---------+--------------------+-------+----------+----------------+
@@ -127,19 +129,22 @@ public function testGetPostRoute(): void
127129
{
128130
$app = @$this->newApp('test', '0.0.1');
129131
$this->createIndexFile();
130-
$app->add(new RouteCommand(['index_root' => 'tests/commands/index.php']));
132+
$app->add(new RouteCommand(['runway' => ['index_root' => 'tests/commands/index.php']]));
131133
@$app->handle(['runway', 'routes', '--post']);
132134

133135
$this->assertStringContainsString('Routes', file_get_contents(static::$ou));
134136

135137
$expected = <<<'output'
138+
Routes
136139
+---------+---------------+-------+----------+------------+
137140
| Pattern | Methods | Alias | Streamed | Middleware |
138141
+---------+---------------+-------+----------+------------+
139142
| /post | POST, OPTIONS | | No | Closure |
140143
+---------+---------------+-------+----------+------------+
141144
output; // phpcs:ignore
142145

146+
$expected = str_replace(["\r\n", "\\n", "\n"], ["\n", "", "\n"], $expected);
147+
143148
$this->assertStringContainsString(
144149
$expected,
145150
$this->removeColors(file_get_contents(static::$ou))

0 commit comments

Comments
 (0)