Created
June 21, 2019 07:55
-
-
Save hitslab/2ee9b892853ad9562fcd72eb4956fce3 to your computer and use it in GitHub Desktop.
Creating symlinks for opencart module development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$openCartPath = '/path/to/opencart/'; | |
function getFiles($dir, &$results = array()) | |
{ | |
$files = scandir($dir); | |
foreach ($files as $key => $value) { | |
$path = realpath($dir . DIRECTORY_SEPARATOR . $value); | |
if (!is_dir($path)) { | |
$results[] = $path; | |
} else if ($value != "." && $value != "..") { | |
getFiles($path, $results); | |
//$results[] = $path; | |
} | |
} | |
return $results; | |
} | |
$links = []; | |
$linksDirs = []; | |
$exclude = [ | |
__DIR__ . DIRECTORY_SEPARATOR . '.git', | |
'.gitignore', | |
'build.sh', | |
__DIR__ . DIRECTORY_SEPARATOR . 'link.php' | |
]; | |
foreach (getFiles('.') as $file) { | |
$isModuleFile = true; | |
foreach ($exclude as $excludeFile) { | |
if (strpos($file, $excludeFile) !== false) { | |
$isModuleFile = false; | |
} | |
} | |
if ($isModuleFile) { | |
$link = str_replace(__DIR__ . DIRECTORY_SEPARATOR, $openCartPath, $file); | |
$linkDir = dirname($link); | |
if (!in_array($linkDir, $linksDirs)) { | |
$linksDirs[] = $linkDir; | |
} | |
$links[] = [ | |
'file' => $file, | |
'link' => $link | |
]; | |
} | |
} | |
foreach ($linksDirs as $dir) { | |
if (!file_exists($dir)) { | |
mkdir($dir, 0777, true); | |
} | |
} | |
if (file_exists('_links-created.json')) { | |
$previouslyCreatedLinksJson = file_get_contents('_links-created.json'); | |
if ($previouslyCreatedLinksJson !== false) { | |
$previouslyCreatedLinks = json_decode($previouslyCreatedLinksJson, true); | |
foreach ($previouslyCreatedLinks as $previouslyCreatedLink) { | |
if (file_exists($previouslyCreatedLink)) { | |
unlink($previouslyCreatedLink); | |
} | |
} | |
} | |
unlink('_links-created.json'); | |
} | |
$createdLinks = []; | |
foreach ($links as $link) { | |
$linkCreated = symlink($link['file'], $link['link']); | |
if ($linkCreated) { | |
$createdLinks[] = $link['link']; | |
} | |
} | |
file_put_contents('_links-created.json', json_encode($createdLinks)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment