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

Commit

Permalink
Refactorize method(s) loadDriverRepository()
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Oct 14, 2015
1 parent c5700d3 commit 9c1660f
Showing 1 changed file with 52 additions and 111 deletions.
163 changes: 52 additions & 111 deletions core/src/core/classes/class.ConfService.php
Expand Up @@ -1475,105 +1475,6 @@ public static function loadRepositoryDriver()
{
return self::getInstance()->loadRepositoryDriverInst();
}
/**
* See static method
* @throws Exception
* @return AJXP_Plugin
*/
public function loadRepositoryDriverInst()
{
if (isSet($this->configs["ACCESS_DRIVER"]) && is_a($this->configs["ACCESS_DRIVER"], "AbstractAccessDriver")) {
return $this->configs["ACCESS_DRIVER"];
}
$this->switchRootDirInst();
$crtRepository = $this->getRepositoryInst();
if($crtRepository == null){
throw new Exception("No active repository found for user!");
}
$accessType = $crtRepository->getAccessType();
$pServ = AJXP_PluginsService::getInstance();
$plugInstance = $pServ->getPluginByTypeName("access", $accessType);

// TRIGGER BEFORE INIT META
$metaSources = $crtRepository->getOption("META_SOURCES");
if (isSet($metaSources) && is_array($metaSources) && count($metaSources)) {
$keys = array_keys($metaSources);
foreach ($keys as $plugId) {
if($plugId == "") continue;
$split = explode(".", $plugId);
$instance = $pServ->getPluginById($plugId);
if (!is_object($instance)) {
continue;
}
if (!method_exists($instance, "beforeInitMeta")) {
continue;
}
try {
$instance->init(AuthService::filterPluginParameters($plugId, $metaSources[$plugId], $crtRepository->getId()));
$instance->beforeInitMeta($plugInstance, $crtRepository);
} catch (Exception $e) {
AJXP_Logger::error(__CLASS__, 'Meta plugin', 'Cannot instanciate Meta plugin, reason : '.$e->getMessage());
$this->errors[] = $e->getMessage();
}
}
}

// INIT MAIN DRIVER
$plugInstance->init($crtRepository);
try {
$plugInstance->initRepository();
$crtRepository->driverInstance = $plugInstance;
} catch (Exception $e) {
// Remove repositories from the lists
$this->removeRepositoryFromCache($crtRepository->getId());
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$crtRepository->getId()) {
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
} else {
$this->switchRootDir();
}
throw $e;
}
$pServ->setPluginUniqueActiveForType("access", $accessType);

// TRIGGER INIT META
$metaSources = $crtRepository->getOption("META_SOURCES");
if (isSet($metaSources) && is_array($metaSources) && count($metaSources)) {
$keys = array_keys($metaSources);
foreach ($keys as $plugId) {
if($plugId == "") continue;
$split = explode(".", $plugId);
$instance = $pServ->getPluginById($plugId);
if (!is_object($instance)) {
continue;
}
try {
$instance->init(AuthService::filterPluginParameters($plugId, $metaSources[$plugId], $crtRepository->getId()));
if (!method_exists($instance, "initMeta")) {
throw new Exception("Meta Source $plugId does not implement the initMeta method.");
}
$instance->initMeta($plugInstance);
} catch (Exception $e) {
AJXP_Logger::error(__CLASS__, 'Meta plugin', 'Cannot instanciate Meta plugin, reason : '.$e->getMessage());
$this->errors[] = $e->getMessage();
}
$pServ->setPluginActive($split[0], $split[1]);
}
}
if (count($this->errors)>0) {
$e = new AJXP_Exception("Error while loading repository feature : ".implode(",",$this->errors));
// Remove repositories from the lists
$this->removeRepositoryFromCache($crtRepository->getId());
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$crtRepository->getId()) {
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
} else {
$this->switchRootDir();
}
throw $e;
}

$this->configs["ACCESS_DRIVER"] = $plugInstance;
return $this->configs["ACCESS_DRIVER"];
}

/**
* @static
Expand All @@ -1582,20 +1483,36 @@ public function loadRepositoryDriverInst()
*/
public static function loadDriverForRepository(&$repository)
{
return self::getInstance()->loadRepositoryDriverREST($repository);
return self::getInstance()->loadRepositoryDriverInst($repository);
}

/**
* See static method
* @param Repository $repository
* @param Repository|null $repository
* @throws AJXP_Exception|Exception
* @return AbstractAccessDriver
*/
public function loadRepositoryDriverREST(&$repository)
private function loadRepositoryDriverInst(&$repository = null)
{
if (isset($repository->driverInstance)) {
return $repository->driverInstance;
$rest = false;
if($repository == null){
if (isSet($this->configs["ACCESS_DRIVER"]) && is_a($this->configs["ACCESS_DRIVER"], "AbstractAccessDriver")) {
return $this->configs["ACCESS_DRIVER"];
}
$this->switchRootDirInst();
$repository = $this->getRepositoryInst();
if($repository == null){
throw new Exception("No active repository found for user!");
}
}else{
$rest = true;
if (isset($repository->driverInstance)) {
return $repository->driverInstance;
}
}
/**
* @var AbstractAccessDriver $plugInstance
*/
$accessType = $repository->getAccessType();
$pServ = AJXP_PluginsService::getInstance();
$plugInstance = $pServ->getPluginByTypeName("access", $accessType);
Expand Down Expand Up @@ -1627,9 +1544,20 @@ public function loadRepositoryDriverREST(&$repository)
$plugInstance->init($repository);
try {
$plugInstance->initRepository();
$repository->driverInstance = $plugInstance;
} catch (Exception $e) {
if(!$rest){
// Remove repositories from the lists
$this->removeRepositoryFromCache($repository->getId());
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$repository->getId()) {
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
} else {
$this->switchRootDir();
}
}
throw $e;
}

AJXP_PluginsService::deferBuildingRegistry();
$pServ->setPluginUniqueActiveForType("access", $accessType);

Expand All @@ -1646,7 +1574,9 @@ public function loadRepositoryDriverREST(&$repository)
}
try {
$instance->init(AuthService::filterPluginParameters($plugId, $metaSources[$plugId], $repository->getId()));
if(!method_exists($instance, "initMeta")) throw new Exception("Meta Source $plugId does not implement the initMeta method.");
if(!method_exists($instance, "initMeta")) {
throw new Exception("Meta Source $plugId does not implement the initMeta method.");
}
$instance->initMeta($plugInstance);
} catch (Exception $e) {
AJXP_Logger::error(__CLASS__, 'Meta plugin', 'Cannot instanciate Meta plugin, reason : '.$e->getMessage());
Expand All @@ -1658,14 +1588,25 @@ public function loadRepositoryDriverREST(&$repository)
AJXP_PluginsService::flushDeferredRegistryBuilding();
if (count($this->errors)>0) {
$e = new AJXP_Exception("Error while loading repository feature : ".implode(",",$this->errors));
if(!$rest){
// Remove repositories from the lists
$this->removeRepositoryFromCache($repository->getId());
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$repository->getId()) {
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
} else {
$this->switchRootDir();
}
}
throw $e;
}

$repository->driverInstance = $plugInstance;
$ctxId = $this->getContextRepositoryId();
if (!empty($ctxId) && $ctxId == $repository->getId()) {
$this->configs["REPOSITORY"] = $repository;
$this->cacheRepository($ctxId, $repository);
if($rest){
$ctxId = $this->getContextRepositoryId();
if (!empty($ctxId) && $ctxId == $repository->getId()) {
$this->configs["REPOSITORY"] = $repository;
$this->cacheRepository($ctxId, $repository);
}
} else {
$this->configs["ACCESS_DRIVER"] = $plugInstance;
}
return $plugInstance;
}
Expand Down

0 comments on commit 9c1660f

Please sign in to comment.