Placeholders pattern: {{variable-name}}
public static fillTemplateString ( array $variables, string $templateString ) : string | ||
$variables | array | associative array with keys as variable names. |
$templateString | string | template string which contains placeholders for variables to be expanded. |
리턴 | string | templateString with expanded variable placeholders |
/** * @test */ public function mergeDriverIsNotAddedWhenPresent() { $gitattributesContents = file_get_contents(__DIR__ . '/../../src/Initialization/.gitattributes.tpl'); $gitConfigContents = "[merge \"vp-ini\"]"; $gitattributesVariables = ['vpdb-dir' => self::$repositoryDir]; $gitattributesContents = StringUtils::fillTemplateString($gitattributesVariables, $gitattributesContents); file_put_contents(self::$repositoryDir . "/.gitattributes", $gitattributesContents); file_put_contents(self::$repositoryDir . "/.git/config", $gitConfigContents); $this->installMergeDriver('auto'); $this->assertEquals($gitConfigContents, file_get_contents(self::$repositoryDir . "/.git/config")); $this->assertEquals($gitattributesContents, file_get_contents(self::$repositoryDir . "/.gitattributes")); }
/** * Uninstalls a merge driver - removes 'vp-ini' sections from both .gitattributes * and .git/config. * * @param string $rootDir * @param string $pluginDir * @param string $vpdbDir */ public static function uninstallMergeDriver($rootDir, $pluginDir, $vpdbDir) { $gitconfigPath = $rootDir . '/.git/config'; $gitattributesPath = $rootDir . '/.gitattributes'; $gitattributesContents = file_get_contents($pluginDir . '/src/Initialization/.gitattributes.tpl'); $gitattributesVariables = ['vpdb-dir' => PathUtils::getRelativePath($rootDir, $vpdbDir)]; $gitattributesContents = StringUtils::fillTemplateString($gitattributesVariables, $gitattributesContents); if (file_exists($gitattributesPath)) { $gitAttributes = file_get_contents($gitattributesPath); $gitAttributes = str_replace($gitattributesContents, '', $gitAttributes); if (trim($gitAttributes) === '') { unlink($gitattributesPath); } else { file_put_contents($gitattributesPath, $gitAttributes); } } if (file_exists($gitconfigPath)) { $gitConfig = file_get_contents($gitconfigPath); // https://regex101.com/r/eJ4rJ5/4 $mergeDriverRegex = "/(\\[merge \\\"vp\\-ini\\\"\\]\\r?\\n)([^\\[]*)/"; $gitConfig = preg_replace($mergeDriverRegex, '', $gitConfig, 1); file_put_contents($gitconfigPath, $gitConfig); } }
/** * Installs Gitignore to the repository root, or does nothing if the file already exists. */ private function installGitignore() { $gitignorePath = VP_PROJECT_ROOT . '/.gitignore'; $projectRoot = realpath(VP_PROJECT_ROOT); $vpGitignore = file_get_contents(__DIR__ . '/.gitignore.tpl'); $gitIgnoreVariables = ['wp-content' => rtrim('/' . PathUtils::getRelativePath($projectRoot, realpath(WP_CONTENT_DIR)), '/'), 'wp-plugins' => rtrim('/' . PathUtils::getRelativePath($projectRoot, realpath(WP_PLUGIN_DIR)), '/'), 'abspath' => rtrim('/' . PathUtils::getRelativePath($projectRoot, realpath(ABSPATH)), '/'), 'abspath-parent' => rtrim('/' . PathUtils::getRelativePath($projectRoot, realpath(dirname(ABSPATH))), '/')]; $vpGitignore = StringUtils::fillTemplateString($gitIgnoreVariables, $vpGitignore); if (is_file($gitignorePath)) { $currentGitignore = file_get_contents($gitignorePath); if (strpos($currentGitignore, $vpGitignore) !== false) { return; } file_put_contents($gitignorePath, "\n" . $vpGitignore, FILE_APPEND); } else { file_put_contents($gitignorePath, $vpGitignore); } }