Browse Source

Merge pull request #24505 from lamrani002/manageApiFileInMB

optimize section for add & remove apiFile
Laurent Destailleur 2 years ago
parent
commit
45cdc26cd7
2 changed files with 120 additions and 69 deletions
  1. 105 0
      htdocs/core/lib/modulebuilder.lib.php
  2. 15 69
      htdocs/modulebuilder/index.php

+ 105 - 0
htdocs/core/lib/modulebuilder.lib.php

@@ -742,3 +742,108 @@ function writePermsInAsciiDoc($file, $destfile)
 	}
 	return 1;
 }
+
+/**
+ * Add Object in ModuleApi File
+ * @param  string $file           path of file
+ * @param  array  $objects        array of objects in the module
+ * @param  string $modulename     name of module
+ * @return int                    1 if OK, -1 if KO
+ */
+function addObjectsToApiFile($file, $objects, $modulename)
+{
+	if (!file_exists($file)) {
+		return -1;
+	}
+	$content = file($file);
+	$includeClass = "dol_include_once('/mymodule/class/myobject.class.php');";
+	$props = "public \$myobject;";
+	$varcomented = "@var MyObject \$myobject {@type MyObject}";
+	$constructObj = "\$this->myobject = new MyObject(\$this->db);";
+
+	// add properties and declare them in consturctor
+	foreach ($content as $lineNumber => &$lineContent) {
+		if (strpos($lineContent, $varcomented) !== false) {
+			$lineContent = '';
+			foreach ($objects as $object) {
+				$lineContent .= "\t * @var ".$object." \$".strtolower($object)." {@type ".$object."}". PHP_EOL;
+			}
+			//var_dump($lineContent);exit;
+		}
+		if (strpos($lineContent, $props) !== false) {
+			$lineContent = '';
+			foreach ($objects as $object) {
+				$lineContent .= "\tpublic \$".strtolower($object).";". PHP_EOL;
+			}
+		}
+		if (strpos($lineContent, $constructObj) !== false) {
+			$lineContent = '';
+			foreach ($objects as $object) {
+				$lineContent .= "\t\t\$this->".strtolower($object)." = new ".$object."(\$this->db);". PHP_EOL;
+			}
+		}
+		if (strpos($lineContent, $includeClass) !== false) {
+			$lineContent = '';
+			foreach ($objects as $object) {
+				$lineContent .= "dol_include_once('/".strtolower($modulename)."/class/".strtolower($object).".class.php');". PHP_EOL;
+			}
+		}
+	}
+	$allContent = implode("", $content);
+	file_put_contents($file, $allContent);
+
+	//add methods for each object
+	$allContent = getFromFile($file, '/*begin methods CRUD*/', '/*end methods CRUD*/');
+	foreach ($objects as $object) {
+		$contentReplaced =str_replace(["myobject","MyObject"], [strtolower($object),$object], $allContent);
+		dolReplaceInFile($file, array('/*end methods CRUD*/' => '/*CRUD FOR '.strtoupper($object).'*/'."\n".$contentReplaced."\n\t".'/*END CRUD FOR '.strtoupper($object).'*/'."\n\t".'/*end methods CRUD*/'));
+	}
+	dolReplaceInFile($file, array($allContent => '','MyModule' => ucfirst($modulename)));
+	return 1;
+}
+
+/**
+ * Remove Object variables and methods from API_Module File
+ * @param string   $file         file api module
+ * @param string   $objectname   name of object whant to remove
+ * @param string   $modulename   name of module
+ * @return int                    1 if OK, -1 if KO
+ */
+function removeObjectFromApiFile($file, $objectname, $modulename)
+{
+	$begin = '/*CRUD FOR '.strtoupper($objectname).'*/';
+	$end = '/*END CRUD FOR '.strtoupper($objectname).'*/';
+	$includeClass = "dol_include_once('/".strtolower($modulename)."/class/".strtolower($objectname).".class.php');";
+	$varcomentedDel = "\t * @var ".$objectname." \$".strtolower($objectname)." {@type ".$objectname."}";
+	$propsDel = "\tpublic \$".strtolower($objectname).";";
+	$constructObjDel = "\t\t\$this->".strtolower($objectname)." = new ".$objectname."(\$this->db);";
+
+	if (!file_exists($file)) {
+		return -1;
+	}
+	$content = file($file);
+	// for delete property and the initialization from the construct
+	foreach ($content as $lineNumber => &$lineContent) {
+		if (strpos($lineContent, $includeClass) !== false) {
+			$lineContent = '';
+		}
+		if (strpos($lineContent, $varcomentedDel) !== false) {
+			$lineContent = '';
+		}
+		if (strpos($lineContent, $propsDel) !== false) {
+			$lineContent = '';
+		}
+		if (strpos($lineContent, $constructObjDel) !== false) {
+			$lineContent = '';
+		}
+	}
+	$allContent = implode("", $content);
+	file_put_contents($file, $allContent);
+	// for delete methods of object
+	$allContent = getFromFile($file, $begin, $end);
+	$check = dolReplaceInFile($file, array($allContent => ''));
+	if ($check) {
+		dolReplaceInFile($file, array($begin => '', $end => ''));
+	}
+	return 1;
+}

+ 15 - 69
htdocs/modulebuilder/index.php

@@ -471,72 +471,10 @@ if ($dirins && in_array($action, array('initapi', 'initphpunit', 'initpagecontac
 		);
 
 		if (count($objects) > 1) {
-			$file = $destfile;
-			$content = file($file);
-			$props = "public \$myobject;";
-			$varcomented = "@var MyObject \$myobject {@type MyObject}";
-			$constructObj = "\$this->myobject = new MyObject(\$this->db);";
-			// add properties and declare them in consturctor
-			foreach ($content as $lineNumber => &$lineContent) {
-				if (strpos($lineContent, $varcomented) !== false) {
-					$lineContent = '';
-					foreach ($objects as $object) {
-						$lineContent .= "\t * @var ".$object." \$".strtolower($object)." {@type ".$object."}". PHP_EOL;
-					}
-					//var_dump($lineContent);exit;
-				}
-				if (strpos($lineContent, $props) !== false) {
-					$lineContent = '';
-					foreach ($objects as $object) {
-						$lineContent .= "\tpublic \$".strtolower($object).";". PHP_EOL;
-					}
-				}
-				if (strpos($lineContent, $constructObj) !== false) {
-					$lineContent = '';
-					foreach ($objects as $object) {
-						$lineContent .= "\t\t\$this->".strtolower($object)."= new ".$object."(\$this->db);". PHP_EOL;
-					}
-				}
-			}
-			$allContent = implode("", $content);
-			file_put_contents($destfile, $allContent);
-		}
-		if (count($objects) > 1) {
-			$search = "/*begin methods CRUD*/";
-			// Open the file and read line by line
-			$handle = fopen($destfile, "r");
-				$i = 1;
-				$lines = array();
-				$props = " public \$myobject; ";
-			while (($line = fgets($handle)) !== false) {
-				//search line begin
-				if (strpos($line, $search) !== false) {
-					$start_line = $i;
-
-					// Copy lines until the end on array
-					while (($line = fgets($handle)) !== false) {
-						if (strpos($line, "/*end methods CRUD*/") !== false) {
-							$end_line = $i;
-							break;
-						}
-						$lines[] = $line;
-						$i++;
-					}
-					break;
-				}
-
-				$i++;
-			}
-			$allContent = implode("", $lines);
-
-			foreach ($objects as $object) {
-				$contentReplaced = str_replace(["myobject","MyObject"], [strtolower($object),$object], $allContent);
-				dolReplaceInFile($destfile, array('/*end methods CRUD*/' => '/*CRUD FOR '.strtoupper($object).'*/'."\n".$contentReplaced."\n\t".'/*END CRUD FOR '.strtoupper($object).'*/'."\n\t".'/*end methods CRUD*/'));
-			}
-				dolReplaceInFile($destfile, array($allContent => ''));
-			fclose($handle);
+			addObjectsToApiFile($destfile, $objects, $modulename);
 		} else {
 			dolReplaceInFile($destfile, $arrayreplacement);
+			dolReplaceInFile($destfile, array('/*begin methods CRUD*/' => '/*begin methods CRUD*/'."\n\t".'/*CRUD FOR '.strtoupper($objectname).'*/', '/*end methods CRUD*/' => '/*END CRUD FOR '.strtoupper($objectname).'*/'."\n\t".'/*end methods CRUD*/'));
 		}
 
 		if ($varnametoupdate) {
@@ -938,17 +876,25 @@ if ($dirins && $action == 'confirm_removefile' && !empty($module)) {
 
 	$relativefilename = dol_sanitizePathName(GETPOST('file', 'restricthtml'));
 
-	// Get list of existing objects
-	$objects = dolGetListOfObjectClasses($destdir);
-
-
 	// Now we delete the file
 	if ($relativefilename) {
 		$dirnametodelete = dirname($relativefilename);
 		$filetodelete = $dirins.'/'.$relativefilename;
 		$dirtodelete  = $dirins.'/'.$dirnametodelete;
 
-		$result = dol_delete_file($filetodelete);
+		//check when we want delete api_file
+		if (strpos($relativefilename, 'api') !== false) {
+			$removeFile = removeObjectFromApiFile($file_api, $objectname, $module);
+			$var = getFromFile($file_api, '/*begin methods CRUD*/', '/*end methods CRUD*/');
+			if (str_word_count($var) == 0) {
+				$result = dol_delete_file($filetodelete);
+			}
+			if ($removeFile) {
+				setEventMessages($langs->trans("ApiObjectDeleted"), null);
+			}
+		} else {
+			$result = dol_delete_file($filetodelete);
+		}
 		if (!$result) {
 			setEventMessages($langs->trans("ErrorFailToDeleteFile", basename($filetodelete)), null, 'errors');
 		} else {