Skip to content

Commit e077110

Browse files
authored
Merge branch '4.5' into make-test
2 parents b2d0ce7 + 99c67e8 commit e077110

File tree

22 files changed

+596
-117
lines changed

22 files changed

+596
-117
lines changed

app/Config/Routing.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class Routing extends BaseRouting
2020
{
2121
/**
22+
* For Defined Routes.
2223
* An array of files that contain route definitions.
2324
* Route files are read in order, with the first match
2425
* found taking precedence.
@@ -30,6 +31,7 @@ class Routing extends BaseRouting
3031
];
3132

3233
/**
34+
* For Defined Routes and Auto Routing.
3335
* The default namespace to use for Controllers when no other
3436
* namespace has been specified.
3537
*
@@ -38,6 +40,7 @@ class Routing extends BaseRouting
3840
public string $defaultNamespace = 'App\Controllers';
3941

4042
/**
43+
* For Auto Routing.
4144
* The default controller to use when no other controller has been
4245
* specified.
4346
*
@@ -46,6 +49,7 @@ class Routing extends BaseRouting
4649
public string $defaultController = 'Home';
4750

4851
/**
52+
* For Defined Routes and Auto Routing.
4953
* The default method to call on the controller when no other
5054
* method has been set in the route.
5155
*
@@ -54,7 +58,8 @@ class Routing extends BaseRouting
5458
public string $defaultMethod = 'index';
5559

5660
/**
57-
* Whether to translate dashes in URIs to underscores.
61+
* For Auto Routing.
62+
* Whether to translate dashes in URIs for controller/method to underscores.
5863
* Primarily useful when using the auto-routing.
5964
*
6065
* Default: false
@@ -91,6 +96,7 @@ class Routing extends BaseRouting
9196
public bool $autoRoute = false;
9297

9398
/**
99+
* For Defined Routes.
94100
* If TRUE, will enable the use of the 'prioritize' option
95101
* when defining routes.
96102
*
@@ -99,7 +105,8 @@ class Routing extends BaseRouting
99105
public bool $prioritize = false;
100106

101107
/**
102-
* Map of URI segments and namespaces. For Auto Routing (Improved).
108+
* For Auto Routing (Improved).
109+
* Map of URI segments and namespaces.
103110
*
104111
* The key is the first URI segment. The value is the controller namespace.
105112
* E.g.,
@@ -110,4 +117,15 @@ class Routing extends BaseRouting
110117
* @var array [ uri_segment => namespace ]
111118
*/
112119
public array $moduleRoutes = [];
120+
121+
/**
122+
* For Auto Routing (Improved).
123+
* Whether to translate dashes in URIs for controller/method to CamelCase.
124+
* E.g., blog-controller -> BlogController
125+
*
126+
* If you enable this, $translateURIDashes is ignored.
127+
*
128+
* Default: false
129+
*/
130+
public bool $translateUriToCamelCase = false;
113131
}

phpstan-baseline.php

-10
Original file line numberDiff line numberDiff line change
@@ -1096,11 +1096,6 @@
10961096
'count' => 1,
10971097
'path' => __DIR__ . '/system/Database/Migration.php',
10981098
];
1099-
$ignoreErrors[] = [
1100-
'message' => '#^Property CodeIgniter\\\\Database\\\\Migration\\:\\:\\$DBGroup \\(string\\) on left side of \\?\\? is not nullable\\.$#',
1101-
'count' => 1,
1102-
'path' => __DIR__ . '/system/Database/Migration.php',
1103-
];
11041099
$ignoreErrors[] = [
11051100
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
11061101
'count' => 8,
@@ -2606,11 +2601,6 @@
26062601
'count' => 1,
26072602
'path' => __DIR__ . '/system/Router/AutoRouter.php',
26082603
];
2609-
$ignoreErrors[] = [
2610-
'message' => '#^Only booleans are allowed in &&, Config\\\\Routing given on the right side\\.$#',
2611-
'count' => 1,
2612-
'path' => __DIR__ . '/system/Router/AutoRouterImproved.php',
2613-
];
26142604
$ignoreErrors[] = [
26152605
'message' => '#^PHPDoc type int of property CodeIgniter\\\\Router\\\\Exceptions\\\\RedirectException\\:\\:\\$code is not the same as PHPDoc type mixed of overridden property Exception\\:\\:\\$code\\.$#',
26162606
'count' => 1,

system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ final class ControllerMethodReader
3636

3737
private bool $translateURIDashes;
3838

39+
private bool $translateUriToCamelCase;
40+
3941
/**
4042
* @param string $namespace the default namespace
4143
*/
@@ -44,8 +46,9 @@ public function __construct(string $namespace, array $httpMethods)
4446
$this->namespace = $namespace;
4547
$this->httpMethods = $httpMethods;
4648

47-
$config = config(Routing::class);
48-
$this->translateURIDashes = $config->translateURIDashes;
49+
$config = config(Routing::class);
50+
$this->translateURIDashes = $config->translateURIDashes;
51+
$this->translateUriToCamelCase = $config->translateUriToCamelCase;
4952
}
5053

5154
/**
@@ -67,15 +70,15 @@ public function read(string $class, string $defaultController = 'Home', string $
6770
$classShortname = $reflection->getShortName();
6871

6972
$output = [];
70-
$classInUri = $this->getUriByClass($classname);
73+
$classInUri = $this->convertClassNameToUri($classname);
7174

7275
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
7376
$methodName = $method->getName();
7477

7578
foreach ($this->httpMethods as $httpVerb) {
7679
if (strpos($methodName, strtolower($httpVerb)) === 0) {
7780
// Remove HTTP verb prefix.
78-
$methodInUri = $this->getUriByMethod($httpVerb, $methodName);
81+
$methodInUri = $this->convertMethodNameToUri($httpVerb, $methodName);
7982

8083
// Check if it is the default method.
8184
if ($methodInUri === $defaultMethod) {
@@ -164,7 +167,7 @@ private function getParameters(ReflectionMethod $method): array
164167
*
165168
* @return string URI path part from the folder(s) and controller
166169
*/
167-
private function getUriByClass(string $classname): string
170+
private function convertClassNameToUri(string $classname): string
168171
{
169172
// remove the namespace
170173
$pattern = '/' . preg_quote($this->namespace, '/') . '/';
@@ -181,25 +184,33 @@ private function getUriByClass(string $classname): string
181184

182185
$classUri = rtrim($classPath, '/');
183186

184-
if ($this->translateURIDashes) {
185-
$classUri = str_replace('_', '-', $classUri);
186-
}
187-
188-
return $classUri;
187+
return $this->translateToUri($classUri);
189188
}
190189

191190
/**
192191
* @return string URI path part from the method
193192
*/
194-
private function getUriByMethod(string $httpVerb, string $methodName): string
193+
private function convertMethodNameToUri(string $httpVerb, string $methodName): string
195194
{
196195
$methodUri = lcfirst(substr($methodName, strlen($httpVerb)));
197196

198-
if ($this->translateURIDashes) {
199-
$methodUri = str_replace('_', '-', $methodUri);
197+
return $this->translateToUri($methodUri);
198+
}
199+
200+
/**
201+
* @param string $string classname or method name
202+
*/
203+
private function translateToUri(string $string): string
204+
{
205+
if ($this->translateUriToCamelCase) {
206+
$string = strtolower(
207+
preg_replace('/([a-z\d])([A-Z])/', '$1-$2', $string)
208+
);
209+
} elseif ($this->translateURIDashes) {
210+
$string = str_replace('_', '-', $string);
200211
}
201212

202-
return $methodUri;
213+
return $string;
203214
}
204215

205216
/**

system/Config/Routing.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class Routing extends BaseConfig
2020
{
2121
/**
22+
* For Defined Routes.
2223
* An array of files that contain route definitions.
2324
* Route files are read in order, with the first match
2425
* found taking precedence.
@@ -30,6 +31,7 @@ class Routing extends BaseConfig
3031
];
3132

3233
/**
34+
* For Defined Routes and Auto Routing.
3335
* The default namespace to use for Controllers when no other
3436
* namespace has been specified.
3537
*
@@ -38,6 +40,7 @@ class Routing extends BaseConfig
3840
public string $defaultNamespace = 'App\Controllers';
3941

4042
/**
43+
* For Auto Routing.
4144
* The default controller to use when no other controller has been
4245
* specified.
4346
*
@@ -46,6 +49,7 @@ class Routing extends BaseConfig
4649
public string $defaultController = 'Home';
4750

4851
/**
52+
* For Defined Routes and Auto Routing.
4953
* The default method to call on the controller when no other
5054
* method has been set in the route.
5155
*
@@ -54,7 +58,8 @@ class Routing extends BaseConfig
5458
public string $defaultMethod = 'index';
5559

5660
/**
57-
* Whether to translate dashes in URIs to underscores.
61+
* For Auto Routing.
62+
* Whether to translate dashes in URIs for controller/method to underscores.
5863
* Primarily useful when using the auto-routing.
5964
*
6065
* Default: false
@@ -91,6 +96,7 @@ class Routing extends BaseConfig
9196
public bool $autoRoute = false;
9297

9398
/**
99+
* For Defined Routes.
94100
* If TRUE, will enable the use of the 'prioritize' option
95101
* when defining routes.
96102
*
@@ -99,7 +105,8 @@ class Routing extends BaseConfig
99105
public bool $prioritize = false;
100106

101107
/**
102-
* Map of URI segments and namespaces. For Auto Routing (Improved).
108+
* For Auto Routing (Improved).
109+
* Map of URI segments and namespaces.
103110
*
104111
* The key is the first URI segment. The value is the controller namespace.
105112
* E.g.,
@@ -110,4 +117,15 @@ class Routing extends BaseConfig
110117
* @var array [ uri_segment => namespace ]
111118
*/
112119
public array $moduleRoutes = [];
120+
121+
/**
122+
* For Auto Routing (Improved).
123+
* Whether to translate dashes in URIs for controller/method to CamelCase.
124+
* E.g., blog-controller -> BlogController
125+
*
126+
* If you enable this, $translateURIDashes is ignored.
127+
*
128+
* Default: false
129+
*/
130+
public bool $translateUriToCamelCase = false;
113131
}

system/Database/Migration.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class Migration
2323
/**
2424
* The name of the database group to use.
2525
*
26-
* @var string
26+
* @var string|null
2727
*/
2828
protected $DBGroup;
2929

@@ -41,12 +41,15 @@ abstract class Migration
4141
*/
4242
protected $forge;
4343

44-
/**
45-
* Constructor.
46-
*/
4744
public function __construct(?Forge $forge = null)
4845
{
49-
$this->forge = $forge ?? Database::forge($this->DBGroup ?? config(Database::class)->defaultGroup);
46+
if (isset($this->DBGroup)) {
47+
$this->forge = Database::forge($this->DBGroup);
48+
} elseif ($forge !== null) {
49+
$this->forge = $forge;
50+
} else {
51+
$this->forge = Database::forge(config(Database::class)->defaultGroup);
52+
}
5053

5154
$this->db = $this->forge->getConnection();
5255
}

system/Database/MigrationRunner.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,12 @@ public function __construct(MigrationsConfig $config, $db = null)
137137
$this->enabled = $config->enabled ?? false;
138138
$this->table = $config->table ?? 'migrations';
139139

140-
// Default name space is the app namespace
141140
$this->namespace = APP_NAMESPACE;
142141

143-
// get default database group
144-
$config = config(Database::class);
145-
$this->group = $config->defaultGroup;
146-
unset($config);
142+
// Even if a DB connection is passed, since it is a test,
143+
// it is assumed to use the default group name
144+
$this->group = is_string($db) ? $db : config(Database::class)->defaultGroup;
147145

148-
// If no db connection passed in, use
149-
// default database group.
150146
$this->db = db_connect($db);
151147
}
152148

@@ -838,8 +834,9 @@ protected function migrate($direction, $migration): bool
838834
throw new RuntimeException($message);
839835
}
840836

841-
$instance = new $class();
842-
$group = $instance->getDBGroup() ?? config(Database::class)->defaultGroup;
837+
/** @var Migration $instance */
838+
$instance = new $class(Database::forge($this->db));
839+
$group = $instance->getDBGroup() ?? $this->group;
843840

844841
if (ENVIRONMENT !== 'testing' && $group === 'tests' && $this->groupFilter !== 'tests') {
845842
// @codeCoverageIgnoreStart
@@ -855,8 +852,6 @@ protected function migrate($direction, $migration): bool
855852
return true;
856853
}
857854

858-
$this->setGroup($group);
859-
860855
if (! is_callable([$instance, $direction])) {
861856
$message = sprintf(lang('Migrations.missingMethod'), $direction);
862857

0 commit comments

Comments
 (0)