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

Commit 9c1660f

Browse files
committed
Refactorize method(s) loadDriverRepository()
1 parent c5700d3 commit 9c1660f

File tree

1 file changed

+52
-111
lines changed

1 file changed

+52
-111
lines changed

core/src/core/classes/class.ConfService.php

Lines changed: 52 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,105 +1475,6 @@ public static function loadRepositoryDriver()
14751475
{
14761476
return self::getInstance()->loadRepositoryDriverInst();
14771477
}
1478-
/**
1479-
* See static method
1480-
* @throws Exception
1481-
* @return AJXP_Plugin
1482-
*/
1483-
public function loadRepositoryDriverInst()
1484-
{
1485-
if (isSet($this->configs["ACCESS_DRIVER"]) && is_a($this->configs["ACCESS_DRIVER"], "AbstractAccessDriver")) {
1486-
return $this->configs["ACCESS_DRIVER"];
1487-
}
1488-
$this->switchRootDirInst();
1489-
$crtRepository = $this->getRepositoryInst();
1490-
if($crtRepository == null){
1491-
throw new Exception("No active repository found for user!");
1492-
}
1493-
$accessType = $crtRepository->getAccessType();
1494-
$pServ = AJXP_PluginsService::getInstance();
1495-
$plugInstance = $pServ->getPluginByTypeName("access", $accessType);
1496-
1497-
// TRIGGER BEFORE INIT META
1498-
$metaSources = $crtRepository->getOption("META_SOURCES");
1499-
if (isSet($metaSources) && is_array($metaSources) && count($metaSources)) {
1500-
$keys = array_keys($metaSources);
1501-
foreach ($keys as $plugId) {
1502-
if($plugId == "") continue;
1503-
$split = explode(".", $plugId);
1504-
$instance = $pServ->getPluginById($plugId);
1505-
if (!is_object($instance)) {
1506-
continue;
1507-
}
1508-
if (!method_exists($instance, "beforeInitMeta")) {
1509-
continue;
1510-
}
1511-
try {
1512-
$instance->init(AuthService::filterPluginParameters($plugId, $metaSources[$plugId], $crtRepository->getId()));
1513-
$instance->beforeInitMeta($plugInstance, $crtRepository);
1514-
} catch (Exception $e) {
1515-
AJXP_Logger::error(__CLASS__, 'Meta plugin', 'Cannot instanciate Meta plugin, reason : '.$e->getMessage());
1516-
$this->errors[] = $e->getMessage();
1517-
}
1518-
}
1519-
}
1520-
1521-
// INIT MAIN DRIVER
1522-
$plugInstance->init($crtRepository);
1523-
try {
1524-
$plugInstance->initRepository();
1525-
$crtRepository->driverInstance = $plugInstance;
1526-
} catch (Exception $e) {
1527-
// Remove repositories from the lists
1528-
$this->removeRepositoryFromCache($crtRepository->getId());
1529-
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$crtRepository->getId()) {
1530-
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
1531-
} else {
1532-
$this->switchRootDir();
1533-
}
1534-
throw $e;
1535-
}
1536-
$pServ->setPluginUniqueActiveForType("access", $accessType);
1537-
1538-
// TRIGGER INIT META
1539-
$metaSources = $crtRepository->getOption("META_SOURCES");
1540-
if (isSet($metaSources) && is_array($metaSources) && count($metaSources)) {
1541-
$keys = array_keys($metaSources);
1542-
foreach ($keys as $plugId) {
1543-
if($plugId == "") continue;
1544-
$split = explode(".", $plugId);
1545-
$instance = $pServ->getPluginById($plugId);
1546-
if (!is_object($instance)) {
1547-
continue;
1548-
}
1549-
try {
1550-
$instance->init(AuthService::filterPluginParameters($plugId, $metaSources[$plugId], $crtRepository->getId()));
1551-
if (!method_exists($instance, "initMeta")) {
1552-
throw new Exception("Meta Source $plugId does not implement the initMeta method.");
1553-
}
1554-
$instance->initMeta($plugInstance);
1555-
} catch (Exception $e) {
1556-
AJXP_Logger::error(__CLASS__, 'Meta plugin', 'Cannot instanciate Meta plugin, reason : '.$e->getMessage());
1557-
$this->errors[] = $e->getMessage();
1558-
}
1559-
$pServ->setPluginActive($split[0], $split[1]);
1560-
}
1561-
}
1562-
if (count($this->errors)>0) {
1563-
$e = new AJXP_Exception("Error while loading repository feature : ".implode(",",$this->errors));
1564-
// Remove repositories from the lists
1565-
$this->removeRepositoryFromCache($crtRepository->getId());
1566-
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$crtRepository->getId()) {
1567-
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
1568-
} else {
1569-
$this->switchRootDir();
1570-
}
1571-
throw $e;
1572-
}
1573-
1574-
$this->configs["ACCESS_DRIVER"] = $plugInstance;
1575-
return $this->configs["ACCESS_DRIVER"];
1576-
}
15771478

15781479
/**
15791480
* @static
@@ -1582,20 +1483,36 @@ public function loadRepositoryDriverInst()
15821483
*/
15831484
public static function loadDriverForRepository(&$repository)
15841485
{
1585-
return self::getInstance()->loadRepositoryDriverREST($repository);
1486+
return self::getInstance()->loadRepositoryDriverInst($repository);
15861487
}
15871488

15881489
/**
15891490
* See static method
1590-
* @param Repository $repository
1491+
* @param Repository|null $repository
15911492
* @throws AJXP_Exception|Exception
15921493
* @return AbstractAccessDriver
15931494
*/
1594-
public function loadRepositoryDriverREST(&$repository)
1495+
private function loadRepositoryDriverInst(&$repository = null)
15951496
{
1596-
if (isset($repository->driverInstance)) {
1597-
return $repository->driverInstance;
1497+
$rest = false;
1498+
if($repository == null){
1499+
if (isSet($this->configs["ACCESS_DRIVER"]) && is_a($this->configs["ACCESS_DRIVER"], "AbstractAccessDriver")) {
1500+
return $this->configs["ACCESS_DRIVER"];
1501+
}
1502+
$this->switchRootDirInst();
1503+
$repository = $this->getRepositoryInst();
1504+
if($repository == null){
1505+
throw new Exception("No active repository found for user!");
1506+
}
1507+
}else{
1508+
$rest = true;
1509+
if (isset($repository->driverInstance)) {
1510+
return $repository->driverInstance;
1511+
}
15981512
}
1513+
/**
1514+
* @var AbstractAccessDriver $plugInstance
1515+
*/
15991516
$accessType = $repository->getAccessType();
16001517
$pServ = AJXP_PluginsService::getInstance();
16011518
$plugInstance = $pServ->getPluginByTypeName("access", $accessType);
@@ -1627,9 +1544,20 @@ public function loadRepositoryDriverREST(&$repository)
16271544
$plugInstance->init($repository);
16281545
try {
16291546
$plugInstance->initRepository();
1547+
$repository->driverInstance = $plugInstance;
16301548
} catch (Exception $e) {
1549+
if(!$rest){
1550+
// Remove repositories from the lists
1551+
$this->removeRepositoryFromCache($repository->getId());
1552+
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$repository->getId()) {
1553+
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
1554+
} else {
1555+
$this->switchRootDir();
1556+
}
1557+
}
16311558
throw $e;
16321559
}
1560+
16331561
AJXP_PluginsService::deferBuildingRegistry();
16341562
$pServ->setPluginUniqueActiveForType("access", $accessType);
16351563

@@ -1646,7 +1574,9 @@ public function loadRepositoryDriverREST(&$repository)
16461574
}
16471575
try {
16481576
$instance->init(AuthService::filterPluginParameters($plugId, $metaSources[$plugId], $repository->getId()));
1649-
if(!method_exists($instance, "initMeta")) throw new Exception("Meta Source $plugId does not implement the initMeta method.");
1577+
if(!method_exists($instance, "initMeta")) {
1578+
throw new Exception("Meta Source $plugId does not implement the initMeta method.");
1579+
}
16501580
$instance->initMeta($plugInstance);
16511581
} catch (Exception $e) {
16521582
AJXP_Logger::error(__CLASS__, 'Meta plugin', 'Cannot instanciate Meta plugin, reason : '.$e->getMessage());
@@ -1658,14 +1588,25 @@ public function loadRepositoryDriverREST(&$repository)
16581588
AJXP_PluginsService::flushDeferredRegistryBuilding();
16591589
if (count($this->errors)>0) {
16601590
$e = new AJXP_Exception("Error while loading repository feature : ".implode(",",$this->errors));
1591+
if(!$rest){
1592+
// Remove repositories from the lists
1593+
$this->removeRepositoryFromCache($repository->getId());
1594+
if (isSet($_SESSION["PREVIOUS_REPO_ID"]) && $_SESSION["PREVIOUS_REPO_ID"] !=$repository->getId()) {
1595+
$this->switchRootDir($_SESSION["PREVIOUS_REPO_ID"]);
1596+
} else {
1597+
$this->switchRootDir();
1598+
}
1599+
}
16611600
throw $e;
16621601
}
1663-
1664-
$repository->driverInstance = $plugInstance;
1665-
$ctxId = $this->getContextRepositoryId();
1666-
if (!empty($ctxId) && $ctxId == $repository->getId()) {
1667-
$this->configs["REPOSITORY"] = $repository;
1668-
$this->cacheRepository($ctxId, $repository);
1602+
if($rest){
1603+
$ctxId = $this->getContextRepositoryId();
1604+
if (!empty($ctxId) && $ctxId == $repository->getId()) {
1605+
$this->configs["REPOSITORY"] = $repository;
1606+
$this->cacheRepository($ctxId, $repository);
1607+
}
1608+
} else {
1609+
$this->configs["ACCESS_DRIVER"] = $plugInstance;
16691610
}
16701611
return $plugInstance;
16711612
}

0 commit comments

Comments
 (0)