Skip to content

Commit

Permalink
Merge branch '4.5' into make-test
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell authored Dec 15, 2023
2 parents b2d0ce7 + 99c67e8 commit e077110
Show file tree
Hide file tree
Showing 22 changed files with 596 additions and 117 deletions.
22 changes: 20 additions & 2 deletions app/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Routing extends BaseRouting
{
/**
* For Defined Routes.
* An array of files that contain route definitions.
* Route files are read in order, with the first match
* found taking precedence.
Expand All @@ -30,6 +31,7 @@ class Routing extends BaseRouting
];

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

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

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

/**
* Whether to translate dashes in URIs to underscores.
* For Auto Routing.
* Whether to translate dashes in URIs for controller/method to underscores.
* Primarily useful when using the auto-routing.
*
* Default: false
Expand Down Expand Up @@ -91,6 +96,7 @@ class Routing extends BaseRouting
public bool $autoRoute = false;

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

/**
* Map of URI segments and namespaces. For Auto Routing (Improved).
* For Auto Routing (Improved).
* Map of URI segments and namespaces.
*
* The key is the first URI segment. The value is the controller namespace.
* E.g.,
Expand All @@ -110,4 +117,15 @@ class Routing extends BaseRouting
* @var array [ uri_segment => namespace ]
*/
public array $moduleRoutes = [];

/**
* For Auto Routing (Improved).
* Whether to translate dashes in URIs for controller/method to CamelCase.
* E.g., blog-controller -> BlogController
*
* If you enable this, $translateURIDashes is ignored.
*
* Default: false
*/
public bool $translateUriToCamelCase = false;
}
10 changes: 0 additions & 10 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Database/Migration.php',
];
$ignoreErrors[] = [
'message' => '#^Property CodeIgniter\\\\Database\\\\Migration\\:\\:\\$DBGroup \\(string\\) on left side of \\?\\? is not nullable\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Database/Migration.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 8,
Expand Down Expand Up @@ -2606,11 +2601,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Router/AutoRouter.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in &&, Config\\\\Routing given on the right side\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Router/AutoRouterImproved.php',
];
$ignoreErrors[] = [
'message' => '#^PHPDoc type int of property CodeIgniter\\\\Router\\\\Exceptions\\\\RedirectException\\:\\:\\$code is not the same as PHPDoc type mixed of overridden property Exception\\:\\:\\$code\\.$#',
'count' => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ final class ControllerMethodReader

private bool $translateURIDashes;

private bool $translateUriToCamelCase;

/**
* @param string $namespace the default namespace
*/
Expand All @@ -44,8 +46,9 @@ public function __construct(string $namespace, array $httpMethods)
$this->namespace = $namespace;
$this->httpMethods = $httpMethods;

$config = config(Routing::class);
$this->translateURIDashes = $config->translateURIDashes;
$config = config(Routing::class);
$this->translateURIDashes = $config->translateURIDashes;
$this->translateUriToCamelCase = $config->translateUriToCamelCase;
}

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

$output = [];
$classInUri = $this->getUriByClass($classname);
$classInUri = $this->convertClassNameToUri($classname);

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

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

// Check if it is the default method.
if ($methodInUri === $defaultMethod) {
Expand Down Expand Up @@ -164,7 +167,7 @@ private function getParameters(ReflectionMethod $method): array
*
* @return string URI path part from the folder(s) and controller
*/
private function getUriByClass(string $classname): string
private function convertClassNameToUri(string $classname): string
{
// remove the namespace
$pattern = '/' . preg_quote($this->namespace, '/') . '/';
Expand All @@ -181,25 +184,33 @@ private function getUriByClass(string $classname): string

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

if ($this->translateURIDashes) {
$classUri = str_replace('_', '-', $classUri);
}

return $classUri;
return $this->translateToUri($classUri);
}

/**
* @return string URI path part from the method
*/
private function getUriByMethod(string $httpVerb, string $methodName): string
private function convertMethodNameToUri(string $httpVerb, string $methodName): string
{
$methodUri = lcfirst(substr($methodName, strlen($httpVerb)));

if ($this->translateURIDashes) {
$methodUri = str_replace('_', '-', $methodUri);
return $this->translateToUri($methodUri);
}

/**
* @param string $string classname or method name
*/
private function translateToUri(string $string): string
{
if ($this->translateUriToCamelCase) {
$string = strtolower(
preg_replace('/([a-z\d])([A-Z])/', '$1-$2', $string)
);
} elseif ($this->translateURIDashes) {
$string = str_replace('_', '-', $string);
}

return $methodUri;
return $string;
}

/**
Expand Down
22 changes: 20 additions & 2 deletions system/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Routing extends BaseConfig
{
/**
* For Defined Routes.
* An array of files that contain route definitions.
* Route files are read in order, with the first match
* found taking precedence.
Expand All @@ -30,6 +31,7 @@ class Routing extends BaseConfig
];

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

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

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

/**
* Whether to translate dashes in URIs to underscores.
* For Auto Routing.
* Whether to translate dashes in URIs for controller/method to underscores.
* Primarily useful when using the auto-routing.
*
* Default: false
Expand Down Expand Up @@ -91,6 +96,7 @@ class Routing extends BaseConfig
public bool $autoRoute = false;

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

/**
* Map of URI segments and namespaces. For Auto Routing (Improved).
* For Auto Routing (Improved).
* Map of URI segments and namespaces.
*
* The key is the first URI segment. The value is the controller namespace.
* E.g.,
Expand All @@ -110,4 +117,15 @@ class Routing extends BaseConfig
* @var array [ uri_segment => namespace ]
*/
public array $moduleRoutes = [];

/**
* For Auto Routing (Improved).
* Whether to translate dashes in URIs for controller/method to CamelCase.
* E.g., blog-controller -> BlogController
*
* If you enable this, $translateURIDashes is ignored.
*
* Default: false
*/
public bool $translateUriToCamelCase = false;
}
13 changes: 8 additions & 5 deletions system/Database/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class Migration
/**
* The name of the database group to use.
*
* @var string
* @var string|null
*/
protected $DBGroup;

Expand All @@ -41,12 +41,15 @@ abstract class Migration
*/
protected $forge;

/**
* Constructor.
*/
public function __construct(?Forge $forge = null)
{
$this->forge = $forge ?? Database::forge($this->DBGroup ?? config(Database::class)->defaultGroup);
if (isset($this->DBGroup)) {
$this->forge = Database::forge($this->DBGroup);
} elseif ($forge !== null) {
$this->forge = $forge;
} else {
$this->forge = Database::forge(config(Database::class)->defaultGroup);
}

$this->db = $this->forge->getConnection();
}
Expand Down
17 changes: 6 additions & 11 deletions system/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,12 @@ public function __construct(MigrationsConfig $config, $db = null)
$this->enabled = $config->enabled ?? false;
$this->table = $config->table ?? 'migrations';

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

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

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

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

$instance = new $class();
$group = $instance->getDBGroup() ?? config(Database::class)->defaultGroup;
/** @var Migration $instance */
$instance = new $class(Database::forge($this->db));
$group = $instance->getDBGroup() ?? $this->group;

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

$this->setGroup($group);

if (! is_callable([$instance, $direction])) {
$message = sprintf(lang('Migrations.missingMethod'), $direction);

Expand Down
Loading

0 comments on commit e077110

Please sign in to comment.