Skip to content

Commit

Permalink
Refactor controller to not use the constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Jul 10, 2018
1 parent fa8c1c7 commit f4c373e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 52 deletions.
6 changes: 5 additions & 1 deletion system/CLI/CommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@

class CommandRunner extends Controller
{

/**
* Stores the info about found Commands.
*
* @var array
*/
protected $commands = [];

/**
* @var \CodeIgniter\Log\Logger
*/
protected $logger;

//--------------------------------------------------------------------

/**
Expand Down
6 changes: 4 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ public function run(RouteCollectionInterface $routes = null, bool $returnRespons
try
{
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
} catch (Router\RedirectException $e)
}
catch (Router\RedirectException $e)
{
$logger = Services::logger();
$logger->info('REDIRECTED ROUTE at ' . $e->getMessage());
Expand Down Expand Up @@ -732,7 +733,8 @@ protected function startController()
*/
protected function createController()
{
$class = new $this->controller($this->request, $this->response);
$class = new $this->controller();
$class->initController($this->request, $this->response, Services::logger());

$this->benchmark->stop('controller_constructor');

Expand Down
25 changes: 14 additions & 11 deletions system/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Log\Logger;
use CodeIgniter\Validation\Validation;
use Psr\Log\LoggerInterface;

/**
* Class Controller
Expand Down Expand Up @@ -99,18 +100,17 @@ class Controller
/**
* Constructor.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param Logger $logger
* @param RequestInterface $request
* @param ResponseInterface $response
* @param \Psr\Log\LoggerInterface $logger
*
* @throws \CodeIgniter\HTTP\RedirectException
*/
public function __construct(RequestInterface $request, ResponseInterface $response, Logger $logger = null)
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->request = $request;

$this->response = $response;

$this->logger = is_null($logger) ? Services::logger(true) : $logger;

$this->logger = $logger;
$this->logger->info('Controller "' . get_class($this) . '" loaded.');

if ($this->forceHTTPS > 0)
Expand All @@ -132,6 +132,8 @@ public function __construct(RequestInterface $request, ResponseInterface $respon
* @param int $duration The number of seconds this link should be
* considered secure for. Only with HSTS header.
* Default value is 1 year.
*
* @throws \CodeIgniter\HTTP\RedirectException
*/
public function forceHTTPS(int $duration = 31536000)
{
Expand Down Expand Up @@ -182,9 +184,10 @@ public function validate($rules, array $messages = []): bool
{
$this->validator = Services::validation();

$success = $this->validator->withRequest($this->request)
->setRules($rules, $messages)
->run();
$success = $this->validator
->withRequest($this->request)
->setRules($rules, $messages)
->run();

return $success;
}
Expand Down
12 changes: 11 additions & 1 deletion tests/system/CLI/CommandRunnerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace CodeIgniter\CLI;

use CodeIgniter\HTTP\UserAgent;
use Config\Services;
use Tests\Support\Config\MockCLIConfig;
use CodeIgniter\Test\Filters\CITestStreamFilter;

Expand All @@ -9,6 +10,13 @@ class CommandRunnerTest extends \CIUnitTestCase

private $stream_filter;

protected $env;
protected $config;
protected $request;
protected $response;
protected $logger;
protected $runner;

public function setUp()
{
parent::setUp();
Expand All @@ -32,7 +40,9 @@ public function setUp()
$this->config = new MockCLIConfig();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->runner = new CommandRunner($this->request, $this->response);
$this->logger = Services::logger();
$this->runner = new CommandRunner();
$this->runner->initController($this->request, $this->response, $this->logger);
}

public function tearDown()
Expand Down
15 changes: 14 additions & 1 deletion tests/system/Commands/CommandsTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
<?php namespace CodeIgniter\Commands;

use Config\Services;
use Tests\Support\Config\MockAppConfig;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\CommandRunner;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Tests\Support\Config\MockLogger;

class CommandsTest extends \CIUnitTestCase
{

private $stream_filter;
protected $env;
protected $config;
protected $request;
protected $response;
protected $logger;
protected $runner;


public function setUp()
{
parent::setUp();

CITestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CITestStreamFilter');

Expand All @@ -32,7 +43,9 @@ public function setUp()
$this->config = new MockAppConfig();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->runner = new CommandRunner($this->request, $this->response);
$this->logger = Services::logger();
$this->runner = new CommandRunner();
$this->runner->initController($this->request, $this->response, $this->logger);
}

public function tearDown()
Expand Down
14 changes: 13 additions & 1 deletion tests/system/Commands/SessionsCommandsTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?php namespace CodeIgniter\Commands;

use Config\Services;
use Tests\Support\Config\MockAppConfig;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\CommandRunner;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Tests\Support\Config\MockLogger;

class SessionsCommandsTest extends \CIUnitTestCase
{
private $stream_filter;
protected $env;
protected $config;
protected $request;
protected $response;
protected $logger;
protected $runner;

public function setUp()
{
parent::setUp();

CITestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CITestStreamFilter');

Expand All @@ -31,7 +41,9 @@ public function setUp()
$this->config = new MockAppConfig();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->runner = new CommandRunner($this->request, $this->response);
$this->logger = Services::logger();
$this->runner = new CommandRunner();
$this->runner->initController($this->request, $this->response, $this->logger);
}

public function tearDown()
Expand Down
28 changes: 21 additions & 7 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace CodeIgniter;

use CodeIgniter\Log\Logger;
use Config\App;
use CodeIgniter\HTTP\UserAgent;
use Tests\Support\Config\MockLogger;
use Tests\Support\MockCodeIgniter;

/**
Expand Down Expand Up @@ -35,6 +37,10 @@ class ControllerTest extends \CIUnitTestCase
* @var \CodeIgniter\HTTP\Response
*/
protected $response;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

//--------------------------------------------------------------------

Expand All @@ -45,6 +51,7 @@ public function setUp()
$this->config = new App();
$this->request = new \CodeIgniter\HTTP\IncomingRequest($this->config, new \CodeIgniter\HTTP\URI('https://somwhere.com'), null, new UserAgent());
$this->response = new \CodeIgniter\HTTP\Response($this->config);
$this->logger = \Config\Services::logger();
$this->codeigniter = new MockCodeIgniter($this->config);
}

Expand All @@ -53,7 +60,8 @@ public function setUp()
public function testConstructor()
{
// make sure we can instantiate one
$this->controller = new Controller($this->request, $this->response);
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);
$this->assertInstanceOf(Controller::class, $this->controller);
}

Expand All @@ -62,38 +70,44 @@ public function testConstructorHTTPS()
$original = $_SERVER;
$_SERVER = ['HTTPS' => 'on'];
// make sure we can instantiate one
$this->controller = new Class($this->request, $this->response) extends Controller
$this->controller = new Class() extends Controller
{

protected $forceHTTPS = 1;
};
$this->controller->initController($this->request, $this->response, $this->logger);

$this->assertInstanceOf(Controller::class, $this->controller);
$_SERVER = $original; // restore so code coverage doesn't break
}

//--------------------------------------------------------------------
public function testCachePage()
{
$this->controller = new Controller($this->request, $this->response);
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);

$this->assertNull($this->controller->cachePage(10));
}

public function testValidate()
{
// make sure we can instantiate one
$this->controller = new Controller($this->request, $this->response);
$this->controller = new Controller();
$this->controller->initController($this->request, $this->response, $this->logger);

// and that we can attempt validation, with no rules
$this->assertFalse($this->controller->validate([]));
}

//--------------------------------------------------------------------
public function testHelpers()
{
$this->controller = new Class($this->request, $this->response) extends Controller
$this->controller = new Class() extends Controller
{

protected $helpers = ['cookie', 'text'];
};
$this->controller->initController($this->request, $this->response, $this->logger);

$this->assertInstanceOf(Controller::class, $this->controller);
}

Expand Down
28 changes: 0 additions & 28 deletions user_guide_src/source/general/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,34 +233,6 @@ your *application/Config/Routes.php* file.

CodeIgniter also permits you to remap your URIs using its :doc:`URI Routing <routing>` feature.

Class Constructors
==================

If you intend to use a constructor in any of your Controllers, you
**MUST** place the following line of code in it::

parent::__construct(...$params);

The reason this line is necessary is because your local constructor will
be overriding the one in the parent controller class so we need to
manually call it.

Example::

<?php
class Blog extends \CodeIgniter\Controller
{
public function __construct(...$params)
{
parent::__construct(...$params);

// Your own constructor code
}
}

Constructors are useful if you need to set some default values, or run a
default process when your class is instantiated. Constructors can't
return a value, but they can do some default work.

Included Properties
===================
Expand Down

0 comments on commit f4c373e

Please sign in to comment.