Skip to content

Commit

Permalink
Added $app->getControllerName
Browse files Browse the repository at this point in the history
  • Loading branch information
AliasIO committed May 4, 2013
1 parent b4c024a commit 348ed56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
42 changes: 25 additions & 17 deletions Swiftlet/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
class App implements Interfaces\App
{
protected
$action = 'index',
$args = array(),
$config = array(),
$action = 'index',
$args = array(),
$config = array(),
$controller,
$hooks = array(),
$plugins = array(),
$rootPath = '/',
$singletons = array(),
$controllerName = 'index',
$hooks = array(),
$plugins = array(),
$rootPath = '/',
$singletons = array(),
$view
;

Expand All @@ -38,34 +39,32 @@ public function run()
}

// Extract controller name, view name, action name and arguments from URL
$controllerName = 'Index';

if ( !empty($_GET['q']) ) {
$this->args = explode('/', $_GET['q']);

if ( $this->args ) {
$controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args)))));
$this->controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args)))));
}

if ( $action = $this->args ? array_shift($this->args) : '' ) {
$this->action = str_replace('-', '', $action);
}
}

if ( !is_file('Swiftlet/Controllers/' . $controllerName . '.php') ) {
$controllerName .= '/Index';
if ( !is_file('Swiftlet/Controllers/' . $this->controllerName . '.php') ) {
$this->controllerName .= '/Index';

if ( !is_file('Swiftlet/Controllers/' . $controllerName . '.php') ) {
$controllerName = 'Error404';
if ( !is_file('Swiftlet/Controllers/' . $this->controllerName . '.php') ) {
$this->controllerName = 'Error404';
}
}

$this->view = new View($this, strtolower($controllerName));
$this->view = new View($this, strtolower($this->controllerName));

// Instantiate the controller
$controllerName = 'Swiftlet\Controllers\\' . str_replace('/', '\\', $controllerName);
$controller = 'Swiftlet\Controllers\\' . str_replace('/', '\\', $this->controllerName);

$this->controller = new $controllerName($this, $this->view);
$this->controller = new $controller($this, $this->view);

// Load plugins
if ( $handle = opendir('Swiftlet/Plugins') ) {
Expand Down Expand Up @@ -149,6 +148,15 @@ public function getRootPath()
return $this->rootPath;
}

/**
* Get the controller name
* @return string
*/
public function getControllerName()
{
return $this->controllerName;
}

/**
* Get the action name
* @return string
Expand Down
10 changes: 10 additions & 0 deletions tests/Swiftlet/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function testRun()
$this->assertInstanceOf('Swiftlet\Interfaces\Controller', $controller);
}

/**
* @covers Swiftlet\App::getControllerName
*/
public function testGetControllerName()
{
$controllerName = $this->app->getControllerName();

$this->assertEquals($controllerName, 'Index');
}

/**
* @covers Swiftlet\App::getAction
*/
Expand Down

0 comments on commit 348ed56

Please sign in to comment.