Skip to content

Commit

Permalink
Merge pull request #26 from lingoda/11-avoid-copy-files-from-h5p-core…
Browse files Browse the repository at this point in the history
…-package

11 - Add ability to copy files instead of symlinks
  • Loading branch information
jorisdugue committed Dec 1, 2020
2 parents d850cc4 + 6edda50 commit c9cc452
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions Command/H5pBundleIncludeAssetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public function __construct(KernelInterface $appKernel)
protected function configure()
{
$this
->setDescription('Include the assets from the h5p vendor bundle in the public resources directory of this bundle.');
->setDescription('Include the assets from the h5p vendor bundle in the public resources directory of this bundle.')
->addOption('copy', 'c', InputOption::VALUE_NONE, 'Copy files')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->includeAssets();
$this->includeAssets($input->getOption('copy') ?? false);

return 0;
return 0;
}

private function includeAssets()
private function includeAssets(bool $copy)
{

//get dir of vendor H5P

$fromDir = $this->appKernel->getProjectDir()."/vendor/h5p/";
Expand All @@ -53,18 +53,39 @@ private function includeAssets()

$coreSubDir = "h5p-core/";
$coreDirs = ["fonts", "images", "js", "styles"];
$this->createSymLinks($fromDir, $toDir, $coreSubDir, $coreDirs);
$this->createFiles($fromDir, $toDir, $coreSubDir, $coreDirs, $copy);

$editorSubDir = "h5p-editor/";
$editorDirs = ["ckeditor", "images", "language", "libs", "scripts", "styles"];
$this->createSymLinks($fromDir, $toDir, $editorSubDir, $editorDirs);
$this->createFiles($fromDir, $toDir, $editorSubDir, $editorDirs, $copy);

}

private function createSymLinks($fromDir, $toDir, $subDir, $subDirs)
private function createFiles($fromDir, $toDir, $subDir, $subDirs, $copy)
{
foreach ($subDirs as $dir) {
symlink($fromDir . $subDir . $dir, $toDir . $subDir . $dir);
$src = $fromDir . $subDir . $dir;
$dist = $toDir . $subDir . $dir;

$copy
? $this->recurseCopy($src, $dist)
: symlink($src, $dist);
}
}

private function recurseCopy($src, $dst)
{
$dir = opendir($src);
@mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
$this->recurseCopy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
}

0 comments on commit c9cc452

Please sign in to comment.