-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from 1 commit
fcbfcd1
f7f8f85
a4f1a0a
e8bd573
90d7f06
736ec74
226e98f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
|
@@ -22,9 +23,15 @@ | |
* Command that places bundle web assets into a given directory. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Gábor Egyed <gabor.egyed@gmail.com> | ||
*/ | ||
class AssetsInstallCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* @var \Symfony\Component\Filesystem\Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -74,11 +81,11 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $input->getArgument('target'))); | ||
} | ||
|
||
$filesystem = $this->getContainer()->get('filesystem'); | ||
$this->filesystem = $this->getContainer()->get('filesystem'); | ||
|
||
// Create the bundles directory otherwise symlink will fail. | ||
$bundlesDir = $targetArg.'/bundles/'; | ||
$filesystem->mkdir($bundlesDir, 0777); | ||
$this->filesystem->mkdir($bundlesDir, 0777); | ||
|
||
// relative implies symlink | ||
$symlink = $input->getOption('symlink') || $input->getOption('relative'); | ||
|
@@ -89,50 +96,78 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
$output->writeln('Installing assets as <comment>hard copies</comment>.'); | ||
} | ||
|
||
$table = new Table($output); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using a table has a huge drawback compared to the previous output: it is not streaming the output anymore, but waiting for all bundles to be processed before rendering anything. And if any exception happens, there won't be any report about the symlinks which have already been created |
||
$table->setHeaders(array('Source', 'Target', 'Method / Error')); | ||
|
||
$ret = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you rename this? i prefer "failed"? |
||
foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { | ||
if (is_dir($originDir = $bundle->getPath().'/Resources/public')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! |
||
$targetDir = $bundlesDir.preg_replace('/bundle$/', '', strtolower($bundle->getName())); | ||
|
||
$output->writeln(sprintf('Installing assets for <comment>%s</comment> into <comment>%s</comment>', $bundle->getNamespace(), $targetDir)); | ||
|
||
$filesystem->remove($targetDir); | ||
$this->filesystem->remove($targetDir); | ||
|
||
if ($symlink) { | ||
if ($input->getOption('relative')) { | ||
$relativeOriginDir = $filesystem->makePathRelative($originDir, realpath($bundlesDir)); | ||
} else { | ||
$relativeOriginDir = $originDir; | ||
} | ||
|
||
try { | ||
$filesystem->symlink($relativeOriginDir, $targetDir); | ||
if (!file_exists($targetDir)) { | ||
throw new IOException('Symbolic link is broken'); | ||
} | ||
$output->writeln('The assets were installed using symbolic links.'); | ||
$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) { | ||
if (!$input->getOption('relative')) { | ||
$this->hardCopy($originDir, $targetDir); | ||
$output->writeln('It looks like your system doesn\'t support symbolic links, so the assets were installed by copying them.'); | ||
} | ||
|
||
// try again without the relative option | ||
try { | ||
$filesystem->symlink($originDir, $targetDir); | ||
if (!file_exists($targetDir)) { | ||
throw new IOException('Symbolic link is broken'); | ||
} | ||
$output->writeln('It looks like your system doesn\'t support relative symbolic links, so the assets were installed by using absolute symbolic links.'); | ||
} catch (IOException $e) { | ||
$this->hardCopy($originDir, $targetDir); | ||
$output->writeln('It looks like your system doesn\'t support symbolic links, so the assets were installed by copying them.'); | ||
} | ||
// fall back to hard copy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you refactor this to a method that can provide a copy or falls back to a hard copy. the code is a way to complex. |
||
} | ||
} else { | ||
} | ||
|
||
try { | ||
$this->hardCopy($originDir, $targetDir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a bit weird, this should be a part of "// fall back to hard copy" please move the code. |
||
$table->addRow(array($bundle->getNamespace(), $targetDir, 'hard copy')); | ||
} catch (IOException $e) { | ||
$table->addRow(array($bundle->getNamespace(), $targetDir, sprintf('<error>%s</error>', $e->getMessage()))); | ||
$ret = 1; | ||
} | ||
} | ||
} | ||
|
||
$table->render(); | ||
|
||
return $ret; | ||
} | ||
|
||
/** | ||
* Creates links with absolute as a fallback. | ||
* | ||
* @param string $origin | ||
* @param string $target | ||
* @param bool $relative | ||
* | ||
* @throws IOException If link can not be created. | ||
* | ||
* @return bool Created a relative link or not. | ||
*/ | ||
private function symlink($origin, $target, $relative = true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method looks weird. from my point of view this is a misuse of try / catch
thy not just something like this? is there really a need for recursion?
returning the Value of $relative doesnt make sense to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is not enough, |
||
{ | ||
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); | ||
} | ||
} catch (IOException $e) { | ||
if ($relative) { | ||
// try again with absolute | ||
return $this->symlink($origin, $target, false); | ||
} | ||
|
||
throw $e; | ||
} | ||
|
||
return $relative; | ||
} | ||
|
||
/** | ||
|
@@ -141,10 +176,8 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
*/ | ||
private function hardCopy($originDir, $targetDir) | ||
{ | ||
$filesystem = $this->getContainer()->get('filesystem'); | ||
|
||
$filesystem->mkdir($targetDir, 0777); | ||
$this->filesystem->mkdir($targetDir, 0777); | ||
// We use a custom iterator to ignore VCS files | ||
$filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir)); | ||
$this->filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you move $filesystem in the class? now the method symlink is highly coupled to the method configure. please just use read it from the container in both methods. its annoying if you dont know that the configure method is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seemed awkward to me to call
$this->getContainer()->get('filesystem')
in a recursive function. It is coupled to execute not configure and I think that is fine because it is called when the command runs and everything is private so it can be used only in this command anyway.