Преглед изворни кода

exported InstanceConfig from ACL

Piotr Labudda пре 8 година
родитељ
комит
af0f935a9b
3 измењених фајлова са 118 додато и 89 уклоњено
  1. 9 88
      SE/se-lib/ACL.php
  2. 108 0
      SE/se-lib/InstanceConfig.php
  3. 1 1
      SE/se-lib/Route/Storage/AclReinstall.php

+ 9 - 88
SE/se-lib/ACL.php

@@ -3,6 +3,7 @@
 Lib::loadClass('Core_AclHelper');
 Lib::loadClass('AntAclBase');
 Lib::loadClass('RefConfig');
+Lib::loadClass('InstanceConfig');
 
 class ACL {
 
@@ -507,46 +508,15 @@ class ACL {
 	}
 
 	public static function getInstanceId($namespace) {
-		$conf = self::getInstanceConfig($namespace);
-		return $conf['id'];
+		return InstanceConfig::getInstanceConfig($namespace)['id'];
 	}
 	public static function getInstanceConfig($namespace) { // @returns { id, namespace, rootNamespace, idInstanceBase, _createdAt }
-		try {
-			$conf = self::fetchInstanceConfig($namespace);
-		} catch (Exception $e) {
-			DB::getPDO()->execSql("
-				create table if not exists `CRM_INSTANCE_CONFIG` (
-					`id` int(11) not null AUTO_INCREMENT,
-					`namespace` varchar(255) NOT NULL DEFAULT '',
-					`rootNamespace` varchar(255) NOT NULL DEFAULT '',
-					`idInstanceBase` int(11) NOT NULL DEFAULT 0,
-					`_createdAt` datetime NOT NULL,
-					UNIQUE KEY `namespace` (`namespace`),
-					KEY `rootNamespace` (`rootNamespace`),
-					PRIMARY KEY (`id`)
-				) ENGINE=MyISAM  DEFAULT CHARSET=latin2
-			");
-			// TODO:?: `_tableInstalled` tinyint(1) not null default 0,
-			$conf = self::fetchInstanceConfig($namespace);
-		}
-		if (!$conf) {
-			$id = DB::getPDO()->insert("CRM_INSTANCE_CONFIG", [
-				'namespace' => $namespace,
-				'rootNamespace' => self::getRootNamespace($namespace),
-				'_createdAt' => 'NOW()',
-			]);
-			$conf = self::fetchInstanceConfig($namespace);
-		}
-		if (!$conf) throw new Exception("Instance not found in config table '{$namespace}'");
-		return $conf;
-	}
-	public static function fetchInstanceConfig($namespace) {
-		return DB::getPDO()->fetchFirst("
-			select c.*
-			from `CRM_INSTANCE_CONFIG` c
-			where c.namespace = '{$namespace}'
-		");
+		return InstanceConfig::getInstanceConfig($namespace);
 	}
+	public static function getInstanceTable($namespace) { // @returns tableName with struct { pk, idInstance, _createdAt }
+		return InstanceConfig::getInstanceTable($namespace);
+	}
+
 	public static function getRootNamespace($namespace) { // TODO: works only for relative urls! - mv to Acl->getRootNamespace
 		Lib::loadClass('SchemaFactory');
 		try {
@@ -569,59 +539,10 @@ class ACL {
 		return "default_db/{$objectItem['_rootTableName']}";
 	}
 	public static function getNamespaceSiblings($namespace) {
-		return array_map(function ($row) {
-			return $row['namespace'];
-		}, DB::getPDO()->fetchAll("
-			select s.namespace
-			from CRM_INSTANCE_CONFIG c
-				join CRM_INSTANCE_CONFIG s on ( s.rootNamespace = c.rootNamespace and s.namespace != c.rootNamespace )
-			where c.namespace = :namespace
-		", [
-			'namespace' => $namespace
-		]));
+		return InstanceConfig::getNamespaceSiblings($namespace);
 	}
 	public static function getFeatureNamespaces($namespace, $pk) {
-		$instanceTable = self::getInstanceTable($namespace);
-		return array_map(function ($row) {
-			return $row['namespace'];
-		}, DB::getPDO()->fetchAll("
-			select c.namespace
-			from `{$instanceTable}` i
-				join `CRM_INSTANCE_CONFIG` c on ( c.id = i.idInstance )
-			where i.pk = :pk
-		", [
-			'pk' => $pk,
-		]));
-	}
-	public static function getInstanceTable($namespace) { // @returns tableName with struct { pk, idInstance, _createdAt }
-		$conf = self::getInstanceConfig($namespace);
-		if (!empty($conf['idInstanceBase'])) return "CRM__#INSTANCE_TABLE__{$conf['idInstanceBase']}";
-
-		$rootNs = $conf['rootNamespace'];
-		$rootConf = self::getInstanceConfig($rootNs);
-		$instanceTableName = "CRM__#INSTANCE_TABLE__{$rootConf['id']}";
-		if (!empty($rootConf['idInstance'])) {
-			$affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
-				'idInstanceBase' => $rootConf['id']
-			]);
-			return $instanceTableName;
-		}
-
-		// TODO: fetch primaryKeyType - TODO: store primaryKey and primaryKeyType in SystemObject item
-		$pkType = 'int';
-		DB::getPDO()->exec("
-			CREATE TABLE IF NOT EXISTS `{$instanceTableName}` (
-				`pk` int(11) NOT NULL COMMENT 'primary key'
-				, `idInstance` int(11) NOT NULL
-				, `_createdAt` datetime NOT NULL
-				, KEY `pk` (`pk`)
-				, KEY `idInstance` (`idInstance`)
-			) ENGINE=MyISAM DEFAULT CHARSET=latin2 COMMENT='{$rootNs} #INSTANCE';
-		");
-		$affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
-			'idInstanceBase' => $rootConf['id']
-		]);
-		return $instanceTableName;
+		return InstanceConfig::getFeatureNamespaces($namespace, $pk);
 	}
 
 	// @params $from - ( ACL | tableName | namespace | etc... - only ACL)

+ 108 - 0
SE/se-lib/InstanceConfig.php

@@ -0,0 +1,108 @@
+<?php
+
+class InstanceConfig {
+
+	static function createInstanceConfigTable() {
+		DB::getPDO()->execSql("
+			create table if not exists `CRM_INSTANCE_CONFIG` (
+				`id` int(11) not null AUTO_INCREMENT,
+				`namespace` varchar(255) NOT NULL DEFAULT '',
+				`rootNamespace` varchar(255) NOT NULL DEFAULT '',
+				`idInstanceBase` int(11) NOT NULL DEFAULT 0,
+				`_createdAt` datetime NOT NULL,
+				UNIQUE KEY `namespace` (`namespace`),
+				KEY `rootNamespace` (`rootNamespace`),
+				PRIMARY KEY (`id`)
+			) ENGINE=MyISAM  DEFAULT CHARSET=latin2
+		");
+	}
+
+	static function getInstanceTable($namespace) { // @returns tableName with struct { pk, idInstance, _createdAt }
+		$conf = self::getInstanceConfig($namespace);
+		if (!empty($conf['idInstanceBase'])) return "CRM__#INSTANCE_TABLE__{$conf['idInstanceBase']}";
+
+		$rootNs = $conf['rootNamespace'];
+		$rootConf = self::getInstanceConfig($rootNs);
+		$instanceTableName = "CRM__#INSTANCE_TABLE__{$rootConf['id']}";
+		if (!empty($rootConf['idInstance'])) {
+			$affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
+				'idInstanceBase' => $rootConf['id']
+			]);
+			return $instanceTableName;
+		}
+
+		// TODO: fetch primaryKeyType - TODO: store primaryKey and primaryKeyType in SystemObject item
+		$pkType = 'int';
+		DB::getPDO()->exec("
+			CREATE TABLE IF NOT EXISTS `{$instanceTableName}` (
+				`pk` int(11) NOT NULL COMMENT 'primary key'
+				, `idInstance` int(11) NOT NULL
+				, `_createdAt` datetime NOT NULL
+				, KEY `pk` (`pk`)
+				, KEY `idInstance` (`idInstance`)
+			) ENGINE=MyISAM DEFAULT CHARSET=latin2 COMMENT='{$rootNs} #INSTANCE';
+		");
+		$affected = DB::getPDO()->update("CRM_INSTANCE_CONFIG", 'rootNamespace', $rootNs, [
+			'idInstanceBase' => $rootConf['id']
+		]);
+		return $instanceTableName;
+	}
+
+	static function getInstanceConfig($namespace) { // @returns { id, namespace, rootNamespace, idInstanceBase, _createdAt }
+		try {
+			$conf = self::fetchInstanceConfig($namespace);
+		} catch (Exception $e) {
+			self::createInstanceConfigTable();
+			// TODO:?: `_tableInstalled` tinyint(1) not null default 0,
+			$conf = self::fetchInstanceConfig($namespace);
+		}
+		if (!$conf) {
+			$id = DB::getPDO()->insert("CRM_INSTANCE_CONFIG", [
+				'namespace' => $namespace,
+				'rootNamespace' => self::getRootNamespace($namespace),
+				'_createdAt' => 'NOW()',
+			]);
+			$conf = self::fetchInstanceConfig($namespace);
+		}
+		if (!$conf) throw new Exception("Instance not found in config table '{$namespace}'");
+		return $conf;
+	}
+
+	static function fetchInstanceConfig($namespace) {
+		return DB::getPDO()->fetchFirst("
+			select c.*
+			from `CRM_INSTANCE_CONFIG` c
+			where c.namespace = :namespace'{$namespace}'
+		", [
+			':namespace' => $namespace
+		]);
+	}
+
+	static function getNamespaceSiblings($namespace) {
+		return array_map(function ($row) {
+			return $row['namespace'];
+		}, DB::getPDO()->fetchAll("
+			select s.namespace
+			from CRM_INSTANCE_CONFIG c
+				join CRM_INSTANCE_CONFIG s on ( s.rootNamespace = c.rootNamespace and s.namespace != c.rootNamespace )
+			where c.namespace = :namespace
+		", [
+			'namespace' => $namespace
+		]));
+	}
+
+	static function getFeatureNamespaces($namespace, $pk) {
+		$instanceTable = self::getInstanceTable($namespace);
+		return array_map(function ($row) {
+			return $row['namespace'];
+		}, DB::getPDO()->fetchAll("
+			select c.namespace
+			from `{$instanceTable}` i
+				join `CRM_INSTANCE_CONFIG` c on ( c.id = i.idInstance )
+			where i.pk = :pk
+		", [
+			'pk' => $pk,
+		]));
+	}
+
+}

+ 1 - 1
SE/se-lib/Route/Storage/AclReinstall.php

@@ -278,7 +278,7 @@ class Route_Storage_AclReinstall extends RouteBase {
 			$dbgInfo = [
 				'idInstance' => ACL::getInstanceId($namespace),
 				'rootInstance' => ACL::getRootNamespace($namespace),
-				'conf' => ACL::fetchInstanceConfig($namespace),
+				// 'conf' => InstanceConfig::fetchInstanceConfig($namespace),
 				// 'table' => ACL::getInstanceTable($namespace), // Object structure not installed 'default_db/{tableName}'
 			];
 			DBG::nicePrint($dbgInfo, "dbg");