Osaris wrote:
> Thanks Loïc, maybe you can put a non-strategic example of your
> middleware. For the database part, you use a different database for
> each of the user account or your database schema contains a relation
> to the user in each table ? I'm more a Mysql/SQlite guy.
A bit edited... but basically I get the forge from the url and then I
set $request->forge = $theforge;
Then in all the views I can access $request->forge to act on it.
It does not look very nice because I am accessing the $GLOBALS directly,
but this is the most efficient way to do it and it is the only place
where I am doing it.
The schema stuff from PostgreSQL allows me to write the application as
if I had only one forge and split at the database level. A postgreSQL
schema is a bit like a database within a database. You can also add a
relation to the user/forge table in each object if you build your
application as being SaaS from the start (it was not my case, this is why).
loïc
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/**
* Forge middleware.
*
* Switch to the right forge based on the url.
*/
class IDFA_Middleware_SetForge
{
/**
* Process the request.
*
* When processing the request, check the hostname and set the
* $request->forge corresponding to the forge. If no
* forge are found simply redirect to the indefero website.
*
* @param Pluf_HTTP_Request The request
* @return bool false or redirect.
*/
function process_request(&$request)
{
$sql = new Pluf_SQL('status=1 AND (domain=%s OR custdomain=%s)',
array($request->http_host,
$request->http_host));
$forges = Pluf::factory('IDFA_Forge')->getList(array('filter' =>
$sql->gen()));
if ($forges->count() != 1) {
return new
Pluf_HTTP_Response_Redirect('
http://www.indefero.net/');
}
if ($forges[0]->allowssl == false and !empty($_SERVER['HTTPS'])) {
return new
Pluf_HTTP_Response_Redirect('http://'.$request->http_host);
}
IDFA_Middleware_SetForge_set($request, $forges[0]);
return false;
}
/**
* Process the response of a view.
*
* Reset the search path.
*
* @param Pluf_HTTP_Request The request
* @param Pluf_HTTP_Response The response
* @return Pluf_HTTP_Response The response
*/
function process_response($request, $response)
{
IDFA_Middleware_SetForge_back();
return $response;
}
}
function IDFA_Middleware_SetForge_set(&$request, $forge)
{
// Dynamic modification of the configuration.
$GLOBALS['_PX_config_old'] = $GLOBALS['_PX_config'];
// Update configuration variables (dynamic modification of
// $GLOBALS['_PX_config'];
IDFA_Middleware_SetForge_updateConf($forge);
$request->forge = $forge; // Here I set the forge to track it
$db = &Pluf::db();
$db->setSearchPath($forge->db_schema); // Here I set the search path
}
function IDFA_Middleware_SetForge_back()
{
$db = &Pluf::db();
$db->setSearchPath();
$GLOBALS['_PX_config'] = $GLOBALS['_PX_config_old'];
unset($GLOBALS['_PX_config_old']);
}
/**
* Directly modify the $GLOBALS array to set the configuration
* variables for this forge.
*
* @param IDFA_Forge
*/
function IDFA_Middleware_SetForge_updateConf($forge)
{
$GLOBALS['_PX_config']['git_repositories'] = ...