Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
fixes #17 : now, everything is encapsulated into a correct namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Taluu committed Sep 9, 2011
1 parent 78e4a01 commit a02e55d
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 68 deletions.
8 changes: 4 additions & 4 deletions apps/error/controller.php
Expand Up @@ -9,15 +9,15 @@
* @version $Id$
*/

namespace Controller\Sub;
namespace Tamed\Example\Controller;

use \Controller\Front;
use \Http\Response;
use \Tamed\Controller\Front;
use \Tamed\Http\Response;

/**
* Error Controller
*
* @package tamed.controller.sub
* @package tamedexample.example.controller
* @author Baptiste "Talus" Clavié <clavie.b@gmail.com>
*/
class Error extends Front {
Expand Down
6 changes: 3 additions & 3 deletions apps/home/controller.php
Expand Up @@ -9,14 +9,14 @@
* @version $Id$
*/

namespace Controller\Sub;
namespace Tamed\Example\Controller;

use \Controller\Front;
use \Tamed\Controller\Front;

/**
* Home Controller
*
* @package tamed.controller.sub
* @package tamedexampled.controller.sub
* @author Baptiste "Talus" Clavié <clavie.b@gmail.com>
*/
class Home extends Front {
Expand Down
6 changes: 3 additions & 3 deletions conf/routes.json
@@ -1,14 +1,14 @@
{
"_isConst": true,

"404" : {
"controller" : "error",
"controller" : "\\Tamed\\Example\\Controller\\Error",
"action" : "notfound_404",
"pattern" : "/error/notfound_404"
},

"homepage" : {
"controller" : "home",
"controller" : "\\Tamed\\Example\\Controller\\Home",
"action" : "index",
"pattern" : "/"
}
Expand Down
2 changes: 1 addition & 1 deletion libs/Configuration/Config.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace Configuration;
namespace Tamed\Configuration;

/**
* Definition of Config class
Expand Down
2 changes: 1 addition & 1 deletion libs/Configuration/Loader.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace Configuration;
namespace Tamed\Configuration;

/**
* Definition of the Loader Class
Expand Down
37 changes: 17 additions & 20 deletions libs/Controller/Front.php
Expand Up @@ -9,12 +9,12 @@
* @version $Id$
*/

namespace Controller;
namespace Tamed\Controller;

use \Http\Response, Http\Request;
use \View\Bridge;
use \Configuration\Loader;
use \Obj, \Debug, \Exception;
use Tamed\Http\Response, Tamed\Http\Request;
use Tamed\View\Bridge;
use Tamed\Configuration\Loader;
use Tamed\Debug, Tamed\Obj;

/**
* Main Controller
Expand Down Expand Up @@ -160,6 +160,8 @@ final private function _main($_action) {
/**
* Prepare the page, matching a route if any
*
* Note that the controller must be in its full qualified name form
*
* @param self $_controller Controller to be used. null if it has to be guessed.
* @param array $options Options array (to define new http handlers, view bridges, ...)
* @return self Matched controller
Expand All @@ -172,7 +174,7 @@ final public static function getController(self $_controller = null, array $_opt
$options = \array_replace(array(
'request' => new Request,
'response' => new Response,
'view' => new \View\PHP
'view' => new \Tamed\View\PHP
), $_options);

Debug::info('Routing');
Expand All @@ -183,19 +185,16 @@ final public static function getController(self $_controller = null, array $_opt
}

\spl_autoload_register(function($controller) {
// -- Verifying that this is a controller : it must have a \Controller\Sub\Name form
// -- Verifying that this is a controller : it must have a \...\Controller\Name form
$parts = \array_values(\array_filter(\explode('\\', $controller)));
$count = count($parts);

if (\count($parts) !== 3) {
return false;
}

if ($parts[0] !== __NAMESPACE__ || $parts[1] !== 'Sub') {
if ($parts[$count - 2] !== 'Controller') {
return false;
}

$controller = \mb_convert_case($parts[2], \MB_CASE_LOWER);
$file = \sprintf('%1$s/../../apps/%2$s/controller.%3$s', __DIR__, $controller, \PHP_EXT);
$file = \sprintf('%1$s/../../apps/%2$s/controller.%3$s', __DIR__, $parts[$count - 1], \PHP_EXT);

if (\is_file($file)) {
require $file;
Expand All @@ -205,27 +204,25 @@ final public static function getController(self $_controller = null, array $_opt
return false;
});

$_controller = \mb_convert_case($route->controller, \MB_CASE_TITLE);
$_controller = __NAMESPACE__ . '\Sub\\' . $_controller;

Debug::info('Trying to start the subcontroller %1$s', $_controller);
return new $_controller($options['request'], $options['response'], $options['view']);
return new $route->controller($options['request'], $options['response'], $options['view']);
}

/**
* Forwards to a new controller and a new action
*
* Note : the controller should be in in full qualified form (namespaces included)
*
* @param string $_controller Controller's name
* @param string $_action Action's name
*/
final protected function _forward($_controller, $_action) {
Debug::info('Forwarding to "%1$s->%2$s"', $_controller, $_action);
$controller = __NAMESPACE__ . '\Sub\\' . \mb_convert_case($_controller, \MB_CASE_TITLE);

/**
* @var \Controller\Front
* @var Front
*/
$controller = new $controller($this->_request, $this->_response, $this->_view);
$controller = new $_controller($this->_request, $this->_response, $this->_view);

$controller->_isForwarded = true;
$controller->_main($_action);
Expand Down
22 changes: 12 additions & 10 deletions libs/Debug.php
Expand Up @@ -9,6 +9,8 @@
* @version $Id$
*/

namespace Tamed;

if (!defined('SAFE')) exit;

/**
Expand Down Expand Up @@ -46,10 +48,10 @@ public static function log($msg, $level = self::LEVEL_INFO) {

$command = 'null';

if (\Obj::$router->hasStarted()) {
$command = \Obj::$router->get('command') ?: '/';
$command .= ' controller:' . \Obj::$router->get('controller');
$command .= ' action:' . \Obj::$router->get('action');
if (Obj::$router->hasStarted()) {
$command = Obj::$router->get('command') ?: '/';
$command .= ' controller:' . Obj::$router->get('controller');
$command .= ' action:' . Obj::$router->get('action');
}

// -- $db[0] is this function... And we have no interest in it, do we ?
Expand All @@ -72,11 +74,11 @@ public static function log($msg, $level = self::LEVEL_INFO) {
}

/**
* Shortcut for \Debug::log($msg, self::LEVEL_INFO)
* Shortcut for Debug::log($msg, self::LEVEL_INFO)
*
* @param string $msg message to be written
* @return Returns TRUE on success or FALSE on failure.
* @see \Debug::log();
* @see Debug::log();
*/
public static function info($msg) {
if (func_num_args() > 1) {
Expand All @@ -89,11 +91,11 @@ public static function info($msg) {
}

/**
* Shortcut for \Debug::log($msg, self::LEVEL_WARNING)
* Shortcut for Debug::log($msg, self::LEVEL_WARNING)
*
* @param string $msg message to be written
* @return Returns TRUE on success or FALSE on failure.
* @see \Debug::log();
* @see Debug::log();
*/
public static function warning($msg) {
if (func_num_args() > 1) {
Expand All @@ -107,11 +109,11 @@ public static function warning($msg) {
}

/**
* Shortcut for \Debug::log($msg, self::LEVEL_FATAL)
* Shortcut for Debug::log($msg, self::LEVEL_FATAL)
*
* @param string $msg message to be written
* @return Returns TRUE on success or FALSE on failure.
* @see \Debug::log();
* @see Debug::log();
*/
public static function fatal($msg) {
if (func_num_args() > 1) {
Expand Down
4 changes: 2 additions & 2 deletions libs/Http/Cookie.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace Http;
namespace Tamed\Http;

/**
* Represents a Cookie
Expand Down Expand Up @@ -48,7 +48,7 @@ class Cookie extends Header {
function __construct($_name, $_value, $_timeout = 0, $_path = './', $_domain = null, $_secure = false, $_httpOnly = false) {
if (!filter_var ($_timeout, FILTER_VALIDATE_INT)) {
if (!$_timeout instanceof \DateTime) {
$_timeout = new DateTime($_timeout);
$_timeout = new \DateTime($_timeout);
}

$_timeout = $_timeout->format('U');
Expand Down
6 changes: 4 additions & 2 deletions libs/Http/Header.php
Expand Up @@ -9,7 +9,9 @@
* @version $Id$
*/

namespace Http;
namespace Tamed\Http;

use \Tamed\Debug;

/**
* Represents a Header
Expand Down Expand Up @@ -53,7 +55,7 @@ public function send($force = false) {
$header = $this->_header;
}

\Debug::info('Sending header (%1$s, code %2$d)', $header, $this->_status);
Debug::info('Sending header (%1$s, code %2$d)', $header, $this->_status);
\header($header, $this->_replace, $this->_status);

$this->_sent = true;
Expand Down
2 changes: 1 addition & 1 deletion libs/Http/Request.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace Http;
namespace Tamed\Http;

/**
* Definition of the HttpRequest class
Expand Down
6 changes: 3 additions & 3 deletions libs/Http/Response.php
Expand Up @@ -9,9 +9,9 @@
* @version $Id$
*/

namespace Http;
namespace Tamed\Http;

use \View\Bridge;
use \Tamed\View\Bridge;

/**
* Definition of the HttpResponse class
Expand Down Expand Up @@ -133,7 +133,7 @@ public function redirect($url, $time = 0, $code = self::FOUND) {
$value = $time . ';url=' . $url;
}

\Debug::info('Redirecting...');
Debug::info('Redirecting...');
$this->_redirect = true;
$this->status($code);
$this->header($header, $value, true, $code);
Expand Down
2 changes: 2 additions & 0 deletions libs/Obj.php
Expand Up @@ -9,6 +9,8 @@
* @version $Id$
*/

namespace Tamed;

if (!defined('SAFE')) exit;

/**
Expand Down
2 changes: 1 addition & 1 deletion libs/Routing/Route.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace Routing;
namespace Tamed\Routing;

if (!defined('SAFE')) exit;

Expand Down
8 changes: 5 additions & 3 deletions libs/Routing/Router.php
Expand Up @@ -9,10 +9,12 @@
* @version $Id$
*/

namespace Routing;
namespace Tamed\Routing;

if (!defined('SAFE')) exit;

use Tamed\Debug;

/**
* Definition of the Router Class
*
Expand Down Expand Up @@ -54,7 +56,7 @@ public function route($_requestURI) {
*/
foreach ($this->_routes as $name => &$route) {
if ($route->match($_requestURI)) {
\Debug::info('Route %s matched', $name);
Debug::info('Route %s matched', $name);
$this->_matchedRoute = $route;
break;
}
Expand All @@ -65,7 +67,7 @@ public function route($_requestURI) {
throw new \Exception('No route to match');
}

\Debug::info('No route found : using 404');
Debug::info('No route found : using 404');
$this->_matchedRoute = $this->_routes['404'];
}

Expand Down
2 changes: 1 addition & 1 deletion libs/View/Bridge.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace View;
namespace Tamed\View;

/**
* Definition of the bridge between the framework & the selected view engine
Expand Down
2 changes: 1 addition & 1 deletion libs/View/PHP.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace View;
namespace Tamed\View;

/**
* Definition of a view written in PHP syntax
Expand Down
2 changes: 1 addition & 1 deletion libs/View/Talus_TPL.php
Expand Up @@ -9,7 +9,7 @@
* @version $Id$
*/

namespace View;
namespace Tamed\View;

require __DIR__ . '/../Vendor/Talus-TPL/lib/Talus_TPL/Main.php';

Expand Down

0 comments on commit a02e55d

Please sign in to comment.