Refactor Appbox

This commit is contained in:
Romain Neutron
2012-02-21 16:06:01 +01:00
parent 375fd3d493
commit e576989c7f
15 changed files with 90 additions and 114 deletions

View File

@@ -45,6 +45,7 @@ class appbox extends base
protected $cache;
protected $connection;
protected $registry;
protected $Core;
const CACHE_LIST_BASES = 'list_bases';
const CACHE_SBAS_IDS = 'sbas_ids';
@@ -54,11 +55,11 @@ class appbox extends base
*
* @return appbox
*/
public static function get_instance(registryInterface &$registry = null)
public static function get_instance(\Alchemy\Phrasea\Core $Core, registryInterface &$registry = null)
{
if (!self::$_instance instanceof self)
{
self::$_instance = new self($registry);
self::$_instance = new self($Core, $registry);
}
return self::$_instance;
@@ -69,11 +70,12 @@ class appbox extends base
*
* @return appbox
*/
protected function __construct(registryInterface $registry = null)
protected function __construct(\Alchemy\Phrasea\Core $Core, registryInterface $registry = null)
{
$this->connection = connection::getPDOConnection();
$this->Core = $Core;
if (!$registry)
$registry = registry::get_instance();
$registry = registry::get_instance($Core);
$this->connection = connection::getPDOConnection(null, $registry);
$this->registry = $registry;
$this->session = Session_Handler::getInstance($this);
@@ -283,7 +285,7 @@ class appbox extends base
$stmt->execute(array(':viewname' => $viewname, ':sbas_id' => $databox->get_sbas_id()));
$stmt->closeCursor();
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES);
cache_databox::update($databox->get_sbas_id(), 'structure');

View File

@@ -161,15 +161,12 @@ abstract class base implements cache_cacheableInterface
return $this->connection;
}
/**
*
* @return cache_adapter
*/
public function get_cache()
{
if (!$this->cache)
$this->cache = cache_adapter::get_instance($this->get_registry());
{
$this->cache = $this->Core->getCache();
}
return $this->cache;
}
@@ -191,7 +188,7 @@ abstract class base implements cache_cacheableInterface
public function set_data_to_cache($value, $option = null, $duration = 0)
{
return $this->get_cache()->set($this->get_cache_key($option), $value, $duration);
return $this->get_cache()->save($this->get_cache_key($option), $value, $duration);
}
public function delete_data_from_cache($option = null)

View File

@@ -44,7 +44,7 @@ class bootstrap
if (\setup::is_installed())
{
$gatekeeper = \gatekeeper::getInstance();
$gatekeeper = \gatekeeper::getInstance(static::$core);
$gatekeeper->check_directory();
}

View File

@@ -126,7 +126,7 @@ class collection implements cache_cacheableInterface
$this->is_active = true;
$this->delete_data_from_cache();
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES);
$this->databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
cache_databox::update($this->databox->get_sbas_id(), 'structure');
@@ -142,7 +142,7 @@ class collection implements cache_cacheableInterface
$stmt->closeCursor();
$this->is_active = false;
$this->delete_data_from_cache();
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES);
$this->databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
cache_databox::update($this->databox->get_sbas_id(), 'structure');
@@ -314,7 +314,7 @@ class collection implements cache_cacheableInterface
$stmt->execute(array(':coll_id' => $this->get_coll_id()));
$stmt->closeCursor();
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$sql = "DELETE FROM bas WHERE base_id = :base_id";
$stmt = $appbox->get_connection()->prepare($sql);
@@ -481,7 +481,7 @@ class collection implements cache_cacheableInterface
$conn = $appbox->get_connection();
$new_bas = false;
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$prefs = '<?xml version="1.0" encoding="UTF-8"?>
@@ -555,7 +555,7 @@ class collection implements cache_cacheableInterface
public static function mount_collection($sbas_id, $coll_id, User_Adapter $user)
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$sql = "INSERT INTO bas (base_id, active, server_coll_id, sbas_id, aliases)

View File

@@ -109,7 +109,7 @@ class connection
* @param string $name
* @return connection_pdo
*/
public static function getPDOConnection($name = null)
public static function getPDOConnection($name = null, registryInterface $registry = null)
{
self::instantiate();
if (trim($name) == '')
@@ -166,7 +166,7 @@ class connection
try
{
self::$_PDO_instance[$name] = new connection_pdo($name, $hostname, $port, $user, $password, $dbname);
self::$_PDO_instance[$name] = new connection_pdo($name, $hostname, $port, $user, $password, $dbname, array(), $registry);
self::$_PDO_instance[$name]->query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
}
catch (Exception $e)

View File

@@ -113,7 +113,7 @@ class databox extends base
{
$this->registry = registry::get_instance();
$this->connection = connection::getPDOConnection($sbas_id);
$this->cache = cache_adapter::get_instance($this->registry);
$this->Core = \bootstrap::getCore();
$this->id = $sbas_id;
$connection_params = phrasea::sbas_params();
@@ -518,7 +518,7 @@ class databox extends base
public static function mount(appbox $appbox, $host, $port, $user, $password, $dbname, registry $registry)
{
$name = 'test';
$connection = new connection_pdo($name, $host, $port, $user, $password, $dbname);
$connection = new connection_pdo($name, $host, $port, $user, $password, $dbname, array(), $registry);
$conn = $appbox->get_connection();
$sql = 'SELECT MAX(ord) as ord FROM sbas';
@@ -668,7 +668,7 @@ class databox extends base
public function delete()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$sql = 'DROP DATABASE `' . $this->get_dbname() . '`';
$stmt = $this->get_connection()->prepare($sql);
@@ -859,7 +859,7 @@ class databox extends base
$this->meta_struct = null;
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES);
$this->delete_data_from_cache(self::CACHE_STRUCTURE);
$this->delete_data_from_cache(self::CACHE_META_STRUCT);

View File

@@ -41,15 +41,16 @@ class gatekeeper
* @var gatekeeper
*/
protected static $_instance;
protected $Core;
/**
*
* @return gatekeeper
*/
public static function getInstance()
public static function getInstance(\Alchemy\Phrasea\Core $Core)
{
if (!(self::$_instance instanceof self))
self::$_instance = new self();
self::$_instance = new self($Core);
return self::$_instance;
}
@@ -58,8 +59,10 @@ class gatekeeper
*
* @return gatekeeper
*/
function __construct()
function __construct(\Alchemy\Phrasea\Core $Core)
{
$this->Core = $Core;
return $this;
}
@@ -72,11 +75,10 @@ class gatekeeper
function check_directory()
{
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$appbox = appbox::get_instance();
$appbox = appbox::get_instance($this->Core);
$session = $appbox->get_session();
if (http_request::is_command_line())
return;
if (isset($_SERVER['PHP_SELF']) && trim($_SERVER['PHP_SELF']))
@@ -129,7 +131,6 @@ class gatekeeper
if ($this->_PHP_SELF == '/thesaurus2/xmlhttp/getterm.x.php'
|| $this->_PHP_SELF == '/thesaurus2/xmlhttp/searchcandidate.x.php'
|| $this->_PHP_SELF == '/thesaurus2/xmlhttp/getsy.x.php')
return;
phrasea::redirect('/login/?redirect=/thesaurus2');
break;
@@ -138,7 +139,6 @@ class gatekeeper
break;
case 'admin':
if ($this->_script_name === 'runscheduler.php')
return;
phrasea::redirect('/login/?redirect=' . $_SERVER['REQUEST_URI']);
break;
@@ -158,7 +158,6 @@ class gatekeeper
return;
case 'setup':
if ($appbox->upgradeavailable())
return;
else
phrasea::redirect('/login/');
@@ -237,7 +236,7 @@ class gatekeeper
*/
protected function give_guest_access()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance($this->Core);
$request = http_request::getInstance();
$session = $appbox->get_session();
@@ -269,13 +268,12 @@ class gatekeeper
*/
protected function token_access()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance($this->Core);
$request = new http_request();
$session = $appbox->get_session();
$parm = $request->get_parms('LOG');
if (is_null($parm["LOG"]))
return $this;
try
@@ -312,7 +310,7 @@ class gatekeeper
*/
public function require_session()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance($this->Core);
$session = $appbox->get_session();
if ($session->is_authenticated())
{

View File

@@ -25,7 +25,7 @@ class lazaret extends set_abstract
*/
function __construct()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
$conn = $appbox->get_connection();

View File

@@ -147,7 +147,7 @@ class lazaretFile
public static function move_uploaded_to_lazaret(
system_file $system_file, $base_id, $filename, $errors='', $status=false)
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
$conn = $appbox->get_connection();

View File

@@ -5,7 +5,7 @@ class liste
public static function filter($lst)
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
if (!is_array($lst))

View File

@@ -65,7 +65,7 @@ class p4file
public static function archiveFile(system_file &$system_file, $base_id, $delete=true, $name=false)
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();

View File

@@ -131,7 +131,7 @@ class phrasea
function getHome($type='PUBLI', $context='prod')
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
$user = User_Adapter::getInstance($session->get_usr_id(), $appbox);
@@ -229,7 +229,7 @@ class phrasea
public static function clear_sbas_params()
{
self::$_sbas_params = null;
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->delete_data_from_cache(self::CACHE_SBAS_PARAMS);
return true;
@@ -241,7 +241,7 @@ class phrasea
return self::$_sbas_params;
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
try
{
self::$_sbas_params = $appbox->get_data_from_cache(self::CACHE_SBAS_PARAMS);
@@ -277,7 +277,7 @@ class phrasea
if (!$usr_id)
return false;
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$user = User_Adapter::getInstance($usr_id, $appbox);
return count($user->ACL()->get_granted_base()) > 0;
@@ -329,7 +329,7 @@ class phrasea
{
if (!self::$_bas2sbas)
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
try
{
self::$_bas2sbas = $appbox->get_data_from_cache(self::CACHE_SBAS_FROM_BAS);
@@ -379,7 +379,7 @@ class phrasea
public static function reset_baseDatas()
{
self::$_coll2bas = self::$_bas2coll = self::$_bas_names = self::$_bas2sbas = null;
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->delete_data_from_cache(
array(
self::CACHE_BAS_2_COLL
@@ -395,7 +395,7 @@ class phrasea
public static function reset_sbasDatas()
{
self::$_sbas_names = self::$_sbas_params = self::$_bas2sbas = null;
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$appbox->delete_data_from_cache(
array(
self::CACHE_SBAS_NAMES
@@ -431,7 +431,7 @@ class phrasea
{
if (!self::$_sbas_names)
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
try
{
self::$_sbas_names = $appbox->get_data_from_cache(self::CACHE_SBAS_NAMES);
@@ -460,7 +460,7 @@ class phrasea
{
if (!self::$_bas_names)
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
try
{
self::$_bas_names = $appbox->get_data_from_cache(self::CACHE_BAS_NAMES);

View File

@@ -20,7 +20,7 @@ class queries
public static function tree_topics()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
@@ -71,7 +71,7 @@ class queries
public static function topics_exists()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
@@ -88,7 +88,7 @@ class queries
public static function dropdown_topics()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
@@ -221,7 +221,7 @@ class queries
public static function history()
{
$appbox = appbox::get_instance();
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$conn = $appbox->get_connection();

View File

@@ -20,7 +20,7 @@ class registry implements registryInterface
/**
*
* @var cache_opcode_adapter
* @var \Alchemy\Phrasea\Cache\Cache
*/
protected $cache;
@@ -42,21 +42,22 @@ class registry implements registryInterface
*/
public static function get_instance()
{
$prefix = crc32(__DIR__);
if (!self::$_instance instanceof self)
self::$_instance = new self(new cache_opcode_adapter($prefix));
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
*
* @param cache_opcode_interface $cache
* @param \Alchemy\Phrasea\Cache\Cache $cache
* @return registry
*/
protected function __construct(cache_opcode_interface $cache)
protected function __construct()
{
$this->cache = $cache;
$this->cache = new Alchemy\Phrasea\Cache\ArrayCache();
$handler = new \Alchemy\Phrasea\Core\Configuration\Handler(
new \Alchemy\Phrasea\Core\Configuration\Application(),
@@ -64,12 +65,12 @@ class registry implements registryInterface
);
$configuration = new \Alchemy\Phrasea\Core\Configuration($handler);
$this->cache->set('GV_RootPath', dirname(dirname(__DIR__)) . '/');
$this->cache->save('GV_RootPath', dirname(dirname(__DIR__)) . '/');
if ($configuration->isInstalled())
{
$this->cache->set('GV_ServerName', $configuration->getPhraseanet()->get('servername'));
$this->cache->set('GV_debug', $configuration->isDebug());
$this->cache->set('GV_maintenance', $configuration->isMaintained());
$this->cache->save('GV_ServerName', $configuration->getPhraseanet()->get('servername'));
$this->cache->save('GV_debug', $configuration->isDebug());
$this->cache->save('GV_maintenance', $configuration->isMaintained());
}
return $this;
@@ -81,7 +82,7 @@ class registry implements registryInterface
*/
protected function load()
{
if ($this->cache->get('registry_loaded') !== true)
if ($this->cache->fetch('registry_loaded') !== true)
{
$rs = array();
$loaded = false;
@@ -119,10 +120,10 @@ class registry implements registryInterface
break;
}
$this->cache->set($row['key'], $value);
$this->cache->save($row['key'], $value);
}
if ($loaded === true)
$this->cache->set('registry_loaded', true);
$this->cache->save('registry_loaded', true);
}
@@ -136,15 +137,15 @@ class registry implements registryInterface
*/
public function get($key, $defaultvalue = null)
{
if (!$this->cache->is_set($key))
if (!$this->cache->contains($key))
$this->load();
if (!$this->cache->is_set($key) && !is_null($defaultvalue))
if (!$this->cache->contains($key) && !is_null($defaultvalue))
return $defaultvalue;
else
return $this->cache->get($key);
return $this->cache->fetch($key);
}
/**
@@ -156,14 +157,6 @@ class registry implements registryInterface
public function set($key, $value, $type)
{
$this->load();
$delete_cache = false;
if ($key === 'GV_cache_server_type')
{
$current_cache = $this->get('GV_cache_server_type');
if ($current_cache !== $value)
$delete_cache = true;
}
switch ($type)
{
@@ -195,13 +188,7 @@ class registry implements registryInterface
$stmt->execute(array(':key' => $key, ':value' => $sql_value, ':type' => $type));
$stmt->closeCursor();
$this->cache->set($key, $value);
if ($delete_cache === true)
{
$cache = cache_adapter::get_instance($this);
$cache->flush();
}
$this->cache->save($key, $value);
return $this;
}
@@ -215,7 +202,7 @@ class registry implements registryInterface
{
$this->load();
return $this->cache->is_set($key);
return $this->cache->contains($key);
}
/**
@@ -233,7 +220,7 @@ class registry implements registryInterface
$stmt->execute(array(':key' => $key));
$stmt->closeCursor();
$this->cache->un_set($key);
$this->cache->delete($key);
return $this;
}

View File

@@ -609,19 +609,16 @@ class setup
function check_cache_memcache()
{
$Core = \bootstrap::getCore();
echo '<h1>' . _('setup:: Serveur Memcached') . '</h1>';
echo '<ul class="setup">';
$registry = registry::get_instance();
if ($registry->get('GV_cache_server_type') !== 'nocache')
if ($Core->getCache()->isServer())
{
$cache = cache_adapter::get_instance(registry::get_instance());
if ($cache->ping())
{
$stats = $cache->getStats();
$stats = $Core->getCache()->getStats();
foreach ($stats as $name => $stat)
{
@@ -636,11 +633,6 @@ class setup
}
}
else
{
echo '<li class="non-blocker">' . sprintf(_('Le serveur memcached ne repond pas, vous devriez desactiver le cache')) . '</li>';
}
}
else
{
echo '<li class="non-blocker">' . sprintf(_('setup::Aucun serveur memcached rattache.')) . '</li>';
}