8000 [FrameworkBundle] Refactored assets:install command, tweaked output by 1ed · Pull Request #13057 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Refactored assets:install command, tweaked output #13057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the code more explicit, simplify foreach body
  • Loading branch information
1ed committed Dec 28, 2014
commit e8bd5732b699f0df3cdd39db9eef92cb00b03512
111 changes: 69 additions & 42 deletions src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
$targetDir = $bundlesDir.preg_replace('/bundle$/', '', strtolower($bundle->getName()));
$this->filesystem->remove($targetDir);

if ($symlink) {
try {
$relative = $this->symlink($originDir, $targetDir, $input->getOption('relative'));
$table->addRow(array(
$bundle->getNamespace(),
$targetDir,
sprintf('%s symbolic link', $relative ? 'relative' : 'absolute'),
));

continue;
} catch (IOException $e) {
// fall back to hard copy
}
}

try {
$this->hardCopy($originDir, $targetDir);
$table->addRow(array($bundle->getNamespace(), $targetDir, 'hard copy'));
if ($symlink) {
if ($input->getOption('relative')) {
$methodOrError = $this->relativeSymlinkWithFallback($originDir, $targetDir);
} else {
$methodOrError = $this->absoluteSymlinkWithFallback($originDir, $targetDir);
}
} else {
$methodOrError = $this->hardCopy($originDir, $targetDir);
}
} catch (IOException $e) {
$table->addRow(array($bundle->getNamespace(), $targetDir, sprintf('<error>%s</error>', $e->getMessage())));
$methodOrError = sprintf('<error>%s</error>', $e->getMessage());
$failed = 1;
}

$table->addRow(array($bundle->getNamespace(), $targetDir, $methodOrError));
}

$table->render();
Expand All @@ -138,51 +132,84 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* Creates links with absolute as a fallback.
* Try to create relative symlink.
*
* @param string $origin
* @param string $target
* @param bool $relative
* Falling back to absolute symlink and finally hard copy.
*
* @throws IOException If link can not be created.
* @param string $originDir
* @param string $targetDir
*
* @return bool Created a relative link or not.
* @return string
*/
private function symlink($origin, $target, $relative = true)
private function relativeSymlinkWithFallback($originDir, $targetDir)
{
try {
$this->filesystem->symlink(
$relative ? $this->filesystem->makePathRelative($origin, realpath(dirname($target))) : $origin,
$target
);
if (!file_exists($target)) {
throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $target), 0, null, $target);
}
$this->symlink($originDir, $targetDir, true);
$method = 'relative symbolic link';
} catch (IOException $e) {
if ($relative) {
// relative link failed, try again with absolute
$this->filesystem->symlink($origin, $target);
if (!file_exists($target)) {
throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $target), 0, null, $target);
}
$method = $this->absoluteSymlinkWithFallback($originDir, $targetDir);
}

return false;
}
return $method;
}

throw $e;
/**
* Try to create absolute symlink.
*
* Falling back to hard copy.
*
* @param string $originDir
* @param string $targetDir
*
* @return string
*/
private function absoluteSymlinkWithFallback($originDir, $targetDir)
{
try {
$this->symlink($originDir, $targetDir);
$method = 'absolute symbolic link';
} catch (IOException $e) {
// fall back to copy
$method = $this->hardCopy($originDir, $targetDir);
}

return $relative;
return $method;
}

/**
* Creates symbolic link.
*
* @param string $originDir
* @param string $targetDir
* @param bool $relative
*
* @throws IOException If link can not be created.
*/
private function symlink($originDir, $targetDir, $relative = false)
{
if ($relative) {
$originDir = $this->filesystem->makePathRelative($originDir, realpath(dirname($targetDir)));
}
$this->filesystem->symlink($originDir, $targetDir);
if (!file_exists($targetDir)) {
throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $targetDir), 0, null, $targetDir);
}
}

/**
* Copies origin to target.
*
* @param string $originDir
* @param string $targetDir
*
* @return string
*/
private function hardCopy($originDir, $targetDir)
{
$this->filesystem->mkdir($targetDir, 0777);
// We use a custom iterator to ignore VCS files
$this->filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir));

return 'hard copy';
}
}
0