mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Refactor CodeIgniter class
Add Bootstrap class and test for CodeIgniter class
This commit is contained in:
parent
665fbb3bd6
commit
9da31d9ab8
@ -38,6 +38,7 @@ switch (ENVIRONMENT)
|
||||
error_reporting(-1);
|
||||
ini_set('display_errors', 1);
|
||||
define('CI_DEBUG', 1);
|
||||
define('SHOW_DEBUG_BACKTRACE', TRUE);
|
||||
break;
|
||||
|
||||
case 'production':
|
||||
@ -150,6 +151,8 @@ define('APPPATH', realpath($application_folder).DIRECTORY_SEPARATOR);
|
||||
*
|
||||
* And away we go...
|
||||
*/
|
||||
require BASEPATH.'Bootstrap.php';
|
||||
require BASEPATH.'CodeIgniter.php';
|
||||
new CodeIgniter\Bootstrap();
|
||||
$codeigniter = new CodeIgniter\CodeIgniter($startMemory, $startTime);
|
||||
$codeigniter->run();
|
||||
|
@ -18,8 +18,7 @@
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">./system</directory>
|
||||
<exclude>
|
||||
<file>./system/CodeIgniter.php</file>
|
||||
<file>./system/Controller.php</file>
|
||||
<file>./system/Bootstrap.php</file>
|
||||
<file>./system/ComposerScripts.php</file>
|
||||
<file>./system/View/Escaper.php</file>
|
||||
<directory suffix=".php">./system/View/Exception</directory>
|
||||
|
169
system/Bootstrap.php
Normal file
169
system/Bootstrap.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?php namespace CodeIgniter;
|
||||
|
||||
/**
|
||||
* CodeIgniter
|
||||
*
|
||||
* An open source application development framework for PHP
|
||||
*
|
||||
* This content is released under the MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 - 2016, British Columbia Institute of Technology
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @author CodeIgniter Dev Team
|
||||
* @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
|
||||
* @license http://opensource.org/licenses/MIT MIT License
|
||||
* @link http://codeigniter.com
|
||||
* @since Version 4.0.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
use Config\App;
|
||||
use Config\Services;
|
||||
use Config\Autoload;
|
||||
|
||||
/**
|
||||
* Class Bootstrap
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @package CodeIgniter
|
||||
*/
|
||||
class Bootstrap
|
||||
{
|
||||
/**
|
||||
* The application configuration object.
|
||||
*
|
||||
* @var \Config\App
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
require_once BASEPATH.'Common.php';
|
||||
require_once APPPATH.'Config/Services.php';
|
||||
|
||||
$this->loadFrameworkConstants();
|
||||
$this->setupAutoloader();
|
||||
$this->setExceptionHandling();
|
||||
$this->loadComposerAutoloader();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the framework constants
|
||||
*/
|
||||
protected function loadFrameworkConstants()
|
||||
{
|
||||
if (file_exists(APPPATH.'Config/'.ENVIRONMENT.'/constants.php'))
|
||||
{
|
||||
require_once APPPATH.'Config/'.ENVIRONMENT.'/constants.php';
|
||||
}
|
||||
|
||||
require_once(APPPATH.'Config/Constants.php');
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Load any environment-specific settings from .env file
|
||||
*/
|
||||
protected function loadDotEnv()
|
||||
{
|
||||
// Load environment settings from .env files
|
||||
// into $_SERVER and $_ENV
|
||||
require BASEPATH.'Config/DotEnv.php';
|
||||
$env = new DotEnv(APPPATH);
|
||||
$env->load();
|
||||
unset($env);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Setup the autoloader
|
||||
*/
|
||||
protected function setupAutoloader()
|
||||
{
|
||||
// The autoloader isn't initialized yet, so load the file manually.
|
||||
require BASEPATH.'Autoloader/Autoloader.php';
|
||||
require APPPATH.'Config/Autoload.php';
|
||||
|
||||
// The Autoloader class only handles namespaces
|
||||
// and "legacy" support.
|
||||
$loader = Services::autoloader();
|
||||
$loader->initialize(new Autoload());
|
||||
|
||||
// The register function will prepend
|
||||
// the psr4 loader.
|
||||
$loader->register();
|
||||
|
||||
return $loader;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Set custom exception handling
|
||||
*/
|
||||
protected function setExceptionHandling()
|
||||
{
|
||||
$this->config = new App();
|
||||
|
||||
Services::exceptions($this->config, true)
|
||||
->initialize();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Should we use a Composer autoloader?
|
||||
*
|
||||
* CodeIgniter provides its own PSR4-compatible autoloader, but many
|
||||
* third-party scripts will take advantage of the extra flexibility
|
||||
* that Composer provides. This allows that support to be provided,
|
||||
* and even with a customizable path to their autoloader.
|
||||
*/
|
||||
protected function loadComposerAutoloader()
|
||||
{
|
||||
$composer_autoload = $this->config->composerAutoload;
|
||||
|
||||
if (empty($composer_autoload))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ($composer_autoload === true)
|
||||
{
|
||||
file_exists(APPPATH.'vendor/autoload.php')
|
||||
? require_once(APPPATH.'vendor/autoload.php')
|
||||
: log_message('error', '$this->config->\'composerAutoload\' is set to TRUE but '.APPPATH.
|
||||
'vendor/autoload.php was not found.');
|
||||
}
|
||||
elseif (file_exists($composer_autoload))
|
||||
{
|
||||
require_once($composer_autoload);
|
||||
}
|
||||
else
|
||||
{
|
||||
log_message('error',
|
||||
'Could not find the specified $this->config->\'composerAutoload\' path: '.$composer_autoload);
|
||||
}
|
||||
}
|
||||
}
|
@ -39,9 +39,7 @@
|
||||
|
||||
use Config\App;
|
||||
use Config\Services;
|
||||
use Config\Autoload;
|
||||
use CodeIgniter\Hooks\Hooks;
|
||||
use CodeIgniter\Config\DotEnv;
|
||||
|
||||
/**
|
||||
* System Initialization Class
|
||||
@ -120,6 +118,14 @@ class CodeIgniter
|
||||
{
|
||||
$this->startMemory = $startMemory;
|
||||
$this->startTime = $startTime;
|
||||
|
||||
// When testing, we need to create more than one instance.
|
||||
if ( ! defined('CI_VERSION'))
|
||||
{
|
||||
define('CI_VERSION', $this->CIVersion);
|
||||
}
|
||||
|
||||
$this->config = new App();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
@ -131,15 +137,6 @@ class CodeIgniter
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
define('CI_VERSION', $this->CIVersion);
|
||||
|
||||
require_once BASEPATH.'Common.php';
|
||||
require_once APPPATH.'Config/Services.php';
|
||||
|
||||
$this->loadFrameworkConstants();
|
||||
$this->setupAutoloader();
|
||||
$this->setExceptionHandling();
|
||||
$this->loadComposerAutoloader();
|
||||
$this->startBenchmark();
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
@ -182,70 +179,6 @@ class CodeIgniter
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Load the framework constants
|
||||
*/
|
||||
protected function loadFrameworkConstants()
|
||||
{
|
||||
if (file_exists(APPPATH.'Config/'.ENVIRONMENT.'/constants.php'))
|
||||
{
|
||||
require_once APPPATH.'Config/'.ENVIRONMENT.'/constants.php';
|
||||
}
|
||||
|
||||
require_once(APPPATH.'Config/Constants.php');
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Load any environment-specific settings from .env file
|
||||
*/
|
||||
protected function loadDotEnv()
|
||||
{
|
||||
// Load environment settings from .env files
|
||||
// into $_SERVER and $_ENV
|
||||
require BASEPATH.'Config/DotEnv.php';
|
||||
$env = new DotEnv(APPPATH);
|
||||
$env->load();
|
||||
unset($env);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Setup the autoloader
|
||||
*/
|
||||
protected function setupAutoloader()
|
||||
{
|
||||
// The autoloader isn't initialized yet, so load the file manually.
|
||||
require BASEPATH.'Autoloader/Autoloader.php';
|
||||
require APPPATH.'Config/Autoload.php';
|
||||
|
||||
// The Autoloader class only handles namespaces
|
||||
// and "legacy" support.
|
||||
$loader = Services::autoloader();
|
||||
$loader->initialize(new Autoload());
|
||||
|
||||
// The register function will prepend
|
||||
// the psr4 loader.
|
||||
$loader->register();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Set custom exception handling
|
||||
*/
|
||||
protected function setExceptionHandling()
|
||||
{
|
||||
$this->config = new App();
|
||||
|
||||
Services::exceptions($this->config, true)
|
||||
->initialize();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Start the Benchmark
|
||||
*
|
||||
@ -263,43 +196,6 @@ class CodeIgniter
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Should we use a Composer autoloader?
|
||||
*
|
||||
* CodeIgniter provides its own PSR4-compatible autoloader, but many
|
||||
* third-party scripts will take advantage of the extra flexibility
|
||||
* that Composer provides. This allows that support to be provided,
|
||||
* and even with a customizable path to their autoloader.
|
||||
*/
|
||||
protected function loadComposerAutoloader()
|
||||
{
|
||||
$composer_autoload = $this->config->composerAutoload;
|
||||
|
||||
if (empty($composer_autoload))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ($composer_autoload === true)
|
||||
{
|
||||
file_exists(APPPATH.'vendor/autoload.php')
|
||||
? require_once(APPPATH.'vendor/autoload.php')
|
||||
: log_message('error', '$this->config->\'composerAutoload\' is set to TRUE but '.APPPATH.
|
||||
'vendor/autoload.php was not found.');
|
||||
}
|
||||
elseif (file_exists($composer_autoload))
|
||||
{
|
||||
require_once($composer_autoload);
|
||||
}
|
||||
else
|
||||
{
|
||||
log_message('error',
|
||||
'Could not find the specified $this->config->\'composerAutoload\' path: '.$composer_autoload);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get our Request object, (either IncomingRequest or CLIRequest)
|
||||
* and set the server protocol based on tne information provided
|
||||
|
@ -44,7 +44,6 @@ use CodeIgniter\Log\Logger;
|
||||
/**
|
||||
* Class Controller
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @package CodeIgniter
|
||||
*/
|
||||
class Controller
|
||||
|
15
tests/_support/MockBootstrap.php
Normal file
15
tests/_support/MockBootstrap.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php namespace CodeIgniter;
|
||||
|
||||
class MockBootstrap extends Bootstrap
|
||||
{
|
||||
protected function setupAutoloader()
|
||||
{
|
||||
$loader = parent::setupAutoloader();
|
||||
|
||||
// Add namespace paths to autoload mocks for testing
|
||||
$loader->addNamespace('CodeIgniter', SUPPORTPATH);
|
||||
$loader->addNamespace('Config', SUPPORTPATH.'Config');
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
8
tests/_support/MockCodeIgniter.php
Normal file
8
tests/_support/MockCodeIgniter.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php namespace CodeIgniter;
|
||||
|
||||
use Config\Services;
|
||||
|
||||
class MockCodeIgniter extends CodeIgniter
|
||||
{
|
||||
|
||||
}
|
@ -7,6 +7,8 @@
|
||||
// tests should have all they need at their fingertips.
|
||||
//
|
||||
|
||||
$startMemory = memory_get_usage();
|
||||
$startTime = microtime(true);
|
||||
|
||||
if (! defined('ENVIRONMENT'))
|
||||
{
|
||||
@ -93,97 +95,17 @@ else
|
||||
define('SUPPORTPATH', realpath(BASEPATH.'../tests/_support/').'/');
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Load Autoloaders
|
||||
// LOAD THE BOOTSTRAP FILE
|
||||
//--------------------------------------------------------------------
|
||||
// CodeIgniter uses 2 autoloaders - a classmap and a PSR4-compatible
|
||||
// autoloader, to help it load files in your application and the
|
||||
// framework itself. To make testing easier, we need to get these
|
||||
// loaded up so the files can be found without us having to require
|
||||
// a lot of files in the tests.
|
||||
//
|
||||
// Below we load a fair chunk of the CodeIgniter.php file to
|
||||
// get lots of moving pieces up and ready.
|
||||
//
|
||||
|
||||
/**
|
||||
* CodeIgniter version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
|
||||
define('CI_VERSION', '4.0-dev');
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------
|
||||
* Load the framework constants
|
||||
* ------------------------------------------------------
|
||||
*/
|
||||
|
||||
if (file_exists(APPPATH.'Config/'.ENVIRONMENT.'/Constants.php'))
|
||||
{
|
||||
require_once APPPATH.'Config/'.ENVIRONMENT.'/Constants.php';
|
||||
}
|
||||
|
||||
require_once(APPPATH.'Config/Constants.php');
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------
|
||||
* Load the global functions
|
||||
* ------------------------------------------------------
|
||||
*/
|
||||
|
||||
require_once BASEPATH.'Common.php';
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------
|
||||
* Load any environment-specific settings from .env file
|
||||
* ------------------------------------------------------
|
||||
*/
|
||||
if (ENVIRONMENT !== 'production')
|
||||
{
|
||||
// Load environment settings from .env files
|
||||
// into $_SERVER and $_ENV
|
||||
require_once BASEPATH.'Config/DotEnv.php';
|
||||
$env = new \CodeIgniter\Config\DotEnv(APPPATH);
|
||||
$env->load();
|
||||
unset($env);
|
||||
}
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------
|
||||
* Get the DI Container ready for use
|
||||
* ------------------------------------------------------
|
||||
*/
|
||||
|
||||
require_once APPPATH.'Config/Services.php';
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------
|
||||
* Setup the autoloader
|
||||
* ------------------------------------------------------
|
||||
*/
|
||||
|
||||
// The autloader isn't initialized yet, so load the file manually.
|
||||
require_once BASEPATH.'Autoloader/Autoloader.php';
|
||||
require_once APPPATH.'Config/Autoload.php';
|
||||
|
||||
// The Autoloader class only handles namespaces
|
||||
// and "legacy" support.
|
||||
$loader = new \CodeIgniter\Autoloader\Autoloader();
|
||||
$loader->initialize(new Config\Autoload());
|
||||
|
||||
// Add namespace paths to autoload mocks for testing
|
||||
$loader->addNamespace('CodeIgniter', SUPPORTPATH);
|
||||
$loader->addNamespace('Config', SUPPORTPATH.'Config');
|
||||
|
||||
// The register function will prepend
|
||||
// the psr4 loader.
|
||||
$loader->register();
|
||||
|
||||
require BASEPATH.'Bootstrap.php';
|
||||
require BASEPATH.'CodeIgniter.php';
|
||||
require SUPPORTPATH.'MockBootstrap.php';
|
||||
require SUPPORTPATH.'MockCodeIgniter.php';
|
||||
new CodeIgniter\MockBootstrap();
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Load our TestCase
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
require_once __DIR__ .'/CIUnitTestCase.php';
|
||||
|
||||
|
36
tests/system/CodeIgniterTest.php
Normal file
36
tests/system/CodeIgniterTest.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php namespace CodeIgniter;
|
||||
|
||||
class CodeIgniterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var \CodeIgniter\CodeIgniter
|
||||
*/
|
||||
protected $codeigniter;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->codeigniter = new MockCodeIgniter(memory_get_usage(), microtime(true));
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testRunDefaultRoute()
|
||||
{
|
||||
$_SERVER['argv'] = [
|
||||
'index.php',
|
||||
'/',
|
||||
];
|
||||
$_SERVER['argc'] = 2;
|
||||
|
||||
ob_start();
|
||||
$this->codeigniter->run();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains('<h1>Welcome to CodeIgniter</h1>', $output);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user