Skip to content

Commit 4ac7efc

Browse files
committed
feat: add parameter source attributes for Query, Header, and Cookie
1 parent 5f89740 commit 4ac7efc

5 files changed

Lines changed: 118 additions & 0 deletions

File tree

system/CodeIgniter.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
use CodeIgniter\Exceptions\LogicException;
1919
use CodeIgniter\Exceptions\PageNotFoundException;
2020
use CodeIgniter\Filters\Filters;
21+
use CodeIgniter\HTTP\Attributes\ParamSource\BaseParamSourceAttribute;
22+
use CodeIgniter\HTTP\Attributes\ParamSource\Cookie;
23+
use CodeIgniter\HTTP\Attributes\ParamSource\Header;
24+
use CodeIgniter\HTTP\Attributes\ParamSource\Query;
2125
use CodeIgniter\HTTP\CLIRequest;
2226
use CodeIgniter\HTTP\Exceptions\FormRequestException;
2327
use CodeIgniter\HTTP\Exceptions\RedirectException;
@@ -728,7 +732,32 @@ private function resolveCallableParams(ReflectionFunctionAbstract $reflection, a
728732
$resolved = [];
729733
$routeIndex = 0;
730734

735+
$request = service('request');
736+
731737
foreach ($reflection->getParameters() as $param) {
738+
$attrs = $param->getAttributes();
739+
740+
if (count($attrs) > 0) {
741+
foreach ($attrs as $attr) {
742+
$instance = $attr->newInstance();
743+
744+
if (! $instance instanceof BaseParamSourceAttribute) {
745+
continue;
746+
}
747+
748+
$key = $instance->getKey($param->getName());
749+
750+
$resolved[] = match (true) {
751+
$instance instanceof Query => $request->getGet($key),
752+
$instance instanceof Header => $request->getHeaderLine($key),
753+
$instance instanceof Cookie => $request->getCookie($key),
754+
default => throw new LogicException('Unsupported parameter attribute: ' . $instance::class),
755+
};
756+
757+
continue 2;
758+
}
759+
}
760+
732761
[$kind, $formRequestClass] = CallableParamClassifier::classify($param);
733762

734763
switch ($kind) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\HTTP\Attributes\ParamSource;
15+
16+
abstract class BaseParamSourceAttribute
17+
{
18+
public function __construct(private readonly ?string $key = null)
19+
{
20+
}
21+
22+
public function getKey(string $paramName): string
23+
{
24+
return $this->key ?? $paramName;
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\HTTP\Attributes\ParamSource;
15+
16+
use Attribute;
17+
18+
#[Attribute(Attribute::TARGET_PARAMETER)]
19+
class Cookie extends BaseParamSourceAttribute
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\HTTP\Attributes\ParamSource;
15+
16+
use Attribute;
17+
18+
#[Attribute(Attribute::TARGET_PARAMETER)]
19+
class Header extends BaseParamSourceAttribute
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\HTTP\Attributes\ParamSource;
15+
16+
use Attribute;
17+
18+
#[Attribute(Attribute::TARGET_PARAMETER)]
19+
class Query extends BaseParamSourceAttribute
20+
{
21+
}

0 commit comments

Comments
 (0)