Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add URIFactory #7239

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector;
use Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector;
Expand Down Expand Up @@ -89,6 +90,11 @@
__DIR__ . '/tests/system/Test/ReflectionHelperTest.php',
],

RemoveUnusedConstructorParamRector::class => [
// there are deprecated parameters
__DIR__ . '/system/Debug/Exceptions.php',
],

// call on purpose for nothing happen check
RemoveEmptyMethodCallRector::class => [
__DIR__ . '/tests',
Expand Down
6 changes: 5 additions & 1 deletion system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ public static function encrypter(?EncryptionConfig $config = null, $getShared =
* - register_shutdown_function
*
* @return Exceptions
*
* @deprecated The parameter $request and $response are deprecated.
*/
public static function exceptions(
?ExceptionsConfig $config = null,
Expand All @@ -262,7 +264,9 @@ public static function exceptions(
}

$config ??= config('Exceptions');
$request ??= AppServices::request();
/** @var ExceptionsConfig $config */

// @TODO remove instantiation of Response in the future.
$response ??= AppServices::response();

return new Exceptions($config, $request, $response);
Expand Down
12 changes: 8 additions & 4 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CodeIgniter\HTTP\ResponseInterface;
use Config\Exceptions as ExceptionsConfig;
use Config\Paths;
use Config\Services;
use ErrorException;
use Psr\Log\LogLevel;
use Throwable;
Expand Down Expand Up @@ -71,15 +72,15 @@ class Exceptions
private ?Throwable $exceptionCaughtByExceptionHandler = null;

/**
* @param CLIRequest|IncomingRequest $request
* @param CLIRequest|IncomingRequest|null $request
*
* @deprecated The parameter $request and $response are deprecated. No longer used.
*/
public function __construct(ExceptionsConfig $config, $request, ResponseInterface $response)
public function __construct(ExceptionsConfig $config, $request, ResponseInterface $response) /** @phpstan-ignore-line */
{
$this->ob_level = ob_get_level();
$this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR;
$this->config = $config;
$this->request = $request;
$this->response = $response;

// workaround for upgraded users
// This causes "Deprecated: Creation of dynamic property" in PHP 8.2.
Expand Down Expand Up @@ -119,6 +120,9 @@ public function exceptionHandler(Throwable $exception)

[$statusCode, $exitCode] = $this->determineCodes($exception);

$this->request = Services::request();
$this->response = Services::response();

if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
log_message('critical', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $exception->getMessage(),
Expand Down
9 changes: 9 additions & 0 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ protected function detectURI(string $protocol, string $baseURL)
/**
* Detects the relative path based on
* the URIProtocol Config setting.
*
* @deprecated Moved to URIFactory.
*/
public function detectPath(string $protocol = ''): string
{
Expand Down Expand Up @@ -265,6 +267,8 @@ public function detectPath(string $protocol = ''): string
* fixing the query string if necessary.
*
* @return string The URI it found.
*
* @deprecated Moved to URIFactory.
*/
protected function parseRequestURI(): string
{
Expand Down Expand Up @@ -323,6 +327,8 @@ protected function parseRequestURI(): string
* Parse QUERY_STRING
*
* Will parse QUERY_STRING and automatically detect the URI from it.
*
* @deprecated Moved to URIFactory.
*/
protected function parseQueryString(): string
{
Expand Down Expand Up @@ -495,6 +501,9 @@ public function setPath(string $path, ?App $config = null)
return $this;
}

/**
* @deprecated Moved to URIFactory.
*/
private function determineHost(App $config, string $baseURL): string
{
$host = parse_url($baseURL, PHP_URL_HOST);
Expand Down
41 changes: 41 additions & 0 deletions system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class URI

/**
* List of URI segments.
* URI Segments mean only the URI path part relative to the baseURL.
*
* Starts at 1 instead of 0
*
Expand Down Expand Up @@ -97,6 +98,16 @@ class URI
*/
protected $path;

/**
* URI path relative to baseURL.
*
* If the baseURL contains sub folders, this value will be different from
* the current URI path.
*
* @var string
*/
protected $routePath;

/**
* The name of any fragment.
*
Expand Down Expand Up @@ -480,6 +491,20 @@ public function getPath(): string
return $this->path ?? '';
}

/**
* Returns the URI path relative to baseURL.
*
* @return string The Route path.
*/
public function getRoutePath(): string
{
if ($this->routePath === null) {
throw new BadMethodCallException('The $routePath is not set.');
}

return $this->routePath;
}

/**
* Retrieve the query string
*/
Expand Down Expand Up @@ -757,6 +782,22 @@ public function setPath(string $path)
return $this;
}

/**
* Sets the route path.
*
* @return $this
*/
public function setRoutePath(string $path)
{
$this->routePath = $this->filterPath($path);

$tempPath = trim($this->routePath, '/');

$this->segments = ($tempPath === '') ? [] : explode('/', $tempPath);

return $this;
}

/**
* Sets the current baseURL.
*
Expand Down
Loading