MockCodeIgniter
extends CodeIgniter
in package
This class is the core of the framework, and will analyse the request, route it to a controller, and send back the response.
Of course, there are variations to that flow, but this is the brains.
Table of Contents
- CI_VERSION = '4.1.3'
- The current version of CodeIgniter Framework
- MIN_PHP_VERSION = '7.3'
- $benchmark : Timer
- Timer instance.
- $cacheTTL : int
- Cache expiration time
- $config : App
- Main application configuration
- $controller : string|Closure
- Controller to use.
- $method : string
- Controller method to invoke.
- $output : string
- Output handler to use.
- $path : string
- Request path to use.
- $request : Request|IncomingRequest|CLIRequest
- Current request.
- $response : ResponseInterface
- Current response.
- $router : Router
- Router to use.
- $startTime : mixed
- App startup time.
- $totalTime : float
- Total app execution time
- $useSafeOutput : bool
- Should the Response instance "pretend" to keep from setting headers/cookies/etc
- __construct() : mixed
- Constructor.
- cache() : void
- Tells the app that the final output should be cached.
- cachePage() : mixed
- Caches the full response from the current request. Used for full-page caching for very high performance.
- displayCache() : bool|ResponseInterface
- Determines if a response has been cached for the given URI.
- displayPerformanceMetrics() : string
- Replaces the memory_usage and elapsed_time tags.
- getPerformanceStats() : array<string|int, mixed>
- Returns an array with our basic performance stats collected.
- initialize() : mixed
- Handles some basic app and environment setup.
- run() : bool|RequestInterface|ResponseInterface|mixed
- Launch the application!
- setPath() : $this
- Allows the request path to be set from outside the class, instead of relying on CLIRequest or IncomingRequest for the path.
- setRequest() : $this
- Sets a Request object to be used for this request.
- spoofRequestMethod() : mixed
- Modifies the Request Object to use a different method if a POST variable called _method is found.
- storePreviousURL() : mixed
- If we have a session object to use, store the current URI as the previous URI. This is called just prior to sending the response to the client, and will make it available next request.
- useSafeOutput() : $this
- Set our Response instance to "pretend" mode so that things like cookies and headers are not actually sent, allowing PHP 7.2+ to not complain when ini_set() function is used.
- bootstrapEnvironment() : mixed
- Load any custom boot files based upon the current environment.
- callExit() : mixed
- Exits the application, setting the exit code for CLI-based applications that might be watching.
- createController() : mixed
- Instantiates the controller class.
- detectEnvironment() : mixed
- You can load different configurations depending on your current environment. Setting the environment also influences things like logging and error reporting.
- determinePath() : mixed
- Determines the path to use for us to try to route to, based on user input (setPath), or the CLI/IncomingRequest path.
- display404errors() : mixed
- Displays a 404 Page Not Found error. If set, will try to call the 404Override controller/method that was set in routing config.
- forceSecureAccess() : mixed
- Force Secure Site Access? If the config value 'forceGlobalSecureRequests' is true, will enforce that all requests to this site are made through HTTPS. Will redirect the user to the current page with HTTPS, as well as set the HTTP Strict Transport Security header for those browsers that support it.
- gatherOutput() : mixed
- Gathers the script output from the buffer, replaces some execution time tag in the output and displays the debug toolbar, if required.
- generateCacheName() : string
- Generates the cache name to use for our full-page caching.
- getRequestObject() : mixed
- Get our Request object, (either IncomingRequest or CLIRequest) and set the server protocol based on the information provided by the server.
- getResponseObject() : mixed
- Get our Response object, and set some default values, including the HTTP protocol version and a default successful response.
- handleRequest() : RequestInterface|ResponseInterface|mixed
- Handles the main request logic and fires the controller.
- initializeKint() : mixed
- Initializes Kint
- resolvePlatformExtensions() : void
- Checks system for missing required PHP extensions.
- runController() : mixed
- Runs the controller, allowing for _remap methods to function.
- sendResponse() : mixed
- Sends the output of this request back to the client.
- startBenchmark() : mixed
- Start the Benchmark
- startController() : mixed
- Now that everything has been setup, this method attempts to run the controller method and make the script go. If it's not able to, will show the appropriate Page Not Found error.
- tryToRouteIt() : string|null
- Try to Route It - As it sounds like, works with the router to match a route against the current URI. If the route is a "redirect route", will also handle the redirect.
Constants
CI_VERSION
The current version of CodeIgniter Framework
public
mixed
CI_VERSION
= '4.1.3'
MIN_PHP_VERSION
private
mixed
MIN_PHP_VERSION
= '7.3'
Properties
$benchmark
Timer instance.
protected
Timer
$benchmark
$cacheTTL
Cache expiration time
protected
static int
$cacheTTL
= 0
$config
Main application configuration
protected
App
$config
$controller
Controller to use.
protected
string|Closure
$controller
$method
Controller method to invoke.
protected
string
$method
$output
Output handler to use.
protected
string
$output
$path
Request path to use.
protected
string
$path
$request
Current request.
protected
Request|IncomingRequest|CLIRequest
$request
$response
Current response.
protected
ResponseInterface
$response
$router
Router to use.
protected
Router
$router
$startTime
App startup time.
protected
mixed
$startTime
$totalTime
Total app execution time
protected
float
$totalTime
$useSafeOutput
Should the Response instance "pretend" to keep from setting headers/cookies/etc
protected
bool
$useSafeOutput
= false
Methods
__construct()
Constructor.
public
__construct(App $config) : mixed
Parameters
- $config : App
Return values
mixed —cache()
Tells the app that the final output should be cached.
public
static cache(int $time) : void
Parameters
- $time : int
Return values
void —cachePage()
Caches the full response from the current request. Used for full-page caching for very high performance.
public
cachePage(Cache $config) : mixed
Parameters
- $config : Cache
Return values
mixed —displayCache()
Determines if a response has been cached for the given URI.
public
displayCache(Cache $config) : bool|ResponseInterface
Parameters
- $config : Cache
Tags
Return values
bool|ResponseInterface —displayPerformanceMetrics()
Replaces the memory_usage and elapsed_time tags.
public
displayPerformanceMetrics(string $output) : string
Parameters
- $output : string
Return values
string —getPerformanceStats()
Returns an array with our basic performance stats collected.
public
getPerformanceStats() : array<string|int, mixed>
Return values
array<string|int, mixed> —initialize()
Handles some basic app and environment setup.
public
initialize() : mixed
Return values
mixed —run()
Launch the application!
public
run([RouteCollectionInterface|null $routes = null ][, bool $returnResponse = false ]) : bool|RequestInterface|ResponseInterface|mixed
This is "the loop" if you will. The main entry point into the script that gets the required class instances, fires off the filters, tries to route the response, loads the controller and generally makes all of the pieces work together.
Parameters
- $routes : RouteCollectionInterface|null = null
- $returnResponse : bool = false
Tags
Return values
bool|RequestInterface|ResponseInterface|mixed —setPath()
Allows the request path to be set from outside the class, instead of relying on CLIRequest or IncomingRequest for the path.
public
setPath(string $path) : $this
This is primarily used by the Console.
Parameters
- $path : string
Return values
$this —setRequest()
Sets a Request object to be used for this request.
public
setRequest(Request $request) : $this
Used when running certain tests.
Parameters
- $request : Request
Return values
$this —spoofRequestMethod()
Modifies the Request Object to use a different method if a POST variable called _method is found.
public
spoofRequestMethod() : mixed
Return values
mixed —storePreviousURL()
If we have a session object to use, store the current URI as the previous URI. This is called just prior to sending the response to the client, and will make it available next request.
public
storePreviousURL(URI|string $uri) : mixed
This helps provider safer, more reliable previous_url() detection.
Parameters
- $uri : URI|string
Return values
mixed —useSafeOutput()
Set our Response instance to "pretend" mode so that things like cookies and headers are not actually sent, allowing PHP 7.2+ to not complain when ini_set() function is used.
public
useSafeOutput([bool $safe = true ]) : $this
Parameters
- $safe : bool = true
Return values
$this —bootstrapEnvironment()
Load any custom boot files based upon the current environment.
protected
bootstrapEnvironment() : mixed
If no boot file exists, we shouldn't continue because something is wrong. At the very least, they should have error reporting setup.
Return values
mixed —callExit()
Exits the application, setting the exit code for CLI-based applications that might be watching.
protected
callExit(mixed $code) : mixed
Made into a separate method so that it can be mocked during testing without actually stopping script execution.
Parameters
- $code : mixed
Return values
mixed —createController()
Instantiates the controller class.
protected
createController() : mixed
Return values
mixed —detectEnvironment()
You can load different configurations depending on your current environment. Setting the environment also influences things like logging and error reporting.
protected
detectEnvironment() : mixed
This can be set to anything, but default usage is:
development testing production
Return values
mixed —determinePath()
Determines the path to use for us to try to route to, based on user input (setPath), or the CLI/IncomingRequest path.
protected
determinePath() : mixed
Return values
mixed —display404errors()
Displays a 404 Page Not Found error. If set, will try to call the 404Override controller/method that was set in routing config.
protected
display404errors(PageNotFoundException $e) : mixed
Parameters
Return values
mixed —forceSecureAccess()
Force Secure Site Access? If the config value 'forceGlobalSecureRequests' is true, will enforce that all requests to this site are made through HTTPS. Will redirect the user to the current page with HTTPS, as well as set the HTTP Strict Transport Security header for those browsers that support it.
protected
forceSecureAccess([int $duration = 31536000 ]) : mixed
Parameters
- $duration : int = 31536000
-
How long the Strict Transport Security should be enforced for this URL.
Return values
mixed —gatherOutput()
Gathers the script output from the buffer, replaces some execution time tag in the output and displays the debug toolbar, if required.
protected
gatherOutput([Cache|null $cacheConfig = null ][, mixed|null $returned = null ]) : mixed
Parameters
- $cacheConfig : Cache|null = null
- $returned : mixed|null = null
Return values
mixed —generateCacheName()
Generates the cache name to use for our full-page caching.
protected
generateCacheName(Cache $config) : string
Parameters
- $config : Cache
Return values
string —getRequestObject()
Get our Request object, (either IncomingRequest or CLIRequest) and set the server protocol based on the information provided by the server.
protected
getRequestObject() : mixed
Return values
mixed —getResponseObject()
Get our Response object, and set some default values, including the HTTP protocol version and a default successful response.
protected
getResponseObject() : mixed
Return values
mixed —handleRequest()
Handles the main request logic and fires the controller.
protected
handleRequest(RouteCollectionInterface|null $routes, Cache $cacheConfig[, bool $returnResponse = false ]) : RequestInterface|ResponseInterface|mixed
Parameters
- $routes : RouteCollectionInterface|null
- $cacheConfig : Cache
- $returnResponse : bool = false
Tags
Return values
RequestInterface|ResponseInterface|mixed —initializeKint()
Initializes Kint
protected
initializeKint() : mixed
Return values
mixed —resolvePlatformExtensions()
Checks system for missing required PHP extensions.
protected
resolvePlatformExtensions() : void
Tags
Return values
void —runController()
Runs the controller, allowing for _remap methods to function.
protected
runController(mixed $class) : mixed
Parameters
- $class : mixed
Return values
mixed —sendResponse()
Sends the output of this request back to the client.
protected
sendResponse() : mixed
This is what they've been waiting for!
Return values
mixed —startBenchmark()
Start the Benchmark
protected
startBenchmark() : mixed
The timer is used to display total script execution both in the debug toolbar, and potentially on the displayed page.
Return values
mixed —startController()
Now that everything has been setup, this method attempts to run the controller method and make the script go. If it's not able to, will show the appropriate Page Not Found error.
protected
startController() : mixed
Return values
mixed —tryToRouteIt()
Try to Route It - As it sounds like, works with the router to match a route against the current URI. If the route is a "redirect route", will also handle the redirect.
protected
tryToRouteIt([RouteCollectionInterface|null $routes = null ]) : string|null
Parameters
- $routes : RouteCollectionInterface|null = null
-
An collection interface to use in place of the config file.