Skip to content

Commit

Permalink
Add Route::allowAll().
Browse files Browse the repository at this point in the history
FrontController uses Routes.
  • Loading branch information
piotrooo committed Sep 26, 2013
1 parent 450c818 commit f445361
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 72 deletions.
13 changes: 5 additions & 8 deletions lib/Ouzo/ControllerResolver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Ouzo;

use Ouzo\Utilities\Strings;

class ControllerResolver
{
function __construct($controllerPath = "\\Controller\\")
Expand All @@ -11,19 +13,14 @@ function __construct($controllerPath = "\\Controller\\")
$this->_uri = new Uri();
}

public function getCurrentController()
public function getController($controller, $action)
{
$controllerName = $this->_uri->getController();
$controllerName = Strings::underscoreToCamelCase($controller);
$controller = $this->controllerPath . $controllerName . "Controller";

$this->_validateControllerExists($controller);

return new $controller($this->_getCurrentAction());
}

private function _getCurrentAction()
{
return $this->_uri->getAction() ? : $this->_defaultAction;
return new $controller($action);
}

private function _validateControllerExists($controller)
Expand Down
53 changes: 27 additions & 26 deletions lib/Ouzo/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

use Exception;
use Ouzo\Logger\Logger;
use Ouzo\Routing\Router;

class FrontController
{
public static $requestId;
public static $userId;

private $_uri;
private $_uriAction;
private $_defaults;
private $_currentAction;
private $_currentController;
private $_currentControllerObject;

public $redirectHandler;
public $sessionInitializer;
Expand All @@ -23,7 +24,6 @@ public function __construct()
{
self::$requestId = uniqid();

$this->_uri = new Uri();
$this->_defaults = Config::getValue('global');

$this->redirectHandler = new RedirectHandler();
Expand All @@ -35,12 +35,18 @@ public function __construct()

public function init()
{
$this->_uriAction = $this->_uri->getAction();
$uri = new Uri();

if ($this->_isNoController()) {
$router = new Router($uri->getPathWithoutPrefix());
$routeRule = $router->findRoute();

$this->_currentController = $routeRule->getController();
$this->_currentAction = $routeRule->isRequireAction() ? $routeRule->getAction() : $uri->getAction();

if (!$routeRule->getController()) {
$this->_redirectToDefault();
} else {
$this->_currentController = $this->controllerResolver->getCurrentController();
$this->_currentControllerObject = $this->controllerResolver->getController($this->_currentController, $this->_currentAction);

$this->sessionInitializer->startSession();

Expand All @@ -58,14 +64,9 @@ public function init()
}
}

public function getCurrentController()
{
return $this->_currentController;
}

private function _isNoController()
public function getCurrentControllerObject()
{
return !$this->_uri->getRawController();
return $this->_currentControllerObject;
}

private function _redirectToDefault()
Expand All @@ -82,15 +83,15 @@ private function _redirect($url)

private function _invokeInit()
{
if (method_exists($this->_currentController, 'init')) {
$this->_currentController->init();
if (method_exists($this->_currentControllerObject, 'init')) {
$this->_currentControllerObject->init();
}
}

private function _invokeBeforeMethods()
{
foreach ($this->_currentController->before as $method) {
if (!$this->_currentController->$method()) {
foreach ($this->_currentControllerObject->before as $method) {
if (!$this->_currentControllerObject->$method()) {
return false;
}

Expand All @@ -103,24 +104,24 @@ private function _invokeBeforeMethods()

private function _invokeAfterMethods()
{
foreach ($this->_currentController->after as $method) {
$this->_currentController->$method();
foreach ($this->_currentControllerObject->after as $method) {
$this->_currentControllerObject->$method();
}
}

private function _invokeAction()
{
$currentAction = $this->_currentController->currentAction;
if (method_exists($this->_currentController, $currentAction)) {
$this->_currentController->$currentAction();
$currentAction = $this->_currentControllerObject->currentAction;
if (method_exists($this->_currentControllerObject, $currentAction)) {
$this->_currentControllerObject->$currentAction();
} else {
throw new FrontControllerException('No action [' . $currentAction . '] defined in controller [' . get_class($this->_currentController) . '].');
throw new FrontControllerException('No action [' . $currentAction . '] defined in controller [' . get_class($this->_currentControllerObject) . '].');
}
}

private function _doActionOnResponse()
{
$controller = $this->_currentController;
$controller = $this->_currentControllerObject;
switch ($controller->getStatusResponse()) {
case 'show':
$controller->display();
Expand Down Expand Up @@ -156,12 +157,12 @@ private function _showOutputBuffer()
private function _logRequest()
{
self::$userId = isset($_SESSION['id_user_ses']) ? $_SESSION['id_user_ses'] : null;
Logger::getLogger(__CLASS__)->info('[Request:/%s/%s]', array($this->_uri->getRawController(), $this->_uriAction));
Logger::getLogger(__CLASS__)->info('[Request:/%s/%s]', array($this->_currentController, $this->_currentAction));
}

private function _isRedirect()
{
return in_array($this->_currentController->getStatusResponse(), array('redirect', 'redirectOld'));
return in_array($this->_currentControllerObject->getStatusResponse(), array('redirect', 'redirectOld'));
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Ouzo/Routing/RouteRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ private function _isEqualOrAnyMethod()

private function _matches($uri)
{
preg_match('#/.+?/(.+?)(/|$)#', $uri, $matches);
if ($this->isInExceptActions(Arrays::getValue($matches, 1, ''))) {
return false;
}
if ($this->getUri() == $uri) {
return true;
}
Expand Down
22 changes: 12 additions & 10 deletions lib/Ouzo/Tests/ControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Ouzo\Tests;

use Ouzo\Config;
use Ouzo\FrontController;

class ControllerTestCase extends DbTransactionalTestCase
{
Expand All @@ -20,7 +21,7 @@ public function __construct()
$this->_sessionInitializer = new MockSessionInitializer();
$this->_downloadHandler = new MockDownloadHandler();

$this->_frontController = new \Ouzo\FrontController();
$this->_frontController = new FrontController();
$this->_frontController->redirectHandler = $this->_redirectHandler;
$this->_frontController->sessionInitializer = $this->_sessionInitializer;
$this->_frontController->downloadHandler = $this->_downloadHandler;
Expand All @@ -30,6 +31,7 @@ public function __construct()
public function get($url)
{
$_SERVER['REQUEST_URI'] = $this->_prefixSystem . $url;
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET = $this->_parseUrlParams($_SERVER['REQUEST_URI']);

$this->_initFrontController();
Expand Down Expand Up @@ -77,17 +79,17 @@ private function _removePrefix($string)

public function assertRenders($string)
{
$statusResponse = $this->_frontController->getCurrentController()->getStatusResponse();
$location = $this->_frontController->getCurrentController()->getRedirectLocation();
$statusResponse = $this->_frontController->getCurrentControllerObject()->getStatusResponse();
$location = $this->_frontController->getCurrentControllerObject()->getRedirectLocation();
if ($statusResponse != 'show') {
$this->fail("Expected render $string but was $statusResponse $location");
}
$this->assertEquals($string, $this->_frontController->getCurrentController()->view->getViewName());
$this->assertEquals($string, $this->_frontController->getCurrentControllerObject()->view->getViewName());
}

public function assertAssignsModel($variable, $modelObject)
{
$modelVariable = $this->_frontController->getCurrentController()->view->$variable;
$modelVariable = $this->_frontController->getCurrentControllerObject()->view->$variable;
$this->assertNotNull($modelVariable);
$this->assertEquals($modelObject->attributes(), $modelVariable->attributes());
}
Expand All @@ -99,13 +101,13 @@ public function assertDownloadFile($file)

public function assertAssignsValue($variable, $value)
{
$this->assertNotNull($this->_frontController->getCurrentController()->view->$variable);
$this->assertEquals($value, $this->_frontController->getCurrentController()->view->$variable);
$this->assertNotNull($this->_frontController->getCurrentControllerObject()->view->$variable);
$this->assertEquals($value, $this->_frontController->getCurrentControllerObject()->view->$variable);
}

public function assertRendersContent($content)
{
$this->assertEquals($content, $this->_frontController->getCurrentController()->layout->layoutContent());
$this->assertEquals($content, $this->_frontController->getCurrentControllerObject()->layout->layoutContent());
}

public function assertRenderedJsonAttributeEquals($attribute, $equals)
Expand All @@ -116,7 +118,7 @@ public function assertRenderedJsonAttributeEquals($attribute, $equals)

public function getAssigned($name)
{
return $this->_frontController->getCurrentController()->view->$name;
return $this->_frontController->getCurrentControllerObject()->view->$name;
}

public function assertAttributesEquals($expected, $actual)
Expand All @@ -126,6 +128,6 @@ public function assertAttributesEquals($expected, $actual)

public function getRenderedJsonAsArray()
{
return json_decode($this->_frontController->getCurrentController()->layout->layoutContent(), true);
return json_decode($this->_frontController->getCurrentControllerObject()->layout->layoutContent(), true);
}
}
11 changes: 11 additions & 0 deletions lib/Ouzo/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ private function _parseParams($params)
return $paramsArray;
}

public function getPath()
{
return $this->_pathProvider->getPath();
}

public function getPathWithoutPrefix()
{
$defaults = Config::getValue('global');
return Strings::removePrefix($this->getPath(), $defaults['prefix_system']);
}

public function getParam($param)
{
$params = $this->getParams();
Expand Down
7 changes: 6 additions & 1 deletion test/lib/Ouzo/BeforeFilterTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
use Ouzo\Controller;
use Ouzo\Routing\Route;
use Ouzo\Tests\ControllerTestCase;

class SampleControllerException extends Exception
Expand Down Expand Up @@ -31,7 +32,7 @@ public function action()

class MockControllerResolver
{
public function getCurrentController()
public function getController()
{
return new SampleController();
}
Expand All @@ -44,13 +45,17 @@ public function setUp()
parent::setUp();
$this->_frontController->controllerResolver = new MockControllerResolver();
$this->_frontController->redirectHandler = $this->getMock('\Ouzo\RedirectHandler', array('redirect'));
Route::$routes = array();
}

/**
* @test
*/
public function shouldNotInvokeActionWhenBeforeFilterReturnFalse()
{
//given
Route::any('/sample/action', 'sample#action');

//when
try {
$this->get('/sample/action');
Expand Down
20 changes: 1 addition & 19 deletions test/lib/Ouzo/ControllerResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,9 @@ public function shouldResolveAction()
$_SERVER['REQUEST_URI'] = "{$config['prefix_system']}/simple_test/action1";

//when
$currentController = $controllerResolver->getCurrentController();
$currentController = $controllerResolver->getController('simple_test', 'action1');

//then
$this->assertEquals('action1', $currentController->currentAction);
}

/**
* @test
*/
public function shouldUseDefaultActionIfNoActionInUrl()
{
//given
$controllerResolver = new ControllerResolver('\\Ouzo\\');

$config = Config::getValue('global');
$_SERVER['REQUEST_URI'] = "{$config['prefix_system']}/simple_test";

//when
$currentController = $controllerResolver->getCurrentController();

//then
$this->assertEquals($config['action'], $currentController->currentAction);
}
}
Loading

0 comments on commit f445361

Please sign in to comment.