Skip to content

Commit 6a2494c

Browse files
authored
Merge pull request #663 from flightphp/download-file
Adjusting PHPStan Linting and updated download()
2 parents c7533d9 + 4d9f0f4 commit 6a2494c

File tree

7 files changed

+206
-169
lines changed

7 files changed

+206
-169
lines changed

flight/Engine.php

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,60 +24,58 @@
2424
* It is responsible for loading an HTTP request, running the assigned services,
2525
* and generating an HTTP response.
2626
*
27-
* @license MIT, http://flightphp.com/license
28-
* @copyright Copyright (c) 2011, Mike Cao <[email protected]>
27+
* @license MIT, https://docs.flightphp.com/license
28+
* @copyright Copyright (c) 2011-2025, Mike Cao <mike@mikecao.com>, n0nag0n <n0nag0n@sky-9.com>
2929
*
30-
* # Core methods
31-
* @method void start() Starts engine
32-
* @method void stop() Stops framework and outputs current response
33-
* @method void halt(int $code = 200, string $message = '', bool $actuallyExit = true) Stops processing and returns a given response.
30+
* @method void start()
31+
* @method void stop()
32+
* @method void halt(int $code = 200, string $message = '', bool $actuallyExit = true)
33+
* @method EventDispatcher eventDispatcher()
34+
* @method Route route(string $pattern, callable|string|array $callback, bool $pass_route = false, string $alias = '')
35+
* @method void group(string $pattern, callable $callback, array $group_middlewares = [])
36+
* @method Route post(string $pattern, callable|string|array $callback, bool $pass_route = false, string $alias = '')
37+
* @method Route put(string $pattern, callable|string|array $callback, bool $pass_route = false, string $alias = '')
38+
* @method Route patch(string $pattern, callable|string|array $callback, bool $pass_route = false, string $alias = '')
39+
* @method Route delete(string $pattern, callable|string|array $callback, bool $pass_route = false, string $alias = '')
40+
* @method void resource(string $pattern, string $controllerClass, array $methods = [])
41+
* @method Router router()
42+
* @method string getUrl(string $alias)
43+
* @method void render(string $file, array $data = null, string $key = null)
44+
* @method View view()
45+
* @method void onEvent(string $event, callable $callback)
46+
* @method void triggerEvent(string $event, ...$args)
47+
* @method Request request()
48+
* @method Response response()
49+
* @method void error(Throwable $e)
50+
* @method void notFound()
51+
* @method void redirect(string $url, int $code = 303)
52+
* @method void json($data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
53+
* @method void jsonHalt($data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
54+
* @method void jsonp($data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
55+
* @method void etag(string $id, string $type = 'strong')
56+
* @method void lastModified(int $time)
57+
* @method void download(string $filePath)
3458
*
35-
* # Class registration
36-
* @method EventDispatcher eventDispatcher() Gets event dispatcher
59+
* @phpstan-template EngineTemplate of object
60+
* @phpstan-method void registerContainerHandler(ContainerInterface|callable(class-string<EngineTemplate> $id, array<int|string, mixed> $params): ?EngineTemplate $containerHandler)
61+
* @phpstan-method Route route(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
62+
* @phpstan-method void group(string $pattern, callable $callback, (class-string|callable|array{0: class-string, 1: string})[] $group_middlewares = [])
63+
* @phpstan-method Route post(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
64+
* @phpstan-method Route put(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
65+
* @phpstan-method Route patch(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
66+
* @phpstan-method Route delete(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
67+
* @phpstan-method void resource(string $pattern, class-string $controllerClass, array<string, string|array<string>> $methods = [])
68+
* @phpstan-method string getUrl(string $alias, array<string, mixed> $params = [])
69+
* @phpstan-method void before(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
70+
* @phpstan-method void after(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
71+
* @phpstan-method void set(string|iterable<string, mixed> $key, ?mixed $value = null)
72+
* @phpstan-method mixed get(?string $key)
73+
* @phpstan-method void render(string $file, ?array<string, mixed> $data = null, ?string $key = null)
74+
* @phpstan-method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
75+
* @phpstan-method void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
76+
* @phpstan-method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
3777
*
38-
* # Routing
39-
* @method Route route(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
40-
* Routes a URL to a callback function with all applicable methods
41-
* @method void group(string $pattern, callable $callback, (class-string|callable|array{0: class-string, 1: string})[] $group_middlewares = [])
42-
* Groups a set of routes together under a common prefix.
43-
* @method Route post(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
44-
* Routes a POST URL to a callback function.
45-
* @method Route put(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
46-
* Routes a PUT URL to a callback function.
47-
* @method Route patch(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
48-
* Routes a PATCH URL to a callback function.
49-
* @method Route delete(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
50-
* Routes a DELETE URL to a callback function.
51-
* @method void resource(string $pattern, class-string $controllerClass, array<string, string|array<string>> $methods = [])
52-
* Adds standardized RESTful routes for a controller.
53-
* @method Router router() Gets router
54-
* @method string getUrl(string $alias) Gets a url from an alias
55-
*
56-
* # Views
57-
* @method void render(string $file, ?array<string,mixed> $data = null, ?string $key = null) Renders template
58-
* @method View view() Gets current view
59-
*
60-
* # Events
61-
* @method void onEvent(string $event, callable $callback) Registers a callback for an event.
62-
* @method void triggerEvent(string $event, ...$args) Triggers an event.
63-
*
64-
* # Request-Response
65-
* @method Request request() Gets current request
66-
* @method Response response() Gets current response
67-
* @method void error(Throwable $e) Sends an HTTP 500 response for any errors.
68-
* @method void notFound() Sends an HTTP 404 response when a URL is not found.
69-
* @method void redirect(string $url, int $code = 303) Redirects the current request to another URL.
70-
* @method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
71-
* Sends a JSON response.
72-
* @method void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
73-
* Sends a JSON response and immediately halts the request.
74-
* @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
75-
* Sends a JSONP response.
76-
*
77-
* # HTTP methods
78-
* @method void etag(string $id, ('strong'|'weak') $type = 'strong') Handles ETag HTTP caching.
79-
* @method void lastModified(int $time) Handles last modified HTTP caching.
80-
* @method void download(string $filePath) Downloads a file
78+
* Note: IDEs will use standard @method tags for autocompletion, while PHPStan will use @phpstan-* tags for advanced type checking.
8179
*
8280
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
8381
*/
@@ -118,7 +116,7 @@ class Engine
118116
/** Class loader. */
119117
protected Loader $loader;
120118

121-
/** Method and class dispatcher. */
119+
/** @var Dispatcher<EngineTemplate> Method and class dispatcher. */
122120
protected Dispatcher $dispatcher;
123121

124122
/** Event dispatcher. */
@@ -370,7 +368,7 @@ public function get(?string $key = null)
370368
*
371369
* @param string|iterable<string, mixed> $key
372370
* Variable name as `string` or an iterable of `'varName' => $varValue`
373-
* @param mixed $value Ignored if `$key` is an `iterable`
371+
* @param ?mixed $value Ignored if `$key` is an `iterable`
374372
*/
375373
public function set($key, $value = null): void
376374
{
@@ -981,14 +979,15 @@ public function _jsonp(
981979
* Downloads a file
982980
*
983981
* @param string $filePath The path to the file to download
982+
* @param string $fileName The name the file should be downloaded as
984983
*
985984
* @throws Exception If the file cannot be found
986985
*
987986
* @return void
988987
*/
989-
public function _download(string $filePath): void
988+
public function _download(string $filePath, string $fileName = ''): void
990989
{
991-
$this->response()->downloadFile($filePath);
990+
$this->response()->downloadFile($filePath, $fileName);
992991
}
993992

994993
/**
@@ -1020,8 +1019,8 @@ public function _etag(string $id, string $type = 'strong'): void
10201019
public function _lastModified(int $time): void
10211020
{
10221021
$this->response()->header('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $time));
1023-
$request = $this->request();
1024-
$ifModifiedSince = $request->header('If-Modified-Since');
1022+
$request = $this->request();
1023+
$ifModifiedSince = $request->header('If-Modified-Since');
10251024

10261025
$hit = isset($ifModifiedSince) && strtotime($ifModifiedSince) === $time;
10271026
$this->triggerEvent('flight.cache.checked', 'lastModified', $hit, 0.0);

0 commit comments

Comments
 (0)